blob: bd4f46248489c0fe51e28b038cbba3c409e17974 [file] [log] [blame]
Andy Greenecc2e722014-10-09 16:57:47 +08001/*
Andy Green2add6342014-10-12 08:38:16 +08002 * lib/hpack.c
3 *
4 * Copyright (C) 2014 Andy Green <andy@warmcat.com>
5 *
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.
10 *
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.
15 *
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,
19 * MA 02110-1301 USA
20 */
21
22#include "private-libwebsockets.h"
23
24/*
Andy Greenecc2e722014-10-09 16:57:47 +080025 * Official static header table for HPACK
26 * +-------+-----------------------------+---------------+
27 | 1 | :authority | |
28 | 2 | :method | GET |
29 | 3 | :method | POST |
30 | 4 | :path | / |
31 | 5 | :path | /index.html |
32 | 6 | :scheme | http |
33 | 7 | :scheme | https |
34 | 8 | :status | 200 |
35 | 9 | :status | 204 |
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 | |
45 | 19 | accept | |
46 | 20 | access-control-allow-origin | |
47 | 21 | age | |
48 | 22 | allow | |
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 | |
58 | 32 | cookie | |
59 | 33 | date | |
60 | 34 | etag | |
61 | 35 | expect | |
62 | 36 | expires | |
63 | 37 | from | |
64 | 38 | host | |
65 | 39 | if-match | |
66 | 40 | if-modified-since | |
67 | 41 | if-none-match | |
68 | 42 | if-range | |
69 | 43 | if-unmodified-since | |
70 | 44 | last-modified | |
71 | 45 | link | |
72 | 46 | location | |
73 | 47 | max-forwards | |
74 | 48 | proxy-authenticate | |
75 | 49 | proxy-authorization | |
76 | 50 | range | |
77 | 51 | referer | |
78 | 52 | refresh | |
79 | 53 | retry-after | |
80 | 54 | server | |
81 | 55 | set-cookie | |
82 | 56 | strict-transport-security | |
83 | 57 | transfer-encoding | |
84 | 58 | user-agent | |
85 | 59 | vary | |
86 | 60 | via | |
87 | 61 | www-authenticate | |
88 +-------+-----------------------------+---------------+
89*/
90
91static const unsigned char static_token[] = {
92 0,
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,
113 WSI_TOKEN_HTTP_AGE,
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,
125 WSI_TOKEN_HTTP_DATE,
126 WSI_TOKEN_HTTP_ETAG,
127 WSI_TOKEN_HTTP_EXPECT,
128 WSI_TOKEN_HTTP_EXPIRES,
129 WSI_TOKEN_HTTP_FROM,
Andy Green2add6342014-10-12 08:38:16 +0800130 WSI_TOKEN_HOST,
Andy Greenecc2e722014-10-09 16:57:47 +0800131 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,
137 WSI_TOKEN_HTTP_LINK,
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,
151 WSI_TOKEN_HTTP_VARY,
152 WSI_TOKEN_HTTP_VIA,
153 WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
154};
155
Andy Green2add6342014-10-12 08:38:16 +0800156/* some of the entries imply values as well as header names */
157
Andy Greenecc2e722014-10-09 16:57:47 +0800158static const char * const http2_canned[] = {
159 "",
160 "",
161 "GET",
162 "POST",
163 "/",
164 "/index.html",
165 "http",
166 "https",
167 "200",
168 "204",
169 "206",
Andy Greenecc2e722014-10-09 16:57:47 +0800170 "304",
171 "400",
172 "404",
173 "500",
174 "",
175 "gzip, deflate"
176};
177
Andy Green2add6342014-10-12 08:38:16 +0800178/* see minihuf.c */
179
Andy Greenecc2e722014-10-09 16:57:47 +0800180#include "huftable.h"
181
Andy Green2add6342014-10-12 08:38:16 +0800182static int huftable_decode(int pos, char c)
Andy Greenecc2e722014-10-09 16:57:47 +0800183{
184 int q = pos + !!c;
185
186 if (lextable_terms[q >> 3] & (1 << (q & 7))) /* terminal */
187 return lextable[q] | 0x8000;
188
189 return pos + (lextable[q] << 1);
190}
191
Andy Green2add6342014-10-12 08:38:16 +0800192static int lws_hpack_update_table_size(struct libwebsocket *wsi, int idx)
Andy Greenecc2e722014-10-09 16:57:47 +0800193{
Andy Green2add6342014-10-12 08:38:16 +0800194 lwsl_info("hpack set table size %d\n", idx);
195 return 0;
Andy Greenecc2e722014-10-09 16:57:47 +0800196}
197
Andy Green2add6342014-10-12 08:38:16 +0800198static int lws_frag_start(struct libwebsocket *wsi, int hdr_token_idx)
Andy Greenecc2e722014-10-09 16:57:47 +0800199{
Andy Green2add6342014-10-12 08:38:16 +0800200 struct allocated_headers * ah = wsi->u.http2.http.ah;
201
202 if (!hdr_token_idx)
203 return 1;
204
205 if (ah->next_frag_index >= ARRAY_SIZE(ah->frag_index))
206 return 1;
207
208 ah->frags[ah->next_frag_index].offset = ah->pos;
209 ah->frags[ah->next_frag_index].len = 0;
210 ah->frags[ah->next_frag_index].next_frag_index = 0;
211
212 ah->frag_index[hdr_token_idx] = ah->next_frag_index;
213
214 return 0;
215}
216
217static int lws_frag_append(struct libwebsocket *wsi, unsigned char c)
218{
219 struct allocated_headers * ah = wsi->u.http2.http.ah;
220
221 ah->data[ah->pos++] = c;
222 ah->frags[ah->next_frag_index].len++;
223
224 return ah->pos >= sizeof(ah->data);
225}
226
227static int lws_frag_end(struct libwebsocket *wsi)
228{
229 if (lws_frag_append(wsi, 0))
230 return 1;
231
232 wsi->u.http2.http.ah->next_frag_index++;
233 return 0;
234}
235
236static void lws_dump_header(struct libwebsocket *wsi, int hdr)
237{
238 char s[200];
239 int len = lws_hdr_copy(wsi, s, sizeof(s) - 1, hdr);
240 s[len] = '\0';
241 lwsl_info(" hdr tok %d '%s'\n", hdr, s);
242}
243
244static int lws_token_from_index(struct libwebsocket *wsi, int index)
245{
246 if (index < ARRAY_SIZE(static_token))
247 return static_token[index];
248
249 // dynamic indexes
250
251 return 0;
252}
253
254static int lws_add_indexed_hdr(struct libwebsocket *wsi, int idx)
255{
256 const char *p;
257 int tok = lws_token_from_index(wsi, idx);
258
259 lwsl_info("adding indexed hdr %d (tok %d)\n", idx, tok);
260
261 if (lws_frag_start(wsi, tok))
262 return 1;
263
264 if (idx < ARRAY_SIZE(http2_canned)) {
265 p = http2_canned[idx];
266 while (*p)
267 if (lws_frag_append(wsi, *p++))
268 return 1;
269 }
270 if (lws_frag_end(wsi))
271 return 1;
272
273 lws_dump_header(wsi, tok);
274
275 return 0;
276}
277
278int lws_hpack_interpret(struct libwebsocket_context *context,
279 struct libwebsocket *wsi, unsigned char c)
280{
281 unsigned int prev;
282 unsigned char c1;
283 int n;
284
Andy Greenecc2e722014-10-09 16:57:47 +0800285 switch (wsi->u.http2.hpack) {
286 case HPKS_TYPE:
287 if (c & 0x80) { /* indexed header field only */
Andy Green2add6342014-10-12 08:38:16 +0800288 /* just a possibly-extended integer */
289 wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_7;
Andy Greenecc2e722014-10-09 16:57:47 +0800290 wsi->u.http2.header_index = c & 0x7f;
Andy Green2add6342014-10-12 08:38:16 +0800291 if ((c & 0x7f) == 0x7f) {
292 wsi->u.http2.hpack_len = c & 0x7f;
293 wsi->u.http2.hpack_m = 0;
294 wsi->u.http2.hpack = HPKS_IDX_EXT;
295 break;
296 }
297 if (lws_add_indexed_hdr(wsi, c & 0x7f))
298 return 1;
Andy Greenecc2e722014-10-09 16:57:47 +0800299 /* stay at same state */
300 break;
301 }
302 if (c & 0x40) { /* literal header incr idx */
Andy Green2add6342014-10-12 08:38:16 +0800303 /*
304 * [possibly-extended hdr idx (6) | new literal hdr name]
305 * H + possibly-extended value length
306 * literal value
307 */
308 wsi->u.http2.header_index = 0;
Andy Greenecc2e722014-10-09 16:57:47 +0800309 if (c == 0x40) { /* literal name */
Andy Green2add6342014-10-12 08:38:16 +0800310 wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE_INCR;
311 wsi->u.http2.value = 0;
Andy Greenecc2e722014-10-09 16:57:47 +0800312 wsi->u.http2.hpack = HPKS_HLEN;
313 break;
314 }
315 /* indexed name */
Andy Green2add6342014-10-12 08:38:16 +0800316 wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_6_VALUE_INCR;
317 if ((c & 0x3f) == 0x3f) {
318 wsi->u.http2.hpack_len = c & 0x3f;
319 wsi->u.http2.hpack_m = 0;
320 wsi->u.http2.hpack = HPKS_IDX_EXT;
321 break;
322 }
Andy Greenecc2e722014-10-09 16:57:47 +0800323 wsi->u.http2.header_index = c & 0x3f;
Andy Green2add6342014-10-12 08:38:16 +0800324 wsi->u.http2.value = 1;
Andy Greenecc2e722014-10-09 16:57:47 +0800325 wsi->u.http2.hpack = HPKS_HLEN;
326 break;
327 }
328 switch(c & 0xf0) {
Andy Green2add6342014-10-12 08:38:16 +0800329 case 0x10: /* literal header never index */
Andy Greenecc2e722014-10-09 16:57:47 +0800330 case 0: /* literal header without indexing */
Andy Green2add6342014-10-12 08:38:16 +0800331 /*
332 * follows 0x40 except 4-bit hdr idx
333 * and don't add to index
334 */
Andy Greenecc2e722014-10-09 16:57:47 +0800335 if (c == 0) { /* literal name */
Andy Green2add6342014-10-12 08:38:16 +0800336 wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE;
337 wsi->u.http2.hpack = HPKS_HLEN;
338 wsi->u.http2.value = 0;
Andy Greenecc2e722014-10-09 16:57:47 +0800339 break;
340 }
341 /* indexed name */
Andy Green2add6342014-10-12 08:38:16 +0800342 wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_4_VALUE;
343 wsi->u.http2.header_index = 0;
344 if ((c & 0xf) == 0xf) {
345 wsi->u.http2.hpack_len = c & 0xf;
346 wsi->u.http2.hpack_m = 0;
347 wsi->u.http2.hpack = HPKS_IDX_EXT;
Andy Greenecc2e722014-10-09 16:57:47 +0800348 break;
349 }
Andy Greenecc2e722014-10-09 16:57:47 +0800350 wsi->u.http2.header_index = c & 0xf;
Andy Green2add6342014-10-12 08:38:16 +0800351 wsi->u.http2.value = 1;
352 wsi->u.http2.hpack = HPKS_HLEN;
Andy Greenecc2e722014-10-09 16:57:47 +0800353 break;
Andy Green2add6342014-10-12 08:38:16 +0800354
Andy Greenecc2e722014-10-09 16:57:47 +0800355 case 0x20:
356 case 0x30: /* header table size update */
Andy Green2add6342014-10-12 08:38:16 +0800357 /* possibly-extended size value (5) */
358 wsi->u.http2.hpack_type = HPKT_SIZE_5;
359 if ((c & 0x1f) == 0x1f) {
360 wsi->u.http2.hpack_len = c & 0x1f;
361 wsi->u.http2.hpack_m = 0;
362 wsi->u.http2.hpack = HPKS_IDX_EXT;
363 break;
364 }
365 lws_hpack_update_table_size(wsi, c & 0x1f);
366 /* stay at HPKS_TYPE state */
Andy Greenecc2e722014-10-09 16:57:47 +0800367 break;
368 }
Andy Green2add6342014-10-12 08:38:16 +0800369 break;
370
371 case HPKS_IDX_EXT:
372 wsi->u.http2.hpack_len += (c & 0x7f) << wsi->u.http2.hpack_m;
373 wsi->u.http2.hpack_m += 7;
374 if (!(c & 0x80)) {
375 switch (wsi->u.http2.hpack_type) {
376 case HPKT_INDEXED_HDR_7:
377 if (lws_add_indexed_hdr(wsi, wsi->u.http2.hpack_len))
378 return 1;
379 wsi->u.http2.hpack = HPKS_TYPE;
380 break;
381 default:
382 wsi->u.http2.header_index = wsi->u.http2.hpack_len;
383 wsi->u.http2.value = 1;
384 wsi->u.http2.hpack = HPKS_HLEN;
385 break;
386 }
387 }
388 break;
389
390 case HPKS_HLEN: /* [ H | 7+ ] */
Andy Greenecc2e722014-10-09 16:57:47 +0800391 wsi->u.http2.huff = !!(c & 0x80);
Andy Green2add6342014-10-12 08:38:16 +0800392 wsi->u.http2.hpack_pos = 0;
Andy Greenecc2e722014-10-09 16:57:47 +0800393 wsi->u.http2.hpack_len = c & 0x7f;
Andy Green2add6342014-10-12 08:38:16 +0800394 if (wsi->u.http2.hpack_len < 0x7f) {
395pre_data:
396 if (wsi->u.http2.value) {
397 if (lws_frag_start(wsi,
398 lws_token_from_index(wsi,
399 wsi->u.http2.header_index)))
400 return 1;
401 } else
402 wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
403 wsi->u.http2.hpack = HPKS_DATA;
Andy Greenecc2e722014-10-09 16:57:47 +0800404 break;
405 }
406 wsi->u.http2.hpack_m = 0;
Andy Green2add6342014-10-12 08:38:16 +0800407 wsi->u.http2.hpack = HPKS_HLEN_EXT;
Andy Greenecc2e722014-10-09 16:57:47 +0800408 break;
Andy Green2add6342014-10-12 08:38:16 +0800409
Andy Greenecc2e722014-10-09 16:57:47 +0800410 case HPKS_HLEN_EXT:
Andy Green2add6342014-10-12 08:38:16 +0800411 wsi->u.http2.hpack_len += (c & 0x7f) <<
412 wsi->u.http2.hpack_m;
Andy Greenecc2e722014-10-09 16:57:47 +0800413 wsi->u.http2.hpack_m += 7;
414 if (!(c & 0x80))
Andy Green2add6342014-10-12 08:38:16 +0800415 goto pre_data;
416
Andy Greenecc2e722014-10-09 16:57:47 +0800417 break;
418
419 case HPKS_DATA:
Andy Green2add6342014-10-12 08:38:16 +0800420 for (n = 0; n < 8; n++) {
421 if (wsi->u.http2.huff) {
422 prev = wsi->u.http2.hpack_pos;
423 wsi->u.http2.hpack_pos =
424 huftable_decode(
425 wsi->u.http2.hpack_pos,
426 (c >> 7) & 1);
427 c <<= 1;
428 if (wsi->u.http2.hpack_pos == 0xffff)
429 return 1;
430 if (!(wsi->u.http2.hpack_pos & 0x8000))
431 continue;
432 c1 = wsi->u.http2.hpack_pos & 0x7fff;
433 wsi->u.http2.hpack_pos = 0;
434
435 if (!c1 && prev == HUFTABLE_0x100_PREV)
436 ; /* EOT */
437 } else {
438 n = 8;
439 c1 = c;
440 }
441 if (wsi->u.http2.value) { /* value */
442 if (lws_frag_append(wsi, c1))
443 return 1;
444 } else { /* name */
445 if (libwebsocket_parse(context, wsi, c1))
446 return 1;
447
448 }
449 }
450 if (--wsi->u.http2.hpack_len == 0) {
451 n = 8;
452 if (wsi->u.http2.value) {
453 if (lws_frag_end(wsi))
454 return 1;
Andy Greenecc2e722014-10-09 16:57:47 +0800455
Andy Green2add6342014-10-12 08:38:16 +0800456 lws_dump_header(wsi, lws_token_from_index(wsi, wsi->u.http2.header_index));
457
458 wsi->u.http2.hpack = HPKS_TYPE;
459 } else { /* name */
460 if (wsi->u.hdr.parser_state < WSI_TOKEN_COUNT)
461
462 wsi->u.http2.value = 1;
463 wsi->u.http2.hpack = HPKS_HLEN;
464 }
465 }
466 break;
Andy Greenecc2e722014-10-09 16:57:47 +0800467 }
Andy Green2add6342014-10-12 08:38:16 +0800468
469 return 0;
Andy Greenecc2e722014-10-09 16:57:47 +0800470}
Andy Green917f43a2014-10-12 14:31:47 +0800471
472static int lws_http2_num(int starting_bits, unsigned long num, unsigned char **p, unsigned char *end)
473{
474 int mask = (1 << starting_bits) - 1;
475
476 if (num < mask) {
477 *((*p)++) |= num;
478 return *p >= end;
479 }
480
481 *((*p)++) |= mask;
482 if (*p >= end)
483 return 1;
484
485 num -= mask;
486 while (num >= 128) {
487 *((*p)++) = 0x80 | (num & 0x7f);
488 if (*p >= end)
489 return 1;
490 num >>= 7;
491 }
492
493 return 0;
494}
495
496int lws_add_http2_header_by_name(struct libwebsocket_context *context,
497 struct libwebsocket *wsi,
498 const unsigned char *name,
499 const unsigned char *value,
500 int length,
501 unsigned char **p,
502 unsigned char *end)
503{
504 int len;
505
506 lwsl_info("%s: %p %s:%s\n", __func__, *p, name, value);
507
508 len = strlen((char *)name);
509 if (len)
510 if (name[len - 1] == ':')
511 len--;
512
513 if (end - *p < len + length + 8)
514 return 1;
515
516 *((*p)++) = 0; /* not indexed, literal name */
517
518 **p = 0; /* non-HUF */
519 if (lws_http2_num(7, len, p, end))
520 return 1;
521 memcpy(*p, name, len);
522 *p += len;
523
524 *(*p) = 0; /* non-HUF */
525 if (lws_http2_num(7, length, p, end))
526 return 1;
527
528 memcpy(*p, value, length);
529 *p += length;
530
531 return 0;
532}
533
534int lws_add_http2_header_by_token(struct libwebsocket_context *context,
535 struct libwebsocket *wsi,
536 enum lws_token_indexes token,
537 const unsigned char *value,
538 int length,
539 unsigned char **p,
540 unsigned char *end)
541{
542 const unsigned char *name;
543
544 name = lws_token_to_string(token);
545 if (!name)
546 return 1;
547
548 return lws_add_http2_header_by_name(context, wsi, name, value, length, p, end);
549}
550
551int lws_add_http2_header_status(struct libwebsocket_context *context,
552 struct libwebsocket *wsi,
553 unsigned int code,
554 unsigned char **p,
555 unsigned char *end)
556{
557 unsigned char status[10];
558 int n;
559
560 n = sprintf((char *)status, "%u", code);
561 if (lws_add_http2_header_by_token(context, wsi, WSI_TOKEN_HTTP_COLON_STATUS, status, n, p, end))
562 return 1;
563
564 return 0;
565}