cow_hpack.erl 68 KB

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