4 * Copyright (C) 2014 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "private-libwebsockets.h"
25 * Official static header table for HPACK
26 * +-------+-----------------------------+---------------+
29 | 3 | :method | POST |
31 | 5 | :path | /index.html |
32 | 6 | :scheme | http |
33 | 7 | :scheme | https |
36 | 10 | :status | 206 |
37 | 11 | :status | 304 |
38 | 12 | :status | 400 |
39 | 13 | :status | 404 |
40 | 14 | :status | 500 |
41 | 15 | accept-charset | |
42 | 16 | accept-encoding | gzip, deflate |
43 | 17 | accept-language | |
44 | 18 | accept-ranges | |
46 | 20 | access-control-allow-origin | |
49 | 23 | authorization | |
50 | 24 | cache-control | |
51 | 25 | content-disposition | |
52 | 26 | content-encoding | |
53 | 27 | content-language | |
54 | 28 | content-length | |
55 | 29 | content-location | |
56 | 30 | content-range | |
57 | 31 | content-type | |
66 | 40 | if-modified-since | |
67 | 41 | if-none-match | |
69 | 43 | if-unmodified-since | |
70 | 44 | last-modified | |
73 | 47 | max-forwards | |
74 | 48 | proxy-authenticate | |
75 | 49 | proxy-authorization | |
79 | 53 | retry-after | |
82 | 56 | strict-transport-security | |
83 | 57 | transfer-encoding | |
87 | 61 | www-authenticate | |
88 +-------+-----------------------------+---------------+
91 static const unsigned char static_token[] = {
93 WSI_TOKEN_HTTP_COLON_AUTHORITY,
94 WSI_TOKEN_HTTP_COLON_METHOD,
95 WSI_TOKEN_HTTP_COLON_METHOD,
96 WSI_TOKEN_HTTP_COLON_PATH,
97 WSI_TOKEN_HTTP_COLON_PATH,
98 WSI_TOKEN_HTTP_COLON_SCHEME,
99 WSI_TOKEN_HTTP_COLON_SCHEME,
100 WSI_TOKEN_HTTP_COLON_STATUS,
101 WSI_TOKEN_HTTP_COLON_STATUS,
102 WSI_TOKEN_HTTP_COLON_STATUS,
103 WSI_TOKEN_HTTP_COLON_STATUS,
104 WSI_TOKEN_HTTP_COLON_STATUS,
105 WSI_TOKEN_HTTP_COLON_STATUS,
106 WSI_TOKEN_HTTP_COLON_STATUS,
107 WSI_TOKEN_HTTP_ACCEPT_CHARSET,
108 WSI_TOKEN_HTTP_ACCEPT_ENCODING,
109 WSI_TOKEN_HTTP_ACCEPT_LANGUAGE,
110 WSI_TOKEN_HTTP_ACCEPT_RANGES,
111 WSI_TOKEN_HTTP_ACCEPT,
112 WSI_TOKEN_HTTP_ACCESS_CONTROL_ALLOW_ORIGIN,
114 WSI_TOKEN_HTTP_ALLOW,
115 WSI_TOKEN_HTTP_AUTHORIZATION,
116 WSI_TOKEN_HTTP_CACHE_CONTROL,
117 WSI_TOKEN_HTTP_CONTENT_DISPOSITION,
118 WSI_TOKEN_HTTP_CONTENT_ENCODING,
119 WSI_TOKEN_HTTP_CONTENT_LANGUAGE,
120 WSI_TOKEN_HTTP_CONTENT_LENGTH,
121 WSI_TOKEN_HTTP_CONTENT_LOCATION,
122 WSI_TOKEN_HTTP_CONTENT_RANGE,
123 WSI_TOKEN_HTTP_CONTENT_TYPE,
124 WSI_TOKEN_HTTP_COOKIE,
127 WSI_TOKEN_HTTP_EXPECT,
128 WSI_TOKEN_HTTP_EXPIRES,
131 WSI_TOKEN_HTTP_IF_MATCH,
132 WSI_TOKEN_HTTP_IF_MODIFIED_SINCE,
133 WSI_TOKEN_HTTP_IF_NONE_MATCH,
134 WSI_TOKEN_HTTP_IF_RANGE,
135 WSI_TOKEN_HTTP_IF_UNMODIFIED_SINCE,
136 WSI_TOKEN_HTTP_LAST_MODIFIED,
138 WSI_TOKEN_HTTP_LOCATION,
139 WSI_TOKEN_HTTP_MAX_FORWARDS,
140 WSI_TOKEN_HTTP_PROXY_AUTHENTICATE,
141 WSI_TOKEN_HTTP_PROXY_AUTHORIZATION,
142 WSI_TOKEN_HTTP_RANGE,
143 WSI_TOKEN_HTTP_REFERER,
144 WSI_TOKEN_HTTP_REFRESH,
145 WSI_TOKEN_HTTP_RETRY_AFTER,
146 WSI_TOKEN_HTTP_SERVER,
147 WSI_TOKEN_HTTP_SET_COOKIE,
148 WSI_TOKEN_HTTP_STRICT_TRANSPORT_SECURITY,
149 WSI_TOKEN_HTTP_TRANSFER_ENCODING,
150 WSI_TOKEN_HTTP_USER_AGENT,
153 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
156 /* some of the entries imply values as well as header names */
158 static const char * const http2_canned[] = {
180 #include "huftable.h"
182 static int huftable_decode(int pos, char c)
186 if (lextable_terms[q >> 3] & (1 << (q & 7))) /* terminal */
187 return lextable[q] | 0x8000;
189 return pos + (lextable[q] << 1);
192 static int lws_hpack_update_table_size(struct lws *wsi, int idx)
194 lwsl_info("hpack set table size %d\n", idx);
198 static int lws_frag_start(struct lws *wsi, int hdr_token_idx)
200 struct allocated_headers * ah = wsi->u.http2.http.ah;
205 if (ah->nfrag >= ARRAY_SIZE(ah->frag_index))
208 ah->frags[ah->nfrag].offset = ah->pos;
209 ah->frags[ah->nfrag].len = 0;
210 ah->frags[ah->nfrag].nfrag = 0;
212 ah->frag_index[hdr_token_idx] = ah->nfrag;
217 static int lws_frag_append(struct lws *wsi, unsigned char c)
219 struct allocated_headers * ah = wsi->u.http2.http.ah;
221 ah->data[ah->pos++] = c;
222 ah->frags[ah->nfrag].len++;
224 return ah->pos >= wsi->context->max_http_header_data;
227 static int lws_frag_end(struct lws *wsi)
229 if (lws_frag_append(wsi, 0))
232 wsi->u.http2.http.ah->nfrag++;
236 static void lws_dump_header(struct lws *wsi, int hdr)
239 int len = lws_hdr_copy(wsi, s, sizeof(s) - 1, hdr);
241 lwsl_info(" hdr tok %d (%s) = '%s'\n", hdr, lws_token_to_string(hdr), s);
245 lws_token_from_index(struct lws *wsi, int index, char **arg, int *len)
247 struct hpack_dynamic_table *dyn;
249 /* dynamic table only belongs to network wsi */
251 wsi = lws_http2_get_network_wsi(wsi);
253 dyn = wsi->u.http2.hpack_dyn_table;
255 if (index < ARRAY_SIZE(static_token))
256 return static_token[index];
261 index -= ARRAY_SIZE(static_token);
262 if (index >= dyn->num_entries)
266 *arg = dyn->args + dyn->entries[index].arg_offset;
267 *len = dyn->entries[index].arg_len;
270 return dyn->entries[index].token;
274 lws_hpack_add_dynamic_header(struct lws *wsi, int token, char *arg, int len)
276 struct hpack_dynamic_table *dyn;
279 wsi = lws_http2_get_network_wsi(wsi);
280 dyn = wsi->u.http2.hpack_dyn_table;
283 dyn = lws_zalloc(sizeof(*dyn));
286 wsi->u.http2.hpack_dyn_table = dyn;
288 dyn->args = lws_malloc(1024);
291 dyn->args_length = 1024;
292 dyn->entries = lws_malloc(sizeof(dyn->entries[0]) * 20);
295 dyn->num_entries = 20;
298 if (dyn->next == dyn->num_entries)
301 if (dyn->args_length - dyn->pos < len)
304 dyn->entries[dyn->next].token = token;
305 dyn->entries[dyn->next].arg_offset = dyn->pos;
307 memcpy(dyn->args + dyn->pos, arg, len);
308 dyn->entries[dyn->next].arg_len = len;
310 lwsl_info("%s: added dynamic hdr %d, token %d (%s), len %d\n",
311 __func__, dyn->next, token, lws_token_to_string(token), len);
322 wsi->u.http2.hpack_dyn_table = NULL;
327 static int lws_write_indexed_hdr(struct lws *wsi, int idx)
330 int tok = lws_token_from_index(wsi, idx, NULL, 0);
332 lwsl_info("writing indexed hdr %d (tok %d '%s')\n", idx, tok,
333 lws_token_to_string(tok));
335 if (lws_frag_start(wsi, tok))
338 if (idx < ARRAY_SIZE(http2_canned)) {
339 p = http2_canned[idx];
341 if (lws_frag_append(wsi, *p++))
344 if (lws_frag_end(wsi))
347 lws_dump_header(wsi, tok);
352 int lws_hpack_interpret(struct lws *wsi, unsigned char c)
358 switch (wsi->u.http2.hpack) {
359 case HPKS_OPT_PADDING:
360 wsi->u.http2.padding = c;
361 lwsl_info("padding %d\n", c);
362 if (wsi->u.http2.flags & LWS_HTTP2_FLAG_PRIORITY) {
363 wsi->u.http2.hpack = HKPS_OPT_E_DEPENDENCY;
364 wsi->u.http2.hpack_m = 4;
366 wsi->u.http2.hpack = HPKS_TYPE;
368 case HKPS_OPT_E_DEPENDENCY:
369 wsi->u.http2.hpack_e_dep <<= 8;
370 wsi->u.http2.hpack_e_dep |= c;
371 if (! --wsi->u.http2.hpack_m) {
372 lwsl_info("hpack_e_dep = 0x%x\n", wsi->u.http2.hpack_e_dep);
373 wsi->u.http2.hpack = HKPS_OPT_WEIGHT;
376 case HKPS_OPT_WEIGHT:
378 wsi->u.http2.hpack = HPKS_TYPE;
383 if (wsi->u.http2.count > (wsi->u.http2.length - wsi->u.http2.padding)) {
384 lwsl_info("padding eat\n");
388 if (c & 0x80) { /* indexed header field only */
389 /* just a possibly-extended integer */
390 wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_7;
391 wsi->u.http2.header_index = c & 0x7f;
392 if ((c & 0x7f) == 0x7f) {
393 wsi->u.http2.hpack_len = c & 0x7f;
394 wsi->u.http2.hpack_m = 0;
395 wsi->u.http2.hpack = HPKS_IDX_EXT;
398 if (lws_write_indexed_hdr(wsi, c & 0x7f))
400 /* stay at same state */
403 if (c & 0x40) { /* literal header incr idx */
405 * [possibly-extended hdr idx (6) | new literal hdr name]
406 * H + possibly-extended value length
409 wsi->u.http2.header_index = 0;
410 if (c == 0x40) { /* literal name */
411 wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE_INCR;
412 wsi->u.http2.value = 0;
413 wsi->u.http2.hpack = HPKS_HLEN;
417 wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_6_VALUE_INCR;
418 if ((c & 0x3f) == 0x3f) {
419 wsi->u.http2.hpack_len = c & 0x3f;
420 wsi->u.http2.hpack_m = 0;
421 wsi->u.http2.hpack = HPKS_IDX_EXT;
424 wsi->u.http2.header_index = c & 0x3f;
425 wsi->u.http2.value = 1;
426 wsi->u.http2.hpack = HPKS_HLEN;
430 case 0x10: /* literal header never index */
431 case 0: /* literal header without indexing */
433 * follows 0x40 except 4-bit hdr idx
434 * and don't add to index
436 if (c == 0) { /* literal name */
437 wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE;
438 wsi->u.http2.hpack = HPKS_HLEN;
439 wsi->u.http2.value = 0;
443 wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_4_VALUE;
444 wsi->u.http2.header_index = 0;
445 if ((c & 0xf) == 0xf) {
446 wsi->u.http2.hpack_len = c & 0xf;
447 wsi->u.http2.hpack_m = 0;
448 wsi->u.http2.hpack = HPKS_IDX_EXT;
451 wsi->u.http2.header_index = c & 0xf;
452 wsi->u.http2.value = 1;
453 wsi->u.http2.hpack = HPKS_HLEN;
457 case 0x30: /* header table size update */
458 /* possibly-extended size value (5) */
459 wsi->u.http2.hpack_type = HPKT_SIZE_5;
460 if ((c & 0x1f) == 0x1f) {
461 wsi->u.http2.hpack_len = c & 0x1f;
462 wsi->u.http2.hpack_m = 0;
463 wsi->u.http2.hpack = HPKS_IDX_EXT;
466 lws_hpack_update_table_size(wsi, c & 0x1f);
467 /* stay at HPKS_TYPE state */
473 wsi->u.http2.hpack_len += (c & 0x7f) << wsi->u.http2.hpack_m;
474 wsi->u.http2.hpack_m += 7;
476 switch (wsi->u.http2.hpack_type) {
477 case HPKT_INDEXED_HDR_7:
478 if (lws_write_indexed_hdr(wsi, wsi->u.http2.hpack_len))
480 wsi->u.http2.hpack = HPKS_TYPE;
483 wsi->u.http2.header_index = wsi->u.http2.hpack_len;
484 wsi->u.http2.value = 1;
485 wsi->u.http2.hpack = HPKS_HLEN;
491 case HPKS_HLEN: /* [ H | 7+ ] */
492 wsi->u.http2.huff = !!(c & 0x80);
493 wsi->u.http2.hpack_pos = 0;
494 wsi->u.http2.hpack_len = c & 0x7f;
495 if (wsi->u.http2.hpack_len < 0x7f) {
497 if (wsi->u.http2.value) {
498 if (lws_frag_start(wsi, lws_token_from_index(wsi,
499 wsi->u.http2.header_index,
503 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
504 wsi->u.http2.hpack = HPKS_DATA;
507 wsi->u.http2.hpack_m = 0;
508 wsi->u.http2.hpack = HPKS_HLEN_EXT;
512 wsi->u.http2.hpack_len += (c & 0x7f) <<
513 wsi->u.http2.hpack_m;
514 wsi->u.http2.hpack_m += 7;
521 for (n = 0; n < 8; n++) {
522 if (wsi->u.http2.huff) {
523 prev = wsi->u.http2.hpack_pos;
524 wsi->u.http2.hpack_pos = huftable_decode(
525 wsi->u.http2.hpack_pos,
528 if (wsi->u.http2.hpack_pos == 0xffff)
530 if (!(wsi->u.http2.hpack_pos & 0x8000))
532 c1 = wsi->u.http2.hpack_pos & 0x7fff;
533 wsi->u.http2.hpack_pos = 0;
535 if (!c1 && prev == HUFTABLE_0x100_PREV)
541 if (wsi->u.http2.value) { /* value */
542 if (lws_frag_append(wsi, c1))
545 if (lws_parse(wsi, c1))
550 if (--wsi->u.http2.hpack_len == 0) {
552 switch (wsi->u.http2.hpack_type) {
553 case HPKT_LITERAL_HDR_VALUE_INCR:
554 case HPKT_INDEXED_HDR_6_VALUE_INCR: // !!!
555 if (lws_hpack_add_dynamic_header(wsi,
556 lws_token_from_index(wsi,
557 wsi->u.http2.header_index,
558 NULL, NULL), NULL, 0))
566 if (wsi->u.http2.value) {
567 if (lws_frag_end(wsi))
570 lws_dump_header(wsi, lws_token_from_index(
571 wsi, wsi->u.http2.header_index,
573 if (wsi->u.http2.count + wsi->u.http2.padding ==
575 wsi->u.http2.hpack = HKPS_OPT_DISCARD_PADDING;
577 wsi->u.http2.hpack = HPKS_TYPE;
579 if (wsi->u.hdr.parser_state < WSI_TOKEN_COUNT)
581 wsi->u.http2.value = 1;
582 wsi->u.http2.hpack = HPKS_HLEN;
586 case HKPS_OPT_DISCARD_PADDING:
587 lwsl_info("eating padding %x\n", c);
588 if (! --wsi->u.http2.padding)
589 wsi->u.http2.hpack = HPKS_TYPE;
596 static int lws_http2_num(int starting_bits, unsigned long num,
597 unsigned char **p, unsigned char *end)
599 int mask = (1 << starting_bits) - 1;
612 *((*p)++) = 0x80 | (num & 0x7f);
621 int lws_add_http2_header_by_name(struct lws *wsi,
622 const unsigned char *name,
623 const unsigned char *value, int length,
624 unsigned char **p, unsigned char *end)
628 lwsl_info("%s: %p %s:%s\n", __func__, *p, name, value);
630 len = strlen((char *)name);
632 if (name[len - 1] == ':')
635 if (end - *p < len + length + 8)
638 *((*p)++) = 0; /* not indexed, literal name */
640 **p = 0; /* non-HUF */
641 if (lws_http2_num(7, len, p, end))
643 memcpy(*p, name, len);
646 *(*p) = 0; /* non-HUF */
647 if (lws_http2_num(7, length, p, end))
650 memcpy(*p, value, length);
656 int lws_add_http2_header_by_token(struct lws *wsi, enum lws_token_indexes token,
657 const unsigned char *value, int length,
658 unsigned char **p, unsigned char *end)
660 const unsigned char *name;
662 name = lws_token_to_string(token);
666 return lws_add_http2_header_by_name(wsi, name, value, length, p, end);
669 int lws_add_http2_header_status(struct lws *wsi,
670 unsigned int code, unsigned char **p,
673 unsigned char status[10];
676 wsi->u.http2.send_END_STREAM = !!(code >= 400);
678 n = sprintf((char *)status, "%u", code);
679 if (lws_add_http2_header_by_token(wsi, WSI_TOKEN_HTTP_COLON_STATUS,