nghttp3_http.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2015 nghttp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "nghttp3_http.h"
  27. #include <string.h>
  28. #include <assert.h>
  29. #ifdef __AVX2__
  30. # include <immintrin.h>
  31. #endif /* __AVX2__ */
  32. #include "nghttp3_stream.h"
  33. #include "nghttp3_macro.h"
  34. #include "nghttp3_conv.h"
  35. #include "nghttp3_unreachable.h"
  36. #include "sfparse/sfparse.h"
  37. static uint8_t downcase(uint8_t c) {
  38. return 'A' <= c && c <= 'Z' ? (uint8_t)(c - 'A' + 'a') : c;
  39. }
  40. /*
  41. * memieq returns 1 if the data pointed by |a| of length |n| equals to
  42. * |b| of the same length in case-insensitive manner. The data
  43. * pointed by |a| must not include upper cased letters (A-Z).
  44. */
  45. static int memieq(const void *a, const void *b, size_t n) {
  46. size_t i;
  47. const uint8_t *aa = a, *bb = b;
  48. for (i = 0; i < n; ++i) {
  49. if (aa[i] != downcase(bb[i])) {
  50. return 0;
  51. }
  52. }
  53. return 1;
  54. }
  55. #define lstrieq(A, B, N) ((sizeof((A)) - 1) == (N) && memieq((A), (B), (N)))
  56. static int64_t parse_uint(const uint8_t *s, size_t len) {
  57. int64_t n = 0;
  58. size_t i;
  59. if (len == 0) {
  60. return -1;
  61. }
  62. for (i = 0; i < len; ++i) {
  63. if ('0' <= s[i] && s[i] <= '9') {
  64. if (n > INT64_MAX / 10) {
  65. return -1;
  66. }
  67. n *= 10;
  68. if (n > INT64_MAX - (s[i] - '0')) {
  69. return -1;
  70. }
  71. n += s[i] - '0';
  72. continue;
  73. }
  74. return -1;
  75. }
  76. return n;
  77. }
  78. static int check_pseudo_header(nghttp3_http_state *http,
  79. const nghttp3_qpack_nv *nv, uint32_t flag) {
  80. if ((http->flags & flag) || nv->value->len == 0) {
  81. return 0;
  82. }
  83. http->flags |= flag;
  84. return 1;
  85. }
  86. static int expect_response_body(nghttp3_http_state *http) {
  87. return (http->flags & NGHTTP3_HTTP_FLAG_METH_HEAD) == 0 &&
  88. http->status_code / 100 != 1 && http->status_code != 304 &&
  89. http->status_code != 204;
  90. }
  91. /* For "http" or "https" URIs, OPTIONS request may have "*" in :path
  92. header field to represent system-wide OPTIONS request. Otherwise,
  93. :path header field value must start with "/". This function must
  94. be called after ":method" header field was received. This function
  95. returns nonzero if path is valid.*/
  96. static int check_path_flags(nghttp3_http_state *http) {
  97. return (http->flags & NGHTTP3_HTTP_FLAG_SCHEME_HTTP) == 0 ||
  98. ((http->flags & NGHTTP3_HTTP_FLAG_PATH_REGULAR) ||
  99. ((http->flags & NGHTTP3_HTTP_FLAG_METH_OPTIONS) &&
  100. (http->flags & NGHTTP3_HTTP_FLAG_PATH_ASTERISK)));
  101. }
  102. static int is_ws(uint8_t c) {
  103. switch (c) {
  104. case ' ':
  105. case '\t':
  106. return 1;
  107. default:
  108. return 0;
  109. }
  110. }
  111. int nghttp3_http_parse_priority(nghttp3_pri *dest, const uint8_t *value,
  112. size_t valuelen) {
  113. nghttp3_pri pri = *dest;
  114. sfparse_parser sfp;
  115. sfparse_vec key;
  116. sfparse_value val;
  117. int rv;
  118. sfparse_parser_init(&sfp, value, valuelen);
  119. for (;;) {
  120. rv = sfparse_parser_dict(&sfp, &key, &val);
  121. if (rv != 0) {
  122. if (rv == SFPARSE_ERR_EOF) {
  123. break;
  124. }
  125. return NGHTTP3_ERR_INVALID_ARGUMENT;
  126. }
  127. if (key.len != 1) {
  128. continue;
  129. }
  130. switch (key.base[0]) {
  131. case 'i':
  132. if (val.type != SFPARSE_TYPE_BOOLEAN) {
  133. return NGHTTP3_ERR_INVALID_ARGUMENT;
  134. }
  135. pri.inc = (uint8_t)val.boolean;
  136. break;
  137. case 'u':
  138. if (val.type != SFPARSE_TYPE_INTEGER ||
  139. val.integer < NGHTTP3_URGENCY_HIGH ||
  140. NGHTTP3_URGENCY_LOW < val.integer) {
  141. return NGHTTP3_ERR_INVALID_ARGUMENT;
  142. }
  143. pri.urgency = (uint32_t)val.integer;
  144. break;
  145. }
  146. }
  147. *dest = pri;
  148. return 0;
  149. }
  150. int nghttp3_pri_parse_priority_versioned(int pri_version, nghttp3_pri *dest,
  151. const uint8_t *value,
  152. size_t valuelen) {
  153. (void)pri_version;
  154. return nghttp3_http_parse_priority(dest, value, valuelen);
  155. }
  156. /* Generated by genauthroitychartbl.py */
  157. static char VALID_AUTHORITY_CHARS[] = {
  158. 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */,
  159. 0 /* EOT */, 0 /* ENQ */, 0 /* ACK */, 0 /* BEL */,
  160. 0 /* BS */, 0 /* HT */, 0 /* LF */, 0 /* VT */,
  161. 0 /* FF */, 0 /* CR */, 0 /* SO */, 0 /* SI */,
  162. 0 /* DLE */, 0 /* DC1 */, 0 /* DC2 */, 0 /* DC3 */,
  163. 0 /* DC4 */, 0 /* NAK */, 0 /* SYN */, 0 /* ETB */,
  164. 0 /* CAN */, 0 /* EM */, 0 /* SUB */, 0 /* ESC */,
  165. 0 /* FS */, 0 /* GS */, 0 /* RS */, 0 /* US */,
  166. 0 /* SPC */, 1 /* ! */, 0 /* " */, 0 /* # */,
  167. 1 /* $ */, 1 /* % */, 1 /* & */, 1 /* ' */,
  168. 1 /* ( */, 1 /* ) */, 1 /* * */, 1 /* + */,
  169. 1 /* , */, 1 /* - */, 1 /* . */, 0 /* / */,
  170. 1 /* 0 */, 1 /* 1 */, 1 /* 2 */, 1 /* 3 */,
  171. 1 /* 4 */, 1 /* 5 */, 1 /* 6 */, 1 /* 7 */,
  172. 1 /* 8 */, 1 /* 9 */, 1 /* : */, 1 /* ; */,
  173. 0 /* < */, 1 /* = */, 0 /* > */, 0 /* ? */,
  174. 0 /* @ */, 1 /* A */, 1 /* B */, 1 /* C */,
  175. 1 /* D */, 1 /* E */, 1 /* F */, 1 /* G */,
  176. 1 /* H */, 1 /* I */, 1 /* J */, 1 /* K */,
  177. 1 /* L */, 1 /* M */, 1 /* N */, 1 /* O */,
  178. 1 /* P */, 1 /* Q */, 1 /* R */, 1 /* S */,
  179. 1 /* T */, 1 /* U */, 1 /* V */, 1 /* W */,
  180. 1 /* X */, 1 /* Y */, 1 /* Z */, 1 /* [ */,
  181. 0 /* \ */, 1 /* ] */, 0 /* ^ */, 1 /* _ */,
  182. 0 /* ` */, 1 /* a */, 1 /* b */, 1 /* c */,
  183. 1 /* d */, 1 /* e */, 1 /* f */, 1 /* g */,
  184. 1 /* h */, 1 /* i */, 1 /* j */, 1 /* k */,
  185. 1 /* l */, 1 /* m */, 1 /* n */, 1 /* o */,
  186. 1 /* p */, 1 /* q */, 1 /* r */, 1 /* s */,
  187. 1 /* t */, 1 /* u */, 1 /* v */, 1 /* w */,
  188. 1 /* x */, 1 /* y */, 1 /* z */, 0 /* { */,
  189. 0 /* | */, 0 /* } */, 1 /* ~ */, 0 /* DEL */,
  190. 0 /* 0x80 */, 0 /* 0x81 */, 0 /* 0x82 */, 0 /* 0x83 */,
  191. 0 /* 0x84 */, 0 /* 0x85 */, 0 /* 0x86 */, 0 /* 0x87 */,
  192. 0 /* 0x88 */, 0 /* 0x89 */, 0 /* 0x8a */, 0 /* 0x8b */,
  193. 0 /* 0x8c */, 0 /* 0x8d */, 0 /* 0x8e */, 0 /* 0x8f */,
  194. 0 /* 0x90 */, 0 /* 0x91 */, 0 /* 0x92 */, 0 /* 0x93 */,
  195. 0 /* 0x94 */, 0 /* 0x95 */, 0 /* 0x96 */, 0 /* 0x97 */,
  196. 0 /* 0x98 */, 0 /* 0x99 */, 0 /* 0x9a */, 0 /* 0x9b */,
  197. 0 /* 0x9c */, 0 /* 0x9d */, 0 /* 0x9e */, 0 /* 0x9f */,
  198. 0 /* 0xa0 */, 0 /* 0xa1 */, 0 /* 0xa2 */, 0 /* 0xa3 */,
  199. 0 /* 0xa4 */, 0 /* 0xa5 */, 0 /* 0xa6 */, 0 /* 0xa7 */,
  200. 0 /* 0xa8 */, 0 /* 0xa9 */, 0 /* 0xaa */, 0 /* 0xab */,
  201. 0 /* 0xac */, 0 /* 0xad */, 0 /* 0xae */, 0 /* 0xaf */,
  202. 0 /* 0xb0 */, 0 /* 0xb1 */, 0 /* 0xb2 */, 0 /* 0xb3 */,
  203. 0 /* 0xb4 */, 0 /* 0xb5 */, 0 /* 0xb6 */, 0 /* 0xb7 */,
  204. 0 /* 0xb8 */, 0 /* 0xb9 */, 0 /* 0xba */, 0 /* 0xbb */,
  205. 0 /* 0xbc */, 0 /* 0xbd */, 0 /* 0xbe */, 0 /* 0xbf */,
  206. 0 /* 0xc0 */, 0 /* 0xc1 */, 0 /* 0xc2 */, 0 /* 0xc3 */,
  207. 0 /* 0xc4 */, 0 /* 0xc5 */, 0 /* 0xc6 */, 0 /* 0xc7 */,
  208. 0 /* 0xc8 */, 0 /* 0xc9 */, 0 /* 0xca */, 0 /* 0xcb */,
  209. 0 /* 0xcc */, 0 /* 0xcd */, 0 /* 0xce */, 0 /* 0xcf */,
  210. 0 /* 0xd0 */, 0 /* 0xd1 */, 0 /* 0xd2 */, 0 /* 0xd3 */,
  211. 0 /* 0xd4 */, 0 /* 0xd5 */, 0 /* 0xd6 */, 0 /* 0xd7 */,
  212. 0 /* 0xd8 */, 0 /* 0xd9 */, 0 /* 0xda */, 0 /* 0xdb */,
  213. 0 /* 0xdc */, 0 /* 0xdd */, 0 /* 0xde */, 0 /* 0xdf */,
  214. 0 /* 0xe0 */, 0 /* 0xe1 */, 0 /* 0xe2 */, 0 /* 0xe3 */,
  215. 0 /* 0xe4 */, 0 /* 0xe5 */, 0 /* 0xe6 */, 0 /* 0xe7 */,
  216. 0 /* 0xe8 */, 0 /* 0xe9 */, 0 /* 0xea */, 0 /* 0xeb */,
  217. 0 /* 0xec */, 0 /* 0xed */, 0 /* 0xee */, 0 /* 0xef */,
  218. 0 /* 0xf0 */, 0 /* 0xf1 */, 0 /* 0xf2 */, 0 /* 0xf3 */,
  219. 0 /* 0xf4 */, 0 /* 0xf5 */, 0 /* 0xf6 */, 0 /* 0xf7 */,
  220. 0 /* 0xf8 */, 0 /* 0xf9 */, 0 /* 0xfa */, 0 /* 0xfb */,
  221. 0 /* 0xfc */, 0 /* 0xfd */, 0 /* 0xfe */, 0 /* 0xff */
  222. };
  223. static int check_authority(const uint8_t *value, size_t len) {
  224. const uint8_t *last;
  225. for (last = value + len; value != last; ++value) {
  226. if (!VALID_AUTHORITY_CHARS[*value]) {
  227. return 0;
  228. }
  229. }
  230. return 1;
  231. }
  232. static int check_scheme(const uint8_t *value, size_t len) {
  233. const uint8_t *last;
  234. if (len == 0) {
  235. return 0;
  236. }
  237. if (!(('A' <= *value && *value <= 'Z') || ('a' <= *value && *value <= 'z'))) {
  238. return 0;
  239. }
  240. last = value + len;
  241. ++value;
  242. for (; value != last; ++value) {
  243. if (!(('A' <= *value && *value <= 'Z') ||
  244. ('a' <= *value && *value <= 'z') ||
  245. ('0' <= *value && *value <= '9') || *value == '+' || *value == '-' ||
  246. *value == '.')) {
  247. return 0;
  248. }
  249. }
  250. return 1;
  251. }
  252. /* Generated by genmethodchartbl.py */
  253. static char VALID_METHOD_CHARS[] = {
  254. 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */,
  255. 0 /* EOT */, 0 /* ENQ */, 0 /* ACK */, 0 /* BEL */,
  256. 0 /* BS */, 0 /* HT */, 0 /* LF */, 0 /* VT */,
  257. 0 /* FF */, 0 /* CR */, 0 /* SO */, 0 /* SI */,
  258. 0 /* DLE */, 0 /* DC1 */, 0 /* DC2 */, 0 /* DC3 */,
  259. 0 /* DC4 */, 0 /* NAK */, 0 /* SYN */, 0 /* ETB */,
  260. 0 /* CAN */, 0 /* EM */, 0 /* SUB */, 0 /* ESC */,
  261. 0 /* FS */, 0 /* GS */, 0 /* RS */, 0 /* US */,
  262. 0 /* SPC */, 1 /* ! */, 0 /* " */, 1 /* # */,
  263. 1 /* $ */, 1 /* % */, 1 /* & */, 1 /* ' */,
  264. 0 /* ( */, 0 /* ) */, 1 /* * */, 1 /* + */,
  265. 0 /* , */, 1 /* - */, 1 /* . */, 0 /* / */,
  266. 1 /* 0 */, 1 /* 1 */, 1 /* 2 */, 1 /* 3 */,
  267. 1 /* 4 */, 1 /* 5 */, 1 /* 6 */, 1 /* 7 */,
  268. 1 /* 8 */, 1 /* 9 */, 0 /* : */, 0 /* ; */,
  269. 0 /* < */, 0 /* = */, 0 /* > */, 0 /* ? */,
  270. 0 /* @ */, 1 /* A */, 1 /* B */, 1 /* C */,
  271. 1 /* D */, 1 /* E */, 1 /* F */, 1 /* G */,
  272. 1 /* H */, 1 /* I */, 1 /* J */, 1 /* K */,
  273. 1 /* L */, 1 /* M */, 1 /* N */, 1 /* O */,
  274. 1 /* P */, 1 /* Q */, 1 /* R */, 1 /* S */,
  275. 1 /* T */, 1 /* U */, 1 /* V */, 1 /* W */,
  276. 1 /* X */, 1 /* Y */, 1 /* Z */, 0 /* [ */,
  277. 0 /* \ */, 0 /* ] */, 1 /* ^ */, 1 /* _ */,
  278. 1 /* ` */, 1 /* a */, 1 /* b */, 1 /* c */,
  279. 1 /* d */, 1 /* e */, 1 /* f */, 1 /* g */,
  280. 1 /* h */, 1 /* i */, 1 /* j */, 1 /* k */,
  281. 1 /* l */, 1 /* m */, 1 /* n */, 1 /* o */,
  282. 1 /* p */, 1 /* q */, 1 /* r */, 1 /* s */,
  283. 1 /* t */, 1 /* u */, 1 /* v */, 1 /* w */,
  284. 1 /* x */, 1 /* y */, 1 /* z */, 0 /* { */,
  285. 1 /* | */, 0 /* } */, 1 /* ~ */, 0 /* DEL */,
  286. 0 /* 0x80 */, 0 /* 0x81 */, 0 /* 0x82 */, 0 /* 0x83 */,
  287. 0 /* 0x84 */, 0 /* 0x85 */, 0 /* 0x86 */, 0 /* 0x87 */,
  288. 0 /* 0x88 */, 0 /* 0x89 */, 0 /* 0x8a */, 0 /* 0x8b */,
  289. 0 /* 0x8c */, 0 /* 0x8d */, 0 /* 0x8e */, 0 /* 0x8f */,
  290. 0 /* 0x90 */, 0 /* 0x91 */, 0 /* 0x92 */, 0 /* 0x93 */,
  291. 0 /* 0x94 */, 0 /* 0x95 */, 0 /* 0x96 */, 0 /* 0x97 */,
  292. 0 /* 0x98 */, 0 /* 0x99 */, 0 /* 0x9a */, 0 /* 0x9b */,
  293. 0 /* 0x9c */, 0 /* 0x9d */, 0 /* 0x9e */, 0 /* 0x9f */,
  294. 0 /* 0xa0 */, 0 /* 0xa1 */, 0 /* 0xa2 */, 0 /* 0xa3 */,
  295. 0 /* 0xa4 */, 0 /* 0xa5 */, 0 /* 0xa6 */, 0 /* 0xa7 */,
  296. 0 /* 0xa8 */, 0 /* 0xa9 */, 0 /* 0xaa */, 0 /* 0xab */,
  297. 0 /* 0xac */, 0 /* 0xad */, 0 /* 0xae */, 0 /* 0xaf */,
  298. 0 /* 0xb0 */, 0 /* 0xb1 */, 0 /* 0xb2 */, 0 /* 0xb3 */,
  299. 0 /* 0xb4 */, 0 /* 0xb5 */, 0 /* 0xb6 */, 0 /* 0xb7 */,
  300. 0 /* 0xb8 */, 0 /* 0xb9 */, 0 /* 0xba */, 0 /* 0xbb */,
  301. 0 /* 0xbc */, 0 /* 0xbd */, 0 /* 0xbe */, 0 /* 0xbf */,
  302. 0 /* 0xc0 */, 0 /* 0xc1 */, 0 /* 0xc2 */, 0 /* 0xc3 */,
  303. 0 /* 0xc4 */, 0 /* 0xc5 */, 0 /* 0xc6 */, 0 /* 0xc7 */,
  304. 0 /* 0xc8 */, 0 /* 0xc9 */, 0 /* 0xca */, 0 /* 0xcb */,
  305. 0 /* 0xcc */, 0 /* 0xcd */, 0 /* 0xce */, 0 /* 0xcf */,
  306. 0 /* 0xd0 */, 0 /* 0xd1 */, 0 /* 0xd2 */, 0 /* 0xd3 */,
  307. 0 /* 0xd4 */, 0 /* 0xd5 */, 0 /* 0xd6 */, 0 /* 0xd7 */,
  308. 0 /* 0xd8 */, 0 /* 0xd9 */, 0 /* 0xda */, 0 /* 0xdb */,
  309. 0 /* 0xdc */, 0 /* 0xdd */, 0 /* 0xde */, 0 /* 0xdf */,
  310. 0 /* 0xe0 */, 0 /* 0xe1 */, 0 /* 0xe2 */, 0 /* 0xe3 */,
  311. 0 /* 0xe4 */, 0 /* 0xe5 */, 0 /* 0xe6 */, 0 /* 0xe7 */,
  312. 0 /* 0xe8 */, 0 /* 0xe9 */, 0 /* 0xea */, 0 /* 0xeb */,
  313. 0 /* 0xec */, 0 /* 0xed */, 0 /* 0xee */, 0 /* 0xef */,
  314. 0 /* 0xf0 */, 0 /* 0xf1 */, 0 /* 0xf2 */, 0 /* 0xf3 */,
  315. 0 /* 0xf4 */, 0 /* 0xf5 */, 0 /* 0xf6 */, 0 /* 0xf7 */,
  316. 0 /* 0xf8 */, 0 /* 0xf9 */, 0 /* 0xfa */, 0 /* 0xfb */,
  317. 0 /* 0xfc */, 0 /* 0xfd */, 0 /* 0xfe */, 0 /* 0xff */
  318. };
  319. static int check_method(const uint8_t *value, size_t len) {
  320. const uint8_t *last;
  321. if (len == 0) {
  322. return 0;
  323. }
  324. for (last = value + len; value != last; ++value) {
  325. if (!VALID_METHOD_CHARS[*value]) {
  326. return 0;
  327. }
  328. }
  329. return 1;
  330. }
  331. /* Generated by genpathchartbl.py */
  332. static char VALID_PATH_CHARS[] = {
  333. 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */,
  334. 0 /* EOT */, 0 /* ENQ */, 0 /* ACK */, 0 /* BEL */,
  335. 0 /* BS */, 0 /* HT */, 0 /* LF */, 0 /* VT */,
  336. 0 /* FF */, 0 /* CR */, 0 /* SO */, 0 /* SI */,
  337. 0 /* DLE */, 0 /* DC1 */, 0 /* DC2 */, 0 /* DC3 */,
  338. 0 /* DC4 */, 0 /* NAK */, 0 /* SYN */, 0 /* ETB */,
  339. 0 /* CAN */, 0 /* EM */, 0 /* SUB */, 0 /* ESC */,
  340. 0 /* FS */, 0 /* GS */, 0 /* RS */, 0 /* US */,
  341. 0 /* SPC */, 1 /* ! */, 1 /* " */, 1 /* # */,
  342. 1 /* $ */, 1 /* % */, 1 /* & */, 1 /* ' */,
  343. 1 /* ( */, 1 /* ) */, 1 /* * */, 1 /* + */,
  344. 1 /* , */, 1 /* - */, 1 /* . */, 1 /* / */,
  345. 1 /* 0 */, 1 /* 1 */, 1 /* 2 */, 1 /* 3 */,
  346. 1 /* 4 */, 1 /* 5 */, 1 /* 6 */, 1 /* 7 */,
  347. 1 /* 8 */, 1 /* 9 */, 1 /* : */, 1 /* ; */,
  348. 1 /* < */, 1 /* = */, 1 /* > */, 1 /* ? */,
  349. 1 /* @ */, 1 /* A */, 1 /* B */, 1 /* C */,
  350. 1 /* D */, 1 /* E */, 1 /* F */, 1 /* G */,
  351. 1 /* H */, 1 /* I */, 1 /* J */, 1 /* K */,
  352. 1 /* L */, 1 /* M */, 1 /* N */, 1 /* O */,
  353. 1 /* P */, 1 /* Q */, 1 /* R */, 1 /* S */,
  354. 1 /* T */, 1 /* U */, 1 /* V */, 1 /* W */,
  355. 1 /* X */, 1 /* Y */, 1 /* Z */, 1 /* [ */,
  356. 1 /* \ */, 1 /* ] */, 1 /* ^ */, 1 /* _ */,
  357. 1 /* ` */, 1 /* a */, 1 /* b */, 1 /* c */,
  358. 1 /* d */, 1 /* e */, 1 /* f */, 1 /* g */,
  359. 1 /* h */, 1 /* i */, 1 /* j */, 1 /* k */,
  360. 1 /* l */, 1 /* m */, 1 /* n */, 1 /* o */,
  361. 1 /* p */, 1 /* q */, 1 /* r */, 1 /* s */,
  362. 1 /* t */, 1 /* u */, 1 /* v */, 1 /* w */,
  363. 1 /* x */, 1 /* y */, 1 /* z */, 1 /* { */,
  364. 1 /* | */, 1 /* } */, 1 /* ~ */, 0 /* DEL */,
  365. 1 /* 0x80 */, 1 /* 0x81 */, 1 /* 0x82 */, 1 /* 0x83 */,
  366. 1 /* 0x84 */, 1 /* 0x85 */, 1 /* 0x86 */, 1 /* 0x87 */,
  367. 1 /* 0x88 */, 1 /* 0x89 */, 1 /* 0x8a */, 1 /* 0x8b */,
  368. 1 /* 0x8c */, 1 /* 0x8d */, 1 /* 0x8e */, 1 /* 0x8f */,
  369. 1 /* 0x90 */, 1 /* 0x91 */, 1 /* 0x92 */, 1 /* 0x93 */,
  370. 1 /* 0x94 */, 1 /* 0x95 */, 1 /* 0x96 */, 1 /* 0x97 */,
  371. 1 /* 0x98 */, 1 /* 0x99 */, 1 /* 0x9a */, 1 /* 0x9b */,
  372. 1 /* 0x9c */, 1 /* 0x9d */, 1 /* 0x9e */, 1 /* 0x9f */,
  373. 1 /* 0xa0 */, 1 /* 0xa1 */, 1 /* 0xa2 */, 1 /* 0xa3 */,
  374. 1 /* 0xa4 */, 1 /* 0xa5 */, 1 /* 0xa6 */, 1 /* 0xa7 */,
  375. 1 /* 0xa8 */, 1 /* 0xa9 */, 1 /* 0xaa */, 1 /* 0xab */,
  376. 1 /* 0xac */, 1 /* 0xad */, 1 /* 0xae */, 1 /* 0xaf */,
  377. 1 /* 0xb0 */, 1 /* 0xb1 */, 1 /* 0xb2 */, 1 /* 0xb3 */,
  378. 1 /* 0xb4 */, 1 /* 0xb5 */, 1 /* 0xb6 */, 1 /* 0xb7 */,
  379. 1 /* 0xb8 */, 1 /* 0xb9 */, 1 /* 0xba */, 1 /* 0xbb */,
  380. 1 /* 0xbc */, 1 /* 0xbd */, 1 /* 0xbe */, 1 /* 0xbf */,
  381. 1 /* 0xc0 */, 1 /* 0xc1 */, 1 /* 0xc2 */, 1 /* 0xc3 */,
  382. 1 /* 0xc4 */, 1 /* 0xc5 */, 1 /* 0xc6 */, 1 /* 0xc7 */,
  383. 1 /* 0xc8 */, 1 /* 0xc9 */, 1 /* 0xca */, 1 /* 0xcb */,
  384. 1 /* 0xcc */, 1 /* 0xcd */, 1 /* 0xce */, 1 /* 0xcf */,
  385. 1 /* 0xd0 */, 1 /* 0xd1 */, 1 /* 0xd2 */, 1 /* 0xd3 */,
  386. 1 /* 0xd4 */, 1 /* 0xd5 */, 1 /* 0xd6 */, 1 /* 0xd7 */,
  387. 1 /* 0xd8 */, 1 /* 0xd9 */, 1 /* 0xda */, 1 /* 0xdb */,
  388. 1 /* 0xdc */, 1 /* 0xdd */, 1 /* 0xde */, 1 /* 0xdf */,
  389. 1 /* 0xe0 */, 1 /* 0xe1 */, 1 /* 0xe2 */, 1 /* 0xe3 */,
  390. 1 /* 0xe4 */, 1 /* 0xe5 */, 1 /* 0xe6 */, 1 /* 0xe7 */,
  391. 1 /* 0xe8 */, 1 /* 0xe9 */, 1 /* 0xea */, 1 /* 0xeb */,
  392. 1 /* 0xec */, 1 /* 0xed */, 1 /* 0xee */, 1 /* 0xef */,
  393. 1 /* 0xf0 */, 1 /* 0xf1 */, 1 /* 0xf2 */, 1 /* 0xf3 */,
  394. 1 /* 0xf4 */, 1 /* 0xf5 */, 1 /* 0xf6 */, 1 /* 0xf7 */,
  395. 1 /* 0xf8 */, 1 /* 0xf9 */, 1 /* 0xfa */, 1 /* 0xfb */,
  396. 1 /* 0xfc */, 1 /* 0xfd */, 1 /* 0xfe */, 1 /* 0xff */
  397. };
  398. static int check_path(const uint8_t *value, size_t len) {
  399. const uint8_t *last;
  400. for (last = value + len; value != last; ++value) {
  401. if (!VALID_PATH_CHARS[*value]) {
  402. return 0;
  403. }
  404. }
  405. return 1;
  406. }
  407. static int http_request_on_header(nghttp3_http_state *http,
  408. nghttp3_qpack_nv *nv, int trailers,
  409. int connect_protocol) {
  410. nghttp3_pri pri;
  411. switch (nv->token) {
  412. case NGHTTP3_QPACK_TOKEN__AUTHORITY:
  413. if (!check_authority(nv->value->base, nv->value->len) ||
  414. !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__AUTHORITY)) {
  415. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  416. }
  417. break;
  418. case NGHTTP3_QPACK_TOKEN__METHOD:
  419. if (!check_method(nv->value->base, nv->value->len) ||
  420. !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__METHOD)) {
  421. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  422. }
  423. switch (nv->value->len) {
  424. case 4:
  425. if (lstreq("HEAD", nv->value->base, nv->value->len)) {
  426. http->flags |= NGHTTP3_HTTP_FLAG_METH_HEAD;
  427. }
  428. break;
  429. case 7:
  430. switch (nv->value->base[6]) {
  431. case 'T':
  432. if (lstreq("CONNECT", nv->value->base, nv->value->len)) {
  433. http->flags |= NGHTTP3_HTTP_FLAG_METH_CONNECT;
  434. }
  435. break;
  436. case 'S':
  437. if (lstreq("OPTIONS", nv->value->base, nv->value->len)) {
  438. http->flags |= NGHTTP3_HTTP_FLAG_METH_OPTIONS;
  439. }
  440. break;
  441. }
  442. break;
  443. }
  444. break;
  445. case NGHTTP3_QPACK_TOKEN__PATH:
  446. if (!check_path(nv->value->base, nv->value->len) ||
  447. !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__PATH)) {
  448. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  449. }
  450. if (nv->value->base[0] == '/') {
  451. http->flags |= NGHTTP3_HTTP_FLAG_PATH_REGULAR;
  452. } else if (nv->value->len == 1 && nv->value->base[0] == '*') {
  453. http->flags |= NGHTTP3_HTTP_FLAG_PATH_ASTERISK;
  454. }
  455. break;
  456. case NGHTTP3_QPACK_TOKEN__SCHEME:
  457. if (!check_scheme(nv->value->base, nv->value->len) ||
  458. !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__SCHEME)) {
  459. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  460. }
  461. /* scheme is case-insensitive:
  462. https://datatracker.ietf.org/doc/html/rfc3986#section-3.1 */
  463. if (lstrieq("http", nv->value->base, nv->value->len) ||
  464. lstrieq("https", nv->value->base, nv->value->len)) {
  465. http->flags |= NGHTTP3_HTTP_FLAG_SCHEME_HTTP;
  466. }
  467. break;
  468. case NGHTTP3_QPACK_TOKEN__PROTOCOL:
  469. if (!connect_protocol ||
  470. !nghttp3_check_header_value(nv->value->base, nv->value->len) ||
  471. !check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__PROTOCOL)) {
  472. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  473. }
  474. break;
  475. case NGHTTP3_QPACK_TOKEN_HOST:
  476. if (!check_authority(nv->value->base, nv->value->len)) {
  477. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  478. }
  479. if (!check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG_HOST)) {
  480. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  481. }
  482. break;
  483. case NGHTTP3_QPACK_TOKEN_CONTENT_LENGTH: {
  484. /* https://tools.ietf.org/html/rfc7230#section-4.1.2: A sender
  485. MUST NOT generate a trailer that contains a field necessary for
  486. message framing (e.g., Transfer-Encoding and Content-Length),
  487. ... */
  488. if (trailers) {
  489. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  490. }
  491. if (http->content_length != -1) {
  492. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  493. }
  494. http->content_length = parse_uint(nv->value->base, nv->value->len);
  495. if (http->content_length == -1) {
  496. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  497. }
  498. break;
  499. }
  500. /* disallowed header fields */
  501. case NGHTTP3_QPACK_TOKEN_CONNECTION:
  502. case NGHTTP3_QPACK_TOKEN_KEEP_ALIVE:
  503. case NGHTTP3_QPACK_TOKEN_PROXY_CONNECTION:
  504. case NGHTTP3_QPACK_TOKEN_TRANSFER_ENCODING:
  505. case NGHTTP3_QPACK_TOKEN_UPGRADE:
  506. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  507. case NGHTTP3_QPACK_TOKEN_TE:
  508. if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
  509. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  510. }
  511. break;
  512. case NGHTTP3_QPACK_TOKEN_PRIORITY:
  513. if (!nghttp3_check_header_value(nv->value->base, nv->value->len)) {
  514. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  515. }
  516. if (trailers || (http->flags & NGHTTP3_HTTP_FLAG_BAD_PRIORITY)) {
  517. break;
  518. }
  519. pri = http->pri;
  520. if (nghttp3_http_parse_priority(&pri, nv->value->base, nv->value->len) ==
  521. 0) {
  522. http->pri = pri;
  523. http->flags |= NGHTTP3_HTTP_FLAG_PRIORITY;
  524. break;
  525. }
  526. http->flags &= ~NGHTTP3_HTTP_FLAG_PRIORITY;
  527. http->flags |= NGHTTP3_HTTP_FLAG_BAD_PRIORITY;
  528. break;
  529. default:
  530. if (nv->name->base[0] == ':') {
  531. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  532. }
  533. if (!nghttp3_check_header_value(nv->value->base, nv->value->len)) {
  534. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  535. }
  536. }
  537. return 0;
  538. }
  539. static int http_response_on_header(nghttp3_http_state *http,
  540. nghttp3_qpack_nv *nv, int trailers) {
  541. switch (nv->token) {
  542. case NGHTTP3_QPACK_TOKEN__STATUS: {
  543. if (!check_pseudo_header(http, nv, NGHTTP3_HTTP_FLAG__STATUS) ||
  544. nv->value->len != 3) {
  545. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  546. }
  547. http->status_code = (int16_t)parse_uint(nv->value->base, nv->value->len);
  548. if (http->status_code < 100 || http->status_code == 101) {
  549. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  550. }
  551. break;
  552. }
  553. case NGHTTP3_QPACK_TOKEN_CONTENT_LENGTH: {
  554. /* https://tools.ietf.org/html/rfc7230#section-4.1.2: A sender
  555. MUST NOT generate a trailer that contains a field necessary for
  556. message framing (e.g., Transfer-Encoding and Content-Length),
  557. ... */
  558. if (trailers) {
  559. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  560. }
  561. if (http->status_code == 204) {
  562. /* content-length header field in 204 response is prohibited by
  563. RFC 7230. But some widely used servers send content-length:
  564. 0. Until they get fixed, we ignore it. */
  565. if (/* Found multiple content-length field */
  566. http->content_length != -1 ||
  567. !lstrieq("0", nv->value->base, nv->value->len)) {
  568. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  569. }
  570. http->content_length = 0;
  571. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  572. }
  573. if (http->status_code / 100 == 1 ||
  574. /* https://tools.ietf.org/html/rfc7230#section-3.3.3 */
  575. (http->status_code / 100 == 2 &&
  576. (http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT))) {
  577. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  578. }
  579. if (http->content_length != -1) {
  580. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  581. }
  582. http->content_length = parse_uint(nv->value->base, nv->value->len);
  583. if (http->content_length == -1) {
  584. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  585. }
  586. break;
  587. }
  588. /* disallowed header fields */
  589. case NGHTTP3_QPACK_TOKEN_CONNECTION:
  590. case NGHTTP3_QPACK_TOKEN_KEEP_ALIVE:
  591. case NGHTTP3_QPACK_TOKEN_PROXY_CONNECTION:
  592. case NGHTTP3_QPACK_TOKEN_TRANSFER_ENCODING:
  593. case NGHTTP3_QPACK_TOKEN_UPGRADE:
  594. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  595. case NGHTTP3_QPACK_TOKEN_TE:
  596. if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
  597. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  598. }
  599. break;
  600. default:
  601. if (nv->name->base[0] == ':') {
  602. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  603. }
  604. if (!nghttp3_check_header_value(nv->value->base, nv->value->len)) {
  605. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  606. }
  607. }
  608. return 0;
  609. }
  610. static int http_check_nonempty_header_name(const uint8_t *name, size_t len);
  611. int nghttp3_http_on_header(nghttp3_http_state *http, nghttp3_qpack_nv *nv,
  612. int request, int trailers, int connect_protocol) {
  613. if (nv->name->len == 0) {
  614. http->flags |= NGHTTP3_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
  615. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  616. }
  617. if (nv->name->base[0] == ':') {
  618. /* pseudo header must have a valid token. */
  619. if (nv->token == -1 || trailers ||
  620. (http->flags & NGHTTP3_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED)) {
  621. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  622. }
  623. } else {
  624. http->flags |= NGHTTP3_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
  625. switch (http_check_nonempty_header_name(nv->name->base, nv->name->len)) {
  626. case 0:
  627. return NGHTTP3_ERR_REMOVE_HTTP_HEADER;
  628. case -1:
  629. /* header field name must be lower-cased without exception */
  630. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  631. }
  632. }
  633. assert(nv->name->len > 0);
  634. if (request) {
  635. return http_request_on_header(http, nv, trailers, connect_protocol);
  636. }
  637. return http_response_on_header(http, nv, trailers);
  638. }
  639. int nghttp3_http_on_request_headers(nghttp3_http_state *http) {
  640. if (!(http->flags & NGHTTP3_HTTP_FLAG__PROTOCOL) &&
  641. (http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT)) {
  642. if ((http->flags & (NGHTTP3_HTTP_FLAG__SCHEME | NGHTTP3_HTTP_FLAG__PATH)) ||
  643. (http->flags & NGHTTP3_HTTP_FLAG__AUTHORITY) == 0) {
  644. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  645. }
  646. http->content_length = -1;
  647. } else {
  648. if ((http->flags & NGHTTP3_HTTP_FLAG_REQ_HEADERS) !=
  649. NGHTTP3_HTTP_FLAG_REQ_HEADERS ||
  650. (http->flags &
  651. (NGHTTP3_HTTP_FLAG__AUTHORITY | NGHTTP3_HTTP_FLAG_HOST)) == 0) {
  652. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  653. }
  654. if ((http->flags & NGHTTP3_HTTP_FLAG__PROTOCOL) &&
  655. ((http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT) == 0 ||
  656. (http->flags & NGHTTP3_HTTP_FLAG__AUTHORITY) == 0)) {
  657. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  658. }
  659. if (!check_path_flags(http)) {
  660. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  661. }
  662. }
  663. return 0;
  664. }
  665. int nghttp3_http_on_response_headers(nghttp3_http_state *http) {
  666. if ((http->flags & NGHTTP3_HTTP_FLAG__STATUS) == 0) {
  667. return NGHTTP3_ERR_MALFORMED_HTTP_HEADER;
  668. }
  669. if (http->status_code / 100 == 1) {
  670. /* non-final response */
  671. http->flags = (http->flags & NGHTTP3_HTTP_FLAG_METH_ALL) |
  672. NGHTTP3_HTTP_FLAG_EXPECT_FINAL_RESPONSE;
  673. http->content_length = -1;
  674. http->status_code = -1;
  675. return 0;
  676. }
  677. http->flags &= ~NGHTTP3_HTTP_FLAG_EXPECT_FINAL_RESPONSE;
  678. if (!expect_response_body(http)) {
  679. http->content_length = 0;
  680. } else if (http->flags & NGHTTP3_HTTP_FLAG_METH_CONNECT) {
  681. http->content_length = -1;
  682. }
  683. return 0;
  684. }
  685. int nghttp3_http_on_remote_end_stream(nghttp3_stream *stream) {
  686. if ((stream->rx.http.flags & NGHTTP3_HTTP_FLAG_EXPECT_FINAL_RESPONSE) ||
  687. (stream->rx.http.content_length != -1 &&
  688. stream->rx.http.content_length != stream->rx.http.recv_content_length)) {
  689. return NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING;
  690. }
  691. return 0;
  692. }
  693. int nghttp3_http_on_data_chunk(nghttp3_stream *stream, size_t n) {
  694. stream->rx.http.recv_content_length += (int64_t)n;
  695. if ((stream->rx.http.flags & NGHTTP3_HTTP_FLAG_EXPECT_FINAL_RESPONSE) ||
  696. (stream->rx.http.content_length != -1 &&
  697. stream->rx.http.recv_content_length > stream->rx.http.content_length)) {
  698. return NGHTTP3_ERR_MALFORMED_HTTP_MESSAGING;
  699. }
  700. return 0;
  701. }
  702. void nghttp3_http_record_request_method(nghttp3_stream *stream,
  703. const nghttp3_nv *nva, size_t nvlen) {
  704. size_t i;
  705. const nghttp3_nv *nv;
  706. /* TODO we should do this strictly. */
  707. for (i = 0; i < nvlen; ++i) {
  708. nv = &nva[i];
  709. if (!(nv->namelen == 7 && nv->name[6] == 'd' &&
  710. memcmp(":metho", nv->name, nv->namelen - 1) == 0)) {
  711. continue;
  712. }
  713. if (lstreq("CONNECT", nv->value, nv->valuelen)) {
  714. stream->rx.http.flags |= NGHTTP3_HTTP_FLAG_METH_CONNECT;
  715. return;
  716. }
  717. if (lstreq("HEAD", nv->value, nv->valuelen)) {
  718. stream->rx.http.flags |= NGHTTP3_HTTP_FLAG_METH_HEAD;
  719. return;
  720. }
  721. return;
  722. }
  723. }
  724. /* Generated by gennmchartbl.py */
  725. static const int VALID_HD_NAME_CHARS[] = {
  726. 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */, 0 /* EOT */,
  727. 0 /* ENQ */, 0 /* ACK */, 0 /* BEL */, 0 /* BS */, 0 /* HT */,
  728. 0 /* LF */, 0 /* VT */, 0 /* FF */, 0 /* CR */, 0 /* SO */,
  729. 0 /* SI */, 0 /* DLE */, 0 /* DC1 */, 0 /* DC2 */, 0 /* DC3 */,
  730. 0 /* DC4 */, 0 /* NAK */, 0 /* SYN */, 0 /* ETB */, 0 /* CAN */,
  731. 0 /* EM */, 0 /* SUB */, 0 /* ESC */, 0 /* FS */, 0 /* GS */,
  732. 0 /* RS */, 0 /* US */, 0 /* SPC */, 1 /* ! */, 0 /* " */,
  733. 1 /* # */, 1 /* $ */, 1 /* % */, 1 /* & */, 1 /* ' */,
  734. 0 /* ( */, 0 /* ) */, 1 /* * */, 1 /* + */, 0 /* , */,
  735. 1 /* - */, 1 /* . */, 0 /* / */, 1 /* 0 */, 1 /* 1 */,
  736. 1 /* 2 */, 1 /* 3 */, 1 /* 4 */, 1 /* 5 */, 1 /* 6 */,
  737. 1 /* 7 */, 1 /* 8 */, 1 /* 9 */, 0 /* : */, 0 /* ; */,
  738. 0 /* < */, 0 /* = */, 0 /* > */, 0 /* ? */, 0 /* @ */,
  739. -1 /* A */, -1 /* B */, -1 /* C */, -1 /* D */, -1 /* E */,
  740. -1 /* F */, -1 /* G */, -1 /* H */, -1 /* I */, -1 /* J */,
  741. -1 /* K */, -1 /* L */, -1 /* M */, -1 /* N */, -1 /* O */,
  742. -1 /* P */, -1 /* Q */, -1 /* R */, -1 /* S */, -1 /* T */,
  743. -1 /* U */, -1 /* V */, -1 /* W */, -1 /* X */, -1 /* Y */,
  744. -1 /* Z */, 0 /* [ */, 0 /* \ */, 0 /* ] */, 1 /* ^ */,
  745. 1 /* _ */, 1 /* ` */, 1 /* a */, 1 /* b */, 1 /* c */,
  746. 1 /* d */, 1 /* e */, 1 /* f */, 1 /* g */, 1 /* h */,
  747. 1 /* i */, 1 /* j */, 1 /* k */, 1 /* l */, 1 /* m */,
  748. 1 /* n */, 1 /* o */, 1 /* p */, 1 /* q */, 1 /* r */,
  749. 1 /* s */, 1 /* t */, 1 /* u */, 1 /* v */, 1 /* w */,
  750. 1 /* x */, 1 /* y */, 1 /* z */, 0 /* { */, 1 /* | */,
  751. 0 /* } */, 1 /* ~ */, 0 /* DEL */, 0 /* 0x80 */, 0 /* 0x81 */,
  752. 0 /* 0x82 */, 0 /* 0x83 */, 0 /* 0x84 */, 0 /* 0x85 */, 0 /* 0x86 */,
  753. 0 /* 0x87 */, 0 /* 0x88 */, 0 /* 0x89 */, 0 /* 0x8a */, 0 /* 0x8b */,
  754. 0 /* 0x8c */, 0 /* 0x8d */, 0 /* 0x8e */, 0 /* 0x8f */, 0 /* 0x90 */,
  755. 0 /* 0x91 */, 0 /* 0x92 */, 0 /* 0x93 */, 0 /* 0x94 */, 0 /* 0x95 */,
  756. 0 /* 0x96 */, 0 /* 0x97 */, 0 /* 0x98 */, 0 /* 0x99 */, 0 /* 0x9a */,
  757. 0 /* 0x9b */, 0 /* 0x9c */, 0 /* 0x9d */, 0 /* 0x9e */, 0 /* 0x9f */,
  758. 0 /* 0xa0 */, 0 /* 0xa1 */, 0 /* 0xa2 */, 0 /* 0xa3 */, 0 /* 0xa4 */,
  759. 0 /* 0xa5 */, 0 /* 0xa6 */, 0 /* 0xa7 */, 0 /* 0xa8 */, 0 /* 0xa9 */,
  760. 0 /* 0xaa */, 0 /* 0xab */, 0 /* 0xac */, 0 /* 0xad */, 0 /* 0xae */,
  761. 0 /* 0xaf */, 0 /* 0xb0 */, 0 /* 0xb1 */, 0 /* 0xb2 */, 0 /* 0xb3 */,
  762. 0 /* 0xb4 */, 0 /* 0xb5 */, 0 /* 0xb6 */, 0 /* 0xb7 */, 0 /* 0xb8 */,
  763. 0 /* 0xb9 */, 0 /* 0xba */, 0 /* 0xbb */, 0 /* 0xbc */, 0 /* 0xbd */,
  764. 0 /* 0xbe */, 0 /* 0xbf */, 0 /* 0xc0 */, 0 /* 0xc1 */, 0 /* 0xc2 */,
  765. 0 /* 0xc3 */, 0 /* 0xc4 */, 0 /* 0xc5 */, 0 /* 0xc6 */, 0 /* 0xc7 */,
  766. 0 /* 0xc8 */, 0 /* 0xc9 */, 0 /* 0xca */, 0 /* 0xcb */, 0 /* 0xcc */,
  767. 0 /* 0xcd */, 0 /* 0xce */, 0 /* 0xcf */, 0 /* 0xd0 */, 0 /* 0xd1 */,
  768. 0 /* 0xd2 */, 0 /* 0xd3 */, 0 /* 0xd4 */, 0 /* 0xd5 */, 0 /* 0xd6 */,
  769. 0 /* 0xd7 */, 0 /* 0xd8 */, 0 /* 0xd9 */, 0 /* 0xda */, 0 /* 0xdb */,
  770. 0 /* 0xdc */, 0 /* 0xdd */, 0 /* 0xde */, 0 /* 0xdf */, 0 /* 0xe0 */,
  771. 0 /* 0xe1 */, 0 /* 0xe2 */, 0 /* 0xe3 */, 0 /* 0xe4 */, 0 /* 0xe5 */,
  772. 0 /* 0xe6 */, 0 /* 0xe7 */, 0 /* 0xe8 */, 0 /* 0xe9 */, 0 /* 0xea */,
  773. 0 /* 0xeb */, 0 /* 0xec */, 0 /* 0xed */, 0 /* 0xee */, 0 /* 0xef */,
  774. 0 /* 0xf0 */, 0 /* 0xf1 */, 0 /* 0xf2 */, 0 /* 0xf3 */, 0 /* 0xf4 */,
  775. 0 /* 0xf5 */, 0 /* 0xf6 */, 0 /* 0xf7 */, 0 /* 0xf8 */, 0 /* 0xf9 */,
  776. 0 /* 0xfa */, 0 /* 0xfb */, 0 /* 0xfc */, 0 /* 0xfd */, 0 /* 0xfe */,
  777. 0 /* 0xff */,
  778. };
  779. int nghttp3_check_header_name(const uint8_t *name, size_t len) {
  780. const uint8_t *last;
  781. if (len == 0) {
  782. return 0;
  783. }
  784. if (*name == ':') {
  785. if (len == 1) {
  786. return 0;
  787. }
  788. ++name;
  789. --len;
  790. }
  791. for (last = name + len; name != last; ++name) {
  792. if (!VALID_HD_NAME_CHARS[*name]) {
  793. return 0;
  794. }
  795. }
  796. return 1;
  797. }
  798. /* http_check_nonempty_header_name validates regular header name
  799. pointed by |name| of length |len|. |len| must be greater than
  800. zero. This function returns 1 if it succeeds, or -1 if the name
  801. contains a character in [A-Z], otherwise 0. */
  802. static int http_check_nonempty_header_name(const uint8_t *name, size_t len) {
  803. const uint8_t *last;
  804. int rv;
  805. for (last = name + len; name != last; ++name) {
  806. rv = VALID_HD_NAME_CHARS[*name];
  807. if (rv != 1) {
  808. return rv;
  809. }
  810. }
  811. return 1;
  812. }
  813. /* Generated by genvchartbl.py */
  814. static const int VALID_HD_VALUE_CHARS[] = {
  815. 0 /* NUL */, 0 /* SOH */, 0 /* STX */, 0 /* ETX */,
  816. 0 /* EOT */, 0 /* ENQ */, 0 /* ACK */, 0 /* BEL */,
  817. 0 /* BS */, 1 /* HT */, 0 /* LF */, 0 /* VT */,
  818. 0 /* FF */, 0 /* CR */, 0 /* SO */, 0 /* SI */,
  819. 0 /* DLE */, 0 /* DC1 */, 0 /* DC2 */, 0 /* DC3 */,
  820. 0 /* DC4 */, 0 /* NAK */, 0 /* SYN */, 0 /* ETB */,
  821. 0 /* CAN */, 0 /* EM */, 0 /* SUB */, 0 /* ESC */,
  822. 0 /* FS */, 0 /* GS */, 0 /* RS */, 0 /* US */,
  823. 1 /* SPC */, 1 /* ! */, 1 /* " */, 1 /* # */,
  824. 1 /* $ */, 1 /* % */, 1 /* & */, 1 /* ' */,
  825. 1 /* ( */, 1 /* ) */, 1 /* * */, 1 /* + */,
  826. 1 /* , */, 1 /* - */, 1 /* . */, 1 /* / */,
  827. 1 /* 0 */, 1 /* 1 */, 1 /* 2 */, 1 /* 3 */,
  828. 1 /* 4 */, 1 /* 5 */, 1 /* 6 */, 1 /* 7 */,
  829. 1 /* 8 */, 1 /* 9 */, 1 /* : */, 1 /* ; */,
  830. 1 /* < */, 1 /* = */, 1 /* > */, 1 /* ? */,
  831. 1 /* @ */, 1 /* A */, 1 /* B */, 1 /* C */,
  832. 1 /* D */, 1 /* E */, 1 /* F */, 1 /* G */,
  833. 1 /* H */, 1 /* I */, 1 /* J */, 1 /* K */,
  834. 1 /* L */, 1 /* M */, 1 /* N */, 1 /* O */,
  835. 1 /* P */, 1 /* Q */, 1 /* R */, 1 /* S */,
  836. 1 /* T */, 1 /* U */, 1 /* V */, 1 /* W */,
  837. 1 /* X */, 1 /* Y */, 1 /* Z */, 1 /* [ */,
  838. 1 /* \ */, 1 /* ] */, 1 /* ^ */, 1 /* _ */,
  839. 1 /* ` */, 1 /* a */, 1 /* b */, 1 /* c */,
  840. 1 /* d */, 1 /* e */, 1 /* f */, 1 /* g */,
  841. 1 /* h */, 1 /* i */, 1 /* j */, 1 /* k */,
  842. 1 /* l */, 1 /* m */, 1 /* n */, 1 /* o */,
  843. 1 /* p */, 1 /* q */, 1 /* r */, 1 /* s */,
  844. 1 /* t */, 1 /* u */, 1 /* v */, 1 /* w */,
  845. 1 /* x */, 1 /* y */, 1 /* z */, 1 /* { */,
  846. 1 /* | */, 1 /* } */, 1 /* ~ */, 0 /* DEL */,
  847. 1 /* 0x80 */, 1 /* 0x81 */, 1 /* 0x82 */, 1 /* 0x83 */,
  848. 1 /* 0x84 */, 1 /* 0x85 */, 1 /* 0x86 */, 1 /* 0x87 */,
  849. 1 /* 0x88 */, 1 /* 0x89 */, 1 /* 0x8a */, 1 /* 0x8b */,
  850. 1 /* 0x8c */, 1 /* 0x8d */, 1 /* 0x8e */, 1 /* 0x8f */,
  851. 1 /* 0x90 */, 1 /* 0x91 */, 1 /* 0x92 */, 1 /* 0x93 */,
  852. 1 /* 0x94 */, 1 /* 0x95 */, 1 /* 0x96 */, 1 /* 0x97 */,
  853. 1 /* 0x98 */, 1 /* 0x99 */, 1 /* 0x9a */, 1 /* 0x9b */,
  854. 1 /* 0x9c */, 1 /* 0x9d */, 1 /* 0x9e */, 1 /* 0x9f */,
  855. 1 /* 0xa0 */, 1 /* 0xa1 */, 1 /* 0xa2 */, 1 /* 0xa3 */,
  856. 1 /* 0xa4 */, 1 /* 0xa5 */, 1 /* 0xa6 */, 1 /* 0xa7 */,
  857. 1 /* 0xa8 */, 1 /* 0xa9 */, 1 /* 0xaa */, 1 /* 0xab */,
  858. 1 /* 0xac */, 1 /* 0xad */, 1 /* 0xae */, 1 /* 0xaf */,
  859. 1 /* 0xb0 */, 1 /* 0xb1 */, 1 /* 0xb2 */, 1 /* 0xb3 */,
  860. 1 /* 0xb4 */, 1 /* 0xb5 */, 1 /* 0xb6 */, 1 /* 0xb7 */,
  861. 1 /* 0xb8 */, 1 /* 0xb9 */, 1 /* 0xba */, 1 /* 0xbb */,
  862. 1 /* 0xbc */, 1 /* 0xbd */, 1 /* 0xbe */, 1 /* 0xbf */,
  863. 1 /* 0xc0 */, 1 /* 0xc1 */, 1 /* 0xc2 */, 1 /* 0xc3 */,
  864. 1 /* 0xc4 */, 1 /* 0xc5 */, 1 /* 0xc6 */, 1 /* 0xc7 */,
  865. 1 /* 0xc8 */, 1 /* 0xc9 */, 1 /* 0xca */, 1 /* 0xcb */,
  866. 1 /* 0xcc */, 1 /* 0xcd */, 1 /* 0xce */, 1 /* 0xcf */,
  867. 1 /* 0xd0 */, 1 /* 0xd1 */, 1 /* 0xd2 */, 1 /* 0xd3 */,
  868. 1 /* 0xd4 */, 1 /* 0xd5 */, 1 /* 0xd6 */, 1 /* 0xd7 */,
  869. 1 /* 0xd8 */, 1 /* 0xd9 */, 1 /* 0xda */, 1 /* 0xdb */,
  870. 1 /* 0xdc */, 1 /* 0xdd */, 1 /* 0xde */, 1 /* 0xdf */,
  871. 1 /* 0xe0 */, 1 /* 0xe1 */, 1 /* 0xe2 */, 1 /* 0xe3 */,
  872. 1 /* 0xe4 */, 1 /* 0xe5 */, 1 /* 0xe6 */, 1 /* 0xe7 */,
  873. 1 /* 0xe8 */, 1 /* 0xe9 */, 1 /* 0xea */, 1 /* 0xeb */,
  874. 1 /* 0xec */, 1 /* 0xed */, 1 /* 0xee */, 1 /* 0xef */,
  875. 1 /* 0xf0 */, 1 /* 0xf1 */, 1 /* 0xf2 */, 1 /* 0xf3 */,
  876. 1 /* 0xf4 */, 1 /* 0xf5 */, 1 /* 0xf6 */, 1 /* 0xf7 */,
  877. 1 /* 0xf8 */, 1 /* 0xf9 */, 1 /* 0xfa */, 1 /* 0xfb */,
  878. 1 /* 0xfc */, 1 /* 0xfd */, 1 /* 0xfe */, 1 /* 0xff */
  879. };
  880. #ifdef __AVX2__
  881. static int contains_bad_header_value_char_avx2(const uint8_t *first,
  882. const uint8_t *last) {
  883. const __m256i ctll = _mm256_set1_epi8(0x00 - 1);
  884. const __m256i ctlr = _mm256_set1_epi8(0x1f + 1);
  885. const __m256i ht = _mm256_set1_epi8('\t');
  886. const __m256i del = _mm256_set1_epi8(0x7f);
  887. __m256i s, x;
  888. uint32_t m;
  889. for (; first != last; first += 32) {
  890. s = _mm256_loadu_si256((void *)first);
  891. x = _mm256_andnot_si256(
  892. _mm256_cmpeq_epi8(s, ht),
  893. _mm256_and_si256(_mm256_cmpgt_epi8(s, ctll), _mm256_cmpgt_epi8(ctlr, s)));
  894. x = _mm256_or_si256(_mm256_cmpeq_epi8(s, del), x);
  895. m = (uint32_t)_mm256_movemask_epi8(x);
  896. if (m) {
  897. return 1;
  898. }
  899. }
  900. return 0;
  901. }
  902. #endif /* __AVX2__ */
  903. int nghttp3_check_header_value(const uint8_t *value, size_t len) {
  904. const uint8_t *last;
  905. #ifdef __AVX2__
  906. const uint8_t *last32;
  907. #endif /* __AVX2__ */
  908. switch (len) {
  909. case 0:
  910. return 1;
  911. case 1:
  912. return !is_ws(*value);
  913. default:
  914. if (is_ws(*value) || is_ws(*(value + len - 1))) {
  915. return 0;
  916. }
  917. }
  918. last = value + len;
  919. #ifdef __AVX2__
  920. if (len >= 32) {
  921. last32 = value + (len & ~0x1fu);
  922. if (contains_bad_header_value_char_avx2(value, last32)) {
  923. return 0;
  924. }
  925. value = last32;
  926. }
  927. #endif /* __AVX2__ */
  928. for (; value != last; ++value) {
  929. if (!VALID_HD_VALUE_CHARS[*value]) {
  930. return 0;
  931. }
  932. }
  933. return 1;
  934. }
  935. int nghttp3_pri_eq(const nghttp3_pri *a, const nghttp3_pri *b) {
  936. return a->urgency == b->urgency && a->inc == b->inc;
  937. }