cow_multipart.erl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. %% Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(cow_multipart).
  15. %% Parsing.
  16. -export([parse_headers/2]).
  17. -export([parse_body/2]).
  18. %% Building.
  19. -export([boundary/0]).
  20. -export([first_part/2]).
  21. -export([part/2]).
  22. -export([close/1]).
  23. %% Headers.
  24. -export([form_data/1]).
  25. -export([parse_content_disposition/1]).
  26. -export([parse_content_transfer_encoding/1]).
  27. -export([parse_content_type/1]).
  28. -type headers() :: [{iodata(), iodata()}].
  29. -export_type([headers/0]).
  30. -include("cow_inline.hrl").
  31. -define(TEST1_MIME, <<
  32. "This is a message with multiple parts in MIME format.\r\n"
  33. "--frontier\r\n"
  34. "Content-Type: text/plain\r\n"
  35. "\r\n"
  36. "This is the body of the message.\r\n"
  37. "--frontier\r\n"
  38. "Content-Type: application/octet-stream\r\n"
  39. "Content-Transfer-Encoding: base64\r\n"
  40. "\r\n"
  41. "PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n"
  42. "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\r\n"
  43. "--frontier--"
  44. >>).
  45. -define(TEST1_BOUNDARY, <<"frontier">>).
  46. -define(TEST2_MIME, <<
  47. "--AaB03x\r\n"
  48. "Content-Disposition: form-data; name=\"submit-name\"\r\n"
  49. "\r\n"
  50. "Larry\r\n"
  51. "--AaB03x\r\n"
  52. "Content-Disposition: form-data; name=\"files\"\r\n"
  53. "Content-Type: multipart/mixed; boundary=BbC04y\r\n"
  54. "\r\n"
  55. "--BbC04y\r\n"
  56. "Content-Disposition: file; filename=\"file1.txt\"\r\n"
  57. "Content-Type: text/plain\r\n"
  58. "\r\n"
  59. "... contents of file1.txt ...\r\n"
  60. "--BbC04y\r\n"
  61. "Content-Disposition: file; filename=\"file2.gif\"\r\n"
  62. "Content-Type: image/gif\r\n"
  63. "Content-Transfer-Encoding: binary\r\n"
  64. "\r\n"
  65. "...contents of file2.gif...\r\n"
  66. "--BbC04y--\r\n"
  67. "--AaB03x--"
  68. >>).
  69. -define(TEST2_BOUNDARY, <<"AaB03x">>).
  70. -define(TEST3_MIME, <<
  71. "This is the preamble.\r\n"
  72. "--boundary\r\n"
  73. "Content-Type: text/plain\r\n"
  74. "\r\n"
  75. "This is the body of the message.\r\n"
  76. "--boundary--"
  77. "\r\nThis is the epilogue. Here it includes leading CRLF"
  78. >>).
  79. -define(TEST3_BOUNDARY, <<"boundary">>).
  80. -define(TEST4_MIME, <<
  81. "This is the preamble.\r\n"
  82. "--boundary\r\n"
  83. "Content-Type: text/plain\r\n"
  84. "\r\n"
  85. "This is the body of the message.\r\n"
  86. "--boundary--"
  87. "\r\n"
  88. >>).
  89. -define(TEST4_BOUNDARY, <<"boundary">>).
  90. %% Parsing.
  91. %%
  92. %% The multipart format is defined in RFC 2045.
  93. %% @doc Parse the headers for the next multipart part.
  94. %%
  95. %% This function skips any preamble before the boundary.
  96. %% The preamble may be retrieved using parse_body/2.
  97. %%
  98. %% This function will accept input of any size, it is
  99. %% up to the caller to limit it if needed.
  100. -spec parse_headers(binary(), binary())
  101. -> more | {more, binary()}
  102. | {ok, headers(), binary()}
  103. | {done, binary()}.
  104. %% If the stream starts with the boundary we can make a few assumptions
  105. %% and quickly figure out if we got the complete list of headers.
  106. parse_headers(<< "--", Stream/bits >>, Boundary) ->
  107. BoundarySize = byte_size(Boundary),
  108. case Stream of
  109. %% Last boundary. Return the epilogue.
  110. << Boundary:BoundarySize/binary, "--", Stream2/bits >> ->
  111. {done, Stream2};
  112. << Boundary:BoundarySize/binary, Stream2/bits >> ->
  113. %% We have all the headers only if there is a \r\n\r\n
  114. %% somewhere in the data after the boundary.
  115. case binary:match(Stream2, <<"\r\n\r\n">>) of
  116. nomatch ->
  117. more;
  118. _ ->
  119. before_parse_headers(Stream2)
  120. end;
  121. %% If there isn't enough to represent Boundary \r\n\r\n
  122. %% then we definitely don't have all the headers.
  123. _ when byte_size(Stream) < byte_size(Boundary) + 4 ->
  124. more;
  125. %% Otherwise we have preamble data to skip.
  126. %% We still got rid of the first two misleading bytes.
  127. _ ->
  128. skip_preamble(Stream, Boundary)
  129. end;
  130. %% Otherwise we have preamble data to skip.
  131. parse_headers(Stream, Boundary) ->
  132. skip_preamble(Stream, Boundary).
  133. %% We need to find the boundary and a \r\n\r\n after that.
  134. %% Since the boundary isn't at the start, it must be right
  135. %% after a \r\n too.
  136. skip_preamble(Stream, Boundary) ->
  137. case binary:match(Stream, <<"\r\n--", Boundary/bits >>) of
  138. %% No boundary, need more data.
  139. nomatch ->
  140. %% We can safely skip the size of the stream
  141. %% minus the last 3 bytes which may be a partial boundary.
  142. SkipSize = byte_size(Stream) - 3,
  143. case SkipSize > 0 of
  144. false ->
  145. more;
  146. true ->
  147. << _:SkipSize/binary, Stream2/bits >> = Stream,
  148. {more, Stream2}
  149. end;
  150. {Start, Length} ->
  151. Start2 = Start + Length,
  152. << _:Start2/binary, Stream2/bits >> = Stream,
  153. case Stream2 of
  154. %% Last boundary. Return the epilogue.
  155. << "--", Stream3/bits >> ->
  156. {done, Stream3};
  157. _ ->
  158. case binary:match(Stream, <<"\r\n\r\n">>) of
  159. %% We don't have the full headers.
  160. nomatch ->
  161. {more, Stream2};
  162. _ ->
  163. before_parse_headers(Stream2)
  164. end
  165. end
  166. end.
  167. %% There is a line break right after the boundary, skip it.
  168. %%
  169. %% We only skip it now because there might be no headers at all,
  170. %% which means the \r\n\r\n indicating the end of headers also
  171. %% includes this line break.
  172. before_parse_headers(<< "\r\n", Stream/bits >>) ->
  173. parse_hd_name(Stream, [], <<>>).
  174. parse_hd_name(<< C, Rest/bits >>, H, SoFar) ->
  175. case C of
  176. $: -> parse_hd_before_value(Rest, H, SoFar);
  177. $\s -> parse_hd_name_ws(Rest, H, SoFar);
  178. $\t -> parse_hd_name_ws(Rest, H, SoFar);
  179. ?INLINE_LOWERCASE(parse_hd_name, Rest, H, SoFar)
  180. end.
  181. parse_hd_name_ws(<< C, Rest/bits >>, H, Name) ->
  182. case C of
  183. $\s -> parse_hd_name_ws(Rest, H, Name);
  184. $\t -> parse_hd_name_ws(Rest, H, Name);
  185. $: -> parse_hd_before_value(Rest, H, Name)
  186. end.
  187. parse_hd_before_value(<< $\s, Rest/bits >>, H, N) ->
  188. parse_hd_before_value(Rest, H, N);
  189. parse_hd_before_value(<< $\t, Rest/bits >>, H, N) ->
  190. parse_hd_before_value(Rest, H, N);
  191. parse_hd_before_value(Buffer, H, N) ->
  192. parse_hd_value(Buffer, H, N, <<>>).
  193. parse_hd_value(<< $\r, Rest/bits >>, Headers, Name, SoFar) ->
  194. case Rest of
  195. << "\n\r\n", Rest2/bits >> ->
  196. {ok, [{Name, SoFar}|Headers], Rest2};
  197. << $\n, C, Rest2/bits >> when C =:= $\s; C =:= $\t ->
  198. parse_hd_value(Rest2, Headers, Name, SoFar);
  199. << $\n, Rest2/bits >> ->
  200. parse_hd_name(Rest2, [{Name, SoFar}|Headers], <<>>)
  201. end;
  202. parse_hd_value(<< C, Rest/bits >>, H, N, SoFar) ->
  203. parse_hd_value(Rest, H, N, << SoFar/binary, C >>).
  204. %% @doc Parse the body of the current multipart part.
  205. %%
  206. %% The body is everything until the next boundary.
  207. -spec parse_body(binary(), binary())
  208. -> {ok, binary()} | {ok, binary(), binary()}
  209. | done | {done, binary()} | {done, binary(), binary()}.
  210. parse_body(Stream, Boundary) ->
  211. BoundarySize = byte_size(Boundary),
  212. case Stream of
  213. << "--", Boundary:BoundarySize/binary, _/bits >> ->
  214. done;
  215. _ ->
  216. case binary:match(Stream, << "\r\n--", Boundary/bits >>) of
  217. %% No boundary, check for a possible partial at the end.
  218. %% Return more or less of the body depending on the result.
  219. nomatch ->
  220. StreamSize = byte_size(Stream),
  221. From = StreamSize - BoundarySize - 3,
  222. MatchOpts = if
  223. %% Binary too small to contain boundary, check it fully.
  224. From < 0 -> [];
  225. %% Optimize, only check the end of the binary.
  226. true -> [{scope, {From, StreamSize - From}}]
  227. end,
  228. case binary:match(Stream, <<"\r">>, MatchOpts) of
  229. nomatch ->
  230. {ok, Stream};
  231. {Pos, _} ->
  232. case Stream of
  233. << Body:Pos/binary >> ->
  234. {ok, Body};
  235. << Body:Pos/binary, Rest/bits >> ->
  236. {ok, Body, Rest}
  237. end
  238. end;
  239. %% Boundary found, this is the last chunk of the body.
  240. {Pos, _} ->
  241. case Stream of
  242. << Body:Pos/binary, "\r\n" >> ->
  243. {done, Body};
  244. << Body:Pos/binary, "\r\n", Rest/bits >> ->
  245. {done, Body, Rest};
  246. << Body:Pos/binary, Rest/bits >> ->
  247. {done, Body, Rest}
  248. end
  249. end
  250. end.
  251. -ifdef(TEST).
  252. parse_test() ->
  253. H1 = [{<<"content-type">>, <<"text/plain">>}],
  254. Body1 = <<"This is the body of the message.">>,
  255. H2 = lists:sort([{<<"content-type">>, <<"application/octet-stream">>},
  256. {<<"content-transfer-encoding">>, <<"base64">>}]),
  257. Body2 = <<"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n"
  258. "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==">>,
  259. {ok, H1, Rest} = parse_headers(?TEST1_MIME, ?TEST1_BOUNDARY),
  260. {done, Body1, Rest2} = parse_body(Rest, ?TEST1_BOUNDARY),
  261. done = parse_body(Rest2, ?TEST1_BOUNDARY),
  262. {ok, H2Unsorted, Rest3} = parse_headers(Rest2, ?TEST1_BOUNDARY),
  263. H2 = lists:sort(H2Unsorted),
  264. {done, Body2, Rest4} = parse_body(Rest3, ?TEST1_BOUNDARY),
  265. done = parse_body(Rest4, ?TEST1_BOUNDARY),
  266. {done, <<>>} = parse_headers(Rest4, ?TEST1_BOUNDARY),
  267. ok.
  268. parse_interleaved_test() ->
  269. H1 = [{<<"content-disposition">>, <<"form-data; name=\"submit-name\"">>}],
  270. Body1 = <<"Larry">>,
  271. H2 = lists:sort([{<<"content-disposition">>, <<"form-data; name=\"files\"">>},
  272. {<<"content-type">>, <<"multipart/mixed; boundary=BbC04y">>}]),
  273. InH1 = lists:sort([{<<"content-disposition">>, <<"file; filename=\"file1.txt\"">>},
  274. {<<"content-type">>, <<"text/plain">>}]),
  275. InBody1 = <<"... contents of file1.txt ...">>,
  276. InH2 = lists:sort([{<<"content-disposition">>, <<"file; filename=\"file2.gif\"">>},
  277. {<<"content-type">>, <<"image/gif">>},
  278. {<<"content-transfer-encoding">>, <<"binary">>}]),
  279. InBody2 = <<"...contents of file2.gif...">>,
  280. {ok, H1, Rest} = parse_headers(?TEST2_MIME, ?TEST2_BOUNDARY),
  281. {done, Body1, Rest2} = parse_body(Rest, ?TEST2_BOUNDARY),
  282. done = parse_body(Rest2, ?TEST2_BOUNDARY),
  283. {ok, H2Unsorted, Rest3} = parse_headers(Rest2, ?TEST2_BOUNDARY),
  284. H2 = lists:sort(H2Unsorted),
  285. {_, ContentType} = lists:keyfind(<<"content-type">>, 1, H2),
  286. {<<"multipart">>, <<"mixed">>, [{<<"boundary">>, InBoundary}]}
  287. = parse_content_type(ContentType),
  288. {ok, InH1Unsorted, InRest} = parse_headers(Rest3, InBoundary),
  289. InH1 = lists:sort(InH1Unsorted),
  290. {done, InBody1, InRest2} = parse_body(InRest, InBoundary),
  291. done = parse_body(InRest2, InBoundary),
  292. {ok, InH2Unsorted, InRest3} = parse_headers(InRest2, InBoundary),
  293. InH2 = lists:sort(InH2Unsorted),
  294. {done, InBody2, InRest4} = parse_body(InRest3, InBoundary),
  295. done = parse_body(InRest4, InBoundary),
  296. {done, Rest4} = parse_headers(InRest4, InBoundary),
  297. {done, <<>>} = parse_headers(Rest4, ?TEST2_BOUNDARY),
  298. ok.
  299. parse_epilogue_test() ->
  300. H1 = [{<<"content-type">>, <<"text/plain">>}],
  301. Body1 = <<"This is the body of the message.">>,
  302. Epilogue = <<"\r\nThis is the epilogue. Here it includes leading CRLF">>,
  303. {ok, H1, Rest} = parse_headers(?TEST3_MIME, ?TEST3_BOUNDARY),
  304. {done, Body1, Rest2} = parse_body(Rest, ?TEST3_BOUNDARY),
  305. done = parse_body(Rest2, ?TEST3_BOUNDARY),
  306. {done, Epilogue} = parse_headers(Rest2, ?TEST3_BOUNDARY),
  307. ok.
  308. parse_epilogue_crlf_test() ->
  309. H1 = [{<<"content-type">>, <<"text/plain">>}],
  310. Body1 = <<"This is the body of the message.">>,
  311. Epilogue = <<"\r\n">>,
  312. {ok, H1, Rest} = parse_headers(?TEST4_MIME, ?TEST4_BOUNDARY),
  313. {done, Body1, Rest2} = parse_body(Rest, ?TEST4_BOUNDARY),
  314. done = parse_body(Rest2, ?TEST4_BOUNDARY),
  315. {done, Epilogue} = parse_headers(Rest2, ?TEST4_BOUNDARY),
  316. ok.
  317. parse_partial_test() ->
  318. {ok, <<0:8000, "abcdef">>, <<"\rghij">>}
  319. = parse_body(<<0:8000, "abcdef\rghij">>, <<"boundary">>),
  320. {ok, <<"abcdef">>, <<"\rghij">>}
  321. = parse_body(<<"abcdef\rghij">>, <<"boundary">>),
  322. {ok, <<"abc">>, <<"\rdef">>}
  323. = parse_body(<<"abc\rdef">>, <<"boundaryboundary">>),
  324. {ok, <<0:8000, "abcdef">>, <<"\r\nghij">>}
  325. = parse_body(<<0:8000, "abcdef\r\nghij">>, <<"boundary">>),
  326. {ok, <<"abcdef">>, <<"\r\nghij">>}
  327. = parse_body(<<"abcdef\r\nghij">>, <<"boundary">>),
  328. {ok, <<"abc">>, <<"\r\ndef">>}
  329. = parse_body(<<"abc\r\ndef">>, <<"boundaryboundary">>),
  330. {ok, <<"boundary">>, <<"\r">>}
  331. = parse_body(<<"boundary\r">>, <<"boundary">>),
  332. {ok, <<"boundary">>, <<"\r\n">>}
  333. = parse_body(<<"boundary\r\n">>, <<"boundary">>),
  334. {ok, <<"boundary">>, <<"\r\n-">>}
  335. = parse_body(<<"boundary\r\n-">>, <<"boundary">>),
  336. {ok, <<"boundary">>, <<"\r\n--">>}
  337. = parse_body(<<"boundary\r\n--">>, <<"boundary">>),
  338. ok.
  339. -endif.
  340. -ifdef(PERF).
  341. perf_parse_multipart(Stream, Boundary) ->
  342. case parse_headers(Stream, Boundary) of
  343. {ok, _, Rest} ->
  344. {_, _, Rest2} = parse_body(Rest, Boundary),
  345. perf_parse_multipart(Rest2, Boundary);
  346. {done, _} ->
  347. ok
  348. end.
  349. horse_parse() ->
  350. horse:repeat(50000,
  351. perf_parse_multipart(?TEST1_MIME, ?TEST1_BOUNDARY)
  352. ).
  353. -endif.
  354. %% Building.
  355. %% @doc Generate a new random boundary.
  356. %%
  357. %% The boundary generated has a low probability of ever appearing
  358. %% in the data.
  359. -spec boundary() -> binary().
  360. boundary() ->
  361. base64:encode(crypto:strong_rand_bytes(48)).
  362. %% @doc Return the first part's head.
  363. %%
  364. %% This works exactly like the part/2 function except there is
  365. %% no leading \r\n. It's not required to use this function,
  366. %% just makes the output a little smaller and prettier.
  367. -spec first_part(binary(), headers()) -> iodata().
  368. first_part(Boundary, Headers) ->
  369. [<<"--">>, Boundary, <<"\r\n">>, headers_to_iolist(Headers, [])].
  370. %% @doc Return a part's head.
  371. -spec part(binary(), headers()) -> iodata().
  372. part(Boundary, Headers) ->
  373. [<<"\r\n--">>, Boundary, <<"\r\n">>, headers_to_iolist(Headers, [])].
  374. headers_to_iolist([], Acc) ->
  375. lists:reverse([<<"\r\n">>|Acc]);
  376. headers_to_iolist([{N, V}|Tail], Acc) ->
  377. %% We don't want to create a sublist so we list the
  378. %% values in reverse order so that it gets reversed properly.
  379. headers_to_iolist(Tail, [<<"\r\n">>, V, <<": ">>, N|Acc]).
  380. %% @doc Return the closing delimiter of the multipart message.
  381. -spec close(binary()) -> iodata().
  382. close(Boundary) ->
  383. [<<"\r\n--">>, Boundary, <<"--">>].
  384. -ifdef(TEST).
  385. build_test() ->
  386. Result = string:to_lower(binary_to_list(?TEST1_MIME)),
  387. Result = string:to_lower(binary_to_list(iolist_to_binary([
  388. <<"This is a message with multiple parts in MIME format.\r\n">>,
  389. first_part(?TEST1_BOUNDARY, [{<<"content-type">>, <<"text/plain">>}]),
  390. <<"This is the body of the message.">>,
  391. part(?TEST1_BOUNDARY, [
  392. {<<"content-type">>, <<"application/octet-stream">>},
  393. {<<"content-transfer-encoding">>, <<"base64">>}]),
  394. <<"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n"
  395. "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==">>,
  396. close(?TEST1_BOUNDARY)
  397. ]))),
  398. ok.
  399. identity_test() ->
  400. B = boundary(),
  401. Preamble = <<"This is a message with multiple parts in MIME format.">>,
  402. H1 = [{<<"content-type">>, <<"text/plain">>}],
  403. Body1 = <<"This is the body of the message.">>,
  404. H2 = lists:sort([{<<"content-type">>, <<"application/octet-stream">>},
  405. {<<"content-transfer-encoding">>, <<"base64">>}]),
  406. Body2 = <<"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n"
  407. "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==">>,
  408. Epilogue = <<"Gotta go fast!">>,
  409. M = iolist_to_binary([
  410. Preamble,
  411. part(B, H1), Body1,
  412. part(B, H2), Body2,
  413. close(B),
  414. Epilogue
  415. ]),
  416. {done, Preamble, M2} = parse_body(M, B),
  417. {ok, H1, M3} = parse_headers(M2, B),
  418. {done, Body1, M4} = parse_body(M3, B),
  419. {ok, H2Unsorted, M5} = parse_headers(M4, B),
  420. H2 = lists:sort(H2Unsorted),
  421. {done, Body2, M6} = parse_body(M5, B),
  422. {done, Epilogue} = parse_headers(M6, B),
  423. ok.
  424. -endif.
  425. -ifdef(PERF).
  426. perf_build_multipart() ->
  427. B = boundary(),
  428. [
  429. <<"preamble\r\n">>,
  430. first_part(B, [{<<"content-type">>, <<"text/plain">>}]),
  431. <<"This is the body of the message.">>,
  432. part(B, [
  433. {<<"content-type">>, <<"application/octet-stream">>},
  434. {<<"content-transfer-encoding">>, <<"base64">>}]),
  435. <<"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n"
  436. "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==">>,
  437. close(B),
  438. <<"epilogue">>
  439. ].
  440. horse_build() ->
  441. horse:repeat(50000,
  442. perf_build_multipart()
  443. ).
  444. -endif.
  445. %% Headers.
  446. %% @doc Convenience function for extracting information from headers
  447. %% when parsing a multipart/form-data stream.
  448. -spec form_data(headers())
  449. -> {data, binary()}
  450. | {file, binary(), binary(), binary(), binary()}.
  451. form_data(Headers) ->
  452. {_, DispositionBin} = lists:keyfind(<<"content-disposition">>, 1, Headers),
  453. {<<"form-data">>, Params} = parse_content_disposition(DispositionBin),
  454. {_, FieldName} = lists:keyfind(<<"name">>, 1, Params),
  455. case lists:keyfind(<<"filename">>, 1, Params) of
  456. false ->
  457. {data, FieldName};
  458. {_, Filename} ->
  459. Type = case lists:keyfind(<<"content-type">>, 1, Headers) of
  460. false -> <<"text/plain">>;
  461. {_, T} -> T
  462. end,
  463. TransferEncoding = case lists:keyfind(
  464. <<"content-transfer-encoding">>, 1, Headers) of
  465. false -> <<"7bit">>;
  466. {_, TE} -> TE
  467. end,
  468. {file, FieldName, Filename, Type, TransferEncoding}
  469. end.
  470. -ifdef(TEST).
  471. form_data_test_() ->
  472. Tests = [
  473. {[{<<"content-disposition">>, <<"form-data; name=\"submit-name\"">>}],
  474. {data, <<"submit-name">>}},
  475. {[{<<"content-disposition">>,
  476. <<"form-data; name=\"files\"; filename=\"file1.txt\"">>},
  477. {<<"content-type">>, <<"text/x-plain">>}],
  478. {file, <<"files">>, <<"file1.txt">>,
  479. <<"text/x-plain">>, <<"7bit">>}}
  480. ],
  481. [{lists:flatten(io_lib:format("~p", [V])),
  482. fun() -> R = form_data(V) end} || {V, R} <- Tests].
  483. -endif.
  484. %% @todo parse_content_description
  485. %% @todo parse_content_id
  486. %% @doc Parse an RFC 2183 content-disposition value.
  487. %% @todo Support RFC 2231.
  488. -spec parse_content_disposition(binary())
  489. -> {binary(), [{binary(), binary()}]}.
  490. parse_content_disposition(Bin) ->
  491. parse_cd_type(Bin, <<>>).
  492. parse_cd_type(<<>>, Acc) ->
  493. {Acc, []};
  494. parse_cd_type(<< C, Rest/bits >>, Acc) ->
  495. case C of
  496. $; -> {Acc, parse_before_param(Rest, [])};
  497. $\s -> {Acc, parse_before_param(Rest, [])};
  498. $\t -> {Acc, parse_before_param(Rest, [])};
  499. ?INLINE_LOWERCASE(parse_cd_type, Rest, Acc)
  500. end.
  501. -ifdef(TEST).
  502. parse_content_disposition_test_() ->
  503. Tests = [
  504. {<<"inline">>, {<<"inline">>, []}},
  505. {<<"attachment">>, {<<"attachment">>, []}},
  506. {<<"attachment; filename=genome.jpeg;"
  507. " modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";">>,
  508. {<<"attachment">>, [
  509. {<<"filename">>, <<"genome.jpeg">>},
  510. {<<"modification-date">>, <<"Wed, 12 Feb 1997 16:29:51 -0500">>}
  511. ]}},
  512. {<<"form-data; name=\"user\"">>,
  513. {<<"form-data">>, [{<<"name">>, <<"user">>}]}},
  514. {<<"form-data; NAME=\"submit-name\"">>,
  515. {<<"form-data">>, [{<<"name">>, <<"submit-name">>}]}},
  516. {<<"form-data; name=\"files\"; filename=\"file1.txt\"">>,
  517. {<<"form-data">>, [
  518. {<<"name">>, <<"files">>},
  519. {<<"filename">>, <<"file1.txt">>}
  520. ]}},
  521. {<<"file; filename=\"file1.txt\"">>,
  522. {<<"file">>, [{<<"filename">>, <<"file1.txt">>}]}},
  523. {<<"file; filename=\"file2.gif\"">>,
  524. {<<"file">>, [{<<"filename">>, <<"file2.gif">>}]}}
  525. ],
  526. [{V, fun() -> R = parse_content_disposition(V) end} || {V, R} <- Tests].
  527. -endif.
  528. -ifdef(PERF).
  529. horse_parse_content_disposition_attachment() ->
  530. horse:repeat(100000,
  531. parse_content_disposition(<<"attachment; filename=genome.jpeg;"
  532. " modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";">>)
  533. ).
  534. horse_parse_content_disposition_form_data() ->
  535. horse:repeat(100000,
  536. parse_content_disposition(
  537. <<"form-data; name=\"files\"; filename=\"file1.txt\"">>)
  538. ).
  539. horse_parse_content_disposition_inline() ->
  540. horse:repeat(100000,
  541. parse_content_disposition(<<"inline">>)
  542. ).
  543. -endif.
  544. %% @doc Parse an RFC 2045 content-transfer-encoding header.
  545. -spec parse_content_transfer_encoding(binary()) -> binary().
  546. parse_content_transfer_encoding(Bin) ->
  547. ?INLINE_LOWERCASE_BC(Bin).
  548. -ifdef(TEST).
  549. parse_content_transfer_encoding_test_() ->
  550. Tests = [
  551. {<<"7bit">>, <<"7bit">>},
  552. {<<"7BIT">>, <<"7bit">>},
  553. {<<"8bit">>, <<"8bit">>},
  554. {<<"binary">>, <<"binary">>},
  555. {<<"quoted-printable">>, <<"quoted-printable">>},
  556. {<<"base64">>, <<"base64">>},
  557. {<<"Base64">>, <<"base64">>},
  558. {<<"BASE64">>, <<"base64">>},
  559. {<<"bAsE64">>, <<"base64">>}
  560. ],
  561. [{V, fun() -> R = parse_content_transfer_encoding(V) end}
  562. || {V, R} <- Tests].
  563. -endif.
  564. -ifdef(PERF).
  565. horse_parse_content_transfer_encoding() ->
  566. horse:repeat(100000,
  567. parse_content_transfer_encoding(<<"QUOTED-PRINTABLE">>)
  568. ).
  569. -endif.
  570. %% @doc Parse an RFC 2045 content-type header.
  571. -spec parse_content_type(binary())
  572. -> {binary(), binary(), [{binary(), binary()}]}.
  573. parse_content_type(Bin) ->
  574. parse_ct_type(Bin, <<>>).
  575. parse_ct_type(<< C, Rest/bits >>, Acc) ->
  576. case C of
  577. $/ -> parse_ct_subtype(Rest, Acc, <<>>);
  578. ?INLINE_LOWERCASE(parse_ct_type, Rest, Acc)
  579. end.
  580. parse_ct_subtype(<<>>, Type, Subtype) when Subtype =/= <<>> ->
  581. {Type, Subtype, []};
  582. parse_ct_subtype(<< C, Rest/bits >>, Type, Acc) ->
  583. case C of
  584. $; -> {Type, Acc, parse_before_param(Rest, [])};
  585. $\s -> {Type, Acc, parse_before_param(Rest, [])};
  586. $\t -> {Type, Acc, parse_before_param(Rest, [])};
  587. ?INLINE_LOWERCASE(parse_ct_subtype, Rest, Type, Acc)
  588. end.
  589. -ifdef(TEST).
  590. parse_content_type_test_() ->
  591. Tests = [
  592. {<<"image/gif">>,
  593. {<<"image">>, <<"gif">>, []}},
  594. {<<"text/plain">>,
  595. {<<"text">>, <<"plain">>, []}},
  596. {<<"text/plain; charset=us-ascii">>,
  597. {<<"text">>, <<"plain">>, [{<<"charset">>, <<"us-ascii">>}]}},
  598. {<<"text/plain; charset=\"us-ascii\"">>,
  599. {<<"text">>, <<"plain">>, [{<<"charset">>, <<"us-ascii">>}]}},
  600. {<<"multipart/form-data; boundary=AaB03x">>,
  601. {<<"multipart">>, <<"form-data">>,
  602. [{<<"boundary">>, <<"AaB03x">>}]}},
  603. {<<"multipart/mixed; boundary=BbC04y">>,
  604. {<<"multipart">>, <<"mixed">>, [{<<"boundary">>, <<"BbC04y">>}]}},
  605. {<<"multipart/mixed; boundary=--------">>,
  606. {<<"multipart">>, <<"mixed">>, [{<<"boundary">>, <<"--------">>}]}},
  607. {<<"application/x-horse; filename=genome.jpeg;"
  608. " some-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";"
  609. " charset=us-ascii; empty=; number=12345">>,
  610. {<<"application">>, <<"x-horse">>, [
  611. {<<"filename">>, <<"genome.jpeg">>},
  612. {<<"some-date">>, <<"Wed, 12 Feb 1997 16:29:51 -0500">>},
  613. {<<"charset">>, <<"us-ascii">>},
  614. {<<"empty">>, <<>>},
  615. {<<"number">>, <<"12345">>}
  616. ]}}
  617. ],
  618. [{V, fun() -> R = parse_content_type(V) end}
  619. || {V, R} <- Tests].
  620. -endif.
  621. -ifdef(PERF).
  622. horse_parse_content_type_zero() ->
  623. horse:repeat(100000,
  624. parse_content_type(<<"text/plain">>)
  625. ).
  626. horse_parse_content_type_one() ->
  627. horse:repeat(100000,
  628. parse_content_type(<<"text/plain; charset=\"us-ascii\"">>)
  629. ).
  630. horse_parse_content_type_five() ->
  631. horse:repeat(100000,
  632. parse_content_type(<<"application/x-horse; filename=genome.jpeg;"
  633. " some-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";"
  634. " charset=us-ascii; empty=; number=12345">>)
  635. ).
  636. -endif.
  637. %% @doc Parse RFC 2045 parameters.
  638. parse_before_param(<<>>, Params) ->
  639. lists:reverse(Params);
  640. parse_before_param(<< C, Rest/bits >>, Params) ->
  641. case C of
  642. $; -> parse_before_param(Rest, Params);
  643. $\s -> parse_before_param(Rest, Params);
  644. $\t -> parse_before_param(Rest, Params);
  645. ?INLINE_LOWERCASE(parse_param_name, Rest, Params, <<>>)
  646. end.
  647. parse_param_name(<<>>, Params, Acc) ->
  648. lists:reverse([{Acc, <<>>}|Params]);
  649. parse_param_name(<< C, Rest/bits >>, Params, Acc) ->
  650. case C of
  651. $= -> parse_param_value(Rest, Params, Acc);
  652. ?INLINE_LOWERCASE(parse_param_name, Rest, Params, Acc)
  653. end.
  654. parse_param_value(<<>>, Params, Name) ->
  655. lists:reverse([{Name, <<>>}|Params]);
  656. parse_param_value(<< C, Rest/bits >>, Params, Name) ->
  657. case C of
  658. $" -> parse_param_quoted_value(Rest, Params, Name, <<>>);
  659. $; -> parse_before_param(Rest, [{Name, <<>>}|Params]);
  660. $\s -> parse_before_param(Rest, [{Name, <<>>}|Params]);
  661. $\t -> parse_before_param(Rest, [{Name, <<>>}|Params]);
  662. C -> parse_param_value(Rest, Params, Name, << C >>)
  663. end.
  664. parse_param_value(<<>>, Params, Name, Acc) ->
  665. lists:reverse([{Name, Acc}|Params]);
  666. parse_param_value(<< C, Rest/bits >>, Params, Name, Acc) ->
  667. case C of
  668. $; -> parse_before_param(Rest, [{Name, Acc}|Params]);
  669. $\s -> parse_before_param(Rest, [{Name, Acc}|Params]);
  670. $\t -> parse_before_param(Rest, [{Name, Acc}|Params]);
  671. C -> parse_param_value(Rest, Params, Name, << Acc/binary, C >>)
  672. end.
  673. %% We expect a final $" so no need to test for <<>>.
  674. parse_param_quoted_value(<< $\\, C, Rest/bits >>, Params, Name, Acc) ->
  675. parse_param_quoted_value(Rest, Params, Name, << Acc/binary, C >>);
  676. parse_param_quoted_value(<< $", Rest/bits >>, Params, Name, Acc) ->
  677. parse_before_param(Rest, [{Name, Acc}|Params]);
  678. parse_param_quoted_value(<< C, Rest/bits >>, Params, Name, Acc)
  679. when C =/= $\r ->
  680. parse_param_quoted_value(Rest, Params, Name, << Acc/binary, C >>).