Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 1 | /* |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 2 | * 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 Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 25 | * 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 | |
| 91 | static 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 Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 130 | WSI_TOKEN_HOST, |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 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, |
| 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 Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 156 | /* some of the entries imply values as well as header names */ |
| 157 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 158 | static 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 Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 170 | "304", |
| 171 | "400", |
| 172 | "404", |
| 173 | "500", |
| 174 | "", |
| 175 | "gzip, deflate" |
| 176 | }; |
| 177 | |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 178 | /* see minihuf.c */ |
| 179 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 180 | #include "huftable.h" |
| 181 | |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 182 | static int huftable_decode(int pos, char c) |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 183 | { |
| 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 Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 192 | static int lws_hpack_update_table_size(struct libwebsocket *wsi, int idx) |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 193 | { |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 194 | lwsl_info("hpack set table size %d\n", idx); |
| 195 | return 0; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 198 | static int lws_frag_start(struct libwebsocket *wsi, int hdr_token_idx) |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 199 | { |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 200 | 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 | |
| 217 | static 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 | |
| 227 | static 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 | |
| 236 | static 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'; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 241 | lwsl_info(" hdr tok %d (%s) = '%s'\n", hdr, lws_token_to_string(hdr), s); |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 244 | static int lws_token_from_index(struct libwebsocket *wsi, int index, char **arg, int *len) |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 245 | { |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 246 | struct hpack_dynamic_table *dyn; |
| 247 | |
| 248 | /* dynamic table only belongs to network wsi */ |
| 249 | |
| 250 | wsi = lws_http2_get_network_wsi(wsi); |
| 251 | |
| 252 | dyn = wsi->u.http2.hpack_dyn_table; |
| 253 | |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 254 | if (index < ARRAY_SIZE(static_token)) |
| 255 | return static_token[index]; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 256 | |
| 257 | if (!dyn) |
| 258 | return 0; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 259 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 260 | index -= ARRAY_SIZE(static_token); |
| 261 | if (index >= dyn->num_entries) |
| 262 | return 0; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 263 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 264 | if (arg && len) { |
| 265 | *arg = dyn->args + dyn->entries[index].arg_offset; |
| 266 | *len = dyn->entries[index].arg_len; |
| 267 | } |
| 268 | |
| 269 | return dyn->entries[index].token; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 270 | } |
| 271 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 272 | static int lws_hpack_add_dynamic_header(struct libwebsocket *wsi, int token, char *arg, int len) |
| 273 | { |
| 274 | struct hpack_dynamic_table *dyn; |
| 275 | int ret = 1; |
| 276 | |
| 277 | wsi = lws_http2_get_network_wsi(wsi); |
| 278 | dyn = wsi->u.http2.hpack_dyn_table; |
| 279 | |
| 280 | if (!dyn) { |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 281 | dyn = lws_zalloc(sizeof(*dyn)); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 282 | if (!dyn) |
| 283 | return 1; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 284 | wsi->u.http2.hpack_dyn_table = dyn; |
| 285 | |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 286 | dyn->args = lws_malloc(1024); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 287 | if (!dyn->args) |
| 288 | goto bail1; |
| 289 | dyn->args_length = 1024; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 290 | dyn->entries = lws_malloc(sizeof(dyn->entries[0]) * 20); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 291 | if (!dyn->entries) |
| 292 | goto bail2; |
| 293 | dyn->num_entries = 20; |
| 294 | } |
| 295 | |
| 296 | if (dyn->next == dyn->num_entries) |
| 297 | return 1; |
| 298 | |
| 299 | if (dyn->args_length - dyn->pos < len) |
| 300 | return 1; |
| 301 | |
| 302 | dyn->entries[dyn->next].token = token; |
| 303 | dyn->entries[dyn->next].arg_offset = dyn->pos; |
| 304 | if (len) |
| 305 | memcpy(dyn->args + dyn->pos, arg, len); |
| 306 | dyn->entries[dyn->next].arg_len = len; |
| 307 | |
| 308 | lwsl_info("%s: added dynamic hdr %d, token %d (%s), len %d\n", __func__, dyn->next, token, lws_token_to_string(token), len); |
| 309 | |
| 310 | dyn->pos += len; |
| 311 | dyn->next++; |
| 312 | |
| 313 | return 0; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 314 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 315 | bail2: |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 316 | lws_free(dyn->args); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 317 | bail1: |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 318 | lws_free(dyn); |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 319 | wsi->u.http2.hpack_dyn_table = NULL; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 320 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | static int lws_write_indexed_hdr(struct libwebsocket *wsi, int idx) |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 325 | { |
| 326 | const char *p; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 327 | int tok = lws_token_from_index(wsi, idx, NULL, 0); |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 328 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 329 | lwsl_info("writing indexed hdr %d (tok %d '%s')\n", idx, tok, lws_token_to_string(tok)); |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 330 | |
| 331 | if (lws_frag_start(wsi, tok)) |
| 332 | return 1; |
| 333 | |
| 334 | if (idx < ARRAY_SIZE(http2_canned)) { |
| 335 | p = http2_canned[idx]; |
| 336 | while (*p) |
| 337 | if (lws_frag_append(wsi, *p++)) |
| 338 | return 1; |
| 339 | } |
| 340 | if (lws_frag_end(wsi)) |
| 341 | return 1; |
| 342 | |
| 343 | lws_dump_header(wsi, tok); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int lws_hpack_interpret(struct libwebsocket_context *context, |
| 349 | struct libwebsocket *wsi, unsigned char c) |
| 350 | { |
| 351 | unsigned int prev; |
| 352 | unsigned char c1; |
| 353 | int n; |
| 354 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 355 | switch (wsi->u.http2.hpack) { |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 356 | case HPKS_OPT_PADDING: |
| 357 | wsi->u.http2.padding = c; |
| 358 | lwsl_info("padding %d\n", c); |
| 359 | if (wsi->u.http2.flags & LWS_HTTP2_FLAG_PRIORITY) { |
| 360 | wsi->u.http2.hpack = HKPS_OPT_E_DEPENDENCY; |
| 361 | wsi->u.http2.hpack_m = 4; |
| 362 | } else |
| 363 | wsi->u.http2.hpack = HPKS_TYPE; |
| 364 | break; |
| 365 | case HKPS_OPT_E_DEPENDENCY: |
| 366 | wsi->u.http2.hpack_e_dep <<= 8; |
| 367 | wsi->u.http2.hpack_e_dep |= c; |
| 368 | if (! --wsi->u.http2.hpack_m) { |
| 369 | lwsl_info("hpack_e_dep = 0x%x\n", wsi->u.http2.hpack_e_dep); |
| 370 | wsi->u.http2.hpack = HKPS_OPT_WEIGHT; |
| 371 | } |
| 372 | break; |
| 373 | case HKPS_OPT_WEIGHT: |
| 374 | /* weight */ |
| 375 | wsi->u.http2.hpack = HPKS_TYPE; |
| 376 | break; |
| 377 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 378 | case HPKS_TYPE: |
Andy Green | bbbf07a | 2014-10-27 16:46:44 +0800 | [diff] [blame] | 379 | |
| 380 | if (wsi->u.http2.count > (wsi->u.http2.length - wsi->u.http2.padding)) { |
| 381 | lwsl_info("padding eat\n"); |
| 382 | break; |
| 383 | } |
| 384 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 385 | if (c & 0x80) { /* indexed header field only */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 386 | /* just a possibly-extended integer */ |
| 387 | wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_7; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 388 | wsi->u.http2.header_index = c & 0x7f; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 389 | if ((c & 0x7f) == 0x7f) { |
| 390 | wsi->u.http2.hpack_len = c & 0x7f; |
| 391 | wsi->u.http2.hpack_m = 0; |
| 392 | wsi->u.http2.hpack = HPKS_IDX_EXT; |
| 393 | break; |
| 394 | } |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 395 | if (lws_write_indexed_hdr(wsi, c & 0x7f)) |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 396 | return 1; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 397 | /* stay at same state */ |
| 398 | break; |
| 399 | } |
| 400 | if (c & 0x40) { /* literal header incr idx */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 401 | /* |
| 402 | * [possibly-extended hdr idx (6) | new literal hdr name] |
| 403 | * H + possibly-extended value length |
| 404 | * literal value |
| 405 | */ |
| 406 | wsi->u.http2.header_index = 0; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 407 | if (c == 0x40) { /* literal name */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 408 | wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE_INCR; |
| 409 | wsi->u.http2.value = 0; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 410 | wsi->u.http2.hpack = HPKS_HLEN; |
| 411 | break; |
| 412 | } |
| 413 | /* indexed name */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 414 | wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_6_VALUE_INCR; |
| 415 | if ((c & 0x3f) == 0x3f) { |
| 416 | wsi->u.http2.hpack_len = c & 0x3f; |
| 417 | wsi->u.http2.hpack_m = 0; |
| 418 | wsi->u.http2.hpack = HPKS_IDX_EXT; |
| 419 | break; |
| 420 | } |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 421 | wsi->u.http2.header_index = c & 0x3f; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 422 | wsi->u.http2.value = 1; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 423 | wsi->u.http2.hpack = HPKS_HLEN; |
| 424 | break; |
| 425 | } |
| 426 | switch(c & 0xf0) { |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 427 | case 0x10: /* literal header never index */ |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 428 | case 0: /* literal header without indexing */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 429 | /* |
| 430 | * follows 0x40 except 4-bit hdr idx |
| 431 | * and don't add to index |
| 432 | */ |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 433 | if (c == 0) { /* literal name */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 434 | wsi->u.http2.hpack_type = HPKT_LITERAL_HDR_VALUE; |
| 435 | wsi->u.http2.hpack = HPKS_HLEN; |
| 436 | wsi->u.http2.value = 0; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | /* indexed name */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 440 | wsi->u.http2.hpack_type = HPKT_INDEXED_HDR_4_VALUE; |
| 441 | wsi->u.http2.header_index = 0; |
| 442 | if ((c & 0xf) == 0xf) { |
| 443 | wsi->u.http2.hpack_len = c & 0xf; |
| 444 | wsi->u.http2.hpack_m = 0; |
| 445 | wsi->u.http2.hpack = HPKS_IDX_EXT; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 446 | break; |
| 447 | } |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 448 | wsi->u.http2.header_index = c & 0xf; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 449 | wsi->u.http2.value = 1; |
| 450 | wsi->u.http2.hpack = HPKS_HLEN; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 451 | break; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 452 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 453 | case 0x20: |
| 454 | case 0x30: /* header table size update */ |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 455 | /* possibly-extended size value (5) */ |
| 456 | wsi->u.http2.hpack_type = HPKT_SIZE_5; |
| 457 | if ((c & 0x1f) == 0x1f) { |
| 458 | wsi->u.http2.hpack_len = c & 0x1f; |
| 459 | wsi->u.http2.hpack_m = 0; |
| 460 | wsi->u.http2.hpack = HPKS_IDX_EXT; |
| 461 | break; |
| 462 | } |
| 463 | lws_hpack_update_table_size(wsi, c & 0x1f); |
| 464 | /* stay at HPKS_TYPE state */ |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 465 | break; |
| 466 | } |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 467 | break; |
| 468 | |
| 469 | case HPKS_IDX_EXT: |
| 470 | wsi->u.http2.hpack_len += (c & 0x7f) << wsi->u.http2.hpack_m; |
| 471 | wsi->u.http2.hpack_m += 7; |
| 472 | if (!(c & 0x80)) { |
| 473 | switch (wsi->u.http2.hpack_type) { |
| 474 | case HPKT_INDEXED_HDR_7: |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 475 | if (lws_write_indexed_hdr(wsi, wsi->u.http2.hpack_len)) |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 476 | return 1; |
| 477 | wsi->u.http2.hpack = HPKS_TYPE; |
| 478 | break; |
| 479 | default: |
| 480 | wsi->u.http2.header_index = wsi->u.http2.hpack_len; |
| 481 | wsi->u.http2.value = 1; |
| 482 | wsi->u.http2.hpack = HPKS_HLEN; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | break; |
| 487 | |
| 488 | case HPKS_HLEN: /* [ H | 7+ ] */ |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 489 | wsi->u.http2.huff = !!(c & 0x80); |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 490 | wsi->u.http2.hpack_pos = 0; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 491 | wsi->u.http2.hpack_len = c & 0x7f; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 492 | if (wsi->u.http2.hpack_len < 0x7f) { |
| 493 | pre_data: |
| 494 | if (wsi->u.http2.value) { |
| 495 | if (lws_frag_start(wsi, |
| 496 | lws_token_from_index(wsi, |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 497 | wsi->u.http2.header_index, NULL, NULL))) |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 498 | return 1; |
| 499 | } else |
| 500 | wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART; |
| 501 | wsi->u.http2.hpack = HPKS_DATA; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 502 | break; |
| 503 | } |
| 504 | wsi->u.http2.hpack_m = 0; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 505 | wsi->u.http2.hpack = HPKS_HLEN_EXT; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 506 | break; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 507 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 508 | case HPKS_HLEN_EXT: |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 509 | wsi->u.http2.hpack_len += (c & 0x7f) << |
| 510 | wsi->u.http2.hpack_m; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 511 | wsi->u.http2.hpack_m += 7; |
| 512 | if (!(c & 0x80)) |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 513 | goto pre_data; |
| 514 | |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 515 | break; |
| 516 | |
| 517 | case HPKS_DATA: |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 518 | for (n = 0; n < 8; n++) { |
| 519 | if (wsi->u.http2.huff) { |
| 520 | prev = wsi->u.http2.hpack_pos; |
| 521 | wsi->u.http2.hpack_pos = |
| 522 | huftable_decode( |
| 523 | wsi->u.http2.hpack_pos, |
| 524 | (c >> 7) & 1); |
| 525 | c <<= 1; |
| 526 | if (wsi->u.http2.hpack_pos == 0xffff) |
| 527 | return 1; |
| 528 | if (!(wsi->u.http2.hpack_pos & 0x8000)) |
| 529 | continue; |
| 530 | c1 = wsi->u.http2.hpack_pos & 0x7fff; |
| 531 | wsi->u.http2.hpack_pos = 0; |
| 532 | |
| 533 | if (!c1 && prev == HUFTABLE_0x100_PREV) |
| 534 | ; /* EOT */ |
| 535 | } else { |
| 536 | n = 8; |
| 537 | c1 = c; |
| 538 | } |
| 539 | if (wsi->u.http2.value) { /* value */ |
| 540 | if (lws_frag_append(wsi, c1)) |
| 541 | return 1; |
| 542 | } else { /* name */ |
| 543 | if (libwebsocket_parse(context, wsi, c1)) |
| 544 | return 1; |
| 545 | |
| 546 | } |
| 547 | } |
| 548 | if (--wsi->u.http2.hpack_len == 0) { |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 549 | |
| 550 | switch (wsi->u.http2.hpack_type) { |
| 551 | case HPKT_LITERAL_HDR_VALUE_INCR: |
| 552 | case HPKT_INDEXED_HDR_6_VALUE_INCR: // !!! |
| 553 | if (lws_hpack_add_dynamic_header(wsi, lws_token_from_index(wsi, wsi->u.http2.header_index, NULL, NULL), NULL, 0)) |
| 554 | return 1; |
| 555 | break; |
| 556 | default: |
| 557 | break; |
| 558 | } |
| 559 | |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 560 | n = 8; |
| 561 | if (wsi->u.http2.value) { |
| 562 | if (lws_frag_end(wsi)) |
| 563 | return 1; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 564 | |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 565 | lws_dump_header(wsi, lws_token_from_index(wsi, wsi->u.http2.header_index, NULL, NULL)); |
| 566 | if (wsi->u.http2.count + wsi->u.http2.padding == wsi->u.http2.length) |
| 567 | wsi->u.http2.hpack = HKPS_OPT_DISCARD_PADDING; |
| 568 | else |
| 569 | wsi->u.http2.hpack = HPKS_TYPE; |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 570 | } else { /* name */ |
| 571 | if (wsi->u.hdr.parser_state < WSI_TOKEN_COUNT) |
| 572 | |
| 573 | wsi->u.http2.value = 1; |
| 574 | wsi->u.http2.hpack = HPKS_HLEN; |
| 575 | } |
| 576 | } |
| 577 | break; |
Andy Green | 200f385 | 2014-10-18 12:23:05 +0800 | [diff] [blame] | 578 | case HKPS_OPT_DISCARD_PADDING: |
| 579 | lwsl_info("eating padding %x\n", c); |
| 580 | if (! --wsi->u.http2.padding) |
| 581 | wsi->u.http2.hpack = HPKS_TYPE; |
| 582 | break; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 583 | } |
Andy Green | 2add634 | 2014-10-12 08:38:16 +0800 | [diff] [blame] | 584 | |
| 585 | return 0; |
Andy Green | ecc2e72 | 2014-10-09 16:57:47 +0800 | [diff] [blame] | 586 | } |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 587 | |
| 588 | static int lws_http2_num(int starting_bits, unsigned long num, unsigned char **p, unsigned char *end) |
| 589 | { |
| 590 | int mask = (1 << starting_bits) - 1; |
| 591 | |
| 592 | if (num < mask) { |
| 593 | *((*p)++) |= num; |
| 594 | return *p >= end; |
| 595 | } |
| 596 | |
| 597 | *((*p)++) |= mask; |
| 598 | if (*p >= end) |
| 599 | return 1; |
| 600 | |
| 601 | num -= mask; |
| 602 | while (num >= 128) { |
| 603 | *((*p)++) = 0x80 | (num & 0x7f); |
| 604 | if (*p >= end) |
| 605 | return 1; |
| 606 | num >>= 7; |
| 607 | } |
| 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | int lws_add_http2_header_by_name(struct libwebsocket_context *context, |
| 613 | struct libwebsocket *wsi, |
| 614 | const unsigned char *name, |
| 615 | const unsigned char *value, |
| 616 | int length, |
| 617 | unsigned char **p, |
| 618 | unsigned char *end) |
| 619 | { |
| 620 | int len; |
| 621 | |
| 622 | lwsl_info("%s: %p %s:%s\n", __func__, *p, name, value); |
| 623 | |
| 624 | len = strlen((char *)name); |
| 625 | if (len) |
| 626 | if (name[len - 1] == ':') |
| 627 | len--; |
| 628 | |
| 629 | if (end - *p < len + length + 8) |
| 630 | return 1; |
| 631 | |
| 632 | *((*p)++) = 0; /* not indexed, literal name */ |
| 633 | |
| 634 | **p = 0; /* non-HUF */ |
| 635 | if (lws_http2_num(7, len, p, end)) |
| 636 | return 1; |
| 637 | memcpy(*p, name, len); |
| 638 | *p += len; |
| 639 | |
| 640 | *(*p) = 0; /* non-HUF */ |
| 641 | if (lws_http2_num(7, length, p, end)) |
| 642 | return 1; |
| 643 | |
| 644 | memcpy(*p, value, length); |
| 645 | *p += length; |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | int lws_add_http2_header_by_token(struct libwebsocket_context *context, |
| 651 | struct libwebsocket *wsi, |
| 652 | enum lws_token_indexes token, |
| 653 | const unsigned char *value, |
| 654 | int length, |
| 655 | unsigned char **p, |
| 656 | unsigned char *end) |
| 657 | { |
| 658 | const unsigned char *name; |
| 659 | |
| 660 | name = lws_token_to_string(token); |
| 661 | if (!name) |
| 662 | return 1; |
| 663 | |
| 664 | return lws_add_http2_header_by_name(context, wsi, name, value, length, p, end); |
| 665 | } |
| 666 | |
| 667 | int lws_add_http2_header_status(struct libwebsocket_context *context, |
| 668 | struct libwebsocket *wsi, |
| 669 | unsigned int code, |
| 670 | unsigned char **p, |
| 671 | unsigned char *end) |
| 672 | { |
| 673 | unsigned char status[10]; |
| 674 | int n; |
| 675 | |
Andy Green | 1cea581 | 2014-10-19 07:36:20 +0800 | [diff] [blame] | 676 | wsi->u.http2.send_END_STREAM = !!(code >= 400); |
Andy Green | b0e2e50 | 2014-10-18 18:54:04 +0800 | [diff] [blame] | 677 | |
Andy Green | 917f43a | 2014-10-12 14:31:47 +0800 | [diff] [blame] | 678 | n = sprintf((char *)status, "%u", code); |
| 679 | if (lws_add_http2_header_by_token(context, wsi, WSI_TOKEN_HTTP_COLON_STATUS, status, n, p, end)) |
| 680 | return 1; |
| 681 | |
| 682 | return 0; |
Alejandro Mery | 6ff2824 | 2014-12-04 23:59:35 +0100 | [diff] [blame] | 683 | } |