diff options
author | Loïc Hoguin <[email protected]> | 2012-05-21 14:43:45 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2012-05-21 14:43:45 +0200 |
commit | ca9278bc27b4d5429e32693901945f5c4796f994 (patch) | |
tree | 0f8a9e5d93e9748484129787981f5bba4e6a252b /src | |
parent | 8363e8995ad0b4f21b2a194c235ae7b92462af2e (diff) | |
download | cowboy-ca9278bc27b4d5429e32693901945f5c4796f994.tar.gz cowboy-ca9278bc27b4d5429e32693901945f5c4796f994.tar.bz2 cowboy-ca9278bc27b4d5429e32693901945f5c4796f994.zip |
Make multipart code use stream_body
Diffstat (limited to 'src')
-rw-r--r-- | src/cowboy_http_req.erl | 26 |
1 files changed, 9 insertions, 17 deletions
diff --git a/src/cowboy_http_req.erl b/src/cowboy_http_req.erl index 411d059..8f1f789 100644 --- a/src/cowboy_http_req.erl +++ b/src/cowboy_http_req.erl @@ -576,21 +576,13 @@ multipart_data(Req=#http_req{body_state=waiting}) -> {{<<"multipart">>, _SubType, Params}, Req2} = parse_header('Content-Type', Req), {_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params), - {Length, Req3=#http_req{buffer=Buffer}} = - parse_header('Content-Length', Req2), - multipart_data(Req3, Length, cowboy_multipart:parser(Boundary), Buffer); + {Length, Req3} = parse_header('Content-Length', Req2), + multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)}); multipart_data(Req=#http_req{body_state={multipart, Length, Cont}}) -> multipart_data(Req, Length, Cont()); multipart_data(Req=#http_req{body_state=done}) -> {eof, Req}. -multipart_data(Req, Length, Parser, Buffer) when byte_size(Buffer) >= Length -> - << Data:Length/binary, Rest/binary >> = Buffer, - multipart_data(Req#http_req{buffer=Rest}, 0, Parser(Data)); -multipart_data(Req, Length, Parser, Buffer) -> - NewLength = Length - byte_size(Buffer), - multipart_data(Req#http_req{buffer= <<>>}, NewLength, Parser(Buffer)). - multipart_data(Req, Length, {headers, Headers, Cont}) -> {{headers, Headers}, Req#http_req{body_state={multipart, Length, Cont}}}; multipart_data(Req, Length, {body, Data, Cont}) -> @@ -601,15 +593,15 @@ multipart_data(Req, 0, eof) -> {eof, Req#http_req{body_state=done}}; multipart_data(Req=#http_req{socket=Socket, transport=Transport}, Length, eof) -> + %% We just want to skip so no need to stream data here. {ok, _Data} = Transport:recv(Socket, Length, 5000), {eof, Req#http_req{body_state=done}}; -multipart_data(Req=#http_req{socket=Socket, transport=Transport}, - Length, {more, Parser}) when Length > 0 -> - case Transport:recv(Socket, 0, 5000) of - {ok, << Data:Length/binary, Buffer/binary >>} -> - multipart_data(Req#http_req{buffer=Buffer}, 0, Parser(Data)); - {ok, Data} -> - multipart_data(Req, Length - byte_size(Data), Parser(Data)) +multipart_data(Req, Length, {more, Parser}) when Length > 0 -> + case stream_body(Req) of + {ok, << Data:Length/binary, Buffer/binary >>, Req2} -> + multipart_data(Req2#http_req{buffer=Buffer}, 0, Parser(Data)); + {ok, Data, Req2} -> + multipart_data(Req2, Length - byte_size(Data), Parser(Data)) end. %% @doc Skip a part returned by the multipart parser. |