Created
April 3, 2015 11:26
-
-
Save maxlapshin/7cd5a06250cb94e28e1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
-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