0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Separate multipart from body_state

This commit is contained in:
Loïc Hoguin 2012-09-15 00:59:49 +02:00
parent 79839b7bb5
commit 4040a9f72d
3 changed files with 10 additions and 16 deletions

View file

@ -39,8 +39,8 @@
meta = [] :: [{atom(), any()}],
%% Request body.
body_state = waiting :: waiting | done | {stream, fun(), any(), fun()}
| {multipart, non_neg_integer(), fun()},
body_state = waiting :: waiting | done | {stream, fun(), any(), fun()},
multipart = undefined :: undefined | {non_neg_integer(), fun()},
buffer = <<>> :: binary(),
%% Response.

View file

@ -424,7 +424,7 @@ ensure_body_processed(Req=#http_req{body_state=waiting}) ->
{ok, Req2} -> {ok, Req2#http_req.buffer};
{error, _Reason} -> {close, <<>>}
end;
ensure_body_processed(Req=#http_req{body_state={multipart, _, _}}) ->
ensure_body_processed(Req=#http_req{body_state={stream, _, _, _}}) ->
{ok, Req2} = cowboy_req:multipart_skip(Req),
ensure_body_processed(Req2).

View file

@ -490,13 +490,7 @@ stream_body(Req=#http_req{buffer=Buffer, body_state={stream, _, _, _}})
stream_body(Req=#http_req{body_state={stream, _, _, _}}) ->
stream_body_recv(Req);
stream_body(Req=#http_req{body_state=done}) ->
{done, Req};
stream_body(Req=#http_req{body_state={multipart, _N, _Fun},
transport=Transport, socket=Socket}) ->
case Transport:recv(Socket, 0, 5000) of
{ok, Data} -> {ok, Data, Req};
{error, Reason} -> {error, Reason}
end.
{done, Req}.
-spec stream_body_recv(Req)
-> {ok, binary(), Req} | {error, atom()} when Req::req().
@ -621,25 +615,25 @@ multipart_data(Req=#http_req{body_state=waiting}) ->
{_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
{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=#http_req{multipart={Length, Cont}}) ->
multipart_data(Req, Length, Cont());
multipart_data(Req=#http_req{body_state=done}) ->
{eof, Req}.
%% @todo Typespecs.
multipart_data(Req, Length, {headers, Headers, Cont}) ->
{{headers, Headers}, Req#http_req{body_state={multipart, Length, Cont}}};
{{headers, Headers}, Req#http_req{multipart={Length, Cont}}};
multipart_data(Req, Length, {body, Data, Cont}) ->
{{body, Data}, Req#http_req{body_state={multipart, Length, Cont}}};
{{body, Data}, Req#http_req{multipart={Length, Cont}}};
multipart_data(Req, Length, {end_of_part, Cont}) ->
{end_of_part, Req#http_req{body_state={multipart, Length, Cont}}};
{end_of_part, Req#http_req{multipart={Length, Cont}}};
multipart_data(Req, 0, eof) ->
{eof, Req#http_req{body_state=done}};
{eof, Req#http_req{body_state=done, multipart=undefined}};
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}};
{eof, Req#http_req{body_state=done, multipart=undefined}};
multipart_data(Req, Length, {more, Parser}) when Length > 0 ->
case stream_body(Req) of
{ok, << Data:Length/binary, Buffer/binary >>, Req2} ->