cow_hpack.erl 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. %% Copyright (c) 2015-2018, 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. %% The current implementation is not suitable for use in
  15. %% intermediaries as the information about headers that
  16. %% should never be indexed is currently lost.
  17. -module(cow_hpack).
  18. -export([init/0]).
  19. -export([init/1]).
  20. -export([set_max_size/2]).
  21. -export([decode/1]).
  22. -export([decode/2]).
  23. -export([encode/1]).
  24. -export([encode/2]).
  25. -export([encode/3]).
  26. -record(state, {
  27. size = 0 :: non_neg_integer(),
  28. max_size = 4096 :: non_neg_integer(),
  29. configured_max_size = 4096 :: non_neg_integer(),
  30. dyn_table = [] :: [{pos_integer(), {binary(), binary()}}]
  31. }).
  32. -opaque state() :: #state{}.
  33. -export_type([state/0]).
  34. -type opts() :: map().
  35. -export_type([opts/0]).
  36. -ifdef(TEST).
  37. -include_lib("proper/include/proper.hrl").
  38. -endif.
  39. %% State initialization.
  40. -spec init() -> state().
  41. init() ->
  42. #state{}.
  43. -spec init(non_neg_integer()) -> state().
  44. init(MaxSize) ->
  45. #state{max_size=MaxSize, configured_max_size=MaxSize}.
  46. %% Update the configured max size.
  47. %%
  48. %% When decoding, the local endpoint also needs to send a SETTINGS
  49. %% frame with this value and it is then up to the remote endpoint
  50. %% to decide what actual limit it will use. The actual limit is
  51. %% signaled via dynamic table size updates in the encoded data.
  52. %%
  53. %% When encoding, the local endpoint will call this function after
  54. %% receiving a SETTINGS frame with this value. The encoder will
  55. %% then use this value as the new max after signaling via a dynamic
  56. %% table size update. The value given as argument may be lower
  57. %% than the one received in the SETTINGS.
  58. -spec set_max_size(non_neg_integer(), State) -> State when State::state().
  59. set_max_size(MaxSize, State) ->
  60. State#state{configured_max_size=MaxSize}.
  61. %% Decoding.
  62. -spec decode(binary()) -> {cow_http:headers(), state()}.
  63. decode(Data) ->
  64. decode(Data, init()).
  65. -spec decode(binary(), State) -> {cow_http:headers(), State} when State::state().
  66. %% Dynamic table size update is only allowed at the beginning of a HEADERS block.
  67. decode(<< 0:2, 1:1, Rest/bits >>, State=#state{configured_max_size=ConfigMaxSize}) ->
  68. {MaxSize, Rest2} = dec_int5(Rest),
  69. if
  70. MaxSize =< ConfigMaxSize ->
  71. State2 = table_update_size(MaxSize, State),
  72. decode(Rest2, State2)
  73. end;
  74. decode(Data, State) ->
  75. decode(Data, State, []).
  76. decode(<<>>, State, Acc) ->
  77. {lists:reverse(Acc), State};
  78. %% Indexed header field representation.
  79. decode(<< 1:1, Rest/bits >>, State, Acc) ->
  80. dec_indexed(Rest, State, Acc);
  81. %% Literal header field with incremental indexing: new name.
  82. decode(<< 0:1, 1:1, 0:6, Rest/bits >>, State, Acc) ->
  83. dec_lit_index_new_name(Rest, State, Acc);
  84. %% Literal header field with incremental indexing: indexed name.
  85. decode(<< 0:1, 1:1, Rest/bits >>, State, Acc) ->
  86. dec_lit_index_indexed_name(Rest, State, Acc);
  87. %% Literal header field without indexing: new name.
  88. decode(<< 0:8, Rest/bits >>, State, Acc) ->
  89. dec_lit_no_index_new_name(Rest, State, Acc);
  90. %% Literal header field without indexing: indexed name.
  91. decode(<< 0:4, Rest/bits >>, State, Acc) ->
  92. dec_lit_no_index_indexed_name(Rest, State, Acc);
  93. %% Literal header field never indexed: new name.
  94. %% @todo Keep track of "never indexed" headers.
  95. decode(<< 0:3, 1:1, 0:4, Rest/bits >>, State, Acc) ->
  96. dec_lit_no_index_new_name(Rest, State, Acc);
  97. %% Literal header field never indexed: indexed name.
  98. %% @todo Keep track of "never indexed" headers.
  99. decode(<< 0:3, 1:1, Rest/bits >>, State, Acc) ->
  100. dec_lit_no_index_indexed_name(Rest, State, Acc).
  101. %% Indexed header field representation.
  102. dec_indexed(Rest, State, Acc) ->
  103. {Index, Rest2} = dec_int7(Rest),
  104. {Name, Value} = table_get(Index, State),
  105. decode(Rest2, State, [{Name, Value}|Acc]).
  106. %% Literal header field with incremental indexing.
  107. dec_lit_index_new_name(Rest, State, Acc) ->
  108. {Name, Rest2} = dec_str(Rest),
  109. dec_lit_index(Rest2, State, Acc, Name).
  110. dec_lit_index_indexed_name(Rest, State, Acc) ->
  111. {Index, Rest2} = dec_int6(Rest),
  112. Name = table_get_name(Index, State),
  113. dec_lit_index(Rest2, State, Acc, Name).
  114. dec_lit_index(Rest, State, Acc, Name) ->
  115. {Value, Rest2} = dec_str(Rest),
  116. State2 = table_insert({Name, Value}, State),
  117. decode(Rest2, State2, [{Name, Value}|Acc]).
  118. %% Literal header field without indexing.
  119. dec_lit_no_index_new_name(Rest, State, Acc) ->
  120. {Name, Rest2} = dec_str(Rest),
  121. dec_lit_no_index(Rest2, State, Acc, Name).
  122. dec_lit_no_index_indexed_name(Rest, State, Acc) ->
  123. {Index, Rest2} = dec_int4(Rest),
  124. Name = table_get_name(Index, State),
  125. dec_lit_no_index(Rest2, State, Acc, Name).
  126. dec_lit_no_index(Rest, State, Acc, Name) ->
  127. {Value, Rest2} = dec_str(Rest),
  128. decode(Rest2, State, [{Name, Value}|Acc]).
  129. %% @todo Literal header field never indexed.
  130. %% Decode an integer.
  131. %% The HPACK format has 4 different integer prefixes length (from 4 to 7)
  132. %% and each can be used to create an indefinite length integer if all bits
  133. %% of the prefix are set to 1.
  134. dec_int4(<< 2#1111:4, Rest/bits >>) ->
  135. dec_big_int(Rest, 15, 0);
  136. dec_int4(<< Int:4, Rest/bits >>) ->
  137. {Int, Rest}.
  138. dec_int5(<< 2#11111:5, Rest/bits >>) ->
  139. dec_big_int(Rest, 31, 0);
  140. dec_int5(<< Int:5, Rest/bits >>) ->
  141. {Int, Rest}.
  142. dec_int6(<< 2#111111:6, Rest/bits >>) ->
  143. dec_big_int(Rest, 63, 0);
  144. dec_int6(<< Int:6, Rest/bits >>) ->
  145. {Int, Rest}.
  146. dec_int7(<< 2#1111111:7, Rest/bits >>) ->
  147. dec_big_int(Rest, 127, 0);
  148. dec_int7(<< Int:7, Rest/bits >>) ->
  149. {Int, Rest}.
  150. dec_big_int(<< 0:1, Value:7, Rest/bits >>, Int, M) ->
  151. {Int + (Value bsl M), Rest};
  152. dec_big_int(<< 1:1, Value:7, Rest/bits >>, Int, M) ->
  153. dec_big_int(Rest, Int + (Value bsl M), M + 7).
  154. %% Decode a string.
  155. dec_str(<< 0:1, Rest/bits >>) ->
  156. {Length, Rest2} = dec_int7(Rest),
  157. << Str:Length/binary, Rest3/bits >> = Rest2,
  158. {Str, Rest3};
  159. dec_str(<< 1:1, Rest/bits >>) ->
  160. {Length, Rest2} = dec_int7(Rest),
  161. dec_huffman(Rest2, Length, 0, ok, <<>>).
  162. %% We use a lookup table that allows us to benefit from
  163. %% the binary match context optimization. A more naive
  164. %% implementation using bit pattern matching cannot reuse
  165. %% a match context because it wouldn't always match on
  166. %% byte boundaries.
  167. %%
  168. %% See cow_hpack_dec_huffman_lookup.hrl for more details.
  169. dec_huffman(<<A:4, B:4, R/bits>>, Len, Huff0, _, Acc) when Len > 0 ->
  170. {_, CharA, Huff1} = dec_huffman_lookup(Huff0, A),
  171. {Result, CharB, Huff} = dec_huffman_lookup(Huff1, B),
  172. case {CharA, CharB} of
  173. {undefined, undefined} -> dec_huffman(R, Len - 1, Huff, Result, Acc);
  174. {CharA, undefined} -> dec_huffman(R, Len - 1, Huff, Result, <<Acc/binary, CharA>>);
  175. {undefined, CharB} -> dec_huffman(R, Len - 1, Huff, Result, <<Acc/binary, CharB>>);
  176. {CharA, CharB} -> dec_huffman(R, Len - 1, Huff, Result, <<Acc/binary, CharA, CharB>>)
  177. end;
  178. dec_huffman(Rest, 0, _, ok, Acc) ->
  179. {Acc, Rest}.
  180. -include("cow_hpack_dec_huffman_lookup.hrl").
  181. -ifdef(TEST).
  182. req_decode_test() ->
  183. %% First request (raw then huffman).
  184. {Headers1, State1} = decode(<< 16#828684410f7777772e6578616d706c652e636f6d:160 >>),
  185. {Headers1, State1} = decode(<< 16#828684418cf1e3c2e5f23a6ba0ab90f4ff:136 >>),
  186. Headers1 = [
  187. {<<":method">>, <<"GET">>},
  188. {<<":scheme">>, <<"http">>},
  189. {<<":path">>, <<"/">>},
  190. {<<":authority">>, <<"www.example.com">>}
  191. ],
  192. #state{size=57, dyn_table=[{57,{<<":authority">>, <<"www.example.com">>}}]} = State1,
  193. %% Second request (raw then huffman).
  194. {Headers2, State2} = decode(<< 16#828684be58086e6f2d6361636865:112 >>, State1),
  195. {Headers2, State2} = decode(<< 16#828684be5886a8eb10649cbf:96 >>, State1),
  196. Headers2 = [
  197. {<<":method">>, <<"GET">>},
  198. {<<":scheme">>, <<"http">>},
  199. {<<":path">>, <<"/">>},
  200. {<<":authority">>, <<"www.example.com">>},
  201. {<<"cache-control">>, <<"no-cache">>}
  202. ],
  203. #state{size=110, dyn_table=[
  204. {53,{<<"cache-control">>, <<"no-cache">>}},
  205. {57,{<<":authority">>, <<"www.example.com">>}}]} = State2,
  206. %% Third request (raw then huffman).
  207. {Headers3, State3} = decode(<< 16#828785bf400a637573746f6d2d6b65790c637573746f6d2d76616c7565:232 >>, State2),
  208. {Headers3, State3} = decode(<< 16#828785bf408825a849e95ba97d7f8925a849e95bb8e8b4bf:192 >>, State2),
  209. Headers3 = [
  210. {<<":method">>, <<"GET">>},
  211. {<<":scheme">>, <<"https">>},
  212. {<<":path">>, <<"/index.html">>},
  213. {<<":authority">>, <<"www.example.com">>},
  214. {<<"custom-key">>, <<"custom-value">>}
  215. ],
  216. #state{size=164, dyn_table=[
  217. {54,{<<"custom-key">>, <<"custom-value">>}},
  218. {53,{<<"cache-control">>, <<"no-cache">>}},
  219. {57,{<<":authority">>, <<"www.example.com">>}}]} = State3,
  220. ok.
  221. resp_decode_test() ->
  222. %% Use a max_size of 256 to trigger header evictions.
  223. State0 = init(256),
  224. %% First response (raw then huffman).
  225. {Headers1, State1} = decode(<< 16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560 >>, State0),
  226. {Headers1, State1} = decode(<< 16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432 >>, State0),
  227. Headers1 = [
  228. {<<":status">>, <<"302">>},
  229. {<<"cache-control">>, <<"private">>},
  230. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  231. {<<"location">>, <<"https://www.example.com">>}
  232. ],
  233. #state{size=222, dyn_table=[
  234. {63,{<<"location">>, <<"https://www.example.com">>}},
  235. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  236. {52,{<<"cache-control">>, <<"private">>}},
  237. {42,{<<":status">>, <<"302">>}}]} = State1,
  238. %% Second response (raw then huffman).
  239. {Headers2, State2} = decode(<< 16#4803333037c1c0bf:64 >>, State1),
  240. {Headers2, State2} = decode(<< 16#4883640effc1c0bf:64 >>, State1),
  241. Headers2 = [
  242. {<<":status">>, <<"307">>},
  243. {<<"cache-control">>, <<"private">>},
  244. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  245. {<<"location">>, <<"https://www.example.com">>}
  246. ],
  247. #state{size=222, dyn_table=[
  248. {42,{<<":status">>, <<"307">>}},
  249. {63,{<<"location">>, <<"https://www.example.com">>}},
  250. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  251. {52,{<<"cache-control">>, <<"private">>}}]} = State2,
  252. %% Third response (raw then huffman).
  253. {Headers3, State3} = decode(<< 16#88c1611d4d6f6e2c203231204f637420323031332032303a31333a323220474d54c05a04677a69707738666f6f3d4153444a4b48514b425a584f5157454f50495541585157454f49553b206d61782d6167653d333630303b2076657273696f6e3d31:784 >>, State2),
  254. {Headers3, State3} = decode(<< 16#88c16196d07abe941054d444a8200595040b8166e084a62d1bffc05a839bd9ab77ad94e7821dd7f2e6c7b335dfdfcd5b3960d5af27087f3672c1ab270fb5291f9587316065c003ed4ee5b1063d5007:632 >>, State2),
  255. Headers3 = [
  256. {<<":status">>, <<"200">>},
  257. {<<"cache-control">>, <<"private">>},
  258. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:22 GMT">>},
  259. {<<"location">>, <<"https://www.example.com">>},
  260. {<<"content-encoding">>, <<"gzip">>},
  261. {<<"set-cookie">>, <<"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1">>}
  262. ],
  263. #state{size=215, dyn_table=[
  264. {98,{<<"set-cookie">>, <<"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1">>}},
  265. {52,{<<"content-encoding">>, <<"gzip">>}},
  266. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:22 GMT">>}}]} = State3,
  267. ok.
  268. table_update_decode_test() ->
  269. %% Use a max_size of 256 to trigger header evictions
  270. %% when the code is not updating the max size.
  271. State0 = init(256),
  272. %% First response (raw then huffman).
  273. {Headers1, State1} = decode(<< 16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560 >>, State0),
  274. {Headers1, State1} = decode(<< 16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432 >>, State0),
  275. Headers1 = [
  276. {<<":status">>, <<"302">>},
  277. {<<"cache-control">>, <<"private">>},
  278. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  279. {<<"location">>, <<"https://www.example.com">>}
  280. ],
  281. #state{size=222, configured_max_size=256, dyn_table=[
  282. {63,{<<"location">>, <<"https://www.example.com">>}},
  283. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  284. {52,{<<"cache-control">>, <<"private">>}},
  285. {42,{<<":status">>, <<"302">>}}]} = State1,
  286. %% Set a new configured max_size to avoid header evictions.
  287. State2 = set_max_size(512, State1),
  288. %% Second response with the table size update (raw then huffman).
  289. MaxSize = enc_big_int(512 - 31, []),
  290. {Headers2, State3} = decode(
  291. iolist_to_binary([<< 2#00111111>>, MaxSize, <<16#4803333037c1c0bf:64>>]),
  292. State2),
  293. {Headers2, State3} = decode(
  294. iolist_to_binary([<< 2#00111111>>, MaxSize, <<16#4883640effc1c0bf:64>>]),
  295. State2),
  296. Headers2 = [
  297. {<<":status">>, <<"307">>},
  298. {<<"cache-control">>, <<"private">>},
  299. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  300. {<<"location">>, <<"https://www.example.com">>}
  301. ],
  302. #state{size=264, configured_max_size=512, dyn_table=[
  303. {42,{<<":status">>, <<"307">>}},
  304. {63,{<<"location">>, <<"https://www.example.com">>}},
  305. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  306. {52,{<<"cache-control">>, <<"private">>}},
  307. {42,{<<":status">>, <<"302">>}}]} = State3,
  308. ok.
  309. table_update_decode_smaller_test() ->
  310. %% Use a max_size of 256 to trigger header evictions
  311. %% when the code is not updating the max size.
  312. State0 = init(256),
  313. %% First response (raw then huffman).
  314. {Headers1, State1} = decode(<< 16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560 >>, State0),
  315. {Headers1, State1} = decode(<< 16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432 >>, State0),
  316. Headers1 = [
  317. {<<":status">>, <<"302">>},
  318. {<<"cache-control">>, <<"private">>},
  319. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  320. {<<"location">>, <<"https://www.example.com">>}
  321. ],
  322. #state{size=222, configured_max_size=256, dyn_table=[
  323. {63,{<<"location">>, <<"https://www.example.com">>}},
  324. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  325. {52,{<<"cache-control">>, <<"private">>}},
  326. {42,{<<":status">>, <<"302">>}}]} = State1,
  327. %% Set a new configured max_size to avoid header evictions.
  328. State2 = set_max_size(512, State1),
  329. %% Second response with the table size update smaller than the limit (raw then huffman).
  330. MaxSize = enc_big_int(400 - 31, []),
  331. {Headers2, State3} = decode(
  332. iolist_to_binary([<< 2#00111111>>, MaxSize, <<16#4803333037c1c0bf:64>>]),
  333. State2),
  334. {Headers2, State3} = decode(
  335. iolist_to_binary([<< 2#00111111>>, MaxSize, <<16#4883640effc1c0bf:64>>]),
  336. State2),
  337. Headers2 = [
  338. {<<":status">>, <<"307">>},
  339. {<<"cache-control">>, <<"private">>},
  340. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  341. {<<"location">>, <<"https://www.example.com">>}
  342. ],
  343. #state{size=264, configured_max_size=512, dyn_table=[
  344. {42,{<<":status">>, <<"307">>}},
  345. {63,{<<"location">>, <<"https://www.example.com">>}},
  346. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  347. {52,{<<"cache-control">>, <<"private">>}},
  348. {42,{<<":status">>, <<"302">>}}]} = State3,
  349. ok.
  350. table_update_decode_too_large_test() ->
  351. %% Use a max_size of 256 to trigger header evictions
  352. %% when the code is not updating the max size.
  353. State0 = init(256),
  354. %% First response (raw then huffman).
  355. {Headers1, State1} = decode(<< 16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560 >>, State0),
  356. {Headers1, State1} = decode(<< 16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432 >>, State0),
  357. Headers1 = [
  358. {<<":status">>, <<"302">>},
  359. {<<"cache-control">>, <<"private">>},
  360. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  361. {<<"location">>, <<"https://www.example.com">>}
  362. ],
  363. #state{size=222, configured_max_size=256, dyn_table=[
  364. {63,{<<"location">>, <<"https://www.example.com">>}},
  365. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  366. {52,{<<"cache-control">>, <<"private">>}},
  367. {42,{<<":status">>, <<"302">>}}]} = State1,
  368. %% Set a new configured max_size to avoid header evictions.
  369. State2 = set_max_size(512, State1),
  370. %% Second response with the table size update (raw then huffman).
  371. MaxSize = enc_big_int(1024 - 31, []),
  372. {'EXIT', _} = (catch decode(
  373. iolist_to_binary([<< 2#00111111>>, MaxSize, <<16#4803333037c1c0bf:64>>]),
  374. State2)),
  375. {'EXIT', _} = (catch decode(
  376. iolist_to_binary([<< 2#00111111>>, MaxSize, <<16#4883640effc1c0bf:64>>]),
  377. State2)),
  378. ok.
  379. table_update_decode_zero_test() ->
  380. State0 = init(256),
  381. %% First response (raw then huffman).
  382. {Headers1, State1} = decode(<< 16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560 >>, State0),
  383. {Headers1, State1} = decode(<< 16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432 >>, State0),
  384. Headers1 = [
  385. {<<":status">>, <<"302">>},
  386. {<<"cache-control">>, <<"private">>},
  387. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  388. {<<"location">>, <<"https://www.example.com">>}
  389. ],
  390. #state{size=222, configured_max_size=256, dyn_table=[
  391. {63,{<<"location">>, <<"https://www.example.com">>}},
  392. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  393. {52,{<<"cache-control">>, <<"private">>}},
  394. {42,{<<":status">>, <<"302">>}}]} = State1,
  395. %% Set a new configured max_size to avoid header evictions.
  396. State2 = set_max_size(512, State1),
  397. %% Second response with the table size update (raw then huffman).
  398. %% We set the table size to 0 to evict all values before setting
  399. %% it to 512 so we only get the second request indexed.
  400. MaxSize = enc_big_int(512 - 31, []),
  401. {Headers1, State3} = decode(iolist_to_binary([
  402. <<2#00100000, 2#00111111>>, MaxSize,
  403. <<16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560>>]),
  404. State2),
  405. {Headers1, State3} = decode(iolist_to_binary([
  406. <<2#00100000, 2#00111111>>, MaxSize,
  407. <<16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432>>]),
  408. State2),
  409. #state{size=222, configured_max_size=512, dyn_table=[
  410. {63,{<<"location">>, <<"https://www.example.com">>}},
  411. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  412. {52,{<<"cache-control">>, <<"private">>}},
  413. {42,{<<":status">>, <<"302">>}}]} = State3,
  414. ok.
  415. horse_decode_raw() ->
  416. horse:repeat(20000,
  417. do_horse_decode_raw()
  418. ).
  419. do_horse_decode_raw() ->
  420. {_, State1} = decode(<<16#828684410f7777772e6578616d706c652e636f6d:160>>),
  421. {_, State2} = decode(<<16#828684be58086e6f2d6361636865:112>>, State1),
  422. {_, _} = decode(<<16#828785bf400a637573746f6d2d6b65790c637573746f6d2d76616c7565:232>>, State2),
  423. ok.
  424. horse_decode_huffman() ->
  425. horse:repeat(20000,
  426. do_horse_decode_huffman()
  427. ).
  428. do_horse_decode_huffman() ->
  429. {_, State1} = decode(<<16#828684418cf1e3c2e5f23a6ba0ab90f4ff:136>>),
  430. {_, State2} = decode(<<16#828684be5886a8eb10649cbf:96>>, State1),
  431. {_, _} = decode(<<16#828785bf408825a849e95ba97d7f8925a849e95bb8e8b4bf:192>>, State2),
  432. ok.
  433. -endif.
  434. %% Encoding.
  435. -spec encode(cow_http:headers()) -> {iodata(), state()}.
  436. encode(Headers) ->
  437. encode(Headers, init(), #{}, []).
  438. -spec encode(cow_http:headers(), State) -> {iodata(), State} when State::state().
  439. encode(Headers, State=#state{max_size=MaxSize, configured_max_size=MaxSize}) ->
  440. encode(Headers, State, #{}, []);
  441. encode(Headers, State0=#state{configured_max_size=MaxSize}) ->
  442. {Data, State} = encode(Headers, State0#state{max_size=MaxSize}, #{}, []),
  443. {[enc_int5(MaxSize, 2#001), Data], State}.
  444. -spec encode(cow_http:headers(), State, opts()) -> {iodata(), State} when State::state().
  445. encode(Headers, State=#state{max_size=MaxSize, configured_max_size=MaxSize}, Opts) ->
  446. encode(Headers, State, Opts, []);
  447. encode(Headers, State0=#state{configured_max_size=MaxSize}, Opts) ->
  448. {Data, State} = encode(Headers, State0#state{max_size=MaxSize}, Opts, []),
  449. {[enc_int5(MaxSize, 2#001), Data], State}.
  450. %% @todo Handle cases where no/never indexing is expected.
  451. encode([], State, _, Acc) ->
  452. {lists:reverse(Acc), State};
  453. encode([{Name, Value0}|Tail], State, Opts, Acc) ->
  454. %% We conditionally call iolist_to_binary/1 because a small
  455. %% but noticeable speed improvement happens when we do this.
  456. Value = if
  457. is_binary(Value0) -> Value0;
  458. true -> iolist_to_binary(Value0)
  459. end,
  460. Header = {Name, Value},
  461. case table_find(Header, State) of
  462. %% Indexed header field representation.
  463. {field, Index} ->
  464. encode(Tail, State, Opts, [enc_int7(Index, 2#1)|Acc]);
  465. %% Literal header field representation: indexed name.
  466. {name, Index} ->
  467. State2 = table_insert(Header, State),
  468. encode(Tail, State2, Opts, [[enc_int6(Index, 2#01), enc_str(Value, Opts)]|Acc]);
  469. %% Literal header field representation: new name.
  470. not_found ->
  471. State2 = table_insert(Header, State),
  472. encode(Tail, State2, Opts, [[<< 0:1, 1:1, 0:6 >>, enc_str(Name, Opts), enc_str(Value, Opts)]|Acc])
  473. end.
  474. %% Encode an integer.
  475. enc_int5(Int, Prefix) when Int < 31 ->
  476. << Prefix:3, Int:5 >>;
  477. enc_int5(Int, Prefix) ->
  478. [<< Prefix:3, 2#11111:5 >>|enc_big_int(Int - 31, [])].
  479. enc_int6(Int, Prefix) when Int < 63 ->
  480. << Prefix:2, Int:6 >>;
  481. enc_int6(Int, Prefix) ->
  482. [<< Prefix:2, 2#111111:6 >>|enc_big_int(Int - 63, [])].
  483. enc_int7(Int, Prefix) when Int < 127 ->
  484. << Prefix:1, Int:7 >>;
  485. enc_int7(Int, Prefix) ->
  486. [<< Prefix:1, 2#1111111:7 >>|enc_big_int(Int - 127, [])].
  487. enc_big_int(Int, Acc) when Int < 128 ->
  488. lists:reverse([<< Int:8 >>|Acc]);
  489. enc_big_int(Int, Acc) ->
  490. enc_big_int(Int bsr 7, [<< 1:1, Int:7 >>|Acc]).
  491. %% Encode a string.
  492. enc_str(Str, Opts) ->
  493. case maps:get(huffman, Opts, true) of
  494. true ->
  495. Str2 = enc_huffman(Str, <<>>),
  496. [enc_int7(byte_size(Str2), 2#1), Str2];
  497. false ->
  498. [enc_int7(byte_size(Str), 2#0), Str]
  499. end.
  500. enc_huffman(<<>>, Acc) ->
  501. case bit_size(Acc) rem 8 of
  502. 1 -> << Acc/bits, 2#1111111:7 >>;
  503. 2 -> << Acc/bits, 2#111111:6 >>;
  504. 3 -> << Acc/bits, 2#11111:5 >>;
  505. 4 -> << Acc/bits, 2#1111:4 >>;
  506. 5 -> << Acc/bits, 2#111:3 >>;
  507. 6 -> << Acc/bits, 2#11:2 >>;
  508. 7 -> << Acc/bits, 2#1:1 >>;
  509. 0 -> Acc
  510. end;
  511. enc_huffman(<< 0, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111000:13 >>);
  512. enc_huffman(<< 1, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011000:23 >>);
  513. enc_huffman(<< 2, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111100010:28 >>);
  514. enc_huffman(<< 3, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111100011:28 >>);
  515. enc_huffman(<< 4, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111100100:28 >>);
  516. enc_huffman(<< 5, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111100101:28 >>);
  517. enc_huffman(<< 6, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111100110:28 >>);
  518. enc_huffman(<< 7, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111100111:28 >>);
  519. enc_huffman(<< 8, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101000:28 >>);
  520. enc_huffman(<< 9, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111101010:24 >>);
  521. enc_huffman(<< 10, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111111111100:30 >>);
  522. enc_huffman(<< 11, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101001:28 >>);
  523. enc_huffman(<< 12, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101010:28 >>);
  524. enc_huffman(<< 13, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111111111101:30 >>);
  525. enc_huffman(<< 14, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101011:28 >>);
  526. enc_huffman(<< 15, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101100:28 >>);
  527. enc_huffman(<< 16, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101101:28 >>);
  528. enc_huffman(<< 17, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101110:28 >>);
  529. enc_huffman(<< 18, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111101111:28 >>);
  530. enc_huffman(<< 19, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110000:28 >>);
  531. enc_huffman(<< 20, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110001:28 >>);
  532. enc_huffman(<< 21, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110010:28 >>);
  533. enc_huffman(<< 22, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111111111110:30 >>);
  534. enc_huffman(<< 23, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110011:28 >>);
  535. enc_huffman(<< 24, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110100:28 >>);
  536. enc_huffman(<< 25, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110101:28 >>);
  537. enc_huffman(<< 26, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110110:28 >>);
  538. enc_huffman(<< 27, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111110111:28 >>);
  539. enc_huffman(<< 28, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111000:28 >>);
  540. enc_huffman(<< 29, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111001:28 >>);
  541. enc_huffman(<< 30, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111010:28 >>);
  542. enc_huffman(<< 31, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111011:28 >>);
  543. enc_huffman(<< 32, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#010100:6 >>);
  544. enc_huffman(<< 33, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111000:10 >>);
  545. enc_huffman(<< 34, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111001:10 >>);
  546. enc_huffman(<< 35, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111010:12 >>);
  547. enc_huffman(<< 36, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111001:13 >>);
  548. enc_huffman(<< 37, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#010101:6 >>);
  549. enc_huffman(<< 38, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111000:8 >>);
  550. enc_huffman(<< 39, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111010:11 >>);
  551. enc_huffman(<< 40, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111010:10 >>);
  552. enc_huffman(<< 41, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111011:10 >>);
  553. enc_huffman(<< 42, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111001:8 >>);
  554. enc_huffman(<< 43, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111011:11 >>);
  555. enc_huffman(<< 44, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111010:8 >>);
  556. enc_huffman(<< 45, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#010110:6 >>);
  557. enc_huffman(<< 46, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#010111:6 >>);
  558. enc_huffman(<< 47, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011000:6 >>);
  559. enc_huffman(<< 48, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00000:5 >>);
  560. enc_huffman(<< 49, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00001:5 >>);
  561. enc_huffman(<< 50, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00010:5 >>);
  562. enc_huffman(<< 51, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011001:6 >>);
  563. enc_huffman(<< 52, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011010:6 >>);
  564. enc_huffman(<< 53, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011011:6 >>);
  565. enc_huffman(<< 54, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011100:6 >>);
  566. enc_huffman(<< 55, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011101:6 >>);
  567. enc_huffman(<< 56, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011110:6 >>);
  568. enc_huffman(<< 57, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#011111:6 >>);
  569. enc_huffman(<< 58, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1011100:7 >>);
  570. enc_huffman(<< 59, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111011:8 >>);
  571. enc_huffman(<< 60, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111100:15 >>);
  572. enc_huffman(<< 61, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100000:6 >>);
  573. enc_huffman(<< 62, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111011:12 >>);
  574. enc_huffman(<< 63, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111100:10 >>);
  575. enc_huffman(<< 64, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111010:13 >>);
  576. enc_huffman(<< 65, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100001:6 >>);
  577. enc_huffman(<< 66, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1011101:7 >>);
  578. enc_huffman(<< 67, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1011110:7 >>);
  579. enc_huffman(<< 68, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1011111:7 >>);
  580. enc_huffman(<< 69, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100000:7 >>);
  581. enc_huffman(<< 70, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100001:7 >>);
  582. enc_huffman(<< 71, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100010:7 >>);
  583. enc_huffman(<< 72, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100011:7 >>);
  584. enc_huffman(<< 73, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100100:7 >>);
  585. enc_huffman(<< 74, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100101:7 >>);
  586. enc_huffman(<< 75, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100110:7 >>);
  587. enc_huffman(<< 76, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1100111:7 >>);
  588. enc_huffman(<< 77, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101000:7 >>);
  589. enc_huffman(<< 78, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101001:7 >>);
  590. enc_huffman(<< 79, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101010:7 >>);
  591. enc_huffman(<< 80, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101011:7 >>);
  592. enc_huffman(<< 81, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101100:7 >>);
  593. enc_huffman(<< 82, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101101:7 >>);
  594. enc_huffman(<< 83, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101110:7 >>);
  595. enc_huffman(<< 84, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1101111:7 >>);
  596. enc_huffman(<< 85, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110000:7 >>);
  597. enc_huffman(<< 86, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110001:7 >>);
  598. enc_huffman(<< 87, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110010:7 >>);
  599. enc_huffman(<< 88, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111100:8 >>);
  600. enc_huffman(<< 89, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110011:7 >>);
  601. enc_huffman(<< 90, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111101:8 >>);
  602. enc_huffman(<< 91, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111011:13 >>);
  603. enc_huffman(<< 92, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111110000:19 >>);
  604. enc_huffman(<< 93, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111100:13 >>);
  605. enc_huffman(<< 94, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111100:14 >>);
  606. enc_huffman(<< 95, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100010:6 >>);
  607. enc_huffman(<< 96, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111101:15 >>);
  608. enc_huffman(<< 97, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00011:5 >>);
  609. enc_huffman(<< 98, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100011:6 >>);
  610. enc_huffman(<< 99, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00100:5 >>);
  611. enc_huffman(<< 100, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100100:6 >>);
  612. enc_huffman(<< 101, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00101:5 >>);
  613. enc_huffman(<< 102, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100101:6 >>);
  614. enc_huffman(<< 103, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100110:6 >>);
  615. enc_huffman(<< 104, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#100111:6 >>);
  616. enc_huffman(<< 105, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00110:5 >>);
  617. enc_huffman(<< 106, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110100:7 >>);
  618. enc_huffman(<< 107, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110101:7 >>);
  619. enc_huffman(<< 108, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#101000:6 >>);
  620. enc_huffman(<< 109, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#101001:6 >>);
  621. enc_huffman(<< 110, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#101010:6 >>);
  622. enc_huffman(<< 111, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#00111:5 >>);
  623. enc_huffman(<< 112, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#101011:6 >>);
  624. enc_huffman(<< 113, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110110:7 >>);
  625. enc_huffman(<< 114, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#101100:6 >>);
  626. enc_huffman(<< 115, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#01000:5 >>);
  627. enc_huffman(<< 116, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#01001:5 >>);
  628. enc_huffman(<< 117, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#101101:6 >>);
  629. enc_huffman(<< 118, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1110111:7 >>);
  630. enc_huffman(<< 119, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111000:7 >>);
  631. enc_huffman(<< 120, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111001:7 >>);
  632. enc_huffman(<< 121, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111010:7 >>);
  633. enc_huffman(<< 122, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111011:7 >>);
  634. enc_huffman(<< 123, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111110:15 >>);
  635. enc_huffman(<< 124, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111100:11 >>);
  636. enc_huffman(<< 125, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111101:14 >>);
  637. enc_huffman(<< 126, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111101:13 >>);
  638. enc_huffman(<< 127, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111100:28 >>);
  639. enc_huffman(<< 128, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111100110:20 >>);
  640. enc_huffman(<< 129, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111010010:22 >>);
  641. enc_huffman(<< 130, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111100111:20 >>);
  642. enc_huffman(<< 131, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111101000:20 >>);
  643. enc_huffman(<< 132, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111010011:22 >>);
  644. enc_huffman(<< 133, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111010100:22 >>);
  645. enc_huffman(<< 134, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111010101:22 >>);
  646. enc_huffman(<< 135, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011001:23 >>);
  647. enc_huffman(<< 136, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111010110:22 >>);
  648. enc_huffman(<< 137, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011010:23 >>);
  649. enc_huffman(<< 138, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011011:23 >>);
  650. enc_huffman(<< 139, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011100:23 >>);
  651. enc_huffman(<< 140, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011101:23 >>);
  652. enc_huffman(<< 141, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011110:23 >>);
  653. enc_huffman(<< 142, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111101011:24 >>);
  654. enc_huffman(<< 143, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111011111:23 >>);
  655. enc_huffman(<< 144, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111101100:24 >>);
  656. enc_huffman(<< 145, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111101101:24 >>);
  657. enc_huffman(<< 146, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111010111:22 >>);
  658. enc_huffman(<< 147, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100000:23 >>);
  659. enc_huffman(<< 148, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111101110:24 >>);
  660. enc_huffman(<< 149, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100001:23 >>);
  661. enc_huffman(<< 150, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100010:23 >>);
  662. enc_huffman(<< 151, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100011:23 >>);
  663. enc_huffman(<< 152, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100100:23 >>);
  664. enc_huffman(<< 153, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111011100:21 >>);
  665. enc_huffman(<< 154, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011000:22 >>);
  666. enc_huffman(<< 155, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100101:23 >>);
  667. enc_huffman(<< 156, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011001:22 >>);
  668. enc_huffman(<< 157, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100110:23 >>);
  669. enc_huffman(<< 158, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111100111:23 >>);
  670. enc_huffman(<< 159, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111101111:24 >>);
  671. enc_huffman(<< 160, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011010:22 >>);
  672. enc_huffman(<< 161, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111011101:21 >>);
  673. enc_huffman(<< 162, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111101001:20 >>);
  674. enc_huffman(<< 163, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011011:22 >>);
  675. enc_huffman(<< 164, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011100:22 >>);
  676. enc_huffman(<< 165, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101000:23 >>);
  677. enc_huffman(<< 166, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101001:23 >>);
  678. enc_huffman(<< 167, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111011110:21 >>);
  679. enc_huffman(<< 168, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101010:23 >>);
  680. enc_huffman(<< 169, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011101:22 >>);
  681. enc_huffman(<< 170, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011110:22 >>);
  682. enc_huffman(<< 171, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111110000:24 >>);
  683. enc_huffman(<< 172, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111011111:21 >>);
  684. enc_huffman(<< 173, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111011111:22 >>);
  685. enc_huffman(<< 174, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101011:23 >>);
  686. enc_huffman(<< 175, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101100:23 >>);
  687. enc_huffman(<< 176, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100000:21 >>);
  688. enc_huffman(<< 177, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100001:21 >>);
  689. enc_huffman(<< 178, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100000:22 >>);
  690. enc_huffman(<< 179, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100010:21 >>);
  691. enc_huffman(<< 180, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101101:23 >>);
  692. enc_huffman(<< 181, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100001:22 >>);
  693. enc_huffman(<< 182, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101110:23 >>);
  694. enc_huffman(<< 183, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111101111:23 >>);
  695. enc_huffman(<< 184, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111101010:20 >>);
  696. enc_huffman(<< 185, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100010:22 >>);
  697. enc_huffman(<< 186, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100011:22 >>);
  698. enc_huffman(<< 187, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100100:22 >>);
  699. enc_huffman(<< 188, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111110000:23 >>);
  700. enc_huffman(<< 189, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100101:22 >>);
  701. enc_huffman(<< 190, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100110:22 >>);
  702. enc_huffman(<< 191, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111110001:23 >>);
  703. enc_huffman(<< 192, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100000:26 >>);
  704. enc_huffman(<< 193, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100001:26 >>);
  705. enc_huffman(<< 194, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111101011:20 >>);
  706. enc_huffman(<< 195, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111110001:19 >>);
  707. enc_huffman(<< 196, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111100111:22 >>);
  708. enc_huffman(<< 197, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111110010:23 >>);
  709. enc_huffman(<< 198, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111101000:22 >>);
  710. enc_huffman(<< 199, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111101100:25 >>);
  711. enc_huffman(<< 200, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100010:26 >>);
  712. enc_huffman(<< 201, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100011:26 >>);
  713. enc_huffman(<< 202, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100100:26 >>);
  714. enc_huffman(<< 203, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111011110:27 >>);
  715. enc_huffman(<< 204, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111011111:27 >>);
  716. enc_huffman(<< 205, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100101:26 >>);
  717. enc_huffman(<< 206, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111110001:24 >>);
  718. enc_huffman(<< 207, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111101101:25 >>);
  719. enc_huffman(<< 208, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111110010:19 >>);
  720. enc_huffman(<< 209, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100011:21 >>);
  721. enc_huffman(<< 210, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100110:26 >>);
  722. enc_huffman(<< 211, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100000:27 >>);
  723. enc_huffman(<< 212, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100001:27 >>);
  724. enc_huffman(<< 213, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111100111:26 >>);
  725. enc_huffman(<< 214, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100010:27 >>);
  726. enc_huffman(<< 215, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111110010:24 >>);
  727. enc_huffman(<< 216, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100100:21 >>);
  728. enc_huffman(<< 217, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100101:21 >>);
  729. enc_huffman(<< 218, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101000:26 >>);
  730. enc_huffman(<< 219, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101001:26 >>);
  731. enc_huffman(<< 220, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111101:28 >>);
  732. enc_huffman(<< 221, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100011:27 >>);
  733. enc_huffman(<< 222, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100100:27 >>);
  734. enc_huffman(<< 223, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100101:27 >>);
  735. enc_huffman(<< 224, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111101100:20 >>);
  736. enc_huffman(<< 225, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111110011:24 >>);
  737. enc_huffman(<< 226, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111101101:20 >>);
  738. enc_huffman(<< 227, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100110:21 >>);
  739. enc_huffman(<< 228, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111101001:22 >>);
  740. enc_huffman(<< 229, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111100111:21 >>);
  741. enc_huffman(<< 230, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111101000:21 >>);
  742. enc_huffman(<< 231, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111110011:23 >>);
  743. enc_huffman(<< 232, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111101010:22 >>);
  744. enc_huffman(<< 233, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111101011:22 >>);
  745. enc_huffman(<< 234, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111101110:25 >>);
  746. enc_huffman(<< 235, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111101111:25 >>);
  747. enc_huffman(<< 236, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111110100:24 >>);
  748. enc_huffman(<< 237, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111110101:24 >>);
  749. enc_huffman(<< 238, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101010:26 >>);
  750. enc_huffman(<< 239, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111110100:23 >>);
  751. enc_huffman(<< 240, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101011:26 >>);
  752. enc_huffman(<< 241, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100110:27 >>);
  753. enc_huffman(<< 242, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101100:26 >>);
  754. enc_huffman(<< 243, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101101:26 >>);
  755. enc_huffman(<< 244, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111100111:27 >>);
  756. enc_huffman(<< 245, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101000:27 >>);
  757. enc_huffman(<< 246, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101001:27 >>);
  758. enc_huffman(<< 247, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101010:27 >>);
  759. enc_huffman(<< 248, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101011:27 >>);
  760. enc_huffman(<< 249, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#1111111111111111111111111110:28 >>);
  761. enc_huffman(<< 250, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101100:27 >>);
  762. enc_huffman(<< 251, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101101:27 >>);
  763. enc_huffman(<< 252, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101110:27 >>);
  764. enc_huffman(<< 253, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111101111:27 >>);
  765. enc_huffman(<< 254, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#111111111111111111111110000:27 >>);
  766. enc_huffman(<< 255, R/bits >>, A) -> enc_huffman(R, << A/bits, 2#11111111111111111111101110:26 >>).
  767. -ifdef(TEST).
  768. req_encode_test() ->
  769. %% First request (raw then huffman).
  770. Headers1 = [
  771. {<<":method">>, <<"GET">>},
  772. {<<":scheme">>, <<"http">>},
  773. {<<":path">>, <<"/">>},
  774. {<<":authority">>, <<"www.example.com">>}
  775. ],
  776. {Raw1, State1} = encode(Headers1, init(), #{huffman => false}),
  777. << 16#828684410f7777772e6578616d706c652e636f6d:160 >> = iolist_to_binary(Raw1),
  778. {Huff1, State1} = encode(Headers1),
  779. << 16#828684418cf1e3c2e5f23a6ba0ab90f4ff:136 >> = iolist_to_binary(Huff1),
  780. #state{size=57, dyn_table=[{57,{<<":authority">>, <<"www.example.com">>}}]} = State1,
  781. %% Second request (raw then huffman).
  782. Headers2 = [
  783. {<<":method">>, <<"GET">>},
  784. {<<":scheme">>, <<"http">>},
  785. {<<":path">>, <<"/">>},
  786. {<<":authority">>, <<"www.example.com">>},
  787. {<<"cache-control">>, <<"no-cache">>}
  788. ],
  789. {Raw2, State2} = encode(Headers2, State1, #{huffman => false}),
  790. << 16#828684be58086e6f2d6361636865:112 >> = iolist_to_binary(Raw2),
  791. {Huff2, State2} = encode(Headers2, State1),
  792. << 16#828684be5886a8eb10649cbf:96 >> = iolist_to_binary(Huff2),
  793. #state{size=110, dyn_table=[
  794. {53,{<<"cache-control">>, <<"no-cache">>}},
  795. {57,{<<":authority">>, <<"www.example.com">>}}]} = State2,
  796. %% Third request (raw then huffman).
  797. Headers3 = [
  798. {<<":method">>, <<"GET">>},
  799. {<<":scheme">>, <<"https">>},
  800. {<<":path">>, <<"/index.html">>},
  801. {<<":authority">>, <<"www.example.com">>},
  802. {<<"custom-key">>, <<"custom-value">>}
  803. ],
  804. {Raw3, State3} = encode(Headers3, State2, #{huffman => false}),
  805. << 16#828785bf400a637573746f6d2d6b65790c637573746f6d2d76616c7565:232 >> = iolist_to_binary(Raw3),
  806. {Huff3, State3} = encode(Headers3, State2),
  807. << 16#828785bf408825a849e95ba97d7f8925a849e95bb8e8b4bf:192 >> = iolist_to_binary(Huff3),
  808. #state{size=164, dyn_table=[
  809. {54,{<<"custom-key">>, <<"custom-value">>}},
  810. {53,{<<"cache-control">>, <<"no-cache">>}},
  811. {57,{<<":authority">>, <<"www.example.com">>}}]} = State3,
  812. ok.
  813. resp_encode_test() ->
  814. %% Use a max_size of 256 to trigger header evictions.
  815. State0 = init(256),
  816. %% First response (raw then huffman).
  817. Headers1 = [
  818. {<<":status">>, <<"302">>},
  819. {<<"cache-control">>, <<"private">>},
  820. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  821. {<<"location">>, <<"https://www.example.com">>}
  822. ],
  823. {Raw1, State1} = encode(Headers1, State0, #{huffman => false}),
  824. << 16#4803333032580770726976617465611d4d6f6e2c203231204f637420323031332032303a31333a323120474d546e1768747470733a2f2f7777772e6578616d706c652e636f6d:560 >> = iolist_to_binary(Raw1),
  825. {Huff1, State1} = encode(Headers1, State0),
  826. << 16#488264025885aec3771a4b6196d07abe941054d444a8200595040b8166e082a62d1bff6e919d29ad171863c78f0b97c8e9ae82ae43d3:432 >> = iolist_to_binary(Huff1),
  827. #state{size=222, dyn_table=[
  828. {63,{<<"location">>, <<"https://www.example.com">>}},
  829. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  830. {52,{<<"cache-control">>, <<"private">>}},
  831. {42,{<<":status">>, <<"302">>}}]} = State1,
  832. %% Second response (raw then huffman).
  833. Headers2 = [
  834. {<<":status">>, <<"307">>},
  835. {<<"cache-control">>, <<"private">>},
  836. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  837. {<<"location">>, <<"https://www.example.com">>}
  838. ],
  839. {Raw2, State2} = encode(Headers2, State1, #{huffman => false}),
  840. << 16#4803333037c1c0bf:64 >> = iolist_to_binary(Raw2),
  841. {Huff2, State2} = encode(Headers2, State1),
  842. << 16#4883640effc1c0bf:64 >> = iolist_to_binary(Huff2),
  843. #state{size=222, dyn_table=[
  844. {42,{<<":status">>, <<"307">>}},
  845. {63,{<<"location">>, <<"https://www.example.com">>}},
  846. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  847. {52,{<<"cache-control">>, <<"private">>}}]} = State2,
  848. %% Third response (raw then huffman).
  849. Headers3 = [
  850. {<<":status">>, <<"200">>},
  851. {<<"cache-control">>, <<"private">>},
  852. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:22 GMT">>},
  853. {<<"location">>, <<"https://www.example.com">>},
  854. {<<"content-encoding">>, <<"gzip">>},
  855. {<<"set-cookie">>, <<"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1">>}
  856. ],
  857. {Raw3, State3} = encode(Headers3, State2, #{huffman => false}),
  858. << 16#88c1611d4d6f6e2c203231204f637420323031332032303a31333a323220474d54c05a04677a69707738666f6f3d4153444a4b48514b425a584f5157454f50495541585157454f49553b206d61782d6167653d333630303b2076657273696f6e3d31:784 >> = iolist_to_binary(Raw3),
  859. {Huff3, State3} = encode(Headers3, State2),
  860. << 16#88c16196d07abe941054d444a8200595040b8166e084a62d1bffc05a839bd9ab77ad94e7821dd7f2e6c7b335dfdfcd5b3960d5af27087f3672c1ab270fb5291f9587316065c003ed4ee5b1063d5007:632 >> = iolist_to_binary(Huff3),
  861. #state{size=215, dyn_table=[
  862. {98,{<<"set-cookie">>, <<"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1">>}},
  863. {52,{<<"content-encoding">>, <<"gzip">>}},
  864. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:22 GMT">>}}]} = State3,
  865. ok.
  866. %% This test assumes that table updates work correctly when decoding.
  867. table_update_encode_test() ->
  868. %% Use a max_size of 256 to trigger header evictions
  869. %% when the code is not updating the max size.
  870. DecState0 = EncState0 = init(256),
  871. %% First response.
  872. Headers1 = [
  873. {<<":status">>, <<"302">>},
  874. {<<"cache-control">>, <<"private">>},
  875. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  876. {<<"location">>, <<"https://www.example.com">>}
  877. ],
  878. {Encoded1, EncState1} = encode(Headers1, EncState0),
  879. {Headers1, DecState1} = decode(iolist_to_binary(Encoded1), DecState0),
  880. #state{size=222, configured_max_size=256, dyn_table=[
  881. {63,{<<"location">>, <<"https://www.example.com">>}},
  882. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  883. {52,{<<"cache-control">>, <<"private">>}},
  884. {42,{<<":status">>, <<"302">>}}]} = DecState1,
  885. #state{size=222, configured_max_size=256, dyn_table=[
  886. {63,{<<"location">>, <<"https://www.example.com">>}},
  887. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  888. {52,{<<"cache-control">>, <<"private">>}},
  889. {42,{<<":status">>, <<"302">>}}]} = EncState1,
  890. %% Set a new configured max_size to avoid header evictions.
  891. DecState2 = set_max_size(512, DecState1),
  892. EncState2 = set_max_size(512, EncState1),
  893. %% Second response.
  894. Headers2 = [
  895. {<<":status">>, <<"307">>},
  896. {<<"cache-control">>, <<"private">>},
  897. {<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>},
  898. {<<"location">>, <<"https://www.example.com">>}
  899. ],
  900. {Encoded2, EncState3} = encode(Headers2, EncState2),
  901. {Headers2, DecState3} = decode(iolist_to_binary(Encoded2), DecState2),
  902. #state{size=264, max_size=512, dyn_table=[
  903. {42,{<<":status">>, <<"307">>}},
  904. {63,{<<"location">>, <<"https://www.example.com">>}},
  905. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  906. {52,{<<"cache-control">>, <<"private">>}},
  907. {42,{<<":status">>, <<"302">>}}]} = DecState3,
  908. #state{size=264, max_size=512, dyn_table=[
  909. {42,{<<":status">>, <<"307">>}},
  910. {63,{<<"location">>, <<"https://www.example.com">>}},
  911. {65,{<<"date">>, <<"Mon, 21 Oct 2013 20:13:21 GMT">>}},
  912. {52,{<<"cache-control">>, <<"private">>}},
  913. {42,{<<":status">>, <<"302">>}}]} = EncState3,
  914. ok.
  915. encode_iolist_test() ->
  916. Headers = [
  917. {<<":method">>, <<"GET">>},
  918. {<<":scheme">>, <<"http">>},
  919. {<<":path">>, <<"/">>},
  920. {<<":authority">>, <<"www.example.com">>},
  921. {<<"content-type">>, [<<"image">>,<<"/">>,<<"png">>,<<>>]}
  922. ],
  923. {_, _} = encode(Headers),
  924. ok.
  925. horse_encode_raw() ->
  926. horse:repeat(20000,
  927. do_horse_encode_raw()
  928. ).
  929. do_horse_encode_raw() ->
  930. Headers1 = [
  931. {<<":method">>, <<"GET">>},
  932. {<<":scheme">>, <<"http">>},
  933. {<<":path">>, <<"/">>},
  934. {<<":authority">>, <<"www.example.com">>}
  935. ],
  936. {_, State1} = encode(Headers1, init(), #{huffman => false}),
  937. Headers2 = [
  938. {<<":method">>, <<"GET">>},
  939. {<<":scheme">>, <<"http">>},
  940. {<<":path">>, <<"/">>},
  941. {<<":authority">>, <<"www.example.com">>},
  942. {<<"cache-control">>, <<"no-cache">>}
  943. ],
  944. {_, State2} = encode(Headers2, State1, #{huffman => false}),
  945. Headers3 = [
  946. {<<":method">>, <<"GET">>},
  947. {<<":scheme">>, <<"https">>},
  948. {<<":path">>, <<"/index.html">>},
  949. {<<":authority">>, <<"www.example.com">>},
  950. {<<"custom-key">>, <<"custom-value">>}
  951. ],
  952. {_, _} = encode(Headers3, State2, #{huffman => false}),
  953. ok.
  954. horse_encode_huffman() ->
  955. horse:repeat(20000,
  956. do_horse_encode_huffman()
  957. ).
  958. do_horse_encode_huffman() ->
  959. Headers1 = [
  960. {<<":method">>, <<"GET">>},
  961. {<<":scheme">>, <<"http">>},
  962. {<<":path">>, <<"/">>},
  963. {<<":authority">>, <<"www.example.com">>}
  964. ],
  965. {_, State1} = encode(Headers1),
  966. Headers2 = [
  967. {<<":method">>, <<"GET">>},
  968. {<<":scheme">>, <<"http">>},
  969. {<<":path">>, <<"/">>},
  970. {<<":authority">>, <<"www.example.com">>},
  971. {<<"cache-control">>, <<"no-cache">>}
  972. ],
  973. {_, State2} = encode(Headers2, State1),
  974. Headers3 = [
  975. {<<":method">>, <<"GET">>},
  976. {<<":scheme">>, <<"https">>},
  977. {<<":path">>, <<"/index.html">>},
  978. {<<":authority">>, <<"www.example.com">>},
  979. {<<"custom-key">>, <<"custom-value">>}
  980. ],
  981. {_, _} = encode(Headers3, State2),
  982. ok.
  983. -endif.
  984. %% Static and dynamic tables.
  985. %% @todo There must be a more efficient way.
  986. table_find(Header = {Name, _}, State) ->
  987. case table_find_field(Header, State) of
  988. not_found ->
  989. case table_find_name(Name, State) of
  990. NotFound = not_found ->
  991. NotFound;
  992. Found ->
  993. {name, Found}
  994. end;
  995. Found ->
  996. {field, Found}
  997. end.
  998. table_find_field({<<":authority">>, <<>>}, _) -> 1;
  999. table_find_field({<<":method">>, <<"GET">>}, _) -> 2;
  1000. table_find_field({<<":method">>, <<"POST">>}, _) -> 3;
  1001. table_find_field({<<":path">>, <<"/">>}, _) -> 4;
  1002. table_find_field({<<":path">>, <<"/index.html">>}, _) -> 5;
  1003. table_find_field({<<":scheme">>, <<"http">>}, _) -> 6;
  1004. table_find_field({<<":scheme">>, <<"https">>}, _) -> 7;
  1005. table_find_field({<<":status">>, <<"200">>}, _) -> 8;
  1006. table_find_field({<<":status">>, <<"204">>}, _) -> 9;
  1007. table_find_field({<<":status">>, <<"206">>}, _) -> 10;
  1008. table_find_field({<<":status">>, <<"304">>}, _) -> 11;
  1009. table_find_field({<<":status">>, <<"400">>}, _) -> 12;
  1010. table_find_field({<<":status">>, <<"404">>}, _) -> 13;
  1011. table_find_field({<<":status">>, <<"500">>}, _) -> 14;
  1012. table_find_field({<<"accept-charset">>, <<>>}, _) -> 15;
  1013. table_find_field({<<"accept-encoding">>, <<"gzip, deflate">>}, _) -> 16;
  1014. table_find_field({<<"accept-language">>, <<>>}, _) -> 17;
  1015. table_find_field({<<"accept-ranges">>, <<>>}, _) -> 18;
  1016. table_find_field({<<"accept">>, <<>>}, _) -> 19;
  1017. table_find_field({<<"access-control-allow-origin">>, <<>>}, _) -> 20;
  1018. table_find_field({<<"age">>, <<>>}, _) -> 21;
  1019. table_find_field({<<"allow">>, <<>>}, _) -> 22;
  1020. table_find_field({<<"authorization">>, <<>>}, _) -> 23;
  1021. table_find_field({<<"cache-control">>, <<>>}, _) -> 24;
  1022. table_find_field({<<"content-disposition">>, <<>>}, _) -> 25;
  1023. table_find_field({<<"content-encoding">>, <<>>}, _) -> 26;
  1024. table_find_field({<<"content-language">>, <<>>}, _) -> 27;
  1025. table_find_field({<<"content-length">>, <<>>}, _) -> 28;
  1026. table_find_field({<<"content-location">>, <<>>}, _) -> 29;
  1027. table_find_field({<<"content-range">>, <<>>}, _) -> 30;
  1028. table_find_field({<<"content-type">>, <<>>}, _) -> 31;
  1029. table_find_field({<<"cookie">>, <<>>}, _) -> 32;
  1030. table_find_field({<<"date">>, <<>>}, _) -> 33;
  1031. table_find_field({<<"etag">>, <<>>}, _) -> 34;
  1032. table_find_field({<<"expect">>, <<>>}, _) -> 35;
  1033. table_find_field({<<"expires">>, <<>>}, _) -> 36;
  1034. table_find_field({<<"from">>, <<>>}, _) -> 37;
  1035. table_find_field({<<"host">>, <<>>}, _) -> 38;
  1036. table_find_field({<<"if-match">>, <<>>}, _) -> 39;
  1037. table_find_field({<<"if-modified-since">>, <<>>}, _) -> 40;
  1038. table_find_field({<<"if-none-match">>, <<>>}, _) -> 41;
  1039. table_find_field({<<"if-range">>, <<>>}, _) -> 42;
  1040. table_find_field({<<"if-unmodified-since">>, <<>>}, _) -> 43;
  1041. table_find_field({<<"last-modified">>, <<>>}, _) -> 44;
  1042. table_find_field({<<"link">>, <<>>}, _) -> 45;
  1043. table_find_field({<<"location">>, <<>>}, _) -> 46;
  1044. table_find_field({<<"max-forwards">>, <<>>}, _) -> 47;
  1045. table_find_field({<<"proxy-authenticate">>, <<>>}, _) -> 48;
  1046. table_find_field({<<"proxy-authorization">>, <<>>}, _) -> 49;
  1047. table_find_field({<<"range">>, <<>>}, _) -> 50;
  1048. table_find_field({<<"referer">>, <<>>}, _) -> 51;
  1049. table_find_field({<<"refresh">>, <<>>}, _) -> 52;
  1050. table_find_field({<<"retry-after">>, <<>>}, _) -> 53;
  1051. table_find_field({<<"server">>, <<>>}, _) -> 54;
  1052. table_find_field({<<"set-cookie">>, <<>>}, _) -> 55;
  1053. table_find_field({<<"strict-transport-security">>, <<>>}, _) -> 56;
  1054. table_find_field({<<"transfer-encoding">>, <<>>}, _) -> 57;
  1055. table_find_field({<<"user-agent">>, <<>>}, _) -> 58;
  1056. table_find_field({<<"vary">>, <<>>}, _) -> 59;
  1057. table_find_field({<<"via">>, <<>>}, _) -> 60;
  1058. table_find_field({<<"www-authenticate">>, <<>>}, _) -> 61;
  1059. table_find_field(Header, #state{dyn_table=DynamicTable}) ->
  1060. table_find_field_dyn(Header, DynamicTable, 62).
  1061. table_find_field_dyn(_, [], _) -> not_found;
  1062. table_find_field_dyn(Header, [{_, Header}|_], Index) -> Index;
  1063. table_find_field_dyn(Header, [_|Tail], Index) -> table_find_field_dyn(Header, Tail, Index + 1).
  1064. table_find_name(<<":authority">>, _) -> 1;
  1065. table_find_name(<<":method">>, _) -> 2;
  1066. table_find_name(<<":path">>, _) -> 4;
  1067. table_find_name(<<":scheme">>, _) -> 6;
  1068. table_find_name(<<":status">>, _) -> 8;
  1069. table_find_name(<<"accept-charset">>, _) -> 15;
  1070. table_find_name(<<"accept-encoding">>, _) -> 16;
  1071. table_find_name(<<"accept-language">>, _) -> 17;
  1072. table_find_name(<<"accept-ranges">>, _) -> 18;
  1073. table_find_name(<<"accept">>, _) -> 19;
  1074. table_find_name(<<"access-control-allow-origin">>, _) -> 20;
  1075. table_find_name(<<"age">>, _) -> 21;
  1076. table_find_name(<<"allow">>, _) -> 22;
  1077. table_find_name(<<"authorization">>, _) -> 23;
  1078. table_find_name(<<"cache-control">>, _) -> 24;
  1079. table_find_name(<<"content-disposition">>, _) -> 25;
  1080. table_find_name(<<"content-encoding">>, _) -> 26;
  1081. table_find_name(<<"content-language">>, _) -> 27;
  1082. table_find_name(<<"content-length">>, _) -> 28;
  1083. table_find_name(<<"content-location">>, _) -> 29;
  1084. table_find_name(<<"content-range">>, _) -> 30;
  1085. table_find_name(<<"content-type">>, _) -> 31;
  1086. table_find_name(<<"cookie">>, _) -> 32;
  1087. table_find_name(<<"date">>, _) -> 33;
  1088. table_find_name(<<"etag">>, _) -> 34;
  1089. table_find_name(<<"expect">>, _) -> 35;
  1090. table_find_name(<<"expires">>, _) -> 36;
  1091. table_find_name(<<"from">>, _) -> 37;
  1092. table_find_name(<<"host">>, _) -> 38;
  1093. table_find_name(<<"if-match">>, _) -> 39;
  1094. table_find_name(<<"if-modified-since">>, _) -> 40;
  1095. table_find_name(<<"if-none-match">>, _) -> 41;
  1096. table_find_name(<<"if-range">>, _) -> 42;
  1097. table_find_name(<<"if-unmodified-since">>, _) -> 43;
  1098. table_find_name(<<"last-modified">>, _) -> 44;
  1099. table_find_name(<<"link">>, _) -> 45;
  1100. table_find_name(<<"location">>, _) -> 46;
  1101. table_find_name(<<"max-forwards">>, _) -> 47;
  1102. table_find_name(<<"proxy-authenticate">>, _) -> 48;
  1103. table_find_name(<<"proxy-authorization">>, _) -> 49;
  1104. table_find_name(<<"range">>, _) -> 50;
  1105. table_find_name(<<"referer">>, _) -> 51;
  1106. table_find_name(<<"refresh">>, _) -> 52;
  1107. table_find_name(<<"retry-after">>, _) -> 53;
  1108. table_find_name(<<"server">>, _) -> 54;
  1109. table_find_name(<<"set-cookie">>, _) -> 55;
  1110. table_find_name(<<"strict-transport-security">>, _) -> 56;
  1111. table_find_name(<<"transfer-encoding">>, _) -> 57;
  1112. table_find_name(<<"user-agent">>, _) -> 58;
  1113. table_find_name(<<"vary">>, _) -> 59;
  1114. table_find_name(<<"via">>, _) -> 60;
  1115. table_find_name(<<"www-authenticate">>, _) -> 61;
  1116. table_find_name(Name, #state{dyn_table=DynamicTable}) ->
  1117. table_find_name_dyn(Name, DynamicTable, 62).
  1118. table_find_name_dyn(_, [], _) -> not_found;
  1119. table_find_name_dyn(Name, [{Name, _}|_], Index) -> Index;
  1120. table_find_name_dyn(Name, [_|Tail], Index) -> table_find_name_dyn(Name, Tail, Index + 1).
  1121. table_get(1, _) -> {<<":authority">>, <<>>};
  1122. table_get(2, _) -> {<<":method">>, <<"GET">>};
  1123. table_get(3, _) -> {<<":method">>, <<"POST">>};
  1124. table_get(4, _) -> {<<":path">>, <<"/">>};
  1125. table_get(5, _) -> {<<":path">>, <<"/index.html">>};
  1126. table_get(6, _) -> {<<":scheme">>, <<"http">>};
  1127. table_get(7, _) -> {<<":scheme">>, <<"https">>};
  1128. table_get(8, _) -> {<<":status">>, <<"200">>};
  1129. table_get(9, _) -> {<<":status">>, <<"204">>};
  1130. table_get(10, _) -> {<<":status">>, <<"206">>};
  1131. table_get(11, _) -> {<<":status">>, <<"304">>};
  1132. table_get(12, _) -> {<<":status">>, <<"400">>};
  1133. table_get(13, _) -> {<<":status">>, <<"404">>};
  1134. table_get(14, _) -> {<<":status">>, <<"500">>};
  1135. table_get(15, _) -> {<<"accept-charset">>, <<>>};
  1136. table_get(16, _) -> {<<"accept-encoding">>, <<"gzip, deflate">>};
  1137. table_get(17, _) -> {<<"accept-language">>, <<>>};
  1138. table_get(18, _) -> {<<"accept-ranges">>, <<>>};
  1139. table_get(19, _) -> {<<"accept">>, <<>>};
  1140. table_get(20, _) -> {<<"access-control-allow-origin">>, <<>>};
  1141. table_get(21, _) -> {<<"age">>, <<>>};
  1142. table_get(22, _) -> {<<"allow">>, <<>>};
  1143. table_get(23, _) -> {<<"authorization">>, <<>>};
  1144. table_get(24, _) -> {<<"cache-control">>, <<>>};
  1145. table_get(25, _) -> {<<"content-disposition">>, <<>>};
  1146. table_get(26, _) -> {<<"content-encoding">>, <<>>};
  1147. table_get(27, _) -> {<<"content-language">>, <<>>};
  1148. table_get(28, _) -> {<<"content-length">>, <<>>};
  1149. table_get(29, _) -> {<<"content-location">>, <<>>};
  1150. table_get(30, _) -> {<<"content-range">>, <<>>};
  1151. table_get(31, _) -> {<<"content-type">>, <<>>};
  1152. table_get(32, _) -> {<<"cookie">>, <<>>};
  1153. table_get(33, _) -> {<<"date">>, <<>>};
  1154. table_get(34, _) -> {<<"etag">>, <<>>};
  1155. table_get(35, _) -> {<<"expect">>, <<>>};
  1156. table_get(36, _) -> {<<"expires">>, <<>>};
  1157. table_get(37, _) -> {<<"from">>, <<>>};
  1158. table_get(38, _) -> {<<"host">>, <<>>};
  1159. table_get(39, _) -> {<<"if-match">>, <<>>};
  1160. table_get(40, _) -> {<<"if-modified-since">>, <<>>};
  1161. table_get(41, _) -> {<<"if-none-match">>, <<>>};
  1162. table_get(42, _) -> {<<"if-range">>, <<>>};
  1163. table_get(43, _) -> {<<"if-unmodified-since">>, <<>>};
  1164. table_get(44, _) -> {<<"last-modified">>, <<>>};
  1165. table_get(45, _) -> {<<"link">>, <<>>};
  1166. table_get(46, _) -> {<<"location">>, <<>>};
  1167. table_get(47, _) -> {<<"max-forwards">>, <<>>};
  1168. table_get(48, _) -> {<<"proxy-authenticate">>, <<>>};
  1169. table_get(49, _) -> {<<"proxy-authorization">>, <<>>};
  1170. table_get(50, _) -> {<<"range">>, <<>>};
  1171. table_get(51, _) -> {<<"referer">>, <<>>};
  1172. table_get(52, _) -> {<<"refresh">>, <<>>};
  1173. table_get(53, _) -> {<<"retry-after">>, <<>>};
  1174. table_get(54, _) -> {<<"server">>, <<>>};
  1175. table_get(55, _) -> {<<"set-cookie">>, <<>>};
  1176. table_get(56, _) -> {<<"strict-transport-security">>, <<>>};
  1177. table_get(57, _) -> {<<"transfer-encoding">>, <<>>};
  1178. table_get(58, _) -> {<<"user-agent">>, <<>>};
  1179. table_get(59, _) -> {<<"vary">>, <<>>};
  1180. table_get(60, _) -> {<<"via">>, <<>>};
  1181. table_get(61, _) -> {<<"www-authenticate">>, <<>>};
  1182. table_get(Index, #state{dyn_table=DynamicTable}) ->
  1183. {_, Header} = lists:nth(Index - 61, DynamicTable),
  1184. Header.
  1185. table_get_name(1, _) -> <<":authority">>;
  1186. table_get_name(2, _) -> <<":method">>;
  1187. table_get_name(3, _) -> <<":method">>;
  1188. table_get_name(4, _) -> <<":path">>;
  1189. table_get_name(5, _) -> <<":path">>;
  1190. table_get_name(6, _) -> <<":scheme">>;
  1191. table_get_name(7, _) -> <<":scheme">>;
  1192. table_get_name(8, _) -> <<":status">>;
  1193. table_get_name(9, _) -> <<":status">>;
  1194. table_get_name(10, _) -> <<":status">>;
  1195. table_get_name(11, _) -> <<":status">>;
  1196. table_get_name(12, _) -> <<":status">>;
  1197. table_get_name(13, _) -> <<":status">>;
  1198. table_get_name(14, _) -> <<":status">>;
  1199. table_get_name(15, _) -> <<"accept-charset">>;
  1200. table_get_name(16, _) -> <<"accept-encoding">>;
  1201. table_get_name(17, _) -> <<"accept-language">>;
  1202. table_get_name(18, _) -> <<"accept-ranges">>;
  1203. table_get_name(19, _) -> <<"accept">>;
  1204. table_get_name(20, _) -> <<"access-control-allow-origin">>;
  1205. table_get_name(21, _) -> <<"age">>;
  1206. table_get_name(22, _) -> <<"allow">>;
  1207. table_get_name(23, _) -> <<"authorization">>;
  1208. table_get_name(24, _) -> <<"cache-control">>;
  1209. table_get_name(25, _) -> <<"content-disposition">>;
  1210. table_get_name(26, _) -> <<"content-encoding">>;
  1211. table_get_name(27, _) -> <<"content-language">>;
  1212. table_get_name(28, _) -> <<"content-length">>;
  1213. table_get_name(29, _) -> <<"content-location">>;
  1214. table_get_name(30, _) -> <<"content-range">>;
  1215. table_get_name(31, _) -> <<"content-type">>;
  1216. table_get_name(32, _) -> <<"cookie">>;
  1217. table_get_name(33, _) -> <<"date">>;
  1218. table_get_name(34, _) -> <<"etag">>;
  1219. table_get_name(35, _) -> <<"expect">>;
  1220. table_get_name(36, _) -> <<"expires">>;
  1221. table_get_name(37, _) -> <<"from">>;
  1222. table_get_name(38, _) -> <<"host">>;
  1223. table_get_name(39, _) -> <<"if-match">>;
  1224. table_get_name(40, _) -> <<"if-modified-since">>;
  1225. table_get_name(41, _) -> <<"if-none-match">>;
  1226. table_get_name(42, _) -> <<"if-range">>;
  1227. table_get_name(43, _) -> <<"if-unmodified-since">>;
  1228. table_get_name(44, _) -> <<"last-modified">>;
  1229. table_get_name(45, _) -> <<"link">>;
  1230. table_get_name(46, _) -> <<"location">>;
  1231. table_get_name(47, _) -> <<"max-forwards">>;
  1232. table_get_name(48, _) -> <<"proxy-authenticate">>;
  1233. table_get_name(49, _) -> <<"proxy-authorization">>;
  1234. table_get_name(50, _) -> <<"range">>;
  1235. table_get_name(51, _) -> <<"referer">>;
  1236. table_get_name(52, _) -> <<"refresh">>;
  1237. table_get_name(53, _) -> <<"retry-after">>;
  1238. table_get_name(54, _) -> <<"server">>;
  1239. table_get_name(55, _) -> <<"set-cookie">>;
  1240. table_get_name(56, _) -> <<"strict-transport-security">>;
  1241. table_get_name(57, _) -> <<"transfer-encoding">>;
  1242. table_get_name(58, _) -> <<"user-agent">>;
  1243. table_get_name(59, _) -> <<"vary">>;
  1244. table_get_name(60, _) -> <<"via">>;
  1245. table_get_name(61, _) -> <<"www-authenticate">>;
  1246. table_get_name(Index, #state{dyn_table=DynamicTable}) ->
  1247. {_, {Name, _}} = lists:nth(Index - 61, DynamicTable),
  1248. Name.
  1249. table_insert(Entry = {Name, Value}, State=#state{size=Size, max_size=MaxSize, dyn_table=DynamicTable}) ->
  1250. EntrySize = byte_size(Name) + byte_size(Value) + 32,
  1251. {DynamicTable2, Size2} = if
  1252. Size + EntrySize > MaxSize ->
  1253. table_resize(DynamicTable, MaxSize - EntrySize, 0, []);
  1254. true ->
  1255. {DynamicTable, Size}
  1256. end,
  1257. State#state{size=Size2 + EntrySize, dyn_table=[{EntrySize, Entry}|DynamicTable2]}.
  1258. table_resize([], _, Size, Acc) ->
  1259. {lists:reverse(Acc), Size};
  1260. table_resize([{EntrySize, _}|_], MaxSize, Size, Acc) when Size + EntrySize > MaxSize ->
  1261. {lists:reverse(Acc), Size};
  1262. table_resize([Entry = {EntrySize, _}|Tail], MaxSize, Size, Acc) ->
  1263. table_resize(Tail, MaxSize, Size + EntrySize, [Entry|Acc]).
  1264. table_update_size(0, State) ->
  1265. State#state{size=0, max_size=0, dyn_table=[]};
  1266. table_update_size(MaxSize, State=#state{max_size=MaxSize}) ->
  1267. State;
  1268. table_update_size(MaxSize, State=#state{dyn_table=DynTable}) ->
  1269. {DynTable2, Size} = table_resize(DynTable, MaxSize, 0, []),
  1270. State#state{size=Size, max_size=MaxSize, dyn_table=DynTable2}.
  1271. -ifdef(TEST).
  1272. prop_str_raw() ->
  1273. ?FORALL(Str, binary(), begin
  1274. {Str, <<>>} =:= dec_str(iolist_to_binary(enc_str(Str, #{huffman => false})))
  1275. end).
  1276. prop_str_huffman() ->
  1277. ?FORALL(Str, binary(), begin
  1278. {Str, <<>>} =:= dec_str(iolist_to_binary(enc_str(Str, #{huffman => true})))
  1279. end).
  1280. -endif.