Skip to content

Instantly share code, notes, and snippets.

@andrzejsliwa
Last active August 26, 2016 07:30
Show Gist options
  • Save andrzejsliwa/3702090 to your computer and use it in GitHub Desktop.
Save andrzejsliwa/3702090 to your computer and use it in GitHub Desktop.
erlang - cowboy debugging helper request/response
%% See LICENSE for licensing information.
-module(cowboy_debug).
-export([onrequest_hook/1]).
-export([onresponse_hook/4]).
onrequest_hook(Req) ->
Method = to_string(extract(cowboy_req:method(Req))),
Path = to_string(extract(cowboy_req:path(Req))),
Params = params_to_string(extract(cowboy_req:qs_vals(Req))),
Host = to_string(extract(cowboy_req:host(Req))),
Port = port_to_string(extract(cowboy_req:port(Req))),
lager:debug("~n~nStarted " ++ Method ++ " " ++ Path ++ Params ++ " for " ++ Host ++ Port ++ "~n"
" qs_vals : " ++ to_native_string(extract(cowboy_req:qs_vals(Req))) ++ "~n"
" raw_qs : " ++ to_native_string(extract(cowboy_req:qs(Req))) ++ "~n"
" bindings : " ++ to_native_string(extract(cowboy_req:bindings(Req))) ++ "~n"
" cookies : " ++ to_native_string(extract(cowboy_req:cookies(Req))) ++ "~n"
" headers : " ++ to_native_string(extract(cowboy_req:headers(Req))) ++ "~n"),
Req.
onresponse_hook(Code, Headers, Response, Req) ->
Method = to_string(extract(cowboy_req:method(Req))),
Path = to_string(extract(cowboy_req:path(Req))),
Params = params_to_string(extract(cowboy_req:qs_vals(Req))),
Host = to_string(extract(cowboy_req:host(Req))),
Port = port_to_string(extract(cowboy_req:port(Req))),
lager:debug(
"~n~nCompleted " ++ to_string(Code) ++ " " ++ Method ++ " " ++ Path ++ Params ++ " for " ++ Host ++ Port ++ "~n"
" cookies : " ++ to_native_string(extract(cowboy_req:cookies(Req))) ++ "~n"
" headers : " ++ to_native_string(Headers) ++ "~n"
" response : " ++ to_native_string(Response)),
Req.
extract({Value, _Req}) ->
Value.
params_to_string(Params) ->
case to_string(Params) of
"" ->
"";
OtherParams -> "?" ++ OtherParams
end.
port_to_string(Port) -> case to_string(Port) of
"80" -> "";
OtherPort -> ":" ++ OtherPort
end.
%% print value as standard format
to_native_string(Value) ->
io_lib:format("~p", [Value]).
%% convert everything to string
to_string(undefined) ->
"";
to_string(Atom) when is_atom(Atom) ->
atom_to_list(Atom);
to_string(Binary) when is_binary(Binary) ->
binary_to_list(Binary);
to_string(Integer) when is_integer(Integer) ->
integer_to_list(Integer);
to_string([])
-> "";
to_string(List) when is_list(List) ->
to_string(List, "").
%% convert lists to string
to_string(Binary, Separator) when is_binary(Binary) ->
to_string(binary_to_list(Binary), Separator);
to_string(List, Separator) when is_list(List) ->
string:join(list_to_string(List, []), Separator).
list_to_string([], Result) ->
lists:reverse(Result);
list_to_string([Head| Rest], Result) ->
list_to_string(Rest, [to_string(Head)|Result]).
Started GET /sdk/src/tab/Tab.js for localhost:8080
qs_vals : undefined
raw_qs : <<"_dc=1347397546129">>
bindings : undefined
cookies : undefined
headers : [{'Connection',<<"keep-alive">>},
{'Cookie',<<"__utma=111872281.400915425.1346312146.1346322221.1346410278.3; __utmz=111872281.1346312146.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); SnapABugHistory=83#">>},
{'Accept-Encoding',<<"gzip, deflate">>},
{'Accept-Language',<<"en-us">>},
{'Referer',<<"http://localhost:8080/">>},
{'Accept',<<"*/*">>},
{'User-Agent',<<"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25">>},
{'Host',<<"localhost:8080">>}]
23:05:46.133 [info]
Completed 200 GET /sdk/src/tab/Tab.js for localhost:8080
cookies : undefined
headers : [{<<"Last-Modified">>,"Tue, 11 Sep 2012 17:35:50 GMT"},
{<<"Content-Type">>,[<<"application">>,<<"/">>,<<"javascript">>,<<>>]},
{<<"Content-Length">>,"3073"},
{<<"Date">>,<<"Tue, 11 Sep 2012 21:05:45 GMT">>},
{<<"Server">>,<<"Cowboy">>},
{<<"Connection">>,<<"keep-alive">>}]
response : <<"{}">>
...
{ok, _} = cowboy:start_http(http, 100,
[{port, 8080}], [
{env, [{dispatch, Dispatch}]},
{onrequest, fun cowboy_debug:onrequest_hook/1},
{onresponse, fun cowboy_debug:onresponse_hook/4}
]),
...
@andrzejsliwa
Copy link
Author

updated for latest cowboy api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment