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:
parent
79839b7bb5
commit
4040a9f72d
3 changed files with 10 additions and 16 deletions
|
@ -39,8 +39,8 @@
|
||||||
meta = [] :: [{atom(), any()}],
|
meta = [] :: [{atom(), any()}],
|
||||||
|
|
||||||
%% Request body.
|
%% Request body.
|
||||||
body_state = waiting :: waiting | done | {stream, fun(), any(), fun()}
|
body_state = waiting :: waiting | done | {stream, fun(), any(), fun()},
|
||||||
| {multipart, non_neg_integer(), fun()},
|
multipart = undefined :: undefined | {non_neg_integer(), fun()},
|
||||||
buffer = <<>> :: binary(),
|
buffer = <<>> :: binary(),
|
||||||
|
|
||||||
%% Response.
|
%% Response.
|
||||||
|
|
|
@ -424,7 +424,7 @@ ensure_body_processed(Req=#http_req{body_state=waiting}) ->
|
||||||
{ok, Req2} -> {ok, Req2#http_req.buffer};
|
{ok, Req2} -> {ok, Req2#http_req.buffer};
|
||||||
{error, _Reason} -> {close, <<>>}
|
{error, _Reason} -> {close, <<>>}
|
||||||
end;
|
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),
|
{ok, Req2} = cowboy_req:multipart_skip(Req),
|
||||||
ensure_body_processed(Req2).
|
ensure_body_processed(Req2).
|
||||||
|
|
||||||
|
|
|
@ -490,13 +490,7 @@ stream_body(Req=#http_req{buffer=Buffer, body_state={stream, _, _, _}})
|
||||||
stream_body(Req=#http_req{body_state={stream, _, _, _}}) ->
|
stream_body(Req=#http_req{body_state={stream, _, _, _}}) ->
|
||||||
stream_body_recv(Req);
|
stream_body_recv(Req);
|
||||||
stream_body(Req=#http_req{body_state=done}) ->
|
stream_body(Req=#http_req{body_state=done}) ->
|
||||||
{done, Req};
|
{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.
|
|
||||||
|
|
||||||
-spec stream_body_recv(Req)
|
-spec stream_body_recv(Req)
|
||||||
-> {ok, binary(), Req} | {error, atom()} when Req::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),
|
{_, Boundary} = lists:keyfind(<<"boundary">>, 1, Params),
|
||||||
{Length, Req3} = parse_header('Content-Length', Req2),
|
{Length, Req3} = parse_header('Content-Length', Req2),
|
||||||
multipart_data(Req3, Length, {more, cowboy_multipart:parser(Boundary)});
|
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, Length, Cont());
|
||||||
multipart_data(Req=#http_req{body_state=done}) ->
|
multipart_data(Req=#http_req{body_state=done}) ->
|
||||||
{eof, Req}.
|
{eof, Req}.
|
||||||
|
|
||||||
%% @todo Typespecs.
|
%% @todo Typespecs.
|
||||||
multipart_data(Req, Length, {headers, Headers, Cont}) ->
|
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}) ->
|
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}) ->
|
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) ->
|
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},
|
multipart_data(Req=#http_req{socket=Socket, transport=Transport},
|
||||||
Length, eof) ->
|
Length, eof) ->
|
||||||
%% We just want to skip so no need to stream data here.
|
%% We just want to skip so no need to stream data here.
|
||||||
{ok, _Data} = Transport:recv(Socket, Length, 5000),
|
{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 ->
|
multipart_data(Req, Length, {more, Parser}) when Length > 0 ->
|
||||||
case stream_body(Req) of
|
case stream_body(Req) of
|
||||||
{ok, << Data:Length/binary, Buffer/binary >>, Req2} ->
|
{ok, << Data:Length/binary, Buffer/binary >>, Req2} ->
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue