nghttp2_http.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2015 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 "nghttp2_http.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <stdio.h>
  29. #include "nghttp2_hd.h"
  30. #include "nghttp2_helper.h"
  31. #include "nghttp2_extpri.h"
  32. #include "sfparse.h"
  33. static uint8_t downcase(uint8_t c) {
  34. return 'A' <= c && c <= 'Z' ? (uint8_t)(c - 'A' + 'a') : c;
  35. }
  36. static int memieq(const void *a, const void *b, size_t n) {
  37. size_t i;
  38. const uint8_t *aa = a, *bb = b;
  39. for (i = 0; i < n; ++i) {
  40. if (downcase(aa[i]) != downcase(bb[i])) {
  41. return 0;
  42. }
  43. }
  44. return 1;
  45. }
  46. #define lstrieq(A, B, N) ((sizeof((A)) - 1) == (N) && memieq((A), (B), (N)))
  47. static int64_t parse_uint(const uint8_t *s, size_t len) {
  48. int64_t n = 0;
  49. size_t i;
  50. if (len == 0) {
  51. return -1;
  52. }
  53. for (i = 0; i < len; ++i) {
  54. if ('0' <= s[i] && s[i] <= '9') {
  55. if (n > INT64_MAX / 10) {
  56. return -1;
  57. }
  58. n *= 10;
  59. if (n > INT64_MAX - (s[i] - '0')) {
  60. return -1;
  61. }
  62. n += s[i] - '0';
  63. continue;
  64. }
  65. return -1;
  66. }
  67. return n;
  68. }
  69. static int check_pseudo_header(nghttp2_stream *stream, const nghttp2_hd_nv *nv,
  70. uint32_t flag) {
  71. if ((stream->http_flags & flag) || nv->value->len == 0) {
  72. return 0;
  73. }
  74. stream->http_flags = stream->http_flags | flag;
  75. return 1;
  76. }
  77. static int expect_response_body(nghttp2_stream *stream) {
  78. return (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_HEAD) == 0 &&
  79. stream->status_code / 100 != 1 && stream->status_code != 304 &&
  80. stream->status_code != 204;
  81. }
  82. /* For "http" or "https" URIs, OPTIONS request may have "*" in :path
  83. header field to represent system-wide OPTIONS request. Otherwise,
  84. :path header field value must start with "/". This function must
  85. be called after ":method" header field was received. This function
  86. returns nonzero if path is valid.*/
  87. static int check_path(nghttp2_stream *stream) {
  88. return (stream->http_flags & NGHTTP2_HTTP_FLAG_SCHEME_HTTP) == 0 ||
  89. ((stream->http_flags & NGHTTP2_HTTP_FLAG_PATH_REGULAR) ||
  90. ((stream->http_flags & NGHTTP2_HTTP_FLAG_METH_OPTIONS) &&
  91. (stream->http_flags & NGHTTP2_HTTP_FLAG_PATH_ASTERISK)));
  92. }
  93. static int http_request_on_header(nghttp2_stream *stream, nghttp2_hd_nv *nv,
  94. int trailer, int connect_protocol) {
  95. nghttp2_extpri extpri;
  96. if (nv->name->base[0] == ':') {
  97. if (trailer ||
  98. (stream->http_flags & NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED)) {
  99. return NGHTTP2_ERR_HTTP_HEADER;
  100. }
  101. }
  102. switch (nv->token) {
  103. case NGHTTP2_TOKEN__AUTHORITY:
  104. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__AUTHORITY)) {
  105. return NGHTTP2_ERR_HTTP_HEADER;
  106. }
  107. break;
  108. case NGHTTP2_TOKEN__METHOD:
  109. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__METHOD)) {
  110. return NGHTTP2_ERR_HTTP_HEADER;
  111. }
  112. switch (nv->value->len) {
  113. case 4:
  114. if (lstreq("HEAD", nv->value->base, nv->value->len)) {
  115. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
  116. }
  117. break;
  118. case 7:
  119. switch (nv->value->base[6]) {
  120. case 'T':
  121. if (lstreq("CONNECT", nv->value->base, nv->value->len)) {
  122. if (stream->stream_id % 2 == 0) {
  123. /* we won't allow CONNECT for push */
  124. return NGHTTP2_ERR_HTTP_HEADER;
  125. }
  126. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_CONNECT;
  127. }
  128. break;
  129. case 'S':
  130. if (lstreq("OPTIONS", nv->value->base, nv->value->len)) {
  131. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_OPTIONS;
  132. }
  133. break;
  134. }
  135. break;
  136. }
  137. break;
  138. case NGHTTP2_TOKEN__PATH:
  139. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__PATH)) {
  140. return NGHTTP2_ERR_HTTP_HEADER;
  141. }
  142. if (nv->value->base[0] == '/') {
  143. stream->http_flags |= NGHTTP2_HTTP_FLAG_PATH_REGULAR;
  144. } else if (nv->value->len == 1 && nv->value->base[0] == '*') {
  145. stream->http_flags |= NGHTTP2_HTTP_FLAG_PATH_ASTERISK;
  146. }
  147. break;
  148. case NGHTTP2_TOKEN__SCHEME:
  149. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__SCHEME)) {
  150. return NGHTTP2_ERR_HTTP_HEADER;
  151. }
  152. if ((nv->value->len == 4 && memieq("http", nv->value->base, 4)) ||
  153. (nv->value->len == 5 && memieq("https", nv->value->base, 5))) {
  154. stream->http_flags |= NGHTTP2_HTTP_FLAG_SCHEME_HTTP;
  155. }
  156. break;
  157. case NGHTTP2_TOKEN__PROTOCOL:
  158. if (!connect_protocol) {
  159. return NGHTTP2_ERR_HTTP_HEADER;
  160. }
  161. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__PROTOCOL)) {
  162. return NGHTTP2_ERR_HTTP_HEADER;
  163. }
  164. break;
  165. case NGHTTP2_TOKEN_HOST:
  166. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG_HOST)) {
  167. return NGHTTP2_ERR_HTTP_HEADER;
  168. }
  169. break;
  170. case NGHTTP2_TOKEN_CONTENT_LENGTH: {
  171. if (stream->content_length != -1) {
  172. return NGHTTP2_ERR_HTTP_HEADER;
  173. }
  174. stream->content_length = parse_uint(nv->value->base, nv->value->len);
  175. if (stream->content_length == -1) {
  176. return NGHTTP2_ERR_HTTP_HEADER;
  177. }
  178. break;
  179. }
  180. /* disallowed header fields */
  181. case NGHTTP2_TOKEN_CONNECTION:
  182. case NGHTTP2_TOKEN_KEEP_ALIVE:
  183. case NGHTTP2_TOKEN_PROXY_CONNECTION:
  184. case NGHTTP2_TOKEN_TRANSFER_ENCODING:
  185. case NGHTTP2_TOKEN_UPGRADE:
  186. return NGHTTP2_ERR_HTTP_HEADER;
  187. case NGHTTP2_TOKEN_TE:
  188. if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
  189. return NGHTTP2_ERR_HTTP_HEADER;
  190. }
  191. break;
  192. case NGHTTP2_TOKEN_PRIORITY:
  193. if (!trailer &&
  194. /* Do not parse the header field in PUSH_PROMISE. */
  195. (stream->stream_id & 1) &&
  196. (stream->flags & NGHTTP2_STREAM_FLAG_NO_RFC7540_PRIORITIES) &&
  197. !(stream->http_flags & NGHTTP2_HTTP_FLAG_BAD_PRIORITY)) {
  198. nghttp2_extpri_from_uint8(&extpri, stream->http_extpri);
  199. if (nghttp2_http_parse_priority(&extpri, nv->value->base,
  200. nv->value->len) == 0) {
  201. stream->http_extpri = nghttp2_extpri_to_uint8(&extpri);
  202. stream->http_flags |= NGHTTP2_HTTP_FLAG_PRIORITY;
  203. } else {
  204. stream->http_flags &= (uint32_t)~NGHTTP2_HTTP_FLAG_PRIORITY;
  205. stream->http_flags |= NGHTTP2_HTTP_FLAG_BAD_PRIORITY;
  206. }
  207. }
  208. break;
  209. default:
  210. if (nv->name->base[0] == ':') {
  211. return NGHTTP2_ERR_HTTP_HEADER;
  212. }
  213. }
  214. if (nv->name->base[0] != ':') {
  215. stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
  216. }
  217. return 0;
  218. }
  219. static int http_response_on_header(nghttp2_stream *stream, nghttp2_hd_nv *nv,
  220. int trailer) {
  221. if (nv->name->base[0] == ':') {
  222. if (trailer ||
  223. (stream->http_flags & NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED)) {
  224. return NGHTTP2_ERR_HTTP_HEADER;
  225. }
  226. }
  227. switch (nv->token) {
  228. case NGHTTP2_TOKEN__STATUS: {
  229. if (!check_pseudo_header(stream, nv, NGHTTP2_HTTP_FLAG__STATUS)) {
  230. return NGHTTP2_ERR_HTTP_HEADER;
  231. }
  232. if (nv->value->len != 3) {
  233. return NGHTTP2_ERR_HTTP_HEADER;
  234. }
  235. stream->status_code = (int16_t)parse_uint(nv->value->base, nv->value->len);
  236. if (stream->status_code == -1 || stream->status_code == 101) {
  237. return NGHTTP2_ERR_HTTP_HEADER;
  238. }
  239. break;
  240. }
  241. case NGHTTP2_TOKEN_CONTENT_LENGTH: {
  242. if (stream->status_code == 204) {
  243. /* content-length header field in 204 response is prohibited by
  244. RFC 7230. But some widely used servers send content-length:
  245. 0. Until they get fixed, we ignore it. */
  246. if (stream->content_length != -1) {
  247. /* Found multiple content-length field */
  248. return NGHTTP2_ERR_HTTP_HEADER;
  249. }
  250. if (!lstrieq("0", nv->value->base, nv->value->len)) {
  251. return NGHTTP2_ERR_HTTP_HEADER;
  252. }
  253. stream->content_length = 0;
  254. return NGHTTP2_ERR_REMOVE_HTTP_HEADER;
  255. }
  256. if (stream->status_code / 100 == 1) {
  257. return NGHTTP2_ERR_HTTP_HEADER;
  258. }
  259. /* https://tools.ietf.org/html/rfc7230#section-3.3.3 */
  260. if (stream->status_code / 100 == 2 &&
  261. (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT)) {
  262. return NGHTTP2_ERR_REMOVE_HTTP_HEADER;
  263. }
  264. if (stream->content_length != -1) {
  265. return NGHTTP2_ERR_HTTP_HEADER;
  266. }
  267. stream->content_length = parse_uint(nv->value->base, nv->value->len);
  268. if (stream->content_length == -1) {
  269. return NGHTTP2_ERR_HTTP_HEADER;
  270. }
  271. break;
  272. }
  273. /* disallowed header fields */
  274. case NGHTTP2_TOKEN_CONNECTION:
  275. case NGHTTP2_TOKEN_KEEP_ALIVE:
  276. case NGHTTP2_TOKEN_PROXY_CONNECTION:
  277. case NGHTTP2_TOKEN_TRANSFER_ENCODING:
  278. case NGHTTP2_TOKEN_UPGRADE:
  279. return NGHTTP2_ERR_HTTP_HEADER;
  280. case NGHTTP2_TOKEN_TE:
  281. if (!lstrieq("trailers", nv->value->base, nv->value->len)) {
  282. return NGHTTP2_ERR_HTTP_HEADER;
  283. }
  284. break;
  285. default:
  286. if (nv->name->base[0] == ':') {
  287. return NGHTTP2_ERR_HTTP_HEADER;
  288. }
  289. }
  290. if (nv->name->base[0] != ':') {
  291. stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
  292. }
  293. return 0;
  294. }
  295. static int check_scheme(const uint8_t *value, size_t len) {
  296. const uint8_t *last;
  297. if (len == 0) {
  298. return 0;
  299. }
  300. if (!(('A' <= *value && *value <= 'Z') || ('a' <= *value && *value <= 'z'))) {
  301. return 0;
  302. }
  303. last = value + len;
  304. ++value;
  305. for (; value != last; ++value) {
  306. if (!(('A' <= *value && *value <= 'Z') ||
  307. ('a' <= *value && *value <= 'z') ||
  308. ('0' <= *value && *value <= '9') || *value == '+' || *value == '-' ||
  309. *value == '.')) {
  310. return 0;
  311. }
  312. }
  313. return 1;
  314. }
  315. static int lws(const uint8_t *s, size_t n) {
  316. size_t i;
  317. for (i = 0; i < n; ++i) {
  318. if (s[i] != ' ' && s[i] != '\t') {
  319. return 0;
  320. }
  321. }
  322. return 1;
  323. }
  324. int nghttp2_http_on_header(nghttp2_session *session, nghttp2_stream *stream,
  325. nghttp2_frame *frame, nghttp2_hd_nv *nv,
  326. int trailer) {
  327. int rv;
  328. /* We are strict for pseudo header field. One bad character should
  329. lead to fail. OTOH, we should be a bit forgiving for regular
  330. headers, since existing public internet has so much illegal
  331. headers floating around and if we kill the stream because of
  332. this, we may disrupt many web sites and/or libraries. So we
  333. become conservative here, and just ignore those illegal regular
  334. headers. */
  335. if (!nghttp2_check_header_name(nv->name->base, nv->name->len)) {
  336. size_t i;
  337. if (nv->name->len > 0 && nv->name->base[0] == ':') {
  338. return NGHTTP2_ERR_HTTP_HEADER;
  339. }
  340. /* header field name must be lower-cased without exception */
  341. for (i = 0; i < nv->name->len; ++i) {
  342. uint8_t c = nv->name->base[i];
  343. if ('A' <= c && c <= 'Z') {
  344. return NGHTTP2_ERR_HTTP_HEADER;
  345. }
  346. }
  347. /* When ignoring regular headers, we set this flag so that we
  348. still enforce header field ordering rule for pseudo header
  349. fields. */
  350. stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
  351. return NGHTTP2_ERR_IGN_HTTP_HEADER;
  352. }
  353. switch (nv->token) {
  354. case NGHTTP2_TOKEN__METHOD:
  355. rv = nghttp2_check_method(nv->value->base, nv->value->len);
  356. break;
  357. case NGHTTP2_TOKEN__PATH:
  358. rv = nghttp2_check_path(nv->value->base, nv->value->len);
  359. break;
  360. case NGHTTP2_TOKEN__AUTHORITY:
  361. case NGHTTP2_TOKEN_HOST:
  362. if (session->server || frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  363. rv = nghttp2_check_authority(nv->value->base, nv->value->len);
  364. } else if (
  365. stream->flags &
  366. NGHTTP2_STREAM_FLAG_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) {
  367. rv = nghttp2_check_header_value(nv->value->base, nv->value->len);
  368. } else {
  369. rv = nghttp2_check_header_value_rfc9113(nv->value->base, nv->value->len);
  370. }
  371. break;
  372. case NGHTTP2_TOKEN__SCHEME:
  373. rv = check_scheme(nv->value->base, nv->value->len);
  374. break;
  375. case NGHTTP2_TOKEN__PROTOCOL:
  376. /* Check the value consists of just white spaces, which was done
  377. in check_pseudo_header before
  378. nghttp2_check_header_value_rfc9113 has been introduced. */
  379. if ((stream->flags &
  380. NGHTTP2_STREAM_FLAG_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) &&
  381. lws(nv->value->base, nv->value->len)) {
  382. rv = 0;
  383. break;
  384. }
  385. /* fall through */
  386. default:
  387. if (stream->flags &
  388. NGHTTP2_STREAM_FLAG_NO_RFC9113_LEADING_AND_TRAILING_WS_VALIDATION) {
  389. rv = nghttp2_check_header_value(nv->value->base, nv->value->len);
  390. } else {
  391. rv = nghttp2_check_header_value_rfc9113(nv->value->base, nv->value->len);
  392. }
  393. }
  394. if (rv == 0) {
  395. assert(nv->name->len > 0);
  396. if (nv->name->base[0] == ':') {
  397. return NGHTTP2_ERR_HTTP_HEADER;
  398. }
  399. /* When ignoring regular headers, we set this flag so that we
  400. still enforce header field ordering rule for pseudo header
  401. fields. */
  402. stream->http_flags |= NGHTTP2_HTTP_FLAG_PSEUDO_HEADER_DISALLOWED;
  403. return NGHTTP2_ERR_IGN_HTTP_HEADER;
  404. }
  405. if (session->server || frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  406. return http_request_on_header(stream, nv, trailer,
  407. session->server &&
  408. session->pending_enable_connect_protocol);
  409. }
  410. return http_response_on_header(stream, nv, trailer);
  411. }
  412. int nghttp2_http_on_request_headers(nghttp2_stream *stream,
  413. nghttp2_frame *frame) {
  414. if (!(stream->http_flags & NGHTTP2_HTTP_FLAG__PROTOCOL) &&
  415. (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT)) {
  416. if ((stream->http_flags &
  417. (NGHTTP2_HTTP_FLAG__SCHEME | NGHTTP2_HTTP_FLAG__PATH)) ||
  418. (stream->http_flags & NGHTTP2_HTTP_FLAG__AUTHORITY) == 0) {
  419. return -1;
  420. }
  421. stream->content_length = -1;
  422. } else {
  423. if ((stream->http_flags & NGHTTP2_HTTP_FLAG_REQ_HEADERS) !=
  424. NGHTTP2_HTTP_FLAG_REQ_HEADERS ||
  425. (stream->http_flags &
  426. (NGHTTP2_HTTP_FLAG__AUTHORITY | NGHTTP2_HTTP_FLAG_HOST)) == 0) {
  427. return -1;
  428. }
  429. if ((stream->http_flags & NGHTTP2_HTTP_FLAG__PROTOCOL) &&
  430. ((stream->http_flags & NGHTTP2_HTTP_FLAG_METH_CONNECT) == 0 ||
  431. (stream->http_flags & NGHTTP2_HTTP_FLAG__AUTHORITY) == 0)) {
  432. return -1;
  433. }
  434. if (!check_path(stream)) {
  435. return -1;
  436. }
  437. }
  438. if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
  439. /* we are going to reuse data fields for upcoming response. Clear
  440. them now, except for method flags. */
  441. stream->http_flags &= NGHTTP2_HTTP_FLAG_METH_ALL;
  442. stream->content_length = -1;
  443. }
  444. return 0;
  445. }
  446. int nghttp2_http_on_response_headers(nghttp2_stream *stream) {
  447. if ((stream->http_flags & NGHTTP2_HTTP_FLAG__STATUS) == 0) {
  448. return -1;
  449. }
  450. if (stream->status_code / 100 == 1) {
  451. /* non-final response */
  452. stream->http_flags = (stream->http_flags & NGHTTP2_HTTP_FLAG_METH_ALL) |
  453. NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE;
  454. stream->content_length = -1;
  455. stream->status_code = -1;
  456. return 0;
  457. }
  458. stream->http_flags =
  459. stream->http_flags & (uint32_t)~NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE;
  460. if (!expect_response_body(stream)) {
  461. stream->content_length = 0;
  462. } else if (stream->http_flags & (NGHTTP2_HTTP_FLAG_METH_CONNECT |
  463. NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND)) {
  464. stream->content_length = -1;
  465. }
  466. return 0;
  467. }
  468. int nghttp2_http_on_trailer_headers(nghttp2_stream *stream,
  469. nghttp2_frame *frame) {
  470. (void)stream;
  471. if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
  472. return -1;
  473. }
  474. return 0;
  475. }
  476. int nghttp2_http_on_remote_end_stream(nghttp2_stream *stream) {
  477. if (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) {
  478. return -1;
  479. }
  480. if (stream->content_length != -1 &&
  481. stream->content_length != stream->recv_content_length) {
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. int nghttp2_http_on_data_chunk(nghttp2_stream *stream, size_t n) {
  487. stream->recv_content_length += (int64_t)n;
  488. if ((stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) ||
  489. (stream->content_length != -1 &&
  490. stream->recv_content_length > stream->content_length)) {
  491. return -1;
  492. }
  493. return 0;
  494. }
  495. void nghttp2_http_record_request_method(nghttp2_stream *stream,
  496. nghttp2_frame *frame) {
  497. const nghttp2_nv *nva;
  498. size_t nvlen;
  499. size_t i;
  500. switch (frame->hd.type) {
  501. case NGHTTP2_HEADERS:
  502. nva = frame->headers.nva;
  503. nvlen = frame->headers.nvlen;
  504. break;
  505. case NGHTTP2_PUSH_PROMISE:
  506. nva = frame->push_promise.nva;
  507. nvlen = frame->push_promise.nvlen;
  508. break;
  509. default:
  510. return;
  511. }
  512. /* TODO we should do this strictly. */
  513. for (i = 0; i < nvlen; ++i) {
  514. const nghttp2_nv *nv = &nva[i];
  515. if (!(nv->namelen == 7 && nv->name[6] == 'd' &&
  516. memcmp(":metho", nv->name, nv->namelen - 1) == 0)) {
  517. continue;
  518. }
  519. if (lstreq("CONNECT", nv->value, nv->valuelen)) {
  520. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_CONNECT;
  521. return;
  522. }
  523. if (lstreq("HEAD", nv->value, nv->valuelen)) {
  524. stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
  525. return;
  526. }
  527. return;
  528. }
  529. }
  530. int nghttp2_http_parse_priority(nghttp2_extpri *dest, const uint8_t *value,
  531. size_t valuelen) {
  532. nghttp2_extpri pri = *dest;
  533. sf_parser sfp;
  534. sf_vec key;
  535. sf_value val;
  536. int rv;
  537. sf_parser_init(&sfp, value, valuelen);
  538. for (;;) {
  539. rv = sf_parser_dict(&sfp, &key, &val);
  540. if (rv != 0) {
  541. if (rv == SF_ERR_EOF) {
  542. break;
  543. }
  544. return NGHTTP2_ERR_INVALID_ARGUMENT;
  545. }
  546. if (key.len != 1) {
  547. continue;
  548. }
  549. switch (key.base[0]) {
  550. case 'i':
  551. if (val.type != SF_TYPE_BOOLEAN) {
  552. return NGHTTP2_ERR_INVALID_ARGUMENT;
  553. }
  554. pri.inc = val.boolean;
  555. break;
  556. case 'u':
  557. if (val.type != SF_TYPE_INTEGER ||
  558. val.integer < NGHTTP2_EXTPRI_URGENCY_HIGH ||
  559. NGHTTP2_EXTPRI_URGENCY_LOW < val.integer) {
  560. return NGHTTP2_ERR_INVALID_ARGUMENT;
  561. }
  562. pri.urgency = (uint32_t)val.integer;
  563. break;
  564. }
  565. }
  566. *dest = pri;
  567. return 0;
  568. }