cow_hpack.erl 66 KB

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