mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Make cowboy_req:read_part return multipart headers as map
This commit is contained in:
parent
6f7b59886e
commit
ec00e3d60e
5 changed files with 19 additions and 20 deletions
|
@ -102,7 +102,7 @@ multipart(Req0) ->
|
||||||
{data, _FieldName} ->
|
{data, _FieldName} ->
|
||||||
{ok, _Body, Req2} = cowboy_req:read_part_body(Req1),
|
{ok, _Body, Req2} = cowboy_req:read_part_body(Req1),
|
||||||
Req2;
|
Req2;
|
||||||
{file, _FieldName, _Filename, _CType, _CTransferEncoding} ->
|
{file, _FieldName, _Filename, _CType} ->
|
||||||
stream_file(Req1)
|
stream_file(Req1)
|
||||||
end,
|
end,
|
||||||
multipart(Req);
|
multipart(Req);
|
||||||
|
|
|
@ -15,7 +15,7 @@ read_part(Req :: cowboy_req:req(), Opts)
|
||||||
-> {ok, Headers, Req} | {done, Req}
|
-> {ok, Headers, Req} | {done, Req}
|
||||||
|
|
||||||
Opts :: cowboy_req:read_body_opts()
|
Opts :: cowboy_req:read_body_opts()
|
||||||
Headers :: cow_multipart:headers()
|
Headers :: #{binary() => binary()}
|
||||||
----
|
----
|
||||||
|
|
||||||
Read the next part of a multipart body.
|
Read the next part of a multipart body.
|
||||||
|
@ -70,7 +70,7 @@ to 5 seconds.
|
||||||
== Return value
|
== Return value
|
||||||
|
|
||||||
An `ok` tuple is returned containing the next part's headers
|
An `ok` tuple is returned containing the next part's headers
|
||||||
as a list of key/values.
|
as a map.
|
||||||
|
|
||||||
A `done` tuple is returned if there are no more parts to read.
|
A `done` tuple is returned if there are no more parts to read.
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
init(Req, Opts) ->
|
init(Req, Opts) ->
|
||||||
{ok, Headers, Req2} = cowboy_req:read_part(Req),
|
{ok, Headers, Req2} = cowboy_req:read_part(Req),
|
||||||
{ok, Data, Req3} = cowboy_req:read_part_body(Req2),
|
{ok, Data, Req3} = cowboy_req:read_part_body(Req2),
|
||||||
{file, <<"inputfile">>, Filename, ContentType, _TE}
|
{file, <<"inputfile">>, Filename, ContentType}
|
||||||
= cow_multipart:form_data(Headers),
|
= cow_multipart:form_data(Headers),
|
||||||
io:format("Received file ~p of content-type ~p as follow:~n~p~n~n",
|
io:format("Received file ~p of content-type ~p as follow:~n~p~n~n",
|
||||||
[Filename, ContentType, Data]),
|
[Filename, ContentType, Data]),
|
||||||
|
|
|
@ -463,7 +463,7 @@ read_part(Req) ->
|
||||||
read_part(Req, #{length => 64000, period => 5000}).
|
read_part(Req, #{length => 64000, period => 5000}).
|
||||||
|
|
||||||
-spec read_part(Req, read_body_opts())
|
-spec read_part(Req, read_body_opts())
|
||||||
-> {ok, cow_multipart:headers(), Req} | {done, Req}
|
-> {ok, #{binary() => binary()}, Req} | {done, Req}
|
||||||
when Req::req().
|
when Req::req().
|
||||||
read_part(Req, Opts) ->
|
read_part(Req, Opts) ->
|
||||||
case maps:is_key(multipart, Req) of
|
case maps:is_key(multipart, Req) of
|
||||||
|
@ -482,9 +482,10 @@ read_part(Buffer, Opts, Req=#{multipart := {Boundary, _}}) ->
|
||||||
{more, Buffer2} ->
|
{more, Buffer2} ->
|
||||||
{Data, Req2} = stream_multipart(Req, Opts),
|
{Data, Req2} = stream_multipart(Req, Opts),
|
||||||
read_part(<< Buffer2/binary, Data/binary >>, Opts, Req2);
|
read_part(<< Buffer2/binary, Data/binary >>, Opts, Req2);
|
||||||
{ok, Headers, Rest} ->
|
{ok, Headers0, Rest} ->
|
||||||
%% @todo We may want headers as a map. Need to check the
|
Headers = maps:from_list(Headers0),
|
||||||
%% rules for multipart header parsing before taking a decision.
|
%% Reject multipart content containing duplicate headers.
|
||||||
|
true = map_size(Headers) =:= length(Headers0),
|
||||||
{ok, Headers, Req#{multipart => {Boundary, Rest}}};
|
{ok, Headers, Req#{multipart => {Boundary, Rest}}};
|
||||||
%% Ignore epilogue.
|
%% Ignore epilogue.
|
||||||
{done, _} ->
|
{done, _} ->
|
||||||
|
|
|
@ -399,14 +399,13 @@ do_multipart(Path, Config) ->
|
||||||
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
||||||
], ReqBody, Config),
|
], ReqBody, Config),
|
||||||
[
|
[
|
||||||
{[{<<"content-type">>, <<"text/plain">>}], <<"Cowboy is an HTTP server.">>},
|
{#{<<"content-type">> := <<"text/plain">>}, <<"Cowboy is an HTTP server.">>},
|
||||||
{LargeHeaders, LargeBody}
|
{LargeHeaders, LargeBody}
|
||||||
] = binary_to_term(RespBody),
|
] = binary_to_term(RespBody),
|
||||||
%% @todo Multipart header order is currently undefined.
|
#{
|
||||||
[
|
<<"content-type">> := <<"application/octet-stream">>,
|
||||||
{<<"content-type">>, <<"application/octet-stream">>},
|
<<"x-custom">> := <<"value">>
|
||||||
{<<"x-custom">>, <<"value">>}
|
} = LargeHeaders,
|
||||||
] = lists:sort(LargeHeaders),
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
read_part_skip_body(Config) ->
|
read_part_skip_body(Config) ->
|
||||||
|
@ -421,14 +420,13 @@ read_part_skip_body(Config) ->
|
||||||
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
{<<"content-type">>, <<"multipart/mixed; boundary=deadbeef">>}
|
||||||
], ReqBody, Config),
|
], ReqBody, Config),
|
||||||
[
|
[
|
||||||
[{<<"content-type">>, <<"text/plain">>}],
|
#{<<"content-type">> := <<"text/plain">>},
|
||||||
LargeHeaders
|
LargeHeaders
|
||||||
] = binary_to_term(RespBody),
|
] = binary_to_term(RespBody),
|
||||||
%% @todo Multipart header order is currently undefined.
|
#{
|
||||||
[
|
<<"content-type">> := <<"application/octet-stream">>,
|
||||||
{<<"content-type">>, <<"application/octet-stream">>},
|
<<"x-custom">> := <<"value">>
|
||||||
{<<"x-custom">>, <<"value">>}
|
} = LargeHeaders,
|
||||||
] = lists:sort(LargeHeaders),
|
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% @todo When reading a multipart body, length and period
|
%% @todo When reading a multipart body, length and period
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue