1 /*
2 * nghttp2 - HTTP/2 C Library
3 *
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 #include "http2.h"
26
27 #include "llhttp.h"
28
29 #include "util.h"
30
31 namespace nghttp2 {
32
33 namespace http2 {
34
get_reason_phrase(unsigned int status_code)35 StringRef get_reason_phrase(unsigned int status_code) {
36 switch (status_code) {
37 case 100:
38 return StringRef::from_lit("Continue");
39 case 101:
40 return StringRef::from_lit("Switching Protocols");
41 case 103:
42 return StringRef::from_lit("Early Hints");
43 case 200:
44 return StringRef::from_lit("OK");
45 case 201:
46 return StringRef::from_lit("Created");
47 case 202:
48 return StringRef::from_lit("Accepted");
49 case 203:
50 return StringRef::from_lit("Non-Authoritative Information");
51 case 204:
52 return StringRef::from_lit("No Content");
53 case 205:
54 return StringRef::from_lit("Reset Content");
55 case 206:
56 return StringRef::from_lit("Partial Content");
57 case 300:
58 return StringRef::from_lit("Multiple Choices");
59 case 301:
60 return StringRef::from_lit("Moved Permanently");
61 case 302:
62 return StringRef::from_lit("Found");
63 case 303:
64 return StringRef::from_lit("See Other");
65 case 304:
66 return StringRef::from_lit("Not Modified");
67 case 305:
68 return StringRef::from_lit("Use Proxy");
69 // case 306: return StringRef::from_lit("(Unused)");
70 case 307:
71 return StringRef::from_lit("Temporary Redirect");
72 case 308:
73 return StringRef::from_lit("Permanent Redirect");
74 case 400:
75 return StringRef::from_lit("Bad Request");
76 case 401:
77 return StringRef::from_lit("Unauthorized");
78 case 402:
79 return StringRef::from_lit("Payment Required");
80 case 403:
81 return StringRef::from_lit("Forbidden");
82 case 404:
83 return StringRef::from_lit("Not Found");
84 case 405:
85 return StringRef::from_lit("Method Not Allowed");
86 case 406:
87 return StringRef::from_lit("Not Acceptable");
88 case 407:
89 return StringRef::from_lit("Proxy Authentication Required");
90 case 408:
91 return StringRef::from_lit("Request Timeout");
92 case 409:
93 return StringRef::from_lit("Conflict");
94 case 410:
95 return StringRef::from_lit("Gone");
96 case 411:
97 return StringRef::from_lit("Length Required");
98 case 412:
99 return StringRef::from_lit("Precondition Failed");
100 case 413:
101 return StringRef::from_lit("Payload Too Large");
102 case 414:
103 return StringRef::from_lit("URI Too Long");
104 case 415:
105 return StringRef::from_lit("Unsupported Media Type");
106 case 416:
107 return StringRef::from_lit("Requested Range Not Satisfiable");
108 case 417:
109 return StringRef::from_lit("Expectation Failed");
110 case 421:
111 return StringRef::from_lit("Misdirected Request");
112 case 425:
113 // https://tools.ietf.org/html/rfc8470
114 return StringRef::from_lit("Too Early");
115 case 426:
116 return StringRef::from_lit("Upgrade Required");
117 case 428:
118 return StringRef::from_lit("Precondition Required");
119 case 429:
120 return StringRef::from_lit("Too Many Requests");
121 case 431:
122 return StringRef::from_lit("Request Header Fields Too Large");
123 case 451:
124 return StringRef::from_lit("Unavailable For Legal Reasons");
125 case 500:
126 return StringRef::from_lit("Internal Server Error");
127 case 501:
128 return StringRef::from_lit("Not Implemented");
129 case 502:
130 return StringRef::from_lit("Bad Gateway");
131 case 503:
132 return StringRef::from_lit("Service Unavailable");
133 case 504:
134 return StringRef::from_lit("Gateway Timeout");
135 case 505:
136 return StringRef::from_lit("HTTP Version Not Supported");
137 case 511:
138 return StringRef::from_lit("Network Authentication Required");
139 default:
140 return StringRef{};
141 }
142 }
143
stringify_status(BlockAllocator & balloc,unsigned int status_code)144 StringRef stringify_status(BlockAllocator &balloc, unsigned int status_code) {
145 switch (status_code) {
146 case 100:
147 return StringRef::from_lit("100");
148 case 101:
149 return StringRef::from_lit("101");
150 case 103:
151 return StringRef::from_lit("103");
152 case 200:
153 return StringRef::from_lit("200");
154 case 201:
155 return StringRef::from_lit("201");
156 case 202:
157 return StringRef::from_lit("202");
158 case 203:
159 return StringRef::from_lit("203");
160 case 204:
161 return StringRef::from_lit("204");
162 case 205:
163 return StringRef::from_lit("205");
164 case 206:
165 return StringRef::from_lit("206");
166 case 300:
167 return StringRef::from_lit("300");
168 case 301:
169 return StringRef::from_lit("301");
170 case 302:
171 return StringRef::from_lit("302");
172 case 303:
173 return StringRef::from_lit("303");
174 case 304:
175 return StringRef::from_lit("304");
176 case 305:
177 return StringRef::from_lit("305");
178 // case 306: return StringRef::from_lit("306");
179 case 307:
180 return StringRef::from_lit("307");
181 case 308:
182 return StringRef::from_lit("308");
183 case 400:
184 return StringRef::from_lit("400");
185 case 401:
186 return StringRef::from_lit("401");
187 case 402:
188 return StringRef::from_lit("402");
189 case 403:
190 return StringRef::from_lit("403");
191 case 404:
192 return StringRef::from_lit("404");
193 case 405:
194 return StringRef::from_lit("405");
195 case 406:
196 return StringRef::from_lit("406");
197 case 407:
198 return StringRef::from_lit("407");
199 case 408:
200 return StringRef::from_lit("408");
201 case 409:
202 return StringRef::from_lit("409");
203 case 410:
204 return StringRef::from_lit("410");
205 case 411:
206 return StringRef::from_lit("411");
207 case 412:
208 return StringRef::from_lit("412");
209 case 413:
210 return StringRef::from_lit("413");
211 case 414:
212 return StringRef::from_lit("414");
213 case 415:
214 return StringRef::from_lit("415");
215 case 416:
216 return StringRef::from_lit("416");
217 case 417:
218 return StringRef::from_lit("417");
219 case 421:
220 return StringRef::from_lit("421");
221 case 426:
222 return StringRef::from_lit("426");
223 case 428:
224 return StringRef::from_lit("428");
225 case 429:
226 return StringRef::from_lit("429");
227 case 431:
228 return StringRef::from_lit("431");
229 case 451:
230 return StringRef::from_lit("451");
231 case 500:
232 return StringRef::from_lit("500");
233 case 501:
234 return StringRef::from_lit("501");
235 case 502:
236 return StringRef::from_lit("502");
237 case 503:
238 return StringRef::from_lit("503");
239 case 504:
240 return StringRef::from_lit("504");
241 case 505:
242 return StringRef::from_lit("505");
243 case 511:
244 return StringRef::from_lit("511");
245 default:
246 return util::make_string_ref_uint(balloc, status_code);
247 }
248 }
249
capitalize(DefaultMemchunks * buf,const StringRef & s)250 void capitalize(DefaultMemchunks *buf, const StringRef &s) {
251 buf->append(util::upcase(s[0]));
252 for (size_t i = 1; i < s.size(); ++i) {
253 if (s[i - 1] == '-') {
254 buf->append(util::upcase(s[i]));
255 } else {
256 buf->append(s[i]);
257 }
258 }
259 }
260
lws(const char * value)261 bool lws(const char *value) {
262 for (; *value; ++value) {
263 switch (*value) {
264 case '\t':
265 case ' ':
266 continue;
267 default:
268 return false;
269 }
270 }
271 return true;
272 }
273
copy_url_component(std::string & dest,const http_parser_url * u,int field,const char * url)274 void copy_url_component(std::string &dest, const http_parser_url *u, int field,
275 const char *url) {
276 if (u->field_set & (1 << field)) {
277 dest.assign(url + u->field_data[field].off, u->field_data[field].len);
278 }
279 }
280
to_header(const uint8_t * name,size_t namelen,const uint8_t * value,size_t valuelen,bool no_index,int32_t token)281 Headers::value_type to_header(const uint8_t *name, size_t namelen,
282 const uint8_t *value, size_t valuelen,
283 bool no_index, int32_t token) {
284 return Header(std::string(reinterpret_cast<const char *>(name), namelen),
285 std::string(reinterpret_cast<const char *>(value), valuelen),
286 no_index, token);
287 }
288
add_header(Headers & nva,const uint8_t * name,size_t namelen,const uint8_t * value,size_t valuelen,bool no_index,int32_t token)289 void add_header(Headers &nva, const uint8_t *name, size_t namelen,
290 const uint8_t *value, size_t valuelen, bool no_index,
291 int32_t token) {
292 if (valuelen > 0) {
293 size_t i, j;
294 for (i = 0; i < valuelen && (value[i] == ' ' || value[i] == '\t'); ++i)
295 ;
296 for (j = valuelen - 1; j > i && (value[j] == ' ' || value[j] == '\t'); --j)
297 ;
298 value += i;
299 valuelen -= i + (valuelen - j - 1);
300 }
301 nva.push_back(to_header(name, namelen, value, valuelen, no_index, token));
302 }
303
get_header(const Headers & nva,const char * name)304 const Headers::value_type *get_header(const Headers &nva, const char *name) {
305 const Headers::value_type *res = nullptr;
306 for (auto &nv : nva) {
307 if (nv.name == name) {
308 res = &nv;
309 }
310 }
311 return res;
312 }
313
non_empty_value(const HeaderRefs::value_type * nv)314 bool non_empty_value(const HeaderRefs::value_type *nv) {
315 return nv && !nv->value.empty();
316 }
317
318 namespace {
make_nv_internal(const std::string & name,const std::string & value,bool no_index,uint8_t nv_flags)319 nghttp2_nv make_nv_internal(const std::string &name, const std::string &value,
320 bool no_index, uint8_t nv_flags) {
321 uint8_t flags;
322
323 flags =
324 nv_flags | (no_index ? NGHTTP2_NV_FLAG_NO_INDEX : NGHTTP2_NV_FLAG_NONE);
325
326 return {(uint8_t *)name.c_str(), (uint8_t *)value.c_str(), name.size(),
327 value.size(), flags};
328 }
329 } // namespace
330
331 namespace {
make_nv_internal(const StringRef & name,const StringRef & value,bool no_index,uint8_t nv_flags)332 nghttp2_nv make_nv_internal(const StringRef &name, const StringRef &value,
333 bool no_index, uint8_t nv_flags) {
334 uint8_t flags;
335
336 flags =
337 nv_flags | (no_index ? NGHTTP2_NV_FLAG_NO_INDEX : NGHTTP2_NV_FLAG_NONE);
338
339 return {(uint8_t *)name.c_str(), (uint8_t *)value.c_str(), name.size(),
340 value.size(), flags};
341 }
342 } // namespace
343
make_nv(const std::string & name,const std::string & value,bool no_index)344 nghttp2_nv make_nv(const std::string &name, const std::string &value,
345 bool no_index) {
346 return make_nv_internal(name, value, no_index, NGHTTP2_NV_FLAG_NONE);
347 }
348
make_nv(const StringRef & name,const StringRef & value,bool no_index)349 nghttp2_nv make_nv(const StringRef &name, const StringRef &value,
350 bool no_index) {
351 return make_nv_internal(name, value, no_index, NGHTTP2_NV_FLAG_NONE);
352 }
353
make_nv_nocopy(const std::string & name,const std::string & value,bool no_index)354 nghttp2_nv make_nv_nocopy(const std::string &name, const std::string &value,
355 bool no_index) {
356 return make_nv_internal(name, value, no_index,
357 NGHTTP2_NV_FLAG_NO_COPY_NAME |
358 NGHTTP2_NV_FLAG_NO_COPY_VALUE);
359 }
360
make_nv_nocopy(const StringRef & name,const StringRef & value,bool no_index)361 nghttp2_nv make_nv_nocopy(const StringRef &name, const StringRef &value,
362 bool no_index) {
363 return make_nv_internal(name, value, no_index,
364 NGHTTP2_NV_FLAG_NO_COPY_NAME |
365 NGHTTP2_NV_FLAG_NO_COPY_VALUE);
366 }
367
368 namespace {
copy_headers_to_nva_internal(std::vector<nghttp2_nv> & nva,const HeaderRefs & headers,uint8_t nv_flags,uint32_t flags)369 void copy_headers_to_nva_internal(std::vector<nghttp2_nv> &nva,
370 const HeaderRefs &headers, uint8_t nv_flags,
371 uint32_t flags) {
372 auto it_forwarded = std::end(headers);
373 auto it_xff = std::end(headers);
374 auto it_xfp = std::end(headers);
375 auto it_via = std::end(headers);
376
377 for (auto it = std::begin(headers); it != std::end(headers); ++it) {
378 auto kv = &(*it);
379 if (kv->name.empty() || kv->name[0] == ':') {
380 continue;
381 }
382 switch (kv->token) {
383 case HD_COOKIE:
384 case HD_CONNECTION:
385 case HD_HOST:
386 case HD_HTTP2_SETTINGS:
387 case HD_KEEP_ALIVE:
388 case HD_PROXY_CONNECTION:
389 case HD_SERVER:
390 case HD_TE:
391 case HD_TRANSFER_ENCODING:
392 case HD_UPGRADE:
393 continue;
394 case HD_EARLY_DATA:
395 if (flags & HDOP_STRIP_EARLY_DATA) {
396 continue;
397 }
398 break;
399 case HD_SEC_WEBSOCKET_ACCEPT:
400 if (flags & HDOP_STRIP_SEC_WEBSOCKET_ACCEPT) {
401 continue;
402 }
403 break;
404 case HD_SEC_WEBSOCKET_KEY:
405 if (flags & HDOP_STRIP_SEC_WEBSOCKET_KEY) {
406 continue;
407 }
408 break;
409 case HD_FORWARDED:
410 if (flags & HDOP_STRIP_FORWARDED) {
411 continue;
412 }
413
414 if (it_forwarded == std::end(headers)) {
415 it_forwarded = it;
416 continue;
417 }
418
419 kv = &(*it_forwarded);
420 it_forwarded = it;
421 break;
422 case HD_X_FORWARDED_FOR:
423 if (flags & HDOP_STRIP_X_FORWARDED_FOR) {
424 continue;
425 }
426
427 if (it_xff == std::end(headers)) {
428 it_xff = it;
429 continue;
430 }
431
432 kv = &(*it_xff);
433 it_xff = it;
434 break;
435 case HD_X_FORWARDED_PROTO:
436 if (flags & HDOP_STRIP_X_FORWARDED_PROTO) {
437 continue;
438 }
439
440 if (it_xfp == std::end(headers)) {
441 it_xfp = it;
442 continue;
443 }
444
445 kv = &(*it_xfp);
446 it_xfp = it;
447 break;
448 case HD_VIA:
449 if (flags & HDOP_STRIP_VIA) {
450 continue;
451 }
452
453 if (it_via == std::end(headers)) {
454 it_via = it;
455 continue;
456 }
457
458 kv = &(*it_via);
459 it_via = it;
460 break;
461 }
462 nva.push_back(
463 make_nv_internal(kv->name, kv->value, kv->no_index, nv_flags));
464 }
465 }
466 } // namespace
467
copy_headers_to_nva(std::vector<nghttp2_nv> & nva,const HeaderRefs & headers,uint32_t flags)468 void copy_headers_to_nva(std::vector<nghttp2_nv> &nva,
469 const HeaderRefs &headers, uint32_t flags) {
470 copy_headers_to_nva_internal(nva, headers, NGHTTP2_NV_FLAG_NONE, flags);
471 }
472
copy_headers_to_nva_nocopy(std::vector<nghttp2_nv> & nva,const HeaderRefs & headers,uint32_t flags)473 void copy_headers_to_nva_nocopy(std::vector<nghttp2_nv> &nva,
474 const HeaderRefs &headers, uint32_t flags) {
475 copy_headers_to_nva_internal(
476 nva, headers,
477 NGHTTP2_NV_FLAG_NO_COPY_NAME | NGHTTP2_NV_FLAG_NO_COPY_VALUE, flags);
478 }
479
build_http1_headers_from_headers(DefaultMemchunks * buf,const HeaderRefs & headers,uint32_t flags)480 void build_http1_headers_from_headers(DefaultMemchunks *buf,
481 const HeaderRefs &headers,
482 uint32_t flags) {
483 auto it_forwarded = std::end(headers);
484 auto it_xff = std::end(headers);
485 auto it_xfp = std::end(headers);
486 auto it_via = std::end(headers);
487
488 for (auto it = std::begin(headers); it != std::end(headers); ++it) {
489 auto kv = &(*it);
490 if (kv->name.empty() || kv->name[0] == ':') {
491 continue;
492 }
493 switch (kv->token) {
494 case HD_CONNECTION:
495 case HD_COOKIE:
496 case HD_HOST:
497 case HD_HTTP2_SETTINGS:
498 case HD_KEEP_ALIVE:
499 case HD_PROXY_CONNECTION:
500 case HD_SERVER:
501 case HD_UPGRADE:
502 continue;
503 case HD_EARLY_DATA:
504 if (flags & HDOP_STRIP_EARLY_DATA) {
505 continue;
506 }
507 break;
508 case HD_TRANSFER_ENCODING:
509 if (flags & HDOP_STRIP_TRANSFER_ENCODING) {
510 continue;
511 }
512 break;
513 case HD_FORWARDED:
514 if (flags & HDOP_STRIP_FORWARDED) {
515 continue;
516 }
517
518 if (it_forwarded == std::end(headers)) {
519 it_forwarded = it;
520 continue;
521 }
522
523 kv = &(*it_forwarded);
524 it_forwarded = it;
525 break;
526 case HD_X_FORWARDED_FOR:
527 if (flags & HDOP_STRIP_X_FORWARDED_FOR) {
528 continue;
529 }
530
531 if (it_xff == std::end(headers)) {
532 it_xff = it;
533 continue;
534 }
535
536 kv = &(*it_xff);
537 it_xff = it;
538 break;
539 case HD_X_FORWARDED_PROTO:
540 if (flags & HDOP_STRIP_X_FORWARDED_PROTO) {
541 continue;
542 }
543
544 if (it_xfp == std::end(headers)) {
545 it_xfp = it;
546 continue;
547 }
548
549 kv = &(*it_xfp);
550 it_xfp = it;
551 break;
552 case HD_VIA:
553 if (flags & HDOP_STRIP_VIA) {
554 continue;
555 }
556
557 if (it_via == std::end(headers)) {
558 it_via = it;
559 continue;
560 }
561
562 kv = &(*it_via);
563 it_via = it;
564 break;
565 }
566 capitalize(buf, kv->name);
567 buf->append(": ");
568 buf->append(kv->value);
569 buf->append("\r\n");
570 }
571 }
572
determine_window_update_transmission(nghttp2_session * session,int32_t stream_id)573 int32_t determine_window_update_transmission(nghttp2_session *session,
574 int32_t stream_id) {
575 int32_t recv_length, window_size;
576 if (stream_id == 0) {
577 recv_length = nghttp2_session_get_effective_recv_data_length(session);
578 window_size = nghttp2_session_get_effective_local_window_size(session);
579 } else {
580 recv_length = nghttp2_session_get_stream_effective_recv_data_length(
581 session, stream_id);
582 window_size = nghttp2_session_get_stream_effective_local_window_size(
583 session, stream_id);
584 }
585 if (recv_length != -1 && window_size != -1) {
586 if (recv_length >= window_size / 2) {
587 return recv_length;
588 }
589 }
590 return -1;
591 }
592
dump_nv(FILE * out,const char ** nv)593 void dump_nv(FILE *out, const char **nv) {
594 for (size_t i = 0; nv[i]; i += 2) {
595 fprintf(out, "%s: %s\n", nv[i], nv[i + 1]);
596 }
597 fputc('\n', out);
598 fflush(out);
599 }
600
dump_nv(FILE * out,const nghttp2_nv * nva,size_t nvlen)601 void dump_nv(FILE *out, const nghttp2_nv *nva, size_t nvlen) {
602 auto end = nva + nvlen;
603 for (; nva != end; ++nva) {
604 fprintf(out, "%s: %s\n", nva->name, nva->value);
605 }
606 fputc('\n', out);
607 fflush(out);
608 }
609
dump_nv(FILE * out,const Headers & nva)610 void dump_nv(FILE *out, const Headers &nva) {
611 for (auto &nv : nva) {
612 fprintf(out, "%s: %s\n", nv.name.c_str(), nv.value.c_str());
613 }
614 fputc('\n', out);
615 fflush(out);
616 }
617
dump_nv(FILE * out,const HeaderRefs & nva)618 void dump_nv(FILE *out, const HeaderRefs &nva) {
619 for (auto &nv : nva) {
620 fprintf(out, "%s: %s\n", nv.name.c_str(), nv.value.c_str());
621 }
622 fputc('\n', out);
623 fflush(out);
624 }
625
erase_header(HeaderRef * hd)626 void erase_header(HeaderRef *hd) {
627 hd->name = StringRef{};
628 hd->token = -1;
629 }
630
rewrite_location_uri(BlockAllocator & balloc,const StringRef & uri,const http_parser_url & u,const StringRef & match_host,const StringRef & request_authority,const StringRef & upstream_scheme)631 StringRef rewrite_location_uri(BlockAllocator &balloc, const StringRef &uri,
632 const http_parser_url &u,
633 const StringRef &match_host,
634 const StringRef &request_authority,
635 const StringRef &upstream_scheme) {
636 // We just rewrite scheme and authority.
637 if ((u.field_set & (1 << UF_HOST)) == 0) {
638 return StringRef{};
639 }
640 auto field = &u.field_data[UF_HOST];
641 if (!util::starts_with(std::begin(match_host), std::end(match_host),
642 &uri[field->off], &uri[field->off] + field->len) ||
643 (match_host.size() != field->len && match_host[field->len] != ':')) {
644 return StringRef{};
645 }
646
647 auto len = 0;
648 if (!request_authority.empty()) {
649 len += upstream_scheme.size() + str_size("://") + request_authority.size();
650 }
651
652 if (u.field_set & (1 << UF_PATH)) {
653 field = &u.field_data[UF_PATH];
654 len += field->len;
655 }
656
657 if (u.field_set & (1 << UF_QUERY)) {
658 field = &u.field_data[UF_QUERY];
659 len += 1 + field->len;
660 }
661
662 if (u.field_set & (1 << UF_FRAGMENT)) {
663 field = &u.field_data[UF_FRAGMENT];
664 len += 1 + field->len;
665 }
666
667 auto iov = make_byte_ref(balloc, len + 1);
668 auto p = iov.base;
669
670 if (!request_authority.empty()) {
671 p = std::copy(std::begin(upstream_scheme), std::end(upstream_scheme), p);
672 p = util::copy_lit(p, "://");
673 p = std::copy(std::begin(request_authority), std::end(request_authority),
674 p);
675 }
676 if (u.field_set & (1 << UF_PATH)) {
677 field = &u.field_data[UF_PATH];
678 p = std::copy_n(&uri[field->off], field->len, p);
679 }
680 if (u.field_set & (1 << UF_QUERY)) {
681 field = &u.field_data[UF_QUERY];
682 *p++ = '?';
683 p = std::copy_n(&uri[field->off], field->len, p);
684 }
685 if (u.field_set & (1 << UF_FRAGMENT)) {
686 field = &u.field_data[UF_FRAGMENT];
687 *p++ = '#';
688 p = std::copy_n(&uri[field->off], field->len, p);
689 }
690
691 *p = '\0';
692
693 return StringRef{iov.base, p};
694 }
695
parse_http_status_code(const StringRef & src)696 int parse_http_status_code(const StringRef &src) {
697 if (src.size() != 3) {
698 return -1;
699 }
700
701 int status = 0;
702 for (auto c : src) {
703 if (!isdigit(c)) {
704 return -1;
705 }
706 status *= 10;
707 status += c - '0';
708 }
709
710 if (status < 100) {
711 return -1;
712 }
713
714 return status;
715 }
716
lookup_token(const StringRef & name)717 int lookup_token(const StringRef &name) {
718 return lookup_token(name.byte(), name.size());
719 }
720
721 // This function was generated by genheaderfunc.py. Inspired by h2o
722 // header lookup. https://github.com/h2o/h2o
lookup_token(const uint8_t * name,size_t namelen)723 int lookup_token(const uint8_t *name, size_t namelen) {
724 switch (namelen) {
725 case 2:
726 switch (name[1]) {
727 case 'e':
728 if (util::streq_l("t", name, 1)) {
729 return HD_TE;
730 }
731 break;
732 }
733 break;
734 case 3:
735 switch (name[2]) {
736 case 'a':
737 if (util::streq_l("vi", name, 2)) {
738 return HD_VIA;
739 }
740 break;
741 }
742 break;
743 case 4:
744 switch (name[3]) {
745 case 'e':
746 if (util::streq_l("dat", name, 3)) {
747 return HD_DATE;
748 }
749 break;
750 case 'k':
751 if (util::streq_l("lin", name, 3)) {
752 return HD_LINK;
753 }
754 break;
755 case 't':
756 if (util::streq_l("hos", name, 3)) {
757 return HD_HOST;
758 }
759 break;
760 }
761 break;
762 case 5:
763 switch (name[4]) {
764 case 'h':
765 if (util::streq_l(":pat", name, 4)) {
766 return HD__PATH;
767 }
768 break;
769 case 't':
770 if (util::streq_l(":hos", name, 4)) {
771 return HD__HOST;
772 }
773 break;
774 }
775 break;
776 case 6:
777 switch (name[5]) {
778 case 'e':
779 if (util::streq_l("cooki", name, 5)) {
780 return HD_COOKIE;
781 }
782 break;
783 case 'r':
784 if (util::streq_l("serve", name, 5)) {
785 return HD_SERVER;
786 }
787 break;
788 case 't':
789 if (util::streq_l("expec", name, 5)) {
790 return HD_EXPECT;
791 }
792 break;
793 }
794 break;
795 case 7:
796 switch (name[6]) {
797 case 'c':
798 if (util::streq_l("alt-sv", name, 6)) {
799 return HD_ALT_SVC;
800 }
801 break;
802 case 'd':
803 if (util::streq_l(":metho", name, 6)) {
804 return HD__METHOD;
805 }
806 break;
807 case 'e':
808 if (util::streq_l(":schem", name, 6)) {
809 return HD__SCHEME;
810 }
811 if (util::streq_l("upgrad", name, 6)) {
812 return HD_UPGRADE;
813 }
814 break;
815 case 'r':
816 if (util::streq_l("traile", name, 6)) {
817 return HD_TRAILER;
818 }
819 break;
820 case 's':
821 if (util::streq_l(":statu", name, 6)) {
822 return HD__STATUS;
823 }
824 break;
825 }
826 break;
827 case 8:
828 switch (name[7]) {
829 case 'n':
830 if (util::streq_l("locatio", name, 7)) {
831 return HD_LOCATION;
832 }
833 break;
834 }
835 break;
836 case 9:
837 switch (name[8]) {
838 case 'd':
839 if (util::streq_l("forwarde", name, 8)) {
840 return HD_FORWARDED;
841 }
842 break;
843 case 'l':
844 if (util::streq_l(":protoco", name, 8)) {
845 return HD__PROTOCOL;
846 }
847 break;
848 }
849 break;
850 case 10:
851 switch (name[9]) {
852 case 'a':
853 if (util::streq_l("early-dat", name, 9)) {
854 return HD_EARLY_DATA;
855 }
856 break;
857 case 'e':
858 if (util::streq_l("keep-aliv", name, 9)) {
859 return HD_KEEP_ALIVE;
860 }
861 break;
862 case 'n':
863 if (util::streq_l("connectio", name, 9)) {
864 return HD_CONNECTION;
865 }
866 break;
867 case 't':
868 if (util::streq_l("user-agen", name, 9)) {
869 return HD_USER_AGENT;
870 }
871 break;
872 case 'y':
873 if (util::streq_l(":authorit", name, 9)) {
874 return HD__AUTHORITY;
875 }
876 break;
877 }
878 break;
879 case 12:
880 switch (name[11]) {
881 case 'e':
882 if (util::streq_l("content-typ", name, 11)) {
883 return HD_CONTENT_TYPE;
884 }
885 break;
886 }
887 break;
888 case 13:
889 switch (name[12]) {
890 case 'l':
891 if (util::streq_l("cache-contro", name, 12)) {
892 return HD_CACHE_CONTROL;
893 }
894 break;
895 }
896 break;
897 case 14:
898 switch (name[13]) {
899 case 'h':
900 if (util::streq_l("content-lengt", name, 13)) {
901 return HD_CONTENT_LENGTH;
902 }
903 break;
904 case 's':
905 if (util::streq_l("http2-setting", name, 13)) {
906 return HD_HTTP2_SETTINGS;
907 }
908 break;
909 }
910 break;
911 case 15:
912 switch (name[14]) {
913 case 'e':
914 if (util::streq_l("accept-languag", name, 14)) {
915 return HD_ACCEPT_LANGUAGE;
916 }
917 break;
918 case 'g':
919 if (util::streq_l("accept-encodin", name, 14)) {
920 return HD_ACCEPT_ENCODING;
921 }
922 break;
923 case 'r':
924 if (util::streq_l("x-forwarded-fo", name, 14)) {
925 return HD_X_FORWARDED_FOR;
926 }
927 break;
928 }
929 break;
930 case 16:
931 switch (name[15]) {
932 case 'n':
933 if (util::streq_l("proxy-connectio", name, 15)) {
934 return HD_PROXY_CONNECTION;
935 }
936 break;
937 }
938 break;
939 case 17:
940 switch (name[16]) {
941 case 'e':
942 if (util::streq_l("if-modified-sinc", name, 16)) {
943 return HD_IF_MODIFIED_SINCE;
944 }
945 break;
946 case 'g':
947 if (util::streq_l("transfer-encodin", name, 16)) {
948 return HD_TRANSFER_ENCODING;
949 }
950 break;
951 case 'o':
952 if (util::streq_l("x-forwarded-prot", name, 16)) {
953 return HD_X_FORWARDED_PROTO;
954 }
955 break;
956 case 'y':
957 if (util::streq_l("sec-websocket-ke", name, 16)) {
958 return HD_SEC_WEBSOCKET_KEY;
959 }
960 break;
961 }
962 break;
963 case 20:
964 switch (name[19]) {
965 case 't':
966 if (util::streq_l("sec-websocket-accep", name, 19)) {
967 return HD_SEC_WEBSOCKET_ACCEPT;
968 }
969 break;
970 }
971 break;
972 }
973 return -1;
974 }
975
init_hdidx(HeaderIndex & hdidx)976 void init_hdidx(HeaderIndex &hdidx) {
977 std::fill(std::begin(hdidx), std::end(hdidx), -1);
978 }
979
index_header(HeaderIndex & hdidx,int32_t token,size_t idx)980 void index_header(HeaderIndex &hdidx, int32_t token, size_t idx) {
981 if (token == -1) {
982 return;
983 }
984 assert(token < HD_MAXIDX);
985 hdidx[token] = idx;
986 }
987
get_header(const HeaderIndex & hdidx,int32_t token,const Headers & nva)988 const Headers::value_type *get_header(const HeaderIndex &hdidx, int32_t token,
989 const Headers &nva) {
990 auto i = hdidx[token];
991 if (i == -1) {
992 return nullptr;
993 }
994 return &nva[i];
995 }
996
get_header(const HeaderIndex & hdidx,int32_t token,Headers & nva)997 Headers::value_type *get_header(const HeaderIndex &hdidx, int32_t token,
998 Headers &nva) {
999 auto i = hdidx[token];
1000 if (i == -1) {
1001 return nullptr;
1002 }
1003 return &nva[i];
1004 }
1005
1006 namespace {
skip_lws(InputIt first,InputIt last)1007 template <typename InputIt> InputIt skip_lws(InputIt first, InputIt last) {
1008 for (; first != last; ++first) {
1009 switch (*first) {
1010 case ' ':
1011 case '\t':
1012 continue;
1013 default:
1014 return first;
1015 }
1016 }
1017 return first;
1018 }
1019 } // namespace
1020
1021 namespace {
1022 template <typename InputIt>
skip_to_next_field(InputIt first,InputIt last)1023 InputIt skip_to_next_field(InputIt first, InputIt last) {
1024 for (; first != last; ++first) {
1025 switch (*first) {
1026 case ' ':
1027 case '\t':
1028 case ',':
1029 continue;
1030 default:
1031 return first;
1032 }
1033 }
1034 return first;
1035 }
1036 } // namespace
1037
1038 namespace {
1039 // Skip to the right dquote ('"'), handling backslash escapes.
1040 // Returns |last| if input is not terminated with '"'.
1041 template <typename InputIt>
skip_to_right_dquote(InputIt first,InputIt last)1042 InputIt skip_to_right_dquote(InputIt first, InputIt last) {
1043 for (; first != last;) {
1044 switch (*first) {
1045 case '"':
1046 return first;
1047 case '\\':
1048 ++first;
1049 if (first == last) {
1050 return first;
1051 }
1052 break;
1053 }
1054 ++first;
1055 }
1056 return first;
1057 }
1058 } // namespace
1059
1060 namespace {
1061 // Returns true if link-param does not match pattern |pat| of length
1062 // |patlen| or it has empty value (""). |pat| should be parmname
1063 // followed by "=".
check_link_param_empty(const char * first,const char * last,const char * pat,size_t patlen)1064 bool check_link_param_empty(const char *first, const char *last,
1065 const char *pat, size_t patlen) {
1066 if (first + patlen <= last) {
1067 if (std::equal(pat, pat + patlen, first, util::CaseCmp())) {
1068 // we only accept URI if pat is followd by "" (e.g.,
1069 // loadpolicy="") here.
1070 if (first + patlen + 2 <= last) {
1071 if (*(first + patlen) != '"' || *(first + patlen + 1) != '"') {
1072 return false;
1073 }
1074 } else {
1075 // here we got invalid production (anchor=") or anchor=?
1076 return false;
1077 }
1078 }
1079 }
1080 return true;
1081 }
1082 } // namespace
1083
1084 namespace {
1085 // Returns true if link-param consists of only parmname, and it
1086 // matches string [pat, pat + patlen).
check_link_param_without_value(const char * first,const char * last,const char * pat,size_t patlen)1087 bool check_link_param_without_value(const char *first, const char *last,
1088 const char *pat, size_t patlen) {
1089 if (first + patlen > last) {
1090 return false;
1091 }
1092
1093 if (first + patlen == last) {
1094 return std::equal(pat, pat + patlen, first, util::CaseCmp());
1095 }
1096
1097 switch (*(first + patlen)) {
1098 case ';':
1099 case ',':
1100 return std::equal(pat, pat + patlen, first, util::CaseCmp());
1101 }
1102
1103 return false;
1104 }
1105 } // namespace
1106
1107 namespace {
1108 std::pair<LinkHeader, const char *>
parse_next_link_header_once(const char * first,const char * last)1109 parse_next_link_header_once(const char *first, const char *last) {
1110 first = skip_to_next_field(first, last);
1111 if (first == last || *first != '<') {
1112 return {{StringRef{}}, last};
1113 }
1114 auto url_first = ++first;
1115 first = std::find(first, last, '>');
1116 if (first == last) {
1117 return {{StringRef{}}, first};
1118 }
1119 auto url_last = first++;
1120 if (first == last) {
1121 return {{StringRef{}}, first};
1122 }
1123 // we expect ';' or ',' here
1124 switch (*first) {
1125 case ',':
1126 return {{StringRef{}}, ++first};
1127 case ';':
1128 ++first;
1129 break;
1130 default:
1131 return {{StringRef{}}, last};
1132 }
1133
1134 auto ok = false;
1135 auto ign = false;
1136 for (;;) {
1137 first = skip_lws(first, last);
1138 if (first == last) {
1139 return {{StringRef{}}, first};
1140 }
1141 // we expect link-param
1142
1143 if (!ign) {
1144 if (!ok) {
1145 // rel can take several relations using quoted form.
1146 static constexpr char PLP[] = "rel=\"";
1147 static constexpr size_t PLPLEN = str_size(PLP);
1148
1149 static constexpr char PLT[] = "preload";
1150 static constexpr size_t PLTLEN = str_size(PLT);
1151 if (first + PLPLEN < last && *(first + PLPLEN - 1) == '"' &&
1152 std::equal(PLP, PLP + PLPLEN, first, util::CaseCmp())) {
1153 // we have to search preload in whitespace separated list:
1154 // rel="preload something http://example.org/foo"
1155 first += PLPLEN;
1156 auto start = first;
1157 for (; first != last;) {
1158 if (*first != ' ' && *first != '"') {
1159 ++first;
1160 continue;
1161 }
1162
1163 if (start == first) {
1164 return {{StringRef{}}, last};
1165 }
1166
1167 if (!ok && start + PLTLEN == first &&
1168 std::equal(PLT, PLT + PLTLEN, start, util::CaseCmp())) {
1169 ok = true;
1170 }
1171
1172 if (*first == '"') {
1173 break;
1174 }
1175 first = skip_lws(first, last);
1176 start = first;
1177 }
1178 if (first == last) {
1179 return {{StringRef{}}, last};
1180 }
1181 assert(*first == '"');
1182 ++first;
1183 if (first == last || *first == ',') {
1184 goto almost_done;
1185 }
1186 if (*first == ';') {
1187 ++first;
1188 // parse next link-param
1189 continue;
1190 }
1191 return {{StringRef{}}, last};
1192 }
1193 }
1194 // we are only interested in rel=preload parameter. Others are
1195 // simply skipped.
1196 static constexpr char PL[] = "rel=preload";
1197 static constexpr size_t PLLEN = str_size(PL);
1198 if (first + PLLEN == last) {
1199 if (std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
1200 // ok = true;
1201 // this is the end of sequence
1202 return {{{url_first, url_last}}, last};
1203 }
1204 } else if (first + PLLEN + 1 <= last) {
1205 switch (*(first + PLLEN)) {
1206 case ',':
1207 if (!std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
1208 break;
1209 }
1210 // ok = true;
1211 // skip including ','
1212 first += PLLEN + 1;
1213 return {{{url_first, url_last}}, first};
1214 case ';':
1215 if (!std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
1216 break;
1217 }
1218 ok = true;
1219 // skip including ';'
1220 first += PLLEN + 1;
1221 // continue parse next link-param
1222 continue;
1223 }
1224 }
1225 // we have to reject URI if we have nonempty anchor parameter.
1226 static constexpr char ANCHOR[] = "anchor=";
1227 static constexpr size_t ANCHORLEN = str_size(ANCHOR);
1228 if (!ign && !check_link_param_empty(first, last, ANCHOR, ANCHORLEN)) {
1229 ign = true;
1230 }
1231
1232 // reject URI if we have non-empty loadpolicy. This could be
1233 // tightened up to just pick up "next" or "insert".
1234 static constexpr char LOADPOLICY[] = "loadpolicy=";
1235 static constexpr size_t LOADPOLICYLEN = str_size(LOADPOLICY);
1236 if (!ign &&
1237 !check_link_param_empty(first, last, LOADPOLICY, LOADPOLICYLEN)) {
1238 ign = true;
1239 }
1240
1241 // reject URI if we have nopush attribute.
1242 static constexpr char NOPUSH[] = "nopush";
1243 static constexpr size_t NOPUSHLEN = str_size(NOPUSH);
1244 if (!ign &&
1245 check_link_param_without_value(first, last, NOPUSH, NOPUSHLEN)) {
1246 ign = true;
1247 }
1248 }
1249
1250 auto param_first = first;
1251 for (; first != last;) {
1252 if (util::in_attr_char(*first)) {
1253 ++first;
1254 continue;
1255 }
1256 // '*' is only allowed at the end of parameter name and must be
1257 // followed by '='
1258 if (last - first >= 2 && first != param_first) {
1259 if (*first == '*' && *(first + 1) == '=') {
1260 ++first;
1261 break;
1262 }
1263 }
1264 if (*first == '=' || *first == ';' || *first == ',') {
1265 break;
1266 }
1267 return {{StringRef{}}, last};
1268 }
1269 if (param_first == first) {
1270 // empty parmname
1271 return {{StringRef{}}, last};
1272 }
1273 // link-param without value is acceptable (see link-extension) if
1274 // it is not followed by '='
1275 if (first == last || *first == ',') {
1276 goto almost_done;
1277 }
1278 if (*first == ';') {
1279 ++first;
1280 // parse next link-param
1281 continue;
1282 }
1283 // now parsing link-param value
1284 assert(*first == '=');
1285 ++first;
1286 if (first == last) {
1287 // empty value is not acceptable
1288 return {{StringRef{}}, first};
1289 }
1290 if (*first == '"') {
1291 // quoted-string
1292 first = skip_to_right_dquote(first + 1, last);
1293 if (first == last) {
1294 return {{StringRef{}}, first};
1295 }
1296 ++first;
1297 if (first == last || *first == ',') {
1298 goto almost_done;
1299 }
1300 if (*first == ';') {
1301 ++first;
1302 // parse next link-param
1303 continue;
1304 }
1305 return {{StringRef{}}, last};
1306 }
1307 // not quoted-string, skip to next ',' or ';'
1308 if (*first == ',' || *first == ';') {
1309 // empty value
1310 return {{StringRef{}}, last};
1311 }
1312 for (; first != last; ++first) {
1313 if (*first == ',' || *first == ';') {
1314 break;
1315 }
1316 }
1317 if (first == last || *first == ',') {
1318 goto almost_done;
1319 }
1320 assert(*first == ';');
1321 ++first;
1322 // parse next link-param
1323 }
1324
1325 almost_done:
1326 assert(first == last || *first == ',');
1327
1328 if (first != last) {
1329 ++first;
1330 }
1331 if (ok && !ign) {
1332 return {{{url_first, url_last}}, first};
1333 }
1334 return {{StringRef{}}, first};
1335 }
1336 } // namespace
1337
parse_link_header(const StringRef & src)1338 std::vector<LinkHeader> parse_link_header(const StringRef &src) {
1339 std::vector<LinkHeader> res;
1340 for (auto first = std::begin(src); first != std::end(src);) {
1341 auto rv = parse_next_link_header_once(first, std::end(src));
1342 first = rv.second;
1343 auto &link = rv.first;
1344 if (!link.uri.empty()) {
1345 res.push_back(link);
1346 }
1347 }
1348 return res;
1349 }
1350
path_join(const StringRef & base_path,const StringRef & base_query,const StringRef & rel_path,const StringRef & rel_query)1351 std::string path_join(const StringRef &base_path, const StringRef &base_query,
1352 const StringRef &rel_path, const StringRef &rel_query) {
1353 BlockAllocator balloc(1024, 1024);
1354
1355 return path_join(balloc, base_path, base_query, rel_path, rel_query).str();
1356 }
1357
expect_response_body(int status_code)1358 bool expect_response_body(int status_code) {
1359 return status_code == 101 ||
1360 (status_code / 100 != 1 && status_code != 304 && status_code != 204);
1361 }
1362
expect_response_body(const std::string & method,int status_code)1363 bool expect_response_body(const std::string &method, int status_code) {
1364 return method != "HEAD" && expect_response_body(status_code);
1365 }
1366
expect_response_body(int method_token,int status_code)1367 bool expect_response_body(int method_token, int status_code) {
1368 return method_token != HTTP_HEAD && expect_response_body(status_code);
1369 }
1370
lookup_method_token(const StringRef & name)1371 int lookup_method_token(const StringRef &name) {
1372 return lookup_method_token(name.byte(), name.size());
1373 }
1374
1375 // This function was generated by genmethodfunc.py.
lookup_method_token(const uint8_t * name,size_t namelen)1376 int lookup_method_token(const uint8_t *name, size_t namelen) {
1377 switch (namelen) {
1378 case 3:
1379 switch (name[2]) {
1380 case 'L':
1381 if (util::streq_l("AC", name, 2)) {
1382 return HTTP_ACL;
1383 }
1384 break;
1385 case 'T':
1386 if (util::streq_l("GE", name, 2)) {
1387 return HTTP_GET;
1388 }
1389 if (util::streq_l("PU", name, 2)) {
1390 return HTTP_PUT;
1391 }
1392 break;
1393 }
1394 break;
1395 case 4:
1396 switch (name[3]) {
1397 case 'D':
1398 if (util::streq_l("BIN", name, 3)) {
1399 return HTTP_BIND;
1400 }
1401 if (util::streq_l("HEA", name, 3)) {
1402 return HTTP_HEAD;
1403 }
1404 break;
1405 case 'E':
1406 if (util::streq_l("MOV", name, 3)) {
1407 return HTTP_MOVE;
1408 }
1409 break;
1410 case 'K':
1411 if (util::streq_l("LIN", name, 3)) {
1412 return HTTP_LINK;
1413 }
1414 if (util::streq_l("LOC", name, 3)) {
1415 return HTTP_LOCK;
1416 }
1417 break;
1418 case 'T':
1419 if (util::streq_l("POS", name, 3)) {
1420 return HTTP_POST;
1421 }
1422 break;
1423 case 'Y':
1424 if (util::streq_l("COP", name, 3)) {
1425 return HTTP_COPY;
1426 }
1427 break;
1428 }
1429 break;
1430 case 5:
1431 switch (name[4]) {
1432 case 'E':
1433 if (util::streq_l("MERG", name, 4)) {
1434 return HTTP_MERGE;
1435 }
1436 if (util::streq_l("PURG", name, 4)) {
1437 return HTTP_PURGE;
1438 }
1439 if (util::streq_l("TRAC", name, 4)) {
1440 return HTTP_TRACE;
1441 }
1442 break;
1443 case 'H':
1444 if (util::streq_l("PATC", name, 4)) {
1445 return HTTP_PATCH;
1446 }
1447 break;
1448 case 'L':
1449 if (util::streq_l("MKCO", name, 4)) {
1450 return HTTP_MKCOL;
1451 }
1452 break;
1453 }
1454 break;
1455 case 6:
1456 switch (name[5]) {
1457 case 'D':
1458 if (util::streq_l("REBIN", name, 5)) {
1459 return HTTP_REBIND;
1460 }
1461 if (util::streq_l("UNBIN", name, 5)) {
1462 return HTTP_UNBIND;
1463 }
1464 break;
1465 case 'E':
1466 if (util::streq_l("DELET", name, 5)) {
1467 return HTTP_DELETE;
1468 }
1469 if (util::streq_l("SOURC", name, 5)) {
1470 return HTTP_SOURCE;
1471 }
1472 break;
1473 case 'H':
1474 if (util::streq_l("SEARC", name, 5)) {
1475 return HTTP_SEARCH;
1476 }
1477 break;
1478 case 'K':
1479 if (util::streq_l("UNLIN", name, 5)) {
1480 return HTTP_UNLINK;
1481 }
1482 if (util::streq_l("UNLOC", name, 5)) {
1483 return HTTP_UNLOCK;
1484 }
1485 break;
1486 case 'T':
1487 if (util::streq_l("REPOR", name, 5)) {
1488 return HTTP_REPORT;
1489 }
1490 break;
1491 case 'Y':
1492 if (util::streq_l("NOTIF", name, 5)) {
1493 return HTTP_NOTIFY;
1494 }
1495 break;
1496 }
1497 break;
1498 case 7:
1499 switch (name[6]) {
1500 case 'H':
1501 if (util::streq_l("MSEARC", name, 6)) {
1502 return HTTP_MSEARCH;
1503 }
1504 break;
1505 case 'S':
1506 if (util::streq_l("OPTION", name, 6)) {
1507 return HTTP_OPTIONS;
1508 }
1509 break;
1510 case 'T':
1511 if (util::streq_l("CONNEC", name, 6)) {
1512 return HTTP_CONNECT;
1513 }
1514 break;
1515 }
1516 break;
1517 case 8:
1518 switch (name[7]) {
1519 case 'D':
1520 if (util::streq_l("PROPFIN", name, 7)) {
1521 return HTTP_PROPFIND;
1522 }
1523 break;
1524 case 'T':
1525 if (util::streq_l("CHECKOU", name, 7)) {
1526 return HTTP_CHECKOUT;
1527 }
1528 break;
1529 }
1530 break;
1531 case 9:
1532 switch (name[8]) {
1533 case 'E':
1534 if (util::streq_l("SUBSCRIB", name, 8)) {
1535 return HTTP_SUBSCRIBE;
1536 }
1537 break;
1538 case 'H':
1539 if (util::streq_l("PROPPATC", name, 8)) {
1540 return HTTP_PROPPATCH;
1541 }
1542 break;
1543 }
1544 break;
1545 case 10:
1546 switch (name[9]) {
1547 case 'R':
1548 if (util::streq_l("MKCALENDA", name, 9)) {
1549 return HTTP_MKCALENDAR;
1550 }
1551 break;
1552 case 'Y':
1553 if (util::streq_l("MKACTIVIT", name, 9)) {
1554 return HTTP_MKACTIVITY;
1555 }
1556 break;
1557 }
1558 break;
1559 case 11:
1560 switch (name[10]) {
1561 case 'E':
1562 if (util::streq_l("UNSUBSCRIB", name, 10)) {
1563 return HTTP_UNSUBSCRIBE;
1564 }
1565 break;
1566 }
1567 break;
1568 }
1569 return -1;
1570 }
1571
to_method_string(int method_token)1572 StringRef to_method_string(int method_token) {
1573 // we happened to use same value for method with llhttp.
1574 return StringRef{
1575 llhttp_method_name(static_cast<llhttp_method>(method_token))};
1576 }
1577
get_pure_path_component(const StringRef & uri)1578 StringRef get_pure_path_component(const StringRef &uri) {
1579 int rv;
1580
1581 http_parser_url u{};
1582 rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u);
1583 if (rv != 0) {
1584 return StringRef{};
1585 }
1586
1587 if (u.field_set & (1 << UF_PATH)) {
1588 auto &f = u.field_data[UF_PATH];
1589 return StringRef{uri.c_str() + f.off, f.len};
1590 }
1591
1592 return StringRef::from_lit("/");
1593 }
1594
construct_push_component(BlockAllocator & balloc,StringRef & scheme,StringRef & authority,StringRef & path,const StringRef & base,const StringRef & uri)1595 int construct_push_component(BlockAllocator &balloc, StringRef &scheme,
1596 StringRef &authority, StringRef &path,
1597 const StringRef &base, const StringRef &uri) {
1598 int rv;
1599 StringRef rel, relq;
1600
1601 if (uri.size() == 0) {
1602 return -1;
1603 }
1604
1605 http_parser_url u{};
1606
1607 rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u);
1608
1609 if (rv != 0) {
1610 if (uri[0] == '/') {
1611 return -1;
1612 }
1613
1614 // treat link_url as relative URI.
1615 auto end = std::find(std::begin(uri), std::end(uri), '#');
1616 auto q = std::find(std::begin(uri), end, '?');
1617
1618 rel = StringRef{std::begin(uri), q};
1619 if (q != end) {
1620 relq = StringRef{q + 1, std::end(uri)};
1621 }
1622 } else {
1623 if (u.field_set & (1 << UF_SCHEMA)) {
1624 scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA);
1625 }
1626
1627 if (u.field_set & (1 << UF_HOST)) {
1628 auto auth = util::get_uri_field(uri.c_str(), u, UF_HOST);
1629 auto len = auth.size();
1630 auto port_exists = u.field_set & (1 << UF_PORT);
1631 if (port_exists) {
1632 len += 1 + str_size("65535");
1633 }
1634 auto iov = make_byte_ref(balloc, len + 1);
1635 auto p = iov.base;
1636 p = std::copy(std::begin(auth), std::end(auth), p);
1637 if (port_exists) {
1638 *p++ = ':';
1639 p = util::utos(p, u.port);
1640 }
1641 *p = '\0';
1642
1643 authority = StringRef{iov.base, p};
1644 }
1645
1646 if (u.field_set & (1 << UF_PATH)) {
1647 auto &f = u.field_data[UF_PATH];
1648 rel = StringRef{uri.c_str() + f.off, f.len};
1649 } else {
1650 rel = StringRef::from_lit("/");
1651 }
1652
1653 if (u.field_set & (1 << UF_QUERY)) {
1654 auto &f = u.field_data[UF_QUERY];
1655 relq = StringRef{uri.c_str() + f.off, f.len};
1656 }
1657 }
1658
1659 path = http2::path_join(balloc, base, StringRef{}, rel, relq);
1660
1661 return 0;
1662 }
1663
1664 namespace {
eat_file(InputIt first,InputIt last)1665 template <typename InputIt> InputIt eat_file(InputIt first, InputIt last) {
1666 if (first == last) {
1667 *first++ = '/';
1668 return first;
1669 }
1670
1671 if (*(last - 1) == '/') {
1672 return last;
1673 }
1674
1675 auto p = last;
1676 for (; p != first && *(p - 1) != '/'; --p)
1677 ;
1678 if (p == first) {
1679 // this should not happened in normal case, where we expect path
1680 // starts with '/'
1681 *first++ = '/';
1682 return first;
1683 }
1684
1685 return p;
1686 }
1687 } // namespace
1688
1689 namespace {
eat_dir(InputIt first,InputIt last)1690 template <typename InputIt> InputIt eat_dir(InputIt first, InputIt last) {
1691 auto p = eat_file(first, last);
1692
1693 --p;
1694
1695 assert(*p == '/');
1696
1697 return eat_file(first, p);
1698 }
1699 } // namespace
1700
path_join(BlockAllocator & balloc,const StringRef & base_path,const StringRef & base_query,const StringRef & rel_path,const StringRef & rel_query)1701 StringRef path_join(BlockAllocator &balloc, const StringRef &base_path,
1702 const StringRef &base_query, const StringRef &rel_path,
1703 const StringRef &rel_query) {
1704 auto res = make_byte_ref(
1705 balloc, std::max(static_cast<size_t>(1), base_path.size()) +
1706 rel_path.size() + 1 +
1707 std::max(base_query.size(), rel_query.size()) + 1);
1708 auto p = res.base;
1709
1710 if (rel_path.empty()) {
1711 if (base_path.empty()) {
1712 *p++ = '/';
1713 } else {
1714 p = std::copy(std::begin(base_path), std::end(base_path), p);
1715 }
1716 if (rel_query.empty()) {
1717 if (!base_query.empty()) {
1718 *p++ = '?';
1719 p = std::copy(std::begin(base_query), std::end(base_query), p);
1720 }
1721 *p = '\0';
1722 return StringRef{res.base, p};
1723 }
1724 *p++ = '?';
1725 p = std::copy(std::begin(rel_query), std::end(rel_query), p);
1726 *p = '\0';
1727 return StringRef{res.base, p};
1728 }
1729
1730 auto first = std::begin(rel_path);
1731 auto last = std::end(rel_path);
1732
1733 if (rel_path[0] == '/') {
1734 *p++ = '/';
1735 ++first;
1736 for (; first != last && *first == '/'; ++first)
1737 ;
1738 } else if (base_path.empty()) {
1739 *p++ = '/';
1740 } else {
1741 p = std::copy(std::begin(base_path), std::end(base_path), p);
1742 }
1743
1744 for (; first != last;) {
1745 if (*first == '.') {
1746 if (first + 1 == last) {
1747 if (*(p - 1) != '/') {
1748 p = eat_file(res.base, p);
1749 }
1750 break;
1751 }
1752 if (*(first + 1) == '/') {
1753 if (*(p - 1) != '/') {
1754 p = eat_file(res.base, p);
1755 }
1756 first += 2;
1757 continue;
1758 }
1759 if (*(first + 1) == '.') {
1760 if (first + 2 == last) {
1761 p = eat_dir(res.base, p);
1762 break;
1763 }
1764 if (*(first + 2) == '/') {
1765 p = eat_dir(res.base, p);
1766 first += 3;
1767 continue;
1768 }
1769 }
1770 }
1771 if (*(p - 1) != '/') {
1772 p = eat_file(res.base, p);
1773 }
1774 auto slash = std::find(first, last, '/');
1775 if (slash == last) {
1776 p = std::copy(first, last, p);
1777 break;
1778 }
1779 p = std::copy(first, slash + 1, p);
1780 first = slash + 1;
1781 for (; first != last && *first == '/'; ++first)
1782 ;
1783 }
1784 if (!rel_query.empty()) {
1785 *p++ = '?';
1786 p = std::copy(std::begin(rel_query), std::end(rel_query), p);
1787 }
1788 *p = '\0';
1789 return StringRef{res.base, p};
1790 }
1791
normalize_path(BlockAllocator & balloc,const StringRef & path,const StringRef & query)1792 StringRef normalize_path(BlockAllocator &balloc, const StringRef &path,
1793 const StringRef &query) {
1794 // First, decode %XX for unreserved characters, then do
1795 // http2::path_join
1796
1797 // We won't find %XX if length is less than 3.
1798 if (path.size() < 3 ||
1799 std::find(std::begin(path), std::end(path), '%') == std::end(path)) {
1800 return path_join(balloc, StringRef{}, StringRef{}, path, query);
1801 }
1802
1803 // includes last terminal NULL.
1804 auto result = make_byte_ref(balloc, path.size() + 1);
1805 auto p = result.base;
1806
1807 auto it = std::begin(path);
1808 for (; it + 2 < std::end(path);) {
1809 if (*it == '%') {
1810 if (util::is_hex_digit(*(it + 1)) && util::is_hex_digit(*(it + 2))) {
1811 auto c =
1812 (util::hex_to_uint(*(it + 1)) << 4) + util::hex_to_uint(*(it + 2));
1813 if (util::in_rfc3986_unreserved_chars(c)) {
1814 *p++ = c;
1815
1816 it += 3;
1817
1818 continue;
1819 }
1820 *p++ = '%';
1821 *p++ = util::upcase(*(it + 1));
1822 *p++ = util::upcase(*(it + 2));
1823
1824 it += 3;
1825
1826 continue;
1827 }
1828 }
1829 *p++ = *it++;
1830 }
1831
1832 p = std::copy(it, std::end(path), p);
1833 *p = '\0';
1834
1835 return path_join(balloc, StringRef{}, StringRef{}, StringRef{result.base, p},
1836 query);
1837 }
1838
normalize_path_colon(BlockAllocator & balloc,const StringRef & path,const StringRef & query)1839 StringRef normalize_path_colon(BlockAllocator &balloc, const StringRef &path,
1840 const StringRef &query) {
1841 // First, decode %XX for unreserved characters and ':', then do
1842 // http2::path_join
1843
1844 // We won't find %XX if length is less than 3.
1845 if (path.size() < 3 ||
1846 std::find(std::begin(path), std::end(path), '%') == std::end(path)) {
1847 return path_join(balloc, StringRef{}, StringRef{}, path, query);
1848 }
1849
1850 // includes last terminal NULL.
1851 auto result = make_byte_ref(balloc, path.size() + 1);
1852 auto p = result.base;
1853
1854 auto it = std::begin(path);
1855 for (; it + 2 < std::end(path);) {
1856 if (*it == '%') {
1857 if (util::is_hex_digit(*(it + 1)) && util::is_hex_digit(*(it + 2))) {
1858 auto c =
1859 (util::hex_to_uint(*(it + 1)) << 4) + util::hex_to_uint(*(it + 2));
1860 if (util::in_rfc3986_unreserved_chars(c) || c == ':') {
1861 *p++ = c;
1862
1863 it += 3;
1864
1865 continue;
1866 }
1867 *p++ = '%';
1868 *p++ = util::upcase(*(it + 1));
1869 *p++ = util::upcase(*(it + 2));
1870
1871 it += 3;
1872
1873 continue;
1874 }
1875 }
1876 *p++ = *it++;
1877 }
1878
1879 p = std::copy(it, std::end(path), p);
1880 *p = '\0';
1881
1882 return path_join(balloc, StringRef{}, StringRef{}, StringRef{result.base, p},
1883 query);
1884 }
1885
normalize_path(const StringRef & path,const StringRef & query)1886 std::string normalize_path(const StringRef &path, const StringRef &query) {
1887 BlockAllocator balloc(1024, 1024);
1888
1889 return normalize_path(balloc, path, query).str();
1890 }
1891
rewrite_clean_path(BlockAllocator & balloc,const StringRef & src)1892 StringRef rewrite_clean_path(BlockAllocator &balloc, const StringRef &src) {
1893 if (src.empty() || src[0] != '/') {
1894 return src;
1895 }
1896 // probably, not necessary most of the case, but just in case.
1897 auto fragment = std::find(std::begin(src), std::end(src), '#');
1898 auto raw_query = std::find(std::begin(src), fragment, '?');
1899 auto query = raw_query;
1900 if (query != fragment) {
1901 ++query;
1902 }
1903 return normalize_path(balloc, StringRef{std::begin(src), raw_query},
1904 StringRef{query, fragment});
1905 }
1906
copy_lower(BlockAllocator & balloc,const StringRef & src)1907 StringRef copy_lower(BlockAllocator &balloc, const StringRef &src) {
1908 auto iov = make_byte_ref(balloc, src.size() + 1);
1909 auto p = iov.base;
1910 p = std::copy(std::begin(src), std::end(src), p);
1911 *p = '\0';
1912 util::inp_strlower(iov.base, p);
1913 return StringRef{iov.base, p};
1914 }
1915
contains_trailers(const StringRef & s)1916 bool contains_trailers(const StringRef &s) {
1917 constexpr auto trailers = StringRef::from_lit("trailers");
1918
1919 for (auto p = std::begin(s), end = std::end(s);; ++p) {
1920 p = std::find_if(p, end, [](char c) { return c != ' ' && c != '\t'; });
1921 if (p == end || static_cast<size_t>(end - p) < trailers.size()) {
1922 return false;
1923 }
1924 if (util::strieq(trailers, StringRef{p, p + trailers.size()})) {
1925 // Make sure that there is no character other than white spaces
1926 // before next "," or end of string.
1927 p = std::find_if(p + trailers.size(), end,
1928 [](char c) { return c != ' ' && c != '\t'; });
1929 if (p == end || *p == ',') {
1930 return true;
1931 }
1932 }
1933 // Skip to next ",".
1934 p = std::find_if(p, end, [](char c) { return c == ','; });
1935 if (p == end) {
1936 return false;
1937 }
1938 }
1939 }
1940
make_websocket_accept_token(uint8_t * dest,const StringRef & key)1941 StringRef make_websocket_accept_token(uint8_t *dest, const StringRef &key) {
1942 static constexpr uint8_t magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
1943 std::array<uint8_t, base64::encode_length(16) + str_size(magic)> s;
1944 auto p = std::copy(std::begin(key), std::end(key), std::begin(s));
1945 std::copy_n(magic, str_size(magic), p);
1946
1947 std::array<uint8_t, 20> h;
1948 if (util::sha1(h.data(), StringRef{std::begin(s), std::end(s)}) != 0) {
1949 return StringRef{};
1950 }
1951
1952 auto end = base64::encode(std::begin(h), std::end(h), dest);
1953 return StringRef{dest, end};
1954 }
1955
legacy_http1(int major,int minor)1956 bool legacy_http1(int major, int minor) {
1957 return major <= 0 || (major == 1 && minor == 0);
1958 }
1959
1960 } // namespace http2
1961
1962 } // namespace nghttp2
1963