cow_http_te.erl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_http_te).
  15. %% Identity.
  16. -export([stream_identity/2]).
  17. -export([identity/1]).
  18. %% Chunked.
  19. -export([stream_chunked/2]).
  20. -export([chunk/1]).
  21. -export([last_chunk/0]).
  22. %% The state type is the same for both identity and chunked.
  23. -type state() :: {non_neg_integer(), non_neg_integer()}.
  24. -type decode_ret() :: more
  25. | {more, Data::binary(), state()}
  26. | {more, Data::binary(), RemLen::non_neg_integer(), state()}
  27. | {more, Data::binary(), Rest::binary(), state()}
  28. | {done, TotalLen::non_neg_integer(), Rest::binary()}
  29. | {done, Data::binary(), TotalLen::non_neg_integer(), Rest::binary()}.
  30. -export_type([decode_ret/0]).
  31. -ifdef(EXTRA).
  32. dripfeed(<< C, Rest/bits >>, Acc, State, F) ->
  33. case F(<< Acc/binary, C >>, State) of
  34. more ->
  35. dripfeed(Rest, << Acc/binary, C >>, State, F);
  36. {more, _, State2} ->
  37. dripfeed(Rest, <<>>, State2, F);
  38. {more, _, _, State2} ->
  39. dripfeed(Rest, <<>>, State2, F);
  40. {more, _, _, Acc2, State2} ->
  41. dripfeed(Rest, Acc2, State2, F);
  42. {done, _, <<>>} ->
  43. ok;
  44. {done, _, _, <<>>} ->
  45. ok
  46. end.
  47. -endif.
  48. %% Identity.
  49. %% @doc Decode an identity stream.
  50. -spec stream_identity(Data, State)
  51. -> {more, Data, Len, State} | {done, Data, Len, Data}
  52. when Data::binary(), State::state(), Len::non_neg_integer().
  53. stream_identity(Data, {Streamed, Total}) ->
  54. Streamed2 = Streamed + byte_size(Data),
  55. if
  56. Streamed2 < Total ->
  57. {more, Data, Total - Streamed2, {Streamed2, Total}};
  58. true ->
  59. Size = Total - Streamed,
  60. << Data2:Size/binary, Rest/bits >> = Data,
  61. {done, Data2, Total, Rest}
  62. end.
  63. -spec identity(Data) -> Data when Data::iodata().
  64. identity(Data) ->
  65. Data.
  66. -ifdef(TEST).
  67. stream_identity_test() ->
  68. {done, <<>>, 0, <<>>}
  69. = stream_identity(identity(<<>>), {0, 0}),
  70. {done, <<"\r\n">>, 2, <<>>}
  71. = stream_identity(identity(<<"\r\n">>), {0, 2}),
  72. {done, << 0:80000 >>, 10000, <<>>}
  73. = stream_identity(identity(<< 0:80000 >>), {0, 10000}),
  74. ok.
  75. stream_identity_parts_test() ->
  76. {more, << 0:8000 >>, 1999, S1}
  77. = stream_identity(<< 0:8000 >>, {0, 2999}),
  78. {more, << 0:8000 >>, 999, S2}
  79. = stream_identity(<< 0:8000 >>, S1),
  80. {done, << 0:7992 >>, 2999, <<>>}
  81. = stream_identity(<< 0:7992 >>, S2),
  82. ok.
  83. -endif.
  84. -ifdef(PERF).
  85. %% Using the same data as the chunked one for comparison.
  86. horse_stream_identity() ->
  87. horse:repeat(10000,
  88. stream_identity(<<
  89. "4\r\n"
  90. "Wiki\r\n"
  91. "5\r\n"
  92. "pedia\r\n"
  93. "e\r\n"
  94. " in\r\n\r\nchunks.\r\n"
  95. "0\r\n"
  96. "\r\n">>, {0, 43})
  97. ).
  98. horse_stream_identity_dripfeed() ->
  99. horse:repeat(10000,
  100. dripfeed(<<
  101. "4\r\n"
  102. "Wiki\r\n"
  103. "5\r\n"
  104. "pedia\r\n"
  105. "e\r\n"
  106. " in\r\n\r\nchunks.\r\n"
  107. "0\r\n"
  108. "\r\n">>, <<>>, {0, 43}, fun stream_identity/2)
  109. ).
  110. -endif.
  111. %% Chunked.
  112. %% @doc Decode a chunked stream.
  113. -spec stream_chunked(Data, State)
  114. -> more | {more, Data, State} | {more, Data, Len, State}
  115. | {more, Data, Data, State}
  116. | {done, Len, Data} | {done, Data, Len, Data}
  117. when Data::binary(), State::state(), Len::non_neg_integer().
  118. stream_chunked(Data, State) ->
  119. stream_chunked(Data, State, <<>>).
  120. %% New chunk.
  121. stream_chunked(Data = << C, _/bits >>, {0, Streamed}, Acc) when C =/= $\r ->
  122. case chunked_len(Data, Streamed, Acc, 0) of
  123. {next, Rest, State, Acc2} ->
  124. stream_chunked(Rest, State, Acc2);
  125. {more, State, Acc2} ->
  126. {more, Acc2, Data, State};
  127. Ret ->
  128. Ret
  129. end;
  130. %% Trailing \r\n before next chunk.
  131. stream_chunked(<< "\r\n", Rest/bits >>, {2, Streamed}, Acc) ->
  132. stream_chunked(Rest, {0, Streamed}, Acc);
  133. %% Trailing \r before next chunk.
  134. stream_chunked(<< "\r" >>, {2, Streamed}, Acc) ->
  135. {more, Acc, {1, Streamed}};
  136. %% Trailing \n before next chunk.
  137. stream_chunked(<< "\n", Rest/bits >>, {1, Streamed}, Acc) ->
  138. stream_chunked(Rest, {0, Streamed}, Acc);
  139. %% More data needed.
  140. stream_chunked(<<>>, State = {Rem, _}, Acc) ->
  141. {more, Acc, Rem, State};
  142. %% Chunk data.
  143. stream_chunked(Data, {Rem, Streamed}, Acc) when Rem > 2 ->
  144. DataSize = byte_size(Data),
  145. RemSize = Rem - 2,
  146. case Data of
  147. << Chunk:RemSize/binary, "\r\n", Rest/bits >> ->
  148. stream_chunked(Rest, {0, Streamed + RemSize}, << Acc/binary, Chunk/binary >>);
  149. << Chunk:RemSize/binary, "\r" >> ->
  150. {more, << Acc/binary, Chunk/binary >>, {1, Streamed + RemSize}};
  151. %% Everything in Data is part of the chunk.
  152. _ ->
  153. Rem2 = Rem - DataSize,
  154. {more, << Acc/binary, Data/binary >>, Rem2, {Rem2, Streamed + DataSize}}
  155. end.
  156. chunked_len(<< $0, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16);
  157. chunked_len(<< $1, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 1);
  158. chunked_len(<< $2, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 2);
  159. chunked_len(<< $3, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 3);
  160. chunked_len(<< $4, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 4);
  161. chunked_len(<< $5, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 5);
  162. chunked_len(<< $6, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 6);
  163. chunked_len(<< $7, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 7);
  164. chunked_len(<< $8, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 8);
  165. chunked_len(<< $9, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 9);
  166. chunked_len(<< $A, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 10);
  167. chunked_len(<< $B, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 11);
  168. chunked_len(<< $C, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 12);
  169. chunked_len(<< $D, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 13);
  170. chunked_len(<< $E, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 14);
  171. chunked_len(<< $F, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 15);
  172. chunked_len(<< $a, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 10);
  173. chunked_len(<< $b, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 11);
  174. chunked_len(<< $c, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 12);
  175. chunked_len(<< $d, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 13);
  176. chunked_len(<< $e, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 14);
  177. chunked_len(<< $f, R/bits >>, S, A, Len) -> chunked_len(R, S, A, Len * 16 + 15);
  178. %% Final chunk.
  179. chunked_len(<< "\r\n\r\n", R/bits >>, S, <<>>, 0) -> {done, S, R};
  180. chunked_len(<< "\r\n\r\n", R/bits >>, S, A, 0) -> {done, A, S, R};
  181. chunked_len(_, _, _, 0) -> more;
  182. %% Normal chunk. Add 2 to Len for the trailing \r\n.
  183. chunked_len(<< "\r\n", R/bits >>, S, A, Len) -> {next, R, {Len + 2, S}, A};
  184. chunked_len(<<"\r">>, _, <<>>, _) -> more;
  185. chunked_len(<<"\r">>, S, A, _) -> {more, {0, S}, A};
  186. chunked_len(<<>>, _, <<>>, _) -> more;
  187. chunked_len(<<>>, S, A, _) -> {more, {0, S}, A}.
  188. %% @doc Encode a chunk.
  189. -spec chunk(D) -> D when D::iodata().
  190. chunk(Data) ->
  191. [integer_to_list(iolist_size(Data), 16), <<"\r\n">>,
  192. Data, <<"\r\n">>].
  193. %% @doc Encode the last chunk of a chunked stream.
  194. -spec last_chunk() -> << _:40 >>.
  195. last_chunk() ->
  196. <<"0\r\n\r\n">>.
  197. -ifdef(TEST).
  198. stream_chunked_identity_test() ->
  199. {done, <<"Wikipedia in\r\n\r\nchunks.">>, 23, <<>>}
  200. = stream_chunked(iolist_to_binary([
  201. chunk("Wiki"),
  202. chunk("pedia"),
  203. chunk(" in\r\n\r\nchunks."),
  204. last_chunk()
  205. ]), {0, 0}),
  206. ok.
  207. stream_chunked_one_pass_test() ->
  208. {done, 0, <<>>} = stream_chunked(<<"0\r\n\r\n">>, {0, 0}),
  209. {done, <<"Wikipedia in\r\n\r\nchunks.">>, 23, <<>>}
  210. = stream_chunked(<<
  211. "4\r\n"
  212. "Wiki\r\n"
  213. "5\r\n"
  214. "pedia\r\n"
  215. "e\r\n"
  216. " in\r\n\r\nchunks.\r\n"
  217. "0\r\n"
  218. "\r\n">>, {0, 0}),
  219. ok.
  220. stream_chunked_n_passes_test() ->
  221. S0 = {0, 0},
  222. more = stream_chunked(<<"4\r">>, S0),
  223. {more, <<>>, 6, S1} = stream_chunked(<<"4\r\n">>, S0),
  224. {more, <<"Wiki">>, 0, S2} = stream_chunked(<<"Wiki\r\n">>, S1),
  225. {more, <<"pedia">>, 0, <<"e\r">>, S3} = stream_chunked(<<"5\r\npedia\r\ne\r">>, S2),
  226. {more, <<" in\r\n\r\nchunks.">>, 2, S4} = stream_chunked(<<"e\r\n in\r\n\r\nchunks.">>, S3),
  227. {done, 23, <<>>} = stream_chunked(<<"\r\n0\r\n\r\n">>, S4),
  228. %% A few extra for coverage purposes.
  229. more = stream_chunked(<<"\n3">>, {1, 0}),
  230. {more, <<"abc">>, 2, {2, 3}} = stream_chunked(<<"\n3\r\nabc">>, {1, 0}),
  231. {more, <<"abc">>, {1, 3}} = stream_chunked(<<"3\r\nabc\r">>, {0, 0}),
  232. {more, <<"abc">>, 0, <<"123">>, {0, 3}} = stream_chunked(<<"3\r\nabc\r\n123">>, {0, 0}),
  233. ok.
  234. stream_chunked_dripfeed_test() ->
  235. dripfeed(<<
  236. "4\r\n"
  237. "Wiki\r\n"
  238. "5\r\n"
  239. "pedia\r\n"
  240. "e\r\n"
  241. " in\r\n\r\nchunks.\r\n"
  242. "0\r\n"
  243. "\r\n">>, <<>>, {0, 0}, fun stream_chunked/2).
  244. stream_chunked_error_test_() ->
  245. Tests = [
  246. {<<>>, undefined},
  247. {<<"\n\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa">>, {2, 0}}
  248. ],
  249. [{lists:flatten(io_lib:format("value ~p state ~p", [V, S])),
  250. fun() -> {'EXIT', _} = (catch stream_chunked(V, S)) end}
  251. || {V, S} <- Tests].
  252. -endif.
  253. -ifdef(PERF).
  254. horse_stream_chunked() ->
  255. horse:repeat(10000,
  256. stream_chunked(<<
  257. "4\r\n"
  258. "Wiki\r\n"
  259. "5\r\n"
  260. "pedia\r\n"
  261. "e\r\n"
  262. " in\r\n\r\nchunks.\r\n"
  263. "0\r\n"
  264. "\r\n">>, {0, 0})
  265. ).
  266. horse_stream_chunked_dripfeed() ->
  267. horse:repeat(10000,
  268. dripfeed(<<
  269. "4\r\n"
  270. "Wiki\r\n"
  271. "5\r\n"
  272. "pedia\r\n"
  273. "e\r\n"
  274. " in\r\n\r\nchunks.\r\n"
  275. "0\r\n"
  276. "\r\n">>, <<>>, {0, 43}, fun stream_chunked/2)
  277. ).
  278. -endif.