Skip to content

Instantly share code, notes, and snippets.

@dvv
Created December 18, 2012 12:13
Show Gist options
  • Save dvv/4327502 to your computer and use it in GitHub Desktop.
Save dvv/4327502 to your computer and use it in GitHub Desktop.
An attempt at hiding cookie session details in cowboy handler
-module(handler).
-author('Vladimir Dronnikov <[email protected]>').
-define(INFO, error_logger:info_report).
-define(ERROR, error_logger:error_report).
%%
%% ------------------------------------------------------------------
%% cowboy HTTP handler API
%% ------------------------------------------------------------------
%%
%-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/2]).
%%
%% ------------------------------------------------------------------
%% cowboy HTTP handler API
%% ------------------------------------------------------------------
%%
-record(state, {
handler,
session
}).
%% Opts :: [{handler, Module} | {handler, {M, F}} | {handler, function()}]
init(_Transport, Req, Opts) ->
% handler
{handler, Handler} = lists:keyfind(handler, 1, Opts),
HandlerFun = case Handler of
Fun when is_function(Fun) -> Fun;
{M, F} -> fun M:F/3;
Mod -> fun Mod:handle/3
end,
% session
Req2 = case proplists:get_value(session, Opts) of
{SessionManager, SessionOpts} ->
% get session
{Session, Cookie, Req3} = SessionManager:get(SessionOpts, Req),
?INFO({insess, Session, Cookie}),
Req4 = cowboy_req:set_meta(session, Session, Req3),
cowboy_req:set_meta(session_setter, fun (Session2, Req5) ->
{Cookie2, Req6} = SessionManager:set(Session2, SessionOpts, Req5),
?INFO({outsess, Session2, Cookie2}),
Req6
end, Req4);
_ ->
cowboy_req:set_meta(session_setter, fun (_Session2, Req5) ->
Req5
end, Req)
end,
%
{ok, Req2, #state{handler = HandlerFun}}.
terminate(_Req, _State) ->
ok.
handle(Req, State = #state{handler = _Handler}) ->
% route request
{Method, Req2} = cowboy_req:method(Req),
{Path, Req3} = cowboy_req:path_info(Req2),
{Status, Headers, Body, Req4} = handler(Method, Path, Req3),
% set session
{Session2, Req5} = cowboy_req:meta(session, Req4),
{Setter, Req6} = cowboy_req:meta(session_setter, Req5),
Req7 = Setter(Session2, Req6),
% respond
{ok, Req8} = cowboy_req:reply(Status, Headers, Body, Req7),
{ok, Req8, State}.
%handler(<<"GET">>, _, Req) ->
handler(_, _, Req) ->
{200, [], [<<"Hello">>, <<" World\n">>], Req}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment