• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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       // quoted-pair
1048     case '\\':
1049       ++first;
1050       if (first == last) {
1051         return first;
1052       }
1053 
1054       switch (*first) {
1055       case '\t':
1056       case ' ':
1057         break;
1058       default:
1059         if ((0x21 <= *first && *first <= 0x7e) /* VCHAR */ ||
1060             (0x80 <= *first && *first <= 0xff) /* obs-text */) {
1061           break;
1062         }
1063 
1064         return last;
1065       }
1066 
1067       break;
1068       // qdtext
1069     case '\t':
1070     case ' ':
1071     case '!':
1072       break;
1073     default:
1074       if ((0x23 <= *first && *first <= 0x5b) ||
1075           (0x5d <= *first && *first <= 0x7e)) {
1076         break;
1077       }
1078 
1079       return last;
1080     }
1081     ++first;
1082   }
1083   return first;
1084 }
1085 } // namespace
1086 
1087 namespace {
1088 // Returns true if link-param does not match pattern |pat| of length
1089 // |patlen| or it has empty value ("").  |pat| should be parmname
1090 // followed by "=".
check_link_param_empty(const char * first,const char * last,const char * pat,size_t patlen)1091 bool check_link_param_empty(const char *first, const char *last,
1092                             const char *pat, size_t patlen) {
1093   if (first + patlen <= last) {
1094     if (std::equal(pat, pat + patlen, first, util::CaseCmp())) {
1095       // we only accept URI if pat is followd by "" (e.g.,
1096       // loadpolicy="") here.
1097       if (first + patlen + 2 <= last) {
1098         if (*(first + patlen) != '"' || *(first + patlen + 1) != '"') {
1099           return false;
1100         }
1101       } else {
1102         // here we got invalid production (anchor=") or anchor=?
1103         return false;
1104       }
1105     }
1106   }
1107   return true;
1108 }
1109 } // namespace
1110 
1111 namespace {
1112 // Returns true if link-param consists of only parmname, and it
1113 // matches string [pat, pat + patlen).
check_link_param_without_value(const char * first,const char * last,const char * pat,size_t patlen)1114 bool check_link_param_without_value(const char *first, const char *last,
1115                                     const char *pat, size_t patlen) {
1116   if (first + patlen > last) {
1117     return false;
1118   }
1119 
1120   if (first + patlen == last) {
1121     return std::equal(pat, pat + patlen, first, util::CaseCmp());
1122   }
1123 
1124   switch (*(first + patlen)) {
1125   case ';':
1126   case ',':
1127     return std::equal(pat, pat + patlen, first, util::CaseCmp());
1128   }
1129 
1130   return false;
1131 }
1132 } // namespace
1133 
1134 namespace {
1135 std::pair<LinkHeader, const char *>
parse_next_link_header_once(const char * first,const char * last)1136 parse_next_link_header_once(const char *first, const char *last) {
1137   first = skip_to_next_field(first, last);
1138   if (first == last || *first != '<') {
1139     return {{StringRef{}}, last};
1140   }
1141   auto url_first = ++first;
1142   first = std::find(first, last, '>');
1143   if (first == last) {
1144     return {{StringRef{}}, first};
1145   }
1146   auto url_last = first++;
1147   if (first == last) {
1148     return {{StringRef{}}, first};
1149   }
1150   // we expect ';' or ',' here
1151   switch (*first) {
1152   case ',':
1153     return {{StringRef{}}, ++first};
1154   case ';':
1155     ++first;
1156     break;
1157   default:
1158     return {{StringRef{}}, last};
1159   }
1160 
1161   auto ok = false;
1162   auto ign = false;
1163   for (;;) {
1164     first = skip_lws(first, last);
1165     if (first == last) {
1166       return {{StringRef{}}, first};
1167     }
1168     // we expect link-param
1169 
1170     if (!ign) {
1171       if (!ok) {
1172         // rel can take several relations using quoted form.
1173         static constexpr char PLP[] = "rel=\"";
1174         static constexpr size_t PLPLEN = str_size(PLP);
1175 
1176         static constexpr char PLT[] = "preload";
1177         static constexpr size_t PLTLEN = str_size(PLT);
1178         if (first + PLPLEN < last && *(first + PLPLEN - 1) == '"' &&
1179             std::equal(PLP, PLP + PLPLEN, first, util::CaseCmp())) {
1180           // we have to search preload in whitespace separated list:
1181           // rel="preload something http://example.org/foo"
1182           first += PLPLEN;
1183           auto start = first;
1184           for (; first != last;) {
1185             if (*first != ' ' && *first != '"') {
1186               ++first;
1187               continue;
1188             }
1189 
1190             if (start == first) {
1191               return {{StringRef{}}, last};
1192             }
1193 
1194             if (!ok && start + PLTLEN == first &&
1195                 std::equal(PLT, PLT + PLTLEN, start, util::CaseCmp())) {
1196               ok = true;
1197             }
1198 
1199             if (*first == '"') {
1200               break;
1201             }
1202             first = skip_lws(first, last);
1203             start = first;
1204           }
1205           if (first == last) {
1206             return {{StringRef{}}, last};
1207           }
1208           assert(*first == '"');
1209           ++first;
1210           if (first == last || *first == ',') {
1211             goto almost_done;
1212           }
1213           if (*first == ';') {
1214             ++first;
1215             // parse next link-param
1216             continue;
1217           }
1218           return {{StringRef{}}, last};
1219         }
1220       }
1221       // we are only interested in rel=preload parameter.  Others are
1222       // simply skipped.
1223       static constexpr char PL[] = "rel=preload";
1224       static constexpr size_t PLLEN = str_size(PL);
1225       if (first + PLLEN == last) {
1226         if (std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
1227           // ok = true;
1228           // this is the end of sequence
1229           return {{{url_first, url_last}}, last};
1230         }
1231       } else if (first + PLLEN + 1 <= last) {
1232         switch (*(first + PLLEN)) {
1233         case ',':
1234           if (!std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
1235             break;
1236           }
1237           // ok = true;
1238           // skip including ','
1239           first += PLLEN + 1;
1240           return {{{url_first, url_last}}, first};
1241         case ';':
1242           if (!std::equal(PL, PL + PLLEN, first, util::CaseCmp())) {
1243             break;
1244           }
1245           ok = true;
1246           // skip including ';'
1247           first += PLLEN + 1;
1248           // continue parse next link-param
1249           continue;
1250         }
1251       }
1252       // we have to reject URI if we have nonempty anchor parameter.
1253       static constexpr char ANCHOR[] = "anchor=";
1254       static constexpr size_t ANCHORLEN = str_size(ANCHOR);
1255       if (!ign && !check_link_param_empty(first, last, ANCHOR, ANCHORLEN)) {
1256         ign = true;
1257       }
1258 
1259       // reject URI if we have non-empty loadpolicy.  This could be
1260       // tightened up to just pick up "next" or "insert".
1261       static constexpr char LOADPOLICY[] = "loadpolicy=";
1262       static constexpr size_t LOADPOLICYLEN = str_size(LOADPOLICY);
1263       if (!ign &&
1264           !check_link_param_empty(first, last, LOADPOLICY, LOADPOLICYLEN)) {
1265         ign = true;
1266       }
1267 
1268       // reject URI if we have nopush attribute.
1269       static constexpr char NOPUSH[] = "nopush";
1270       static constexpr size_t NOPUSHLEN = str_size(NOPUSH);
1271       if (!ign &&
1272           check_link_param_without_value(first, last, NOPUSH, NOPUSHLEN)) {
1273         ign = true;
1274       }
1275     }
1276 
1277     auto param_first = first;
1278     for (; first != last;) {
1279       if (util::in_attr_char(*first)) {
1280         ++first;
1281         continue;
1282       }
1283       // '*' is only allowed at the end of parameter name and must be
1284       // followed by '='
1285       if (last - first >= 2 && first != param_first) {
1286         if (*first == '*' && *(first + 1) == '=') {
1287           ++first;
1288           break;
1289         }
1290       }
1291       if (*first == '=' || *first == ';' || *first == ',') {
1292         break;
1293       }
1294       return {{StringRef{}}, last};
1295     }
1296     if (param_first == first) {
1297       // empty parmname
1298       return {{StringRef{}}, last};
1299     }
1300     // link-param without value is acceptable (see link-extension) if
1301     // it is not followed by '='
1302     if (first == last || *first == ',') {
1303       goto almost_done;
1304     }
1305     if (*first == ';') {
1306       ++first;
1307       // parse next link-param
1308       continue;
1309     }
1310     // now parsing link-param value
1311     assert(*first == '=');
1312     ++first;
1313     if (first == last) {
1314       // empty value is not acceptable
1315       return {{StringRef{}}, first};
1316     }
1317     if (*first == '"') {
1318       // quoted-string
1319       first = skip_to_right_dquote(first + 1, last);
1320       if (first == last) {
1321         return {{StringRef{}}, first};
1322       }
1323       ++first;
1324       if (first == last || *first == ',') {
1325         goto almost_done;
1326       }
1327       if (*first == ';') {
1328         ++first;
1329         // parse next link-param
1330         continue;
1331       }
1332       return {{StringRef{}}, last};
1333     }
1334     // not quoted-string, skip to next ',' or ';'
1335     if (*first == ',' || *first == ';') {
1336       // empty value
1337       return {{StringRef{}}, last};
1338     }
1339     for (; first != last; ++first) {
1340       if (*first == ',' || *first == ';') {
1341         break;
1342       }
1343     }
1344     if (first == last || *first == ',') {
1345       goto almost_done;
1346     }
1347     assert(*first == ';');
1348     ++first;
1349     // parse next link-param
1350   }
1351 
1352 almost_done:
1353   assert(first == last || *first == ',');
1354 
1355   if (first != last) {
1356     ++first;
1357   }
1358   if (ok && !ign) {
1359     return {{{url_first, url_last}}, first};
1360   }
1361   return {{StringRef{}}, first};
1362 }
1363 } // namespace
1364 
parse_link_header(const StringRef & src)1365 std::vector<LinkHeader> parse_link_header(const StringRef &src) {
1366   std::vector<LinkHeader> res;
1367   for (auto first = std::begin(src); first != std::end(src);) {
1368     auto rv = parse_next_link_header_once(first, std::end(src));
1369     first = rv.second;
1370     auto &link = rv.first;
1371     if (!link.uri.empty()) {
1372       res.push_back(link);
1373     }
1374   }
1375   return res;
1376 }
1377 
path_join(const StringRef & base_path,const StringRef & base_query,const StringRef & rel_path,const StringRef & rel_query)1378 std::string path_join(const StringRef &base_path, const StringRef &base_query,
1379                       const StringRef &rel_path, const StringRef &rel_query) {
1380   BlockAllocator balloc(1024, 1024);
1381 
1382   return path_join(balloc, base_path, base_query, rel_path, rel_query).str();
1383 }
1384 
expect_response_body(int status_code)1385 bool expect_response_body(int status_code) {
1386   return status_code == 101 ||
1387          (status_code / 100 != 1 && status_code != 304 && status_code != 204);
1388 }
1389 
expect_response_body(const std::string & method,int status_code)1390 bool expect_response_body(const std::string &method, int status_code) {
1391   return method != "HEAD" && expect_response_body(status_code);
1392 }
1393 
expect_response_body(int method_token,int status_code)1394 bool expect_response_body(int method_token, int status_code) {
1395   return method_token != HTTP_HEAD && expect_response_body(status_code);
1396 }
1397 
lookup_method_token(const StringRef & name)1398 int lookup_method_token(const StringRef &name) {
1399   return lookup_method_token(name.byte(), name.size());
1400 }
1401 
1402 // This function was generated by genmethodfunc.py.
lookup_method_token(const uint8_t * name,size_t namelen)1403 int lookup_method_token(const uint8_t *name, size_t namelen) {
1404   switch (namelen) {
1405   case 3:
1406     switch (name[2]) {
1407     case 'L':
1408       if (util::streq_l("AC", name, 2)) {
1409         return HTTP_ACL;
1410       }
1411       break;
1412     case 'T':
1413       if (util::streq_l("GE", name, 2)) {
1414         return HTTP_GET;
1415       }
1416       if (util::streq_l("PU", name, 2)) {
1417         return HTTP_PUT;
1418       }
1419       break;
1420     }
1421     break;
1422   case 4:
1423     switch (name[3]) {
1424     case 'D':
1425       if (util::streq_l("BIN", name, 3)) {
1426         return HTTP_BIND;
1427       }
1428       if (util::streq_l("HEA", name, 3)) {
1429         return HTTP_HEAD;
1430       }
1431       break;
1432     case 'E':
1433       if (util::streq_l("MOV", name, 3)) {
1434         return HTTP_MOVE;
1435       }
1436       break;
1437     case 'K':
1438       if (util::streq_l("LIN", name, 3)) {
1439         return HTTP_LINK;
1440       }
1441       if (util::streq_l("LOC", name, 3)) {
1442         return HTTP_LOCK;
1443       }
1444       break;
1445     case 'T':
1446       if (util::streq_l("POS", name, 3)) {
1447         return HTTP_POST;
1448       }
1449       break;
1450     case 'Y':
1451       if (util::streq_l("COP", name, 3)) {
1452         return HTTP_COPY;
1453       }
1454       break;
1455     }
1456     break;
1457   case 5:
1458     switch (name[4]) {
1459     case 'E':
1460       if (util::streq_l("MERG", name, 4)) {
1461         return HTTP_MERGE;
1462       }
1463       if (util::streq_l("PURG", name, 4)) {
1464         return HTTP_PURGE;
1465       }
1466       if (util::streq_l("TRAC", name, 4)) {
1467         return HTTP_TRACE;
1468       }
1469       break;
1470     case 'H':
1471       if (util::streq_l("PATC", name, 4)) {
1472         return HTTP_PATCH;
1473       }
1474       break;
1475     case 'L':
1476       if (util::streq_l("MKCO", name, 4)) {
1477         return HTTP_MKCOL;
1478       }
1479       break;
1480     }
1481     break;
1482   case 6:
1483     switch (name[5]) {
1484     case 'D':
1485       if (util::streq_l("REBIN", name, 5)) {
1486         return HTTP_REBIND;
1487       }
1488       if (util::streq_l("UNBIN", name, 5)) {
1489         return HTTP_UNBIND;
1490       }
1491       break;
1492     case 'E':
1493       if (util::streq_l("DELET", name, 5)) {
1494         return HTTP_DELETE;
1495       }
1496       if (util::streq_l("SOURC", name, 5)) {
1497         return HTTP_SOURCE;
1498       }
1499       break;
1500     case 'H':
1501       if (util::streq_l("SEARC", name, 5)) {
1502         return HTTP_SEARCH;
1503       }
1504       break;
1505     case 'K':
1506       if (util::streq_l("UNLIN", name, 5)) {
1507         return HTTP_UNLINK;
1508       }
1509       if (util::streq_l("UNLOC", name, 5)) {
1510         return HTTP_UNLOCK;
1511       }
1512       break;
1513     case 'T':
1514       if (util::streq_l("REPOR", name, 5)) {
1515         return HTTP_REPORT;
1516       }
1517       break;
1518     case 'Y':
1519       if (util::streq_l("NOTIF", name, 5)) {
1520         return HTTP_NOTIFY;
1521       }
1522       break;
1523     }
1524     break;
1525   case 7:
1526     switch (name[6]) {
1527     case 'H':
1528       if (util::streq_l("MSEARC", name, 6)) {
1529         return HTTP_MSEARCH;
1530       }
1531       break;
1532     case 'S':
1533       if (util::streq_l("OPTION", name, 6)) {
1534         return HTTP_OPTIONS;
1535       }
1536       break;
1537     case 'T':
1538       if (util::streq_l("CONNEC", name, 6)) {
1539         return HTTP_CONNECT;
1540       }
1541       break;
1542     }
1543     break;
1544   case 8:
1545     switch (name[7]) {
1546     case 'D':
1547       if (util::streq_l("PROPFIN", name, 7)) {
1548         return HTTP_PROPFIND;
1549       }
1550       break;
1551     case 'T':
1552       if (util::streq_l("CHECKOU", name, 7)) {
1553         return HTTP_CHECKOUT;
1554       }
1555       break;
1556     }
1557     break;
1558   case 9:
1559     switch (name[8]) {
1560     case 'E':
1561       if (util::streq_l("SUBSCRIB", name, 8)) {
1562         return HTTP_SUBSCRIBE;
1563       }
1564       break;
1565     case 'H':
1566       if (util::streq_l("PROPPATC", name, 8)) {
1567         return HTTP_PROPPATCH;
1568       }
1569       break;
1570     }
1571     break;
1572   case 10:
1573     switch (name[9]) {
1574     case 'R':
1575       if (util::streq_l("MKCALENDA", name, 9)) {
1576         return HTTP_MKCALENDAR;
1577       }
1578       break;
1579     case 'Y':
1580       if (util::streq_l("MKACTIVIT", name, 9)) {
1581         return HTTP_MKACTIVITY;
1582       }
1583       break;
1584     }
1585     break;
1586   case 11:
1587     switch (name[10]) {
1588     case 'E':
1589       if (util::streq_l("UNSUBSCRIB", name, 10)) {
1590         return HTTP_UNSUBSCRIBE;
1591       }
1592       break;
1593     }
1594     break;
1595   }
1596   return -1;
1597 }
1598 
to_method_string(int method_token)1599 StringRef to_method_string(int method_token) {
1600   // we happened to use same value for method with llhttp.
1601   return StringRef{
1602       llhttp_method_name(static_cast<llhttp_method>(method_token))};
1603 }
1604 
get_pure_path_component(const StringRef & uri)1605 StringRef get_pure_path_component(const StringRef &uri) {
1606   int rv;
1607 
1608   http_parser_url u{};
1609   rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u);
1610   if (rv != 0) {
1611     return StringRef{};
1612   }
1613 
1614   if (u.field_set & (1 << UF_PATH)) {
1615     auto &f = u.field_data[UF_PATH];
1616     return StringRef{uri.c_str() + f.off, f.len};
1617   }
1618 
1619   return StringRef::from_lit("/");
1620 }
1621 
construct_push_component(BlockAllocator & balloc,StringRef & scheme,StringRef & authority,StringRef & path,const StringRef & base,const StringRef & uri)1622 int construct_push_component(BlockAllocator &balloc, StringRef &scheme,
1623                              StringRef &authority, StringRef &path,
1624                              const StringRef &base, const StringRef &uri) {
1625   int rv;
1626   StringRef rel, relq;
1627 
1628   if (uri.size() == 0) {
1629     return -1;
1630   }
1631 
1632   http_parser_url u{};
1633 
1634   rv = http_parser_parse_url(uri.c_str(), uri.size(), 0, &u);
1635 
1636   if (rv != 0) {
1637     if (uri[0] == '/') {
1638       return -1;
1639     }
1640 
1641     // treat link_url as relative URI.
1642     auto end = std::find(std::begin(uri), std::end(uri), '#');
1643     auto q = std::find(std::begin(uri), end, '?');
1644 
1645     rel = StringRef{std::begin(uri), q};
1646     if (q != end) {
1647       relq = StringRef{q + 1, std::end(uri)};
1648     }
1649   } else {
1650     if (u.field_set & (1 << UF_SCHEMA)) {
1651       scheme = util::get_uri_field(uri.c_str(), u, UF_SCHEMA);
1652     }
1653 
1654     if (u.field_set & (1 << UF_HOST)) {
1655       auto auth = util::get_uri_field(uri.c_str(), u, UF_HOST);
1656       auto len = auth.size();
1657       auto port_exists = u.field_set & (1 << UF_PORT);
1658       if (port_exists) {
1659         len += 1 + str_size("65535");
1660       }
1661       auto iov = make_byte_ref(balloc, len + 1);
1662       auto p = iov.base;
1663       p = std::copy(std::begin(auth), std::end(auth), p);
1664       if (port_exists) {
1665         *p++ = ':';
1666         p = util::utos(p, u.port);
1667       }
1668       *p = '\0';
1669 
1670       authority = StringRef{iov.base, p};
1671     }
1672 
1673     if (u.field_set & (1 << UF_PATH)) {
1674       auto &f = u.field_data[UF_PATH];
1675       rel = StringRef{uri.c_str() + f.off, f.len};
1676     } else {
1677       rel = StringRef::from_lit("/");
1678     }
1679 
1680     if (u.field_set & (1 << UF_QUERY)) {
1681       auto &f = u.field_data[UF_QUERY];
1682       relq = StringRef{uri.c_str() + f.off, f.len};
1683     }
1684   }
1685 
1686   path = http2::path_join(balloc, base, StringRef{}, rel, relq);
1687 
1688   return 0;
1689 }
1690 
1691 namespace {
eat_file(InputIt first,InputIt last)1692 template <typename InputIt> InputIt eat_file(InputIt first, InputIt last) {
1693   if (first == last) {
1694     *first++ = '/';
1695     return first;
1696   }
1697 
1698   if (*(last - 1) == '/') {
1699     return last;
1700   }
1701 
1702   auto p = last;
1703   for (; p != first && *(p - 1) != '/'; --p)
1704     ;
1705   if (p == first) {
1706     // this should not happened in normal case, where we expect path
1707     // starts with '/'
1708     *first++ = '/';
1709     return first;
1710   }
1711 
1712   return p;
1713 }
1714 } // namespace
1715 
1716 namespace {
eat_dir(InputIt first,InputIt last)1717 template <typename InputIt> InputIt eat_dir(InputIt first, InputIt last) {
1718   auto p = eat_file(first, last);
1719 
1720   --p;
1721 
1722   assert(*p == '/');
1723 
1724   return eat_file(first, p);
1725 }
1726 } // namespace
1727 
path_join(BlockAllocator & balloc,const StringRef & base_path,const StringRef & base_query,const StringRef & rel_path,const StringRef & rel_query)1728 StringRef path_join(BlockAllocator &balloc, const StringRef &base_path,
1729                     const StringRef &base_query, const StringRef &rel_path,
1730                     const StringRef &rel_query) {
1731   auto res = make_byte_ref(
1732       balloc, std::max(static_cast<size_t>(1), base_path.size()) +
1733                   rel_path.size() + 1 +
1734                   std::max(base_query.size(), rel_query.size()) + 1);
1735   auto p = res.base;
1736 
1737   if (rel_path.empty()) {
1738     if (base_path.empty()) {
1739       *p++ = '/';
1740     } else {
1741       p = std::copy(std::begin(base_path), std::end(base_path), p);
1742     }
1743     if (rel_query.empty()) {
1744       if (!base_query.empty()) {
1745         *p++ = '?';
1746         p = std::copy(std::begin(base_query), std::end(base_query), p);
1747       }
1748       *p = '\0';
1749       return StringRef{res.base, p};
1750     }
1751     *p++ = '?';
1752     p = std::copy(std::begin(rel_query), std::end(rel_query), p);
1753     *p = '\0';
1754     return StringRef{res.base, p};
1755   }
1756 
1757   auto first = std::begin(rel_path);
1758   auto last = std::end(rel_path);
1759 
1760   if (rel_path[0] == '/') {
1761     *p++ = '/';
1762     ++first;
1763     for (; first != last && *first == '/'; ++first)
1764       ;
1765   } else if (base_path.empty()) {
1766     *p++ = '/';
1767   } else {
1768     p = std::copy(std::begin(base_path), std::end(base_path), p);
1769   }
1770 
1771   for (; first != last;) {
1772     if (*first == '.') {
1773       if (first + 1 == last) {
1774         if (*(p - 1) != '/') {
1775           p = eat_file(res.base, p);
1776         }
1777         break;
1778       }
1779       if (*(first + 1) == '/') {
1780         if (*(p - 1) != '/') {
1781           p = eat_file(res.base, p);
1782         }
1783         first += 2;
1784         continue;
1785       }
1786       if (*(first + 1) == '.') {
1787         if (first + 2 == last) {
1788           p = eat_dir(res.base, p);
1789           break;
1790         }
1791         if (*(first + 2) == '/') {
1792           p = eat_dir(res.base, p);
1793           first += 3;
1794           continue;
1795         }
1796       }
1797     }
1798     if (*(p - 1) != '/') {
1799       p = eat_file(res.base, p);
1800     }
1801     auto slash = std::find(first, last, '/');
1802     if (slash == last) {
1803       p = std::copy(first, last, p);
1804       break;
1805     }
1806     p = std::copy(first, slash + 1, p);
1807     first = slash + 1;
1808     for (; first != last && *first == '/'; ++first)
1809       ;
1810   }
1811   if (!rel_query.empty()) {
1812     *p++ = '?';
1813     p = std::copy(std::begin(rel_query), std::end(rel_query), p);
1814   }
1815   *p = '\0';
1816   return StringRef{res.base, p};
1817 }
1818 
normalize_path(BlockAllocator & balloc,const StringRef & path,const StringRef & query)1819 StringRef normalize_path(BlockAllocator &balloc, const StringRef &path,
1820                          const StringRef &query) {
1821   // First, decode %XX for unreserved characters, then do
1822   // http2::path_join
1823 
1824   // We won't find %XX if length is less than 3.
1825   if (path.size() < 3 ||
1826       std::find(std::begin(path), std::end(path), '%') == std::end(path)) {
1827     return path_join(balloc, StringRef{}, StringRef{}, path, query);
1828   }
1829 
1830   // includes last terminal NULL.
1831   auto result = make_byte_ref(balloc, path.size() + 1);
1832   auto p = result.base;
1833 
1834   auto it = std::begin(path);
1835   for (; it + 2 < std::end(path);) {
1836     if (*it == '%') {
1837       if (util::is_hex_digit(*(it + 1)) && util::is_hex_digit(*(it + 2))) {
1838         auto c =
1839             (util::hex_to_uint(*(it + 1)) << 4) + util::hex_to_uint(*(it + 2));
1840         if (util::in_rfc3986_unreserved_chars(c)) {
1841           *p++ = c;
1842 
1843           it += 3;
1844 
1845           continue;
1846         }
1847         *p++ = '%';
1848         *p++ = util::upcase(*(it + 1));
1849         *p++ = util::upcase(*(it + 2));
1850 
1851         it += 3;
1852 
1853         continue;
1854       }
1855     }
1856     *p++ = *it++;
1857   }
1858 
1859   p = std::copy(it, std::end(path), p);
1860   *p = '\0';
1861 
1862   return path_join(balloc, StringRef{}, StringRef{}, StringRef{result.base, p},
1863                    query);
1864 }
1865 
normalize_path_colon(BlockAllocator & balloc,const StringRef & path,const StringRef & query)1866 StringRef normalize_path_colon(BlockAllocator &balloc, const StringRef &path,
1867                                const StringRef &query) {
1868   // First, decode %XX for unreserved characters and ':', then do
1869   // http2::path_join
1870 
1871   // We won't find %XX if length is less than 3.
1872   if (path.size() < 3 ||
1873       std::find(std::begin(path), std::end(path), '%') == std::end(path)) {
1874     return path_join(balloc, StringRef{}, StringRef{}, path, query);
1875   }
1876 
1877   // includes last terminal NULL.
1878   auto result = make_byte_ref(balloc, path.size() + 1);
1879   auto p = result.base;
1880 
1881   auto it = std::begin(path);
1882   for (; it + 2 < std::end(path);) {
1883     if (*it == '%') {
1884       if (util::is_hex_digit(*(it + 1)) && util::is_hex_digit(*(it + 2))) {
1885         auto c =
1886             (util::hex_to_uint(*(it + 1)) << 4) + util::hex_to_uint(*(it + 2));
1887         if (util::in_rfc3986_unreserved_chars(c) || c == ':') {
1888           *p++ = c;
1889 
1890           it += 3;
1891 
1892           continue;
1893         }
1894         *p++ = '%';
1895         *p++ = util::upcase(*(it + 1));
1896         *p++ = util::upcase(*(it + 2));
1897 
1898         it += 3;
1899 
1900         continue;
1901       }
1902     }
1903     *p++ = *it++;
1904   }
1905 
1906   p = std::copy(it, std::end(path), p);
1907   *p = '\0';
1908 
1909   return path_join(balloc, StringRef{}, StringRef{}, StringRef{result.base, p},
1910                    query);
1911 }
1912 
normalize_path(const StringRef & path,const StringRef & query)1913 std::string normalize_path(const StringRef &path, const StringRef &query) {
1914   BlockAllocator balloc(1024, 1024);
1915 
1916   return normalize_path(balloc, path, query).str();
1917 }
1918 
rewrite_clean_path(BlockAllocator & balloc,const StringRef & src)1919 StringRef rewrite_clean_path(BlockAllocator &balloc, const StringRef &src) {
1920   if (src.empty() || src[0] != '/') {
1921     return src;
1922   }
1923   // probably, not necessary most of the case, but just in case.
1924   auto fragment = std::find(std::begin(src), std::end(src), '#');
1925   auto raw_query = std::find(std::begin(src), fragment, '?');
1926   auto query = raw_query;
1927   if (query != fragment) {
1928     ++query;
1929   }
1930   return normalize_path(balloc, StringRef{std::begin(src), raw_query},
1931                         StringRef{query, fragment});
1932 }
1933 
copy_lower(BlockAllocator & balloc,const StringRef & src)1934 StringRef copy_lower(BlockAllocator &balloc, const StringRef &src) {
1935   auto iov = make_byte_ref(balloc, src.size() + 1);
1936   auto p = iov.base;
1937   p = std::copy(std::begin(src), std::end(src), p);
1938   *p = '\0';
1939   util::inp_strlower(iov.base, p);
1940   return StringRef{iov.base, p};
1941 }
1942 
contains_trailers(const StringRef & s)1943 bool contains_trailers(const StringRef &s) {
1944   constexpr auto trailers = StringRef::from_lit("trailers");
1945 
1946   for (auto p = std::begin(s), end = std::end(s);; ++p) {
1947     p = std::find_if(p, end, [](char c) { return c != ' ' && c != '\t'; });
1948     if (p == end || static_cast<size_t>(end - p) < trailers.size()) {
1949       return false;
1950     }
1951     if (util::strieq(trailers, StringRef{p, p + trailers.size()})) {
1952       // Make sure that there is no character other than white spaces
1953       // before next "," or end of string.
1954       p = std::find_if(p + trailers.size(), end,
1955                        [](char c) { return c != ' ' && c != '\t'; });
1956       if (p == end || *p == ',') {
1957         return true;
1958       }
1959     }
1960     // Skip to next ",".
1961     p = std::find_if(p, end, [](char c) { return c == ','; });
1962     if (p == end) {
1963       return false;
1964     }
1965   }
1966 }
1967 
make_websocket_accept_token(uint8_t * dest,const StringRef & key)1968 StringRef make_websocket_accept_token(uint8_t *dest, const StringRef &key) {
1969   static constexpr uint8_t magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
1970   std::array<uint8_t, base64::encode_length(16) + str_size(magic)> s;
1971   auto p = std::copy(std::begin(key), std::end(key), std::begin(s));
1972   std::copy_n(magic, str_size(magic), p);
1973 
1974   std::array<uint8_t, 20> h;
1975   if (util::sha1(h.data(), StringRef{std::begin(s), std::end(s)}) != 0) {
1976     return StringRef{};
1977   }
1978 
1979   auto end = base64::encode(std::begin(h), std::end(h), dest);
1980   return StringRef{dest, end};
1981 }
1982 
legacy_http1(int major,int minor)1983 bool legacy_http1(int major, int minor) {
1984   return major <= 0 || (major == 1 && minor == 0);
1985 }
1986 
check_transfer_encoding(const StringRef & s)1987 bool check_transfer_encoding(const StringRef &s) {
1988   if (s.empty()) {
1989     return false;
1990   }
1991 
1992   auto it = std::begin(s);
1993 
1994   for (;;) {
1995     // token
1996     if (!util::in_token(*it)) {
1997       return false;
1998     }
1999 
2000     ++it;
2001 
2002     for (; it != std::end(s) && util::in_token(*it); ++it)
2003       ;
2004 
2005     if (it == std::end(s)) {
2006       return true;
2007     }
2008 
2009     for (;;) {
2010       // OWS
2011       it = skip_lws(it, std::end(s));
2012       if (it == std::end(s)) {
2013         return false;
2014       }
2015 
2016       if (*it == ',') {
2017         ++it;
2018 
2019         it = skip_lws(it, std::end(s));
2020         if (it == std::end(s)) {
2021           return false;
2022         }
2023 
2024         break;
2025       }
2026 
2027       if (*it != ';') {
2028         return false;
2029       }
2030 
2031       ++it;
2032 
2033       // transfer-parameter follows
2034 
2035       // OWS
2036       it = skip_lws(it, std::end(s));
2037       if (it == std::end(s)) {
2038         return false;
2039       }
2040 
2041       // token
2042       if (!util::in_token(*it)) {
2043         return false;
2044       }
2045 
2046       ++it;
2047 
2048       for (; it != std::end(s) && util::in_token(*it); ++it)
2049         ;
2050 
2051       if (it == std::end(s)) {
2052         return false;
2053       }
2054 
2055       // No BWS allowed
2056       if (*it != '=') {
2057         return false;
2058       }
2059 
2060       ++it;
2061 
2062       if (util::in_token(*it)) {
2063         // token
2064         ++it;
2065 
2066         for (; it != std::end(s) && util::in_token(*it); ++it)
2067           ;
2068       } else if (*it == '"') {
2069         // quoted-string
2070         ++it;
2071 
2072         it = skip_to_right_dquote(it, std::end(s));
2073         if (it == std::end(s)) {
2074           return false;
2075         }
2076 
2077         ++it;
2078       } else {
2079         return false;
2080       }
2081 
2082       if (it == std::end(s)) {
2083         return true;
2084       }
2085     }
2086   }
2087 }
2088 
2089 } // namespace http2
2090 
2091 } // namespace nghttp2
2092