You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nginx_hpack_push_1.15.3.patch 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. diff --git a/auto/modules b/auto/modules
  2. index 09bfcb08..645eb897 100644
  3. --- a/auto/modules
  4. +++ b/auto/modules
  5. @@ -438,6 +438,10 @@ if [ $HTTP = YES ]; then
  6. . auto/module
  7. fi
  8. + if [ $HTTP_V2_HPACK_ENC = YES ]; then
  9. + have=NGX_HTTP_V2_HPACK_ENC . auto/have
  10. + fi
  11. +
  12. if :; then
  13. ngx_module_name=ngx_http_static_module
  14. ngx_module_incs=
  15. diff --git a/auto/options b/auto/options
  16. index d8b421b0..55b339e6 100644
  17. --- a/auto/options
  18. +++ b/auto/options
  19. @@ -59,6 +59,7 @@ HTTP_CHARSET=YES
  20. HTTP_GZIP=YES
  21. HTTP_SSL=NO
  22. HTTP_V2=NO
  23. +HTTP_V2_HPACK_ENC=NO
  24. HTTP_SSI=YES
  25. HTTP_POSTPONE=NO
  26. HTTP_REALIP=NO
  27. @@ -225,6 +226,7 @@ $0: warning: the \"--with-ipv6\" option is deprecated"
  28. --with-http_ssl_module) HTTP_SSL=YES ;;
  29. --with-http_v2_module) HTTP_V2=YES ;;
  30. + --with-http_v2_hpack_enc) HTTP_V2_HPACK_ENC=YES ;;
  31. --with-http_realip_module) HTTP_REALIP=YES ;;
  32. --with-http_addition_module) HTTP_ADDITION=YES ;;
  33. --with-http_xslt_module) HTTP_XSLT=YES ;;
  34. @@ -440,6 +442,7 @@ cat << END
  35. --with-http_ssl_module enable ngx_http_ssl_module
  36. --with-http_v2_module enable ngx_http_v2_module
  37. + --with-http_v2_hpack_enc enable ngx_http_v2_hpack_enc
  38. --with-http_realip_module enable ngx_http_realip_module
  39. --with-http_addition_module enable ngx_http_addition_module
  40. --with-http_xslt_module enable ngx_http_xslt_module
  41. diff --git a/src/core/ngx_murmurhash.c b/src/core/ngx_murmurhash.c
  42. index 5ade658d..4932f20d 100644
  43. --- a/src/core/ngx_murmurhash.c
  44. +++ b/src/core/ngx_murmurhash.c
  45. @@ -50,3 +50,63 @@ ngx_murmur_hash2(u_char *data, size_t len)
  46. return h;
  47. }
  48. +
  49. +
  50. +uint64_t
  51. +ngx_murmur_hash2_64(u_char *data, size_t len, uint64_t seed)
  52. +{
  53. + uint64_t h, k;
  54. +
  55. + h = seed ^ len;
  56. +
  57. + while (len >= 8) {
  58. + k = data[0];
  59. + k |= data[1] << 8;
  60. + k |= data[2] << 16;
  61. + k |= data[3] << 24;
  62. + k |= (uint64_t)data[4] << 32;
  63. + k |= (uint64_t)data[5] << 40;
  64. + k |= (uint64_t)data[6] << 48;
  65. + k |= (uint64_t)data[7] << 56;
  66. +
  67. + k *= 0xc6a4a7935bd1e995ull;
  68. + k ^= k >> 47;
  69. + k *= 0xc6a4a7935bd1e995ull;
  70. +
  71. + h ^= k;
  72. + h *= 0xc6a4a7935bd1e995ull;
  73. +
  74. + data += 8;
  75. + len -= 8;
  76. + }
  77. +
  78. + switch (len) {
  79. + case 7:
  80. + h ^= (uint64_t)data[6] << 48;
  81. + /* fall through */
  82. + case 6:
  83. + h ^= (uint64_t)data[5] << 40;
  84. + /* fall through */
  85. + case 5:
  86. + h ^= (uint64_t)data[4] << 32;
  87. + /* fall through */
  88. + case 4:
  89. + h ^= data[3] << 24;
  90. + /* fall through */
  91. + case 3:
  92. + h ^= data[2] << 16;
  93. + /* fall through */
  94. + case 2:
  95. + h ^= data[1] << 8;
  96. + /* fall through */
  97. + case 1:
  98. + h ^= data[0];
  99. + h *= 0xc6a4a7935bd1e995ull;
  100. + }
  101. +
  102. + h ^= h >> 47;
  103. + h *= 0xc6a4a7935bd1e995ull;
  104. + h ^= h >> 47;
  105. +
  106. + return h;
  107. +}
  108. diff --git a/src/core/ngx_murmurhash.h b/src/core/ngx_murmurhash.h
  109. index 54e867d3..322b3df9 100644
  110. --- a/src/core/ngx_murmurhash.h
  111. +++ b/src/core/ngx_murmurhash.h
  112. @@ -15,5 +15,7 @@
  113. uint32_t ngx_murmur_hash2(u_char *data, size_t len);
  114. +uint64_t ngx_murmur_hash2_64(u_char *data, size_t len, uint64_t seed);
  115. +
  116. #endif /* _NGX_MURMURHASH_H_INCLUDED_ */
  117. diff --git a/src/http/v2/ngx_http_v2.c b/src/http/v2/ngx_http_v2.c
  118. index e7770192..c18b60ca 100644
  119. --- a/src/http/v2/ngx_http_v2.c
  120. +++ b/src/http/v2/ngx_http_v2.c
  121. @@ -270,6 +270,8 @@ ngx_http_v2_init(ngx_event_t *rev)
  122. h2c->frame_size = NGX_HTTP_V2_DEFAULT_FRAME_SIZE;
  123. + h2c->max_hpack_table_size = NGX_HTTP_V2_DEFAULT_HPACK_TABLE_SIZE;
  124. +
  125. h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);
  126. h2c->concurrent_pushes = h2scf->concurrent_pushes;
  127. @@ -2076,6 +2078,14 @@ ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c, u_char *pos,
  128. case NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING:
  129. h2c->table_update = 1;
  130. +
  131. + if (value > NGX_HTTP_V2_MAX_HPACK_TABLE_SIZE) {
  132. + h2c->max_hpack_table_size = NGX_HTTP_V2_MAX_HPACK_TABLE_SIZE;
  133. + } else {
  134. + h2c->max_hpack_table_size = value;
  135. + }
  136. +
  137. + h2c->indicate_resize = 1;
  138. break;
  139. default:
  140. diff --git a/src/http/v2/ngx_http_v2.h b/src/http/v2/ngx_http_v2.h
  141. index ebd0e77c..b152b504 100644
  142. --- a/src/http/v2/ngx_http_v2.h
  143. +++ b/src/http/v2/ngx_http_v2.h
  144. @@ -52,6 +52,14 @@
  145. #define NGX_HTTP_V2_MAX_WINDOW ((1U << 31) - 1)
  146. #define NGX_HTTP_V2_DEFAULT_WINDOW 65535
  147. +#define HPACK_ENC_HTABLE_SZ 128 /* better to keep a PoT < 64k */
  148. +#define HPACK_ENC_HTABLE_ENTRIES ((HPACK_ENC_HTABLE_SZ * 100) / 128)
  149. +#define HPACK_ENC_DYNAMIC_KEY_TBL_SZ 10 /* 10 is sufficient for most */
  150. +#define HPACK_ENC_MAX_ENTRY 512 /* longest header size to match */
  151. +
  152. +#define NGX_HTTP_V2_DEFAULT_HPACK_TABLE_SIZE 4096
  153. +#define NGX_HTTP_V2_MAX_HPACK_TABLE_SIZE 16384 /* < 64k */
  154. +
  155. #define NGX_HTTP_V2_DEFAULT_WEIGHT 16
  156. @@ -115,6 +123,46 @@ typedef struct {
  157. } ngx_http_v2_hpack_t;
  158. +#if (NGX_HTTP_V2_HPACK_ENC)
  159. +typedef struct {
  160. + uint64_t hash_val;
  161. + uint32_t index;
  162. + uint16_t pos;
  163. + uint16_t klen, vlen;
  164. + uint16_t size;
  165. + uint16_t next;
  166. +} ngx_http_v2_hpack_enc_entry_t;
  167. +
  168. +
  169. +typedef struct {
  170. + uint64_t hash_val;
  171. + uint32_t index;
  172. + uint16_t pos;
  173. + uint16_t klen;
  174. +} ngx_http_v2_hpack_name_entry_t;
  175. +
  176. +
  177. +typedef struct {
  178. + size_t size; /* size as defined in RFC 7541 */
  179. + uint32_t top; /* the last entry */
  180. + uint32_t pos;
  181. + uint16_t n_elems; /* number of elements */
  182. + uint16_t base; /* index of the oldest entry */
  183. + uint16_t last; /* index of the newest entry */
  184. +
  185. + /* hash table for dynamic entries, instead using a generic hash table,
  186. + which would be too slow to process a significant amount of headers,
  187. + this table is not determenistic, and might ocasionally fail to insert
  188. + a value, at the cost of slightly worse compression, but significantly
  189. + faster performance */
  190. + ngx_http_v2_hpack_enc_entry_t htable[HPACK_ENC_HTABLE_SZ];
  191. + ngx_http_v2_hpack_name_entry_t heads[HPACK_ENC_DYNAMIC_KEY_TBL_SZ];
  192. + u_char storage[NGX_HTTP_V2_MAX_HPACK_TABLE_SIZE +
  193. + HPACK_ENC_MAX_ENTRY];
  194. +} ngx_http_v2_hpack_enc_t;
  195. +#endif
  196. +
  197. +
  198. struct ngx_http_v2_connection_s {
  199. ngx_connection_t *connection;
  200. ngx_http_connection_t *http_connection;
  201. @@ -130,6 +178,8 @@ struct ngx_http_v2_connection_s {
  202. size_t frame_size;
  203. + size_t max_hpack_table_size;
  204. +
  205. ngx_queue_t waiting;
  206. ngx_http_v2_state_t state;
  207. @@ -157,6 +207,11 @@ struct ngx_http_v2_connection_s {
  208. unsigned blocked:1;
  209. unsigned goaway:1;
  210. unsigned push_disabled:1;
  211. + unsigned indicate_resize:1;
  212. +
  213. +#if (NGX_HTTP_V2_HPACK_ENC)
  214. + ngx_http_v2_hpack_enc_t hpack_enc;
  215. +#endif
  216. };
  217. @@ -198,6 +253,8 @@ struct ngx_http_v2_stream_s {
  218. ngx_array_t *cookies;
  219. + size_t header_limit;
  220. +
  221. ngx_pool_t *pool;
  222. unsigned waiting:1;
  223. @@ -410,4 +467,35 @@ u_char *ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len,
  224. u_char *tmp, ngx_uint_t lower);
  225. +u_char *ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len,
  226. + u_char *tmp, ngx_uint_t lower);
  227. +
  228. +u_char *
  229. +ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix, ngx_uint_t value);
  230. +
  231. +#define ngx_http_v2_write_name(dst, src, len, tmp) \
  232. + ngx_http_v2_string_encode(dst, src, len, tmp, 1)
  233. +#define ngx_http_v2_write_value(dst, src, len, tmp) \
  234. + ngx_http_v2_string_encode(dst, src, len, tmp, 0)
  235. +
  236. +u_char *
  237. +ngx_http_v2_write_header(ngx_http_v2_connection_t *h2c, u_char *pos,
  238. + u_char *key, size_t key_len, u_char *value, size_t value_len,
  239. + u_char *tmp);
  240. +
  241. +void
  242. +ngx_http_v2_table_resize(ngx_http_v2_connection_t *h2c);
  243. +
  244. +#define ngx_http_v2_write_header_str(key, value) \
  245. + ngx_http_v2_write_header(h2c, pos, (u_char *) key, sizeof(key) - 1, \
  246. + (u_char *) value, sizeof(value) - 1, tmp);
  247. +
  248. +#define ngx_http_v2_write_header_tbl(key, val) \
  249. + ngx_http_v2_write_header(h2c, pos, (u_char *) key, sizeof(key) - 1, \
  250. + val.data, val.len, tmp);
  251. +
  252. +#define ngx_http_v2_write_header_pot(key, val) \
  253. + ngx_http_v2_write_header(h2c, pos, (u_char *) key, sizeof(key) - 1, \
  254. + val->data, val->len, tmp);
  255. +
  256. #endif /* _NGX_HTTP_V2_H_INCLUDED_ */
  257. diff --git a/src/http/v2/ngx_http_v2_encode.c b/src/http/v2/ngx_http_v2_encode.c
  258. index ac792084..d1fb7217 100644
  259. --- a/src/http/v2/ngx_http_v2_encode.c
  260. +++ b/src/http/v2/ngx_http_v2_encode.c
  261. @@ -10,7 +10,7 @@
  262. #include <ngx_http.h>
  263. -static u_char *ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix,
  264. +u_char *ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix,
  265. ngx_uint_t value);
  266. @@ -40,7 +40,7 @@ ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len, u_char *tmp,
  267. }
  268. -static u_char *
  269. +u_char *
  270. ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix, ngx_uint_t value)
  271. {
  272. if (value < prefix) {
  273. diff --git a/src/http/v2/ngx_http_v2_filter_module.c b/src/http/v2/ngx_http_v2_filter_module.c
  274. index 853faefd..61e1f051 100644
  275. --- a/src/http/v2/ngx_http_v2_filter_module.c
  276. +++ b/src/http/v2/ngx_http_v2_filter_module.c
  277. @@ -23,10 +23,53 @@
  278. #define ngx_http_v2_literal_size(h) \
  279. (ngx_http_v2_integer_octets(sizeof(h) - 1) + sizeof(h) - 1)
  280. +#define ngx_http_v2_indexed(i) (128 + (i))
  281. +#define ngx_http_v2_inc_indexed(i) (64 + (i))
  282. +
  283. +#define NGX_HTTP_V2_ENCODE_RAW 0
  284. +#define NGX_HTTP_V2_ENCODE_HUFF 0x80
  285. +
  286. +#define NGX_HTTP_V2_AUTHORITY_INDEX 1
  287. +#define NGX_HTTP_V2_METHOD_GET_INDEX 2
  288. +#define NGX_HTTP_V2_PATH_INDEX 4
  289. +
  290. +#define NGX_HTTP_V2_SCHEME_HTTP_INDEX 6
  291. +#define NGX_HTTP_V2_SCHEME_HTTPS_INDEX 7
  292. +
  293. +#define NGX_HTTP_V2_STATUS_INDEX 8
  294. +#define NGX_HTTP_V2_STATUS_200_INDEX 8
  295. +#define NGX_HTTP_V2_STATUS_204_INDEX 9
  296. +#define NGX_HTTP_V2_STATUS_206_INDEX 10
  297. +#define NGX_HTTP_V2_STATUS_304_INDEX 11
  298. +#define NGX_HTTP_V2_STATUS_400_INDEX 12
  299. +#define NGX_HTTP_V2_STATUS_404_INDEX 13
  300. +#define NGX_HTTP_V2_STATUS_500_INDEX 14
  301. +
  302. +#define NGX_HTTP_V2_ACCEPT_ENCODING_INDEX 16
  303. +#define NGX_HTTP_V2_ACCEPT_LANGUAGE_INDEX 17
  304. +#define NGX_HTTP_V2_CONTENT_LENGTH_INDEX 28
  305. +#define NGX_HTTP_V2_CONTENT_TYPE_INDEX 31
  306. +#define NGX_HTTP_V2_DATE_INDEX 33
  307. +#define NGX_HTTP_V2_LAST_MODIFIED_INDEX 44
  308. +#define NGX_HTTP_V2_LOCATION_INDEX 46
  309. +#define NGX_HTTP_V2_SERVER_INDEX 54
  310. +#define NGX_HTTP_V2_USER_AGENT_INDEX 58
  311. +#define NGX_HTTP_V2_VARY_INDEX 59
  312. #define NGX_HTTP_V2_NO_TRAILERS (ngx_http_v2_out_frame_t *) -1
  313. +static const struct {
  314. + u_char *name;
  315. + u_char const len;
  316. +} push_header[] = {
  317. + { (u_char*)":authority" , 10 },
  318. + { (u_char*)"accept-encoding" , 15 },
  319. + { (u_char*)"accept-language" , 15 },
  320. + { (u_char*)"user-agent" , 10 }
  321. +};
  322. +
  323. +
  324. typedef struct {
  325. ngx_str_t name;
  326. u_char index;
  327. @@ -155,11 +198,9 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
  328. #endif
  329. static size_t nginx_ver_len = ngx_http_v2_literal_size(NGINX_VER);
  330. - static u_char nginx_ver[ngx_http_v2_literal_size(NGINX_VER)];
  331. static size_t nginx_ver_build_len =
  332. ngx_http_v2_literal_size(NGINX_VER_BUILD);
  333. - static u_char nginx_ver_build[ngx_http_v2_literal_size(NGINX_VER_BUILD)];
  334. stream = r->stream;
  335. @@ -435,7 +476,7 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
  336. }
  337. tmp = ngx_palloc(r->pool, tmp_len);
  338. - pos = ngx_pnalloc(r->pool, len);
  339. + pos = ngx_pnalloc(r->pool, len + 15 + 1);
  340. if (pos == NULL || tmp == NULL) {
  341. return NGX_ERROR;
  342. @@ -443,11 +484,16 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
  343. start = pos;
  344. - if (h2c->table_update) {
  345. - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  346. - "http2 table size update: 0");
  347. - *pos++ = (1 << 5) | 0;
  348. - h2c->table_update = 0;
  349. + h2c = r->stream->connection;
  350. +
  351. + if (h2c->indicate_resize) {
  352. + *pos = 32;
  353. + pos = ngx_http_v2_write_int(pos, ngx_http_v2_prefix(5),
  354. + h2c->max_hpack_table_size);
  355. + h2c->indicate_resize = 0;
  356. +#if (NGX_HTTP_V2_HPACK_ENC)
  357. + ngx_http_v2_table_resize(h2c);
  358. +#endif
  359. }
  360. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  361. @@ -458,67 +504,28 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
  362. *pos++ = status;
  363. } else {
  364. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_STATUS_INDEX);
  365. - *pos++ = NGX_HTTP_V2_ENCODE_RAW | 3;
  366. - pos = ngx_sprintf(pos, "%03ui", r->headers_out.status);
  367. + ngx_sprintf(pos + 8, "%O3ui", r->headers_out.status);
  368. + pos = ngx_http_v2_write_header(h2c, pos, (u_char *)":status",
  369. + sizeof(":status") - 1, pos + 8, 3, tmp);
  370. }
  371. if (r->headers_out.server == NULL) {
  372. -
  373. if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
  374. - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  375. - "http2 output header: \"server: %s\"",
  376. - NGINX_VER);
  377. + pos = ngx_http_v2_write_header_str("server", NGINX_VER);
  378. } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
  379. - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  380. - "http2 output header: \"server: %s\"",
  381. - NGINX_VER_BUILD);
  382. + pos = ngx_http_v2_write_header_str("server", NGINX_VER_BUILD);
  383. } else {
  384. - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  385. - "http2 output header: \"server: nginx\"");
  386. - }
  387. -
  388. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_SERVER_INDEX);
  389. -
  390. - if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) {
  391. - if (nginx_ver[0] == '\0') {
  392. - p = ngx_http_v2_write_value(nginx_ver, (u_char *) NGINX_VER,
  393. - sizeof(NGINX_VER) - 1, tmp);
  394. - nginx_ver_len = p - nginx_ver;
  395. - }
  396. -
  397. - pos = ngx_cpymem(pos, nginx_ver, nginx_ver_len);
  398. -
  399. - } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) {
  400. - if (nginx_ver_build[0] == '\0') {
  401. - p = ngx_http_v2_write_value(nginx_ver_build,
  402. - (u_char *) NGINX_VER_BUILD,
  403. - sizeof(NGINX_VER_BUILD) - 1, tmp);
  404. - nginx_ver_build_len = p - nginx_ver_build;
  405. - }
  406. -
  407. - pos = ngx_cpymem(pos, nginx_ver_build, nginx_ver_build_len);
  408. -
  409. - } else {
  410. - pos = ngx_cpymem(pos, nginx, sizeof(nginx));
  411. + pos = ngx_http_v2_write_header_str("server", "nginx");
  412. }
  413. }
  414. if (r->headers_out.date == NULL) {
  415. - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  416. - "http2 output header: \"date: %V\"",
  417. - &ngx_cached_http_time);
  418. -
  419. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_DATE_INDEX);
  420. - pos = ngx_http_v2_write_value(pos, ngx_cached_http_time.data,
  421. - ngx_cached_http_time.len, tmp);
  422. + pos = ngx_http_v2_write_header_tbl("date", ngx_cached_http_time);
  423. }
  424. if (r->headers_out.content_type.len) {
  425. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_CONTENT_TYPE_INDEX);
  426. -
  427. if (r->headers_out.content_type_len == r->headers_out.content_type.len
  428. && r->headers_out.charset.len)
  429. {
  430. @@ -544,64 +551,36 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
  431. r->headers_out.content_type.data = p - len;
  432. }
  433. - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  434. - "http2 output header: \"content-type: %V\"",
  435. - &r->headers_out.content_type);
  436. -
  437. - pos = ngx_http_v2_write_value(pos, r->headers_out.content_type.data,
  438. - r->headers_out.content_type.len, tmp);
  439. + pos = ngx_http_v2_write_header_tbl("content-type",
  440. + r->headers_out.content_type);
  441. }
  442. if (r->headers_out.content_length == NULL
  443. && r->headers_out.content_length_n >= 0)
  444. {
  445. - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  446. - "http2 output header: \"content-length: %O\"",
  447. - r->headers_out.content_length_n);
  448. -
  449. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_CONTENT_LENGTH_INDEX);
  450. -
  451. - p = pos;
  452. - pos = ngx_sprintf(pos + 1, "%O", r->headers_out.content_length_n);
  453. - *p = NGX_HTTP_V2_ENCODE_RAW | (u_char) (pos - p - 1);
  454. + p = ngx_sprintf(pos + 15, "%O", r->headers_out.content_length_n);
  455. + pos = ngx_http_v2_write_header(h2c, pos, (u_char *)"content-length",
  456. + sizeof("content-length") - 1, pos + 15,
  457. + p - (pos + 15), tmp);
  458. }
  459. if (r->headers_out.last_modified == NULL
  460. && r->headers_out.last_modified_time != -1)
  461. {
  462. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_LAST_MODIFIED_INDEX);
  463. -
  464. - ngx_http_time(pos, r->headers_out.last_modified_time);
  465. + ngx_http_time(pos + 14, r->headers_out.last_modified_time);
  466. len = sizeof("Wed, 31 Dec 1986 18:00:00 GMT") - 1;
  467. -
  468. - ngx_log_debug2(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  469. - "http2 output header: \"last-modified: %*s\"",
  470. - len, pos);
  471. -
  472. - /*
  473. - * Date will always be encoded using huffman in the temporary buffer,
  474. - * so it's safe here to use src and dst pointing to the same address.
  475. - */
  476. - pos = ngx_http_v2_write_value(pos, pos, len, tmp);
  477. + pos = ngx_http_v2_write_header(h2c, pos, (u_char *)"last-modified",
  478. + sizeof("last-modified") - 1, pos + 14,
  479. + len, tmp);
  480. }
  481. if (r->headers_out.location && r->headers_out.location->value.len) {
  482. - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  483. - "http2 output header: \"location: %V\"",
  484. - &r->headers_out.location->value);
  485. -
  486. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_LOCATION_INDEX);
  487. - pos = ngx_http_v2_write_value(pos, r->headers_out.location->value.data,
  488. - r->headers_out.location->value.len, tmp);
  489. + pos = ngx_http_v2_write_header_tbl("location", r->headers_out.location->value);
  490. }
  491. #if (NGX_HTTP_GZIP)
  492. if (r->gzip_vary) {
  493. - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  494. - "http2 output header: \"vary: Accept-Encoding\"");
  495. -
  496. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_VARY_INDEX);
  497. - pos = ngx_cpymem(pos, accept_encoding, sizeof(accept_encoding));
  498. + pos = ngx_http_v2_write_header_str("vary", "Accept-Encoding");
  499. }
  500. #endif
  501. @@ -624,23 +603,10 @@ ngx_http_v2_header_filter(ngx_http_request_t *r)
  502. continue;
  503. }
  504. -#if (NGX_DEBUG)
  505. - if (fc->log->log_level & NGX_LOG_DEBUG_HTTP) {
  506. - ngx_strlow(tmp, header[i].key.data, header[i].key.len);
  507. -
  508. - ngx_log_debug3(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  509. - "http2 output header: \"%*s: %V\"",
  510. - header[i].key.len, tmp, &header[i].value);
  511. - }
  512. -#endif
  513. -
  514. - *pos++ = 0;
  515. -
  516. - pos = ngx_http_v2_write_name(pos, header[i].key.data,
  517. - header[i].key.len, tmp);
  518. + pos = ngx_http_v2_write_header(h2c, pos, header[i].key.data,
  519. + header[i].key.len, header[i].value.data,
  520. + header[i].value.len, tmp);
  521. - pos = ngx_http_v2_write_value(pos, header[i].value.data,
  522. - header[i].value.len, tmp);
  523. }
  524. fin = r->header_only
  525. @@ -998,6 +964,7 @@ ngx_http_v2_push_resource(ngx_http_request_t *r, ngx_str_t *path,
  526. for (i = 0; i < NGX_HTTP_V2_PUSH_HEADERS; i++) {
  527. len += binary[i].len;
  528. + len += push_header[i].len + 1;
  529. }
  530. pos = ngx_pnalloc(r->pool, len);
  531. @@ -1007,12 +974,17 @@ ngx_http_v2_push_resource(ngx_http_request_t *r, ngx_str_t *path,
  532. start = pos;
  533. - if (h2c->table_update) {
  534. - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  535. - "http2 table size update: 0");
  536. - *pos++ = (1 << 5) | 0;
  537. - h2c->table_update = 0;
  538. - }
  539. + h2c = r->stream->connection;
  540. +
  541. + if (h2c->indicate_resize) {
  542. + *pos = 32;
  543. + pos = ngx_http_v2_write_int(pos, ngx_http_v2_prefix(5),
  544. + h2c->max_hpack_table_size);
  545. + h2c->indicate_resize = 0;
  546. +#if (NGX_HTTP_V2_HPACK_ENC)
  547. + ngx_http_v2_table_resize(h2c);
  548. +#endif
  549. + }
  550. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  551. "http2 push header: \":method: GET\"");
  552. @@ -1022,8 +994,7 @@ ngx_http_v2_push_resource(ngx_http_request_t *r, ngx_str_t *path,
  553. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  554. "http2 push header: \":path: %V\"", path);
  555. - *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_PATH_INDEX);
  556. - pos = ngx_http_v2_write_value(pos, path->data, path->len, tmp);
  557. + pos = ngx_http_v2_write_header_pot(":path", path);
  558. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  559. "http2 push header: \":scheme: %V\"", &r->schema);
  560. @@ -1048,11 +1019,15 @@ ngx_http_v2_push_resource(ngx_http_request_t *r, ngx_str_t *path,
  561. continue;
  562. }
  563. + value = &(*h)->value;
  564. +
  565. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  566. "http2 push header: \"%V: %V\"",
  567. &ph[i].name, &(*h)->value);
  568. - pos = ngx_cpymem(pos, binary[i].data, binary[i].len);
  569. + pos = ngx_http_v2_write_header(h2c, pos,
  570. + push_header[i].name, push_header[i].len, value->data, value->len,
  571. + tmp);
  572. }
  573. frame = ngx_http_v2_create_push_frame(r, start, pos);
  574. diff --git a/src/http/v2/ngx_http_v2_table.c b/src/http/v2/ngx_http_v2_table.c
  575. index 7d49803f..b9ee2048 100644
  576. --- a/src/http/v2/ngx_http_v2_table.c
  577. +++ b/src/http/v2/ngx_http_v2_table.c
  578. @@ -361,3 +361,434 @@ ngx_http_v2_table_size(ngx_http_v2_connection_t *h2c, size_t size)
  579. return NGX_OK;
  580. }
  581. +
  582. +
  583. +#if (NGX_HTTP_V2_HPACK_ENC)
  584. +
  585. +static ngx_int_t
  586. +hpack_get_static_index(ngx_http_v2_connection_t *h2c, u_char *val, size_t len);
  587. +
  588. +static ngx_int_t
  589. +hpack_get_dynamic_index(ngx_http_v2_connection_t *h2c, uint64_t key_hash,
  590. + uint8_t *key, size_t key_len);
  591. +
  592. +
  593. +void
  594. +ngx_http_v2_table_resize(ngx_http_v2_connection_t *h2c)
  595. +{
  596. + ngx_http_v2_hpack_enc_entry_t *table;
  597. + uint64_t idx;
  598. +
  599. + table = h2c->hpack_enc.htable;
  600. +
  601. + while (h2c->hpack_enc.size > h2c->max_hpack_table_size) {
  602. + idx = h2c->hpack_enc.base;
  603. + h2c->hpack_enc.base = table[idx].next;
  604. + h2c->hpack_enc.size -= table[idx].size;
  605. + table[idx].hash_val = 0;
  606. + h2c->hpack_enc.n_elems--;
  607. + }
  608. +}
  609. +
  610. +
  611. +/* checks if a header is in the hpack table - if so returns the table entry,
  612. + otherwise encodes and inserts into the table and returns 0,
  613. + if failed to insert into table, returns -1 */
  614. +static ngx_int_t
  615. +ngx_http_v2_table_encode_strings(ngx_http_v2_connection_t *h2c,
  616. + size_t key_len, size_t val_len, uint8_t *key, uint8_t *val,
  617. + ngx_int_t *header_idx)
  618. +{
  619. + uint64_t hash_val, key_hash, idx, lru;
  620. + int i;
  621. + size_t size = key_len + val_len + 32;
  622. + uint8_t *storage = h2c->hpack_enc.storage;
  623. +
  624. + ngx_http_v2_hpack_enc_entry_t *table;
  625. + ngx_http_v2_hpack_name_entry_t *name;
  626. +
  627. + *header_idx = NGX_ERROR;
  628. + /* step 1: compute the hash value of header */
  629. + if (size > HPACK_ENC_MAX_ENTRY || size > h2c->max_hpack_table_size) {
  630. + return NGX_ERROR;
  631. + }
  632. +
  633. + key_hash = ngx_murmur_hash2_64(key, key_len, 0x01234);
  634. + hash_val = ngx_murmur_hash2_64(val, val_len, key_hash);
  635. +
  636. + if (hash_val == 0) {
  637. + return NGX_ERROR;
  638. + }
  639. +
  640. + /* step 2: check if full header in the table */
  641. + idx = hash_val;
  642. + i = -1;
  643. + while (idx) {
  644. + /* at most 8 locations are checked, but most will be done in 1 or 2 */
  645. + table = &h2c->hpack_enc.htable[idx % HPACK_ENC_HTABLE_SZ];
  646. + if (table->hash_val == hash_val
  647. + && table->klen == key_len
  648. + && table->vlen == val_len
  649. + && ngx_memcmp(key, storage + table->pos, key_len) == 0
  650. + && ngx_memcmp(val, storage + table->pos + key_len, val_len) == 0)
  651. + {
  652. + return (h2c->hpack_enc.top - table->index) + 61;
  653. + }
  654. +
  655. + if (table->hash_val == 0 && i == -1) {
  656. + i = idx % HPACK_ENC_HTABLE_SZ;
  657. + break;
  658. + }
  659. +
  660. + idx >>= 8;
  661. + }
  662. +
  663. + /* step 3: check if key is in one of the tables */
  664. + *header_idx = hpack_get_static_index(h2c, key, key_len);
  665. +
  666. + if (i == -1) {
  667. + return NGX_ERROR;
  668. + }
  669. +
  670. + if (*header_idx == NGX_ERROR) {
  671. + *header_idx = hpack_get_dynamic_index(h2c, key_hash, key, key_len);
  672. + }
  673. +
  674. + /* step 4: store the new entry */
  675. + table = h2c->hpack_enc.htable;
  676. +
  677. + if (h2c->hpack_enc.top == 0xffffffff) {
  678. + /* just to be on the safe side, avoid overflow */
  679. + ngx_memset(&h2c->hpack_enc, 0, sizeof(ngx_http_v2_hpack_enc_t));
  680. + }
  681. +
  682. + while ((h2c->hpack_enc.size + size > h2c->max_hpack_table_size)
  683. + || h2c->hpack_enc.n_elems == HPACK_ENC_HTABLE_ENTRIES) {
  684. + /* make space for the new entry first */
  685. + idx = h2c->hpack_enc.base;
  686. + h2c->hpack_enc.base = table[idx].next;
  687. + h2c->hpack_enc.size -= table[idx].size;
  688. + table[idx].hash_val = 0;
  689. + h2c->hpack_enc.n_elems--;
  690. + }
  691. +
  692. + table[i] = (ngx_http_v2_hpack_enc_entry_t){.hash_val = hash_val,
  693. + .index = h2c->hpack_enc.top,
  694. + .pos = h2c->hpack_enc.pos,
  695. + .klen = key_len,
  696. + .vlen = val_len,
  697. + .size = size,
  698. + .next = 0};
  699. +
  700. + table[h2c->hpack_enc.last].next = i;
  701. + if (h2c->hpack_enc.n_elems == 0) {
  702. + h2c->hpack_enc.base = i;
  703. + }
  704. +
  705. + h2c->hpack_enc.last = i;
  706. + h2c->hpack_enc.top++;
  707. + h2c->hpack_enc.size += size;
  708. + h2c->hpack_enc.n_elems++;
  709. +
  710. + /* update header name lookup */
  711. + if (*header_idx == NGX_ERROR ) {
  712. + lru = h2c->hpack_enc.top;
  713. +
  714. + for (i=0; i<HPACK_ENC_DYNAMIC_KEY_TBL_SZ; i++) {
  715. +
  716. + name = &h2c->hpack_enc.heads[i];
  717. +
  718. + if ( name->hash_val == 0 || (name->hash_val == key_hash
  719. + && ngx_memcmp(storage + name->pos, key, key_len) == 0) )
  720. + {
  721. + name->hash_val = key_hash;
  722. + name->pos = h2c->hpack_enc.pos;
  723. + name->index = h2c->hpack_enc.top - 1;
  724. + break;
  725. + }
  726. +
  727. + if (lru > name->index) {
  728. + lru = name->index;
  729. + idx = i;
  730. + }
  731. + }
  732. +
  733. + if (i == HPACK_ENC_DYNAMIC_KEY_TBL_SZ) {
  734. + name = &h2c->hpack_enc.heads[idx];
  735. + name->hash_val = hash_val;
  736. + name->pos = h2c->hpack_enc.pos;
  737. + name->index = h2c->hpack_enc.top - 1;
  738. + }
  739. + }
  740. +
  741. + ngx_memcpy(storage + h2c->hpack_enc.pos, key, key_len);
  742. + ngx_memcpy(storage + h2c->hpack_enc.pos + key_len, val, val_len);
  743. +
  744. + h2c->hpack_enc.pos += size;
  745. + if (h2c->hpack_enc.pos > NGX_HTTP_V2_MAX_HPACK_TABLE_SIZE) {
  746. + h2c->hpack_enc.pos = 0;
  747. + }
  748. +
  749. + return NGX_OK;
  750. +}
  751. +
  752. +
  753. +u_char *
  754. +ngx_http_v2_write_header(ngx_http_v2_connection_t *h2c, u_char *pos,
  755. + u_char *key, size_t key_len,
  756. + u_char *value, size_t value_len,
  757. + u_char *tmp)
  758. +{
  759. + ngx_int_t idx, header_idx;
  760. +
  761. + ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  762. + "http2 output header: %*s: %*s", key_len, key, value_len,
  763. + value);
  764. +
  765. + /* attempt to find the value in the dynamic table */
  766. + idx = ngx_http_v2_table_encode_strings(h2c, key_len, value_len, key, value,
  767. + &header_idx);
  768. +
  769. + if (idx > 0) {
  770. + /* positive index indicates success */
  771. + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  772. + "http2 hpack encode: Indexed Header Field: %ud", idx);
  773. +
  774. + *pos = 128;
  775. + pos = ngx_http_v2_write_int(pos, ngx_http_v2_prefix(7), idx);
  776. +
  777. + } else {
  778. +
  779. + if (header_idx == NGX_ERROR) { /* if key is not present */
  780. +
  781. + if (idx == NGX_ERROR) { /* if header was not added */
  782. + *pos++ = 0;
  783. +
  784. + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  785. + "http2 hpack encode: Literal Header Field without"
  786. + " Indexing — New Name");
  787. + } else { /* if header was added */
  788. + *pos++ = 64;
  789. +
  790. + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  791. + "http2 hpack encode: Literal Header Field with "
  792. + "Incremental Indexing — New Name");
  793. + }
  794. +
  795. + pos = ngx_http_v2_write_name(pos, key, key_len, tmp);
  796. +
  797. + } else { /* if key is present */
  798. +
  799. + if (idx == NGX_ERROR) {
  800. + *pos = 0;
  801. + pos = ngx_http_v2_write_int(pos, ngx_http_v2_prefix(4), header_idx);
  802. +
  803. + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  804. + "http2 hpack encode: Literal Header Field without"
  805. + " Indexing — Indexed Name: %ud", header_idx);
  806. + } else {
  807. + *pos = 64;
  808. + pos = ngx_http_v2_write_int(pos, ngx_http_v2_prefix(6), header_idx);
  809. +
  810. + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  811. + "http2 hpack encode: Literal Header Field with "
  812. + "Incremental Indexing — Indexed Name: %ud", header_idx);
  813. + }
  814. + }
  815. +
  816. + pos = ngx_http_v2_write_value(pos, value, value_len, tmp);
  817. + }
  818. +
  819. + return pos;
  820. +}
  821. +
  822. +
  823. +static ngx_int_t
  824. +hpack_get_dynamic_index(ngx_http_v2_connection_t *h2c, uint64_t key_hash,
  825. + uint8_t *key, size_t key_len)
  826. +{
  827. + ngx_http_v2_hpack_name_entry_t *name;
  828. + int i;
  829. +
  830. + for (i=0; i<HPACK_ENC_DYNAMIC_KEY_TBL_SZ; i++) {
  831. + name = &h2c->hpack_enc.heads[i];
  832. +
  833. + if (name->hash_val == key_hash
  834. + && ngx_memcmp(h2c->hpack_enc.storage + name->pos, key, key_len) == 0)
  835. + {
  836. + if (name->index >= h2c->hpack_enc.top - h2c->hpack_enc.n_elems) {
  837. + return (h2c->hpack_enc.top - name->index) + 61;
  838. + }
  839. + break;
  840. + }
  841. + }
  842. +
  843. + return NGX_ERROR;
  844. +}
  845. +
  846. +
  847. +/* decide if a given header is present in the static dictionary, this could be
  848. + done in several ways, but it seems the fastest one is "exhaustive" search */
  849. +static ngx_int_t
  850. +hpack_get_static_index(ngx_http_v2_connection_t *h2c, u_char *val, size_t len)
  851. +{
  852. + /* the static dictionary of response only headers,
  853. + although response headers can be put by origin,
  854. + that would be rare */
  855. + static const struct {
  856. + u_char len;
  857. + const u_char val[28];
  858. + u_char idx;
  859. + } server_headers[] = {
  860. + { 3, "age", 21},//0
  861. + { 3, "via", 60},
  862. + { 4, "date", 33},//2
  863. + { 4, "etag", 34},
  864. + { 4, "link", 45},
  865. + { 4, "vary", 59},
  866. + { 5, "allow", 22},//6
  867. + { 6, "server", 54},//7
  868. + { 7, "expires", 36},//8
  869. + { 7, "refresh", 52},
  870. + { 8, "location", 46},//10
  871. + {10, "set-cookie", 55},//11
  872. + {11, "retry-after", 53},//12
  873. + {12, "content-type", 31},//13
  874. + {13, "content-range", 30},//14
  875. + {13, "accept-ranges", 18},
  876. + {13, "cache-control", 24},
  877. + {13, "last-modified", 44},
  878. + {14, "content-length", 28},//18
  879. + {16, "content-encoding", 26},//19
  880. + {16, "content-language", 27},
  881. + {16, "content-location", 29},
  882. + {16, "www-authenticate", 61},
  883. + {17, "transfer-encoding", 57},//23
  884. + {18, "proxy-authenticate", 48},//24
  885. + {19, "content-disposition", 25},//25
  886. + {25, "strict-transport-security", 56},//26
  887. + {27, "access-control-allow-origin", 20},//27
  888. + {99, "", 99},
  889. + }, *header;
  890. +
  891. + /* for a given length, where to start the search
  892. + since minimal length is 3, the table has a -3
  893. + offset */
  894. + static const int8_t start_at[] = {
  895. + [3-3] = 0,
  896. + [4-3] = 2,
  897. + [5-3] = 6,
  898. + [6-3] = 7,
  899. + [7-3] = 8,
  900. + [8-3] = 10,
  901. + [9-3] = -1,
  902. + [10-3] = 11,
  903. + [11-3] = 12,
  904. + [12-3] = 13,
  905. + [13-3] = 14,
  906. + [14-3] = 18,
  907. + [15-3] = -1,
  908. + [16-3] = 19,
  909. + [17-3] = 23,
  910. + [18-3] = 24,
  911. + [19-3] = 25,
  912. + [20-3] = -1,
  913. + [21-3] = -1,
  914. + [22-3] = -1,
  915. + [23-3] = -1,
  916. + [24-3] = -1,
  917. + [25-3] = 26,
  918. + [26-3] = -1,
  919. + [27-3] = 27,
  920. + };
  921. +
  922. + uint64_t pref;
  923. + size_t save_len = len, i;
  924. + int8_t start;
  925. +
  926. + /* early exit for out of bounds lengths */
  927. + if (len < 3 || len > 27) {
  928. + return NGX_ERROR;
  929. + }
  930. +
  931. + start = start_at[len - 3];
  932. + if (start == -1) {
  933. + /* exit for non existent lengths */
  934. + return NGX_ERROR;
  935. + }
  936. +
  937. + header = &server_headers[start_at[len - 3]];
  938. +
  939. + /* load first 8 bytes of key, for fast comparison */
  940. + if (len < 8) {
  941. + pref = 0;
  942. + if (len >= 4) {
  943. + pref = *(uint32_t *)(val + len - 4) | 0x20202020;
  944. + len -= 4;
  945. + }
  946. + while (len > 0) { /* 3 iterations at most */
  947. + pref = (pref << 8) ^ (val[len - 1] | 0x20);
  948. + len--;
  949. + }
  950. + } else {
  951. + pref = *(uint64_t *)val | 0x2020202020202020;
  952. + len -= 8;
  953. + }
  954. +
  955. + /* iterate over headers with the right length */
  956. + while (header->len == save_len) {
  957. + /* quickly compare the first 8 bytes, most tests will end here */
  958. + if (pref != *(uint64_t *) header->val) {
  959. + header++;
  960. + continue;
  961. + }
  962. +
  963. + if (len == 0) {
  964. + /* len == 0, indicates prefix held the entire key */
  965. + return header->idx;
  966. + }
  967. + /* for longer keys compare the rest */
  968. + i = 1 + (save_len + 7) % 8; /* align so we can compare in quadwords */
  969. +
  970. + while (i + 8 <= save_len) { /* 3 iterations at most */
  971. + if ( *(uint64_t *)&header->val[i]
  972. + != (*(uint64_t *) &val[i]| 0x2020202020202020) )
  973. + {
  974. + header++;
  975. + i = 0;
  976. + break;
  977. + }
  978. + i += 8;
  979. + }
  980. +
  981. + if (i == 0) {
  982. + continue;
  983. + }
  984. +
  985. + /* found the corresponding entry in the static dictionary */
  986. + return header->idx;
  987. + }
  988. +
  989. + return NGX_ERROR;
  990. +}
  991. +
  992. +#else
  993. +
  994. +u_char *
  995. +ngx_http_v2_write_header(ngx_http_v2_connection_t *h2c, u_char *pos,
  996. + u_char *key, size_t key_len,
  997. + u_char *value, size_t value_len,
  998. + u_char *tmp)
  999. +{
  1000. + ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1001. + "http2 output header: %*s: %*s", key_len, key, value_len,
  1002. + value);
  1003. +
  1004. + *pos++ = 64;
  1005. + pos = ngx_http_v2_write_name(pos, key, key_len, tmp);
  1006. + pos = ngx_http_v2_write_value(pos, value, value_len, tmp);
  1007. +
  1008. + return pos;
  1009. +}
  1010. +
  1011. +#endif