Last active
August 26, 2016 07:30
-
-
Save andrzejsliwa/3702090 to your computer and use it in GitHub Desktop.
erlang - cowboy debugging helper request/response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 : <<"{}">> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
{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} | |
]), | |
... |
updated for latest cowboy api.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was trying this out, and had to add a line to your code to make it work.
to_string('undefined', _Seperator) ->
"undefined".
because it seems the host was undefined as you can see in the Completed 101 GET below.
Started GET / for my.domain.com:7004
qs_vals : undefined
raw_qs : <<"ts=1382971249858">>
bindings : undefined
cookies : undefined
headers : [{'Cookie',<<"fbm_xxxxx=base_domain=.domain.com; _smsg_xxxxxx=true; fbm_xxxxxxxxx=base_domain=.domain.com; fbm_xxxxxxxxxxxxx=base_domain=.domain.com">>},
{'User-Agent',<<"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36">>},
{<<"Sec-Websocket-Extensions">>,<<"x-webkit-deflate-frame">>},
{<<"Sec-Websocket-Version">>,<<"13">>},
{<<"Sec-Websocket-Key">>,<<"aaaaaaaaaaaaaaaaaaaaaaaaaa">>},
{'Cache-Control',<<"no-cache">>},
{'Pragma',<<"no-cache">>},
{<<"Origin">>,<<"https://test.com:1008">>},
{'Host',<<"my.domain.com:7004">>},
{'Connection',<<"Upgrade">>},
{'Upgrade',<<"websocket">>}]
2013-10-28 07:40:50.276 [info] <0.1466.0>@cowboy_debug:onresponse_hook:33
Completed 101 GET / for undefined:7004
cookies : []
headers : [{<<"Upgrade">>,<<"websocket">>},
{<<"Sec-Websocket-Accept">>,<<"bbbbbbbbbbbbbbbbbbbbbbbbbbbb">>},
{<<"Connection">>,<<"Upgrade">>}]
Do you think this reflects a problem in my code?
Thanks