Skip to content

Instantly share code, notes, and snippets.

@maxlapshin
Created April 3, 2015 11:26
Show Gist options
  • Save maxlapshin/7cd5a06250cb94e28e1d to your computer and use it in GitHub Desktop.
Save maxlapshin/7cd5a06250cb94e28e1d to your computer and use it in GitHub Desktop.
-module(double_gzip).
-compile(export_all).
% curl -v http://localhost:4020/ -H "Accept-Encoding: gzip" gives trash
% curl -v http://localhost:4021/ -H "Accept-Encoding: gzip" is ok
start() ->
{ok,_} = application:ensure_all_started(cowboy),
Dispatch = cowboy_router:compile([
{'_', [{"/", ?MODULE, []}]}
]),
{ok,_} = cowboy:start_http(double_gzip, 1, [{port,4020}], [{onresponse,fun ?MODULE:capitalize_hook/4},{compress,true},{env, [{dispatch, Dispatch}]}]),
{ok,_} = cowboy:start_http(no_gzip, 1, [{port,4021}], [{onresponse,fun ?MODULE:capitalize_hook/4},{compress,false},{env, [{dispatch, Dispatch}]}]),
ok.
capitalize_hook(Status, Headers, Body, Req) ->
Headers2 = [{cowboy_bstr:capitalize_token(iolist_to_binary(K)),iolist_to_binary(V)} || {K,V} <- Headers],
{ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req),
Req2.
init(_,Req,_) -> {ok, Req, state}.
handle(Req, _) ->
{ok, Bin} = file:read_file("double_gzip.erl"),
{ok, Req1} = cowboy_req:reply(200, [{<<"Content-Type">>,<<"text/plain">>}], [Bin,"\n"], Req),
{ok, Req1, undefined}.
terminate(_,_,_) -> ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment