diff options
author | rambocoder <[email protected]> | 2013-03-06 08:50:45 -0500 |
---|---|---|
committer | rambocoder <[email protected]> | 2013-03-06 08:50:45 -0500 |
commit | 84d7671e91bb2dee2081172dbf651860134ae75e (patch) | |
tree | 1fe31a5ec0edf33a2070d3ab253b5f0f02cb9913 /test/http_handler_echo_body.erl | |
parent | 233cf43ab9c6c16d22e14039a79606fc935693d6 (diff) | |
download | cowboy-84d7671e91bb2dee2081172dbf651860134ae75e.tar.gz cowboy-84d7671e91bb2dee2081172dbf651860134ae75e.tar.bz2 cowboy-84d7671e91bb2dee2081172dbf651860134ae75e.zip |
Check the length before reading the body in body/1 and body_qs/1
Diffstat (limited to 'test/http_handler_echo_body.erl')
-rw-r--r-- | test/http_handler_echo_body.erl | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/test/http_handler_echo_body.erl b/test/http_handler_echo_body.erl index 31595d5..4b9e765 100644 --- a/test/http_handler_echo_body.erl +++ b/test/http_handler_echo_body.erl @@ -9,11 +9,37 @@ init({_, http}, Req, _) -> handle(Req, State) -> true = cowboy_req:has_body(Req), - {ok, Body, Req2} = cowboy_req:body(Req), - {Size, Req3} = cowboy_req:body_length(Req2), + {ok, Req3} = case cowboy_req:body(1000000, Req) of + {error, chunked} -> handle_chunked(Req); + {error, badlength} -> handle_badlength(Req); + {ok, Body, Req2} -> handle_body(Req2, Body) + end, + {ok, Req3, State}. + +handle_chunked(Req) -> + {ok, Data, Req2} = read_body(Req, <<>>, 1000000), + {ok, Req3} = cowboy_req:reply(200, [], Data, Req2), + {ok, Req3}. + +handle_badlength(Req) -> + {ok, Req2} = cowboy_req:reply(413, [], <<"Request entity too large">>, Req), + {ok, Req2}. + +handle_body(Req, Body) -> + {Size, Req2} = cowboy_req:body_length(Req), Size = byte_size(Body), - {ok, Req4} = cowboy_req:reply(200, [], Body, Req3), - {ok, Req4, State}. + {ok, Req3} = cowboy_req:reply(200, [], Body, Req2), + {ok, Req3}. terminate(_, _, _) -> ok. + +% Read chunked request content +read_body(Req, Acc, BodyLengthRemaining) -> + case cowboy_req:stream_body(Req) of + {ok, Data, Req2} -> + BodyLengthRem = BodyLengthRemaining - byte_size(Data), + read_body(Req2, << Acc/binary, Data/binary >>, BodyLengthRem); + {done, Req2} -> + {ok, Acc, Req2} + end. |