1515-include (" hackney_lib.hrl" ).
1616
1717-export ([encode_form /1 , encode_form /2 ,
18- decode_form /2 , decode_form / 3 ,
18+ decode_form /2 ,
1919 boundary /0 ,
2020 parser /1 ]).
2121
@@ -113,11 +113,8 @@ encode_form(Parts, Boundary) ->
113113% % @doc decode a multipart form.
114114- spec decode_form (binary (), binary ()) -> {ok , list ()} | {error , term ()}.
115115decode_form (Boundary , Body ) ->
116- decode_form (Boundary , Body , []).
117-
118- decode_form (Boundary , Body , Acc ) ->
119116 Parser = parser (Boundary ),
120- decode_form1 (Parser (Body ), Acc ).
117+ decode_form1 (Parser (Body ), [[]] ).
121118
122119% % @doc Return a multipart parser for the given boundary.
123120- spec parser (binary ()) -> part_parser ().
@@ -131,8 +128,8 @@ boundary() ->
131128
132129% % @doc create a generic multipart header
133130mp_header (Headers , Boundary ) ->
134- iolist_to_binary ([<< " -- " , Boundary / binary , " \r\n " >> ,
135- hackney_headers : to_binary ( Headers )]) .
131+ BinHeaders = hackney_headers : to_binary ( Headers ) ,
132+ << " -- " , Boundary / binary , " \r\n " , BinHeaders / binary >> .
136133
137134% % @doc return the boundary ennding a multipart
138135mp_eof (Boundary ) ->
@@ -242,6 +239,8 @@ unique(Size, Acc) ->
242239 Random = $a + random :uniform ($z - $a ),
243240 unique (Size , <<Acc /binary , Random >>).
244241
242+ decode_form1 (eof , [[]|Acc ]) ->
243+ {ok , lists :reverse (Acc )};
245244decode_form1 (eof , Acc ) ->
246245 {ok , lists :reverse (Acc )};
247246decode_form1 ({headers , Headers , Fun }, [Last | Rest ]) ->
@@ -256,6 +255,10 @@ decode_form1({body, Bin, Fun}, [Last | Rest]) ->
256255 {body , << Body /binary , Bin /binary >>})
257256 end ,
258257 decode_form1 (Fun (), [Last1 | Rest ]);
258+ decode_form1 ({end_of_part , Fun }, [Last | Rest ]) ->
259+ Headers = proplists :get_value (headers , Last , []),
260+ Body = proplists :get_value (body , Last , []),
261+ decode_form1 (Fun (), [[], {Headers , Body } | Rest ]);
259262decode_form1 ({more , Fun }, Acc ) ->
260263 {error , {more , Fun , Acc }}.
261264
0 commit comments