ngx_http_v3_request.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. /*
  2. * Copyright (C) Roman Arutyunyan
  3. * Copyright (C) Nginx, Inc.
  4. */
  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8. static void ngx_http_v3_init_request_stream(ngx_connection_t *c);
  9. static void ngx_http_v3_wait_request_handler(ngx_event_t *rev);
  10. static void ngx_http_v3_cleanup_connection(void *data);
  11. static void ngx_http_v3_cleanup_request(void *data);
  12. static void ngx_http_v3_process_request(ngx_event_t *rev);
  13. static ngx_int_t ngx_http_v3_process_header(ngx_http_request_t *r,
  14. ngx_str_t *name, ngx_str_t *value);
  15. static ngx_int_t ngx_http_v3_validate_header(ngx_http_request_t *r,
  16. ngx_str_t *name, ngx_str_t *value);
  17. static ngx_int_t ngx_http_v3_process_pseudo_header(ngx_http_request_t *r,
  18. ngx_str_t *name, ngx_str_t *value);
  19. static ngx_int_t ngx_http_v3_init_pseudo_headers(ngx_http_request_t *r);
  20. static ngx_int_t ngx_http_v3_process_request_header(ngx_http_request_t *r);
  21. static ngx_int_t ngx_http_v3_cookie(ngx_http_request_t *r, ngx_str_t *value);
  22. static ngx_int_t ngx_http_v3_construct_cookie_header(ngx_http_request_t *r);
  23. static void ngx_http_v3_read_client_request_body_handler(ngx_http_request_t *r);
  24. static ngx_int_t ngx_http_v3_do_read_client_request_body(ngx_http_request_t *r);
  25. static ngx_int_t ngx_http_v3_request_body_filter(ngx_http_request_t *r,
  26. ngx_chain_t *in);
  27. static const struct {
  28. ngx_str_t name;
  29. ngx_uint_t method;
  30. } ngx_http_v3_methods[] = {
  31. { ngx_string("GET"), NGX_HTTP_GET },
  32. { ngx_string("POST"), NGX_HTTP_POST },
  33. { ngx_string("HEAD"), NGX_HTTP_HEAD },
  34. { ngx_string("OPTIONS"), NGX_HTTP_OPTIONS },
  35. { ngx_string("PROPFIND"), NGX_HTTP_PROPFIND },
  36. { ngx_string("PUT"), NGX_HTTP_PUT },
  37. { ngx_string("MKCOL"), NGX_HTTP_MKCOL },
  38. { ngx_string("DELETE"), NGX_HTTP_DELETE },
  39. { ngx_string("COPY"), NGX_HTTP_COPY },
  40. { ngx_string("MOVE"), NGX_HTTP_MOVE },
  41. { ngx_string("PROPPATCH"), NGX_HTTP_PROPPATCH },
  42. { ngx_string("LOCK"), NGX_HTTP_LOCK },
  43. { ngx_string("UNLOCK"), NGX_HTTP_UNLOCK },
  44. { ngx_string("PATCH"), NGX_HTTP_PATCH },
  45. { ngx_string("TRACE"), NGX_HTTP_TRACE },
  46. { ngx_string("CONNECT"), NGX_HTTP_CONNECT }
  47. };
  48. void
  49. ngx_http_v3_init_stream(ngx_connection_t *c)
  50. {
  51. ngx_http_connection_t *hc, *phc;
  52. ngx_http_v3_srv_conf_t *h3scf;
  53. ngx_http_core_loc_conf_t *clcf;
  54. hc = c->data;
  55. hc->ssl = 1;
  56. clcf = ngx_http_get_module_loc_conf(hc->conf_ctx, ngx_http_core_module);
  57. if (c->quic == NULL) {
  58. h3scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v3_module);
  59. h3scf->quic.idle_timeout = clcf->keepalive_timeout;
  60. ngx_quic_run(c, &h3scf->quic);
  61. return;
  62. }
  63. phc = ngx_http_quic_get_connection(c);
  64. if (phc->ssl_servername) {
  65. hc->ssl_servername = phc->ssl_servername;
  66. #if (NGX_PCRE)
  67. hc->ssl_servername_regex = phc->ssl_servername_regex;
  68. #endif
  69. hc->conf_ctx = phc->conf_ctx;
  70. ngx_set_connection_log(c, clcf->error_log);
  71. }
  72. if (c->quic->id & NGX_QUIC_STREAM_UNIDIRECTIONAL) {
  73. ngx_http_v3_init_uni_stream(c);
  74. } else {
  75. ngx_http_v3_init_request_stream(c);
  76. }
  77. }
  78. ngx_int_t
  79. ngx_http_v3_init(ngx_connection_t *c)
  80. {
  81. unsigned int len;
  82. const unsigned char *data;
  83. ngx_http_v3_session_t *h3c;
  84. ngx_http_v3_srv_conf_t *h3scf;
  85. ngx_http_core_loc_conf_t *clcf;
  86. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 init");
  87. if (ngx_http_v3_init_session(c) != NGX_OK) {
  88. return NGX_ERROR;
  89. }
  90. h3c = ngx_http_v3_get_session(c);
  91. clcf = ngx_http_v3_get_module_loc_conf(c, ngx_http_core_module);
  92. ngx_add_timer(&h3c->keepalive, clcf->keepalive_timeout);
  93. h3scf = ngx_http_v3_get_module_srv_conf(c, ngx_http_v3_module);
  94. if (h3scf->enable_hq) {
  95. if (!h3scf->enable) {
  96. h3c->hq = 1;
  97. return NGX_OK;
  98. }
  99. SSL_get0_alpn_selected(c->ssl->connection, &data, &len);
  100. if (len == sizeof(NGX_HTTP_V3_HQ_PROTO) - 1
  101. && ngx_strncmp(data, NGX_HTTP_V3_HQ_PROTO, len) == 0)
  102. {
  103. h3c->hq = 1;
  104. return NGX_OK;
  105. }
  106. }
  107. if (ngx_http_v3_send_settings(c) != NGX_OK) {
  108. return NGX_ERROR;
  109. }
  110. if (h3scf->max_table_capacity > 0) {
  111. if (ngx_http_v3_get_uni_stream(c, NGX_HTTP_V3_STREAM_DECODER) == NULL) {
  112. return NGX_ERROR;
  113. }
  114. }
  115. return NGX_OK;
  116. }
  117. void
  118. ngx_http_v3_shutdown(ngx_connection_t *c)
  119. {
  120. ngx_http_v3_session_t *h3c;
  121. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 shutdown");
  122. h3c = ngx_http_v3_get_session(c);
  123. if (h3c == NULL) {
  124. ngx_quic_finalize_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
  125. "connection shutdown");
  126. return;
  127. }
  128. if (!h3c->goaway) {
  129. h3c->goaway = 1;
  130. if (!h3c->hq) {
  131. (void) ngx_http_v3_send_goaway(c, h3c->next_request_id);
  132. }
  133. ngx_http_v3_shutdown_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
  134. "connection shutdown");
  135. }
  136. }
  137. static void
  138. ngx_http_v3_init_request_stream(ngx_connection_t *c)
  139. {
  140. uint64_t n;
  141. ngx_event_t *rev;
  142. ngx_pool_cleanup_t *cln;
  143. ngx_http_connection_t *hc;
  144. ngx_http_v3_session_t *h3c;
  145. ngx_http_core_loc_conf_t *clcf;
  146. ngx_http_core_srv_conf_t *cscf;
  147. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 init request stream");
  148. #if (NGX_STAT_STUB)
  149. (void) ngx_atomic_fetch_add(ngx_stat_active, 1);
  150. #endif
  151. hc = c->data;
  152. clcf = ngx_http_get_module_loc_conf(hc->conf_ctx, ngx_http_core_module);
  153. n = c->quic->id >> 2;
  154. if (n >= clcf->keepalive_requests * 2) {
  155. ngx_http_v3_finalize_connection(c, NGX_HTTP_V3_ERR_EXCESSIVE_LOAD,
  156. "too many requests per connection");
  157. ngx_http_close_connection(c);
  158. return;
  159. }
  160. h3c = ngx_http_v3_get_session(c);
  161. if (h3c->goaway) {
  162. c->close = 1;
  163. ngx_http_close_connection(c);
  164. return;
  165. }
  166. h3c->next_request_id = c->quic->id + 0x04;
  167. if (n + 1 == clcf->keepalive_requests
  168. || ngx_current_msec - c->start_time > clcf->keepalive_time)
  169. {
  170. h3c->goaway = 1;
  171. if (!h3c->hq) {
  172. if (ngx_http_v3_send_goaway(c, h3c->next_request_id) != NGX_OK) {
  173. ngx_http_close_connection(c);
  174. return;
  175. }
  176. }
  177. ngx_http_v3_shutdown_connection(c, NGX_HTTP_V3_ERR_NO_ERROR,
  178. "reached maximum number of requests");
  179. }
  180. cln = ngx_pool_cleanup_add(c->pool, 0);
  181. if (cln == NULL) {
  182. ngx_http_close_connection(c);
  183. return;
  184. }
  185. cln->handler = ngx_http_v3_cleanup_connection;
  186. cln->data = c;
  187. h3c->nrequests++;
  188. if (h3c->keepalive.timer_set) {
  189. ngx_del_timer(&h3c->keepalive);
  190. }
  191. rev = c->read;
  192. if (!h3c->hq) {
  193. rev->handler = ngx_http_v3_wait_request_handler;
  194. c->write->handler = ngx_http_empty_handler;
  195. }
  196. if (rev->ready) {
  197. rev->handler(rev);
  198. return;
  199. }
  200. cscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_core_module);
  201. ngx_add_timer(rev, cscf->client_header_timeout);
  202. ngx_reusable_connection(c, 1);
  203. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  204. ngx_http_close_connection(c);
  205. return;
  206. }
  207. }
  208. static void
  209. ngx_http_v3_wait_request_handler(ngx_event_t *rev)
  210. {
  211. size_t size;
  212. ssize_t n;
  213. ngx_buf_t *b;
  214. ngx_connection_t *c;
  215. ngx_pool_cleanup_t *cln;
  216. ngx_http_request_t *r;
  217. ngx_http_connection_t *hc;
  218. ngx_http_core_srv_conf_t *cscf;
  219. c = rev->data;
  220. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 wait request handler");
  221. if (rev->timedout) {
  222. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  223. c->timedout = 1;
  224. ngx_http_close_connection(c);
  225. return;
  226. }
  227. if (c->close) {
  228. ngx_http_close_connection(c);
  229. return;
  230. }
  231. hc = c->data;
  232. cscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_core_module);
  233. size = cscf->client_header_buffer_size;
  234. b = c->buffer;
  235. if (b == NULL) {
  236. b = ngx_create_temp_buf(c->pool, size);
  237. if (b == NULL) {
  238. ngx_http_close_connection(c);
  239. return;
  240. }
  241. c->buffer = b;
  242. } else if (b->start == NULL) {
  243. b->start = ngx_palloc(c->pool, size);
  244. if (b->start == NULL) {
  245. ngx_http_close_connection(c);
  246. return;
  247. }
  248. b->pos = b->start;
  249. b->last = b->start;
  250. b->end = b->last + size;
  251. }
  252. n = c->recv(c, b->last, size);
  253. if (n == NGX_AGAIN) {
  254. if (!rev->timer_set) {
  255. ngx_add_timer(rev, cscf->client_header_timeout);
  256. ngx_reusable_connection(c, 1);
  257. }
  258. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  259. ngx_http_close_connection(c);
  260. return;
  261. }
  262. /*
  263. * We are trying to not hold c->buffer's memory for an idle connection.
  264. */
  265. if (ngx_pfree(c->pool, b->start) == NGX_OK) {
  266. b->start = NULL;
  267. }
  268. return;
  269. }
  270. if (n == NGX_ERROR) {
  271. ngx_http_close_connection(c);
  272. return;
  273. }
  274. if (n == 0) {
  275. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  276. "client closed connection");
  277. ngx_http_close_connection(c);
  278. return;
  279. }
  280. b->last += n;
  281. c->log->action = "reading client request";
  282. ngx_reusable_connection(c, 0);
  283. r = ngx_http_create_request(c);
  284. if (r == NULL) {
  285. ngx_http_close_connection(c);
  286. return;
  287. }
  288. r->http_version = NGX_HTTP_VERSION_30;
  289. r->v3_parse = ngx_pcalloc(r->pool, sizeof(ngx_http_v3_parse_t));
  290. if (r->v3_parse == NULL) {
  291. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  292. return;
  293. }
  294. r->v3_parse->header_limit = cscf->large_client_header_buffers.size
  295. * cscf->large_client_header_buffers.num;
  296. c->data = r;
  297. c->requests = (c->quic->id >> 2) + 1;
  298. cln = ngx_pool_cleanup_add(r->pool, 0);
  299. if (cln == NULL) {
  300. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  301. return;
  302. }
  303. cln->handler = ngx_http_v3_cleanup_request;
  304. cln->data = r;
  305. rev->handler = ngx_http_v3_process_request;
  306. ngx_http_v3_process_request(rev);
  307. }
  308. void
  309. ngx_http_v3_reset_stream(ngx_connection_t *c)
  310. {
  311. ngx_http_v3_session_t *h3c;
  312. h3c = ngx_http_v3_get_session(c);
  313. if (!c->read->eof && !h3c->hq
  314. && h3c->known_streams[NGX_HTTP_V3_STREAM_SERVER_DECODER]
  315. && (c->quic->id & NGX_QUIC_STREAM_UNIDIRECTIONAL) == 0)
  316. {
  317. (void) ngx_http_v3_send_cancel_stream(c, c->quic->id);
  318. }
  319. if (c->timedout) {
  320. ngx_quic_reset_stream(c, NGX_HTTP_V3_ERR_GENERAL_PROTOCOL_ERROR);
  321. } else if (c->close) {
  322. ngx_quic_reset_stream(c, NGX_HTTP_V3_ERR_REQUEST_REJECTED);
  323. } else if (c->requests == 0 || c->error) {
  324. ngx_quic_reset_stream(c, NGX_HTTP_V3_ERR_INTERNAL_ERROR);
  325. }
  326. }
  327. static void
  328. ngx_http_v3_cleanup_connection(void *data)
  329. {
  330. ngx_connection_t *c = data;
  331. ngx_http_v3_session_t *h3c;
  332. ngx_http_core_loc_conf_t *clcf;
  333. h3c = ngx_http_v3_get_session(c);
  334. if (--h3c->nrequests == 0) {
  335. clcf = ngx_http_v3_get_module_loc_conf(c, ngx_http_core_module);
  336. ngx_add_timer(&h3c->keepalive, clcf->keepalive_timeout);
  337. }
  338. }
  339. static void
  340. ngx_http_v3_cleanup_request(void *data)
  341. {
  342. ngx_http_request_t *r = data;
  343. if (!r->response_sent) {
  344. r->connection->error = 1;
  345. }
  346. }
  347. static void
  348. ngx_http_v3_process_request(ngx_event_t *rev)
  349. {
  350. u_char *p;
  351. ssize_t n;
  352. ngx_buf_t *b;
  353. ngx_int_t rc;
  354. ngx_connection_t *c;
  355. ngx_http_request_t *r;
  356. ngx_http_v3_session_t *h3c;
  357. ngx_http_core_srv_conf_t *cscf;
  358. ngx_http_v3_parse_headers_t *st;
  359. c = rev->data;
  360. r = c->data;
  361. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http3 process request");
  362. if (rev->timedout) {
  363. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  364. c->timedout = 1;
  365. ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  366. return;
  367. }
  368. h3c = ngx_http_v3_get_session(c);
  369. st = &r->v3_parse->headers;
  370. b = r->header_in;
  371. for ( ;; ) {
  372. if (b->pos == b->last) {
  373. if (rev->ready) {
  374. n = c->recv(c, b->start, b->end - b->start);
  375. } else {
  376. n = NGX_AGAIN;
  377. }
  378. if (n == NGX_AGAIN) {
  379. if (!rev->timer_set) {
  380. cscf = ngx_http_get_module_srv_conf(r,
  381. ngx_http_core_module);
  382. ngx_add_timer(rev, cscf->client_header_timeout);
  383. }
  384. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  385. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  386. }
  387. break;
  388. }
  389. if (n == 0) {
  390. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  391. "client prematurely closed connection");
  392. }
  393. if (n == 0 || n == NGX_ERROR) {
  394. c->error = 1;
  395. c->log->action = "reading client request";
  396. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  397. break;
  398. }
  399. b->pos = b->start;
  400. b->last = b->start + n;
  401. }
  402. p = b->pos;
  403. rc = ngx_http_v3_parse_headers(c, st, b);
  404. if (rc > 0) {
  405. ngx_quic_reset_stream(c, rc);
  406. ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  407. "client sent invalid header");
  408. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  409. break;
  410. }
  411. if (rc == NGX_ERROR) {
  412. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  413. break;
  414. }
  415. r->request_length += b->pos - p;
  416. h3c->total_bytes += b->pos - p;
  417. if (ngx_http_v3_check_flood(c) != NGX_OK) {
  418. ngx_http_close_request(r, NGX_HTTP_CLOSE);
  419. break;
  420. }
  421. if (rc == NGX_BUSY) {
  422. if (rev->error) {
  423. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  424. break;
  425. }
  426. if (!rev->timer_set) {
  427. cscf = ngx_http_get_module_srv_conf(r,
  428. ngx_http_core_module);
  429. ngx_add_timer(rev, cscf->client_header_timeout);
  430. }
  431. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  432. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  433. }
  434. break;
  435. }
  436. if (rc == NGX_AGAIN) {
  437. continue;
  438. }
  439. /* rc == NGX_OK || rc == NGX_DONE */
  440. h3c->payload_bytes += ngx_http_v3_encode_field_l(NULL,
  441. &st->field_rep.field.name,
  442. &st->field_rep.field.value);
  443. if (ngx_http_v3_process_header(r, &st->field_rep.field.name,
  444. &st->field_rep.field.value)
  445. != NGX_OK)
  446. {
  447. break;
  448. }
  449. if (rc == NGX_DONE) {
  450. if (ngx_http_v3_process_request_header(r) != NGX_OK) {
  451. break;
  452. }
  453. ngx_http_process_request(r);
  454. break;
  455. }
  456. }
  457. ngx_http_run_posted_requests(c);
  458. return;
  459. }
  460. static ngx_int_t
  461. ngx_http_v3_process_header(ngx_http_request_t *r, ngx_str_t *name,
  462. ngx_str_t *value)
  463. {
  464. size_t len;
  465. ngx_table_elt_t *h;
  466. ngx_http_header_t *hh;
  467. ngx_http_core_srv_conf_t *cscf;
  468. ngx_http_core_main_conf_t *cmcf;
  469. static ngx_str_t cookie = ngx_string("cookie");
  470. len = name->len + value->len;
  471. if (len > r->v3_parse->header_limit) {
  472. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  473. "client sent too large header");
  474. ngx_http_finalize_request(r, NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
  475. return NGX_ERROR;
  476. }
  477. r->v3_parse->header_limit -= len;
  478. if (ngx_http_v3_validate_header(r, name, value) != NGX_OK) {
  479. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  480. return NGX_ERROR;
  481. }
  482. if (r->invalid_header) {
  483. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  484. if (cscf->ignore_invalid_headers) {
  485. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  486. "client sent invalid header: \"%V\"", name);
  487. return NGX_OK;
  488. }
  489. }
  490. if (name->len && name->data[0] == ':') {
  491. return ngx_http_v3_process_pseudo_header(r, name, value);
  492. }
  493. if (ngx_http_v3_init_pseudo_headers(r) != NGX_OK) {
  494. return NGX_ERROR;
  495. }
  496. if (name->len == cookie.len
  497. && ngx_memcmp(name->data, cookie.data, cookie.len) == 0)
  498. {
  499. if (ngx_http_v3_cookie(r, value) != NGX_OK) {
  500. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  501. return NGX_ERROR;
  502. }
  503. } else {
  504. h = ngx_list_push(&r->headers_in.headers);
  505. if (h == NULL) {
  506. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  507. return NGX_ERROR;
  508. }
  509. h->key = *name;
  510. h->value = *value;
  511. h->lowcase_key = h->key.data;
  512. h->hash = ngx_hash_key(h->key.data, h->key.len);
  513. cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  514. hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
  515. h->lowcase_key, h->key.len);
  516. if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
  517. return NGX_ERROR;
  518. }
  519. }
  520. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  521. "http3 header: \"%V: %V\"", name, value);
  522. return NGX_OK;
  523. }
  524. static ngx_int_t
  525. ngx_http_v3_validate_header(ngx_http_request_t *r, ngx_str_t *name,
  526. ngx_str_t *value)
  527. {
  528. u_char ch;
  529. ngx_uint_t i;
  530. ngx_http_core_srv_conf_t *cscf;
  531. r->invalid_header = 0;
  532. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  533. for (i = (name->data[0] == ':'); i != name->len; i++) {
  534. ch = name->data[i];
  535. if ((ch >= 'a' && ch <= 'z')
  536. || (ch == '-')
  537. || (ch >= '0' && ch <= '9')
  538. || (ch == '_' && cscf->underscores_in_headers))
  539. {
  540. continue;
  541. }
  542. if (ch <= 0x20 || ch == 0x7f || ch == ':'
  543. || (ch >= 'A' && ch <= 'Z'))
  544. {
  545. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  546. "client sent invalid header name: \"%V\"", name);
  547. return NGX_ERROR;
  548. }
  549. r->invalid_header = 1;
  550. }
  551. for (i = 0; i != value->len; i++) {
  552. ch = value->data[i];
  553. if (ch == '\0' || ch == LF || ch == CR) {
  554. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  555. "client sent header \"%V\" with "
  556. "invalid value: \"%V\"", name, value);
  557. return NGX_ERROR;
  558. }
  559. }
  560. return NGX_OK;
  561. }
  562. static ngx_int_t
  563. ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name,
  564. ngx_str_t *value)
  565. {
  566. u_char ch, c;
  567. ngx_uint_t i;
  568. if (r->request_line.len) {
  569. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  570. "client sent out of order pseudo-headers");
  571. goto failed;
  572. }
  573. if (name->len == 7 && ngx_strncmp(name->data, ":method", 7) == 0) {
  574. if (r->method_name.len) {
  575. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  576. "client sent duplicate \":method\" header");
  577. goto failed;
  578. }
  579. if (value->len == 0) {
  580. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  581. "client sent empty \":method\" header");
  582. goto failed;
  583. }
  584. r->method_name = *value;
  585. for (i = 0; i < sizeof(ngx_http_v3_methods)
  586. / sizeof(ngx_http_v3_methods[0]); i++)
  587. {
  588. if (value->len == ngx_http_v3_methods[i].name.len
  589. && ngx_strncmp(value->data,
  590. ngx_http_v3_methods[i].name.data, value->len)
  591. == 0)
  592. {
  593. r->method = ngx_http_v3_methods[i].method;
  594. break;
  595. }
  596. }
  597. for (i = 0; i < value->len; i++) {
  598. ch = value->data[i];
  599. if ((ch < 'A' || ch > 'Z') && ch != '_' && ch != '-') {
  600. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  601. "client sent invalid method: \"%V\"", value);
  602. goto failed;
  603. }
  604. }
  605. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  606. "http3 method \"%V\" %ui", value, r->method);
  607. return NGX_OK;
  608. }
  609. if (name->len == 5 && ngx_strncmp(name->data, ":path", 5) == 0) {
  610. if (r->uri_start) {
  611. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  612. "client sent duplicate \":path\" header");
  613. goto failed;
  614. }
  615. if (value->len == 0) {
  616. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  617. "client sent empty \":path\" header");
  618. goto failed;
  619. }
  620. r->uri_start = value->data;
  621. r->uri_end = value->data + value->len;
  622. if (ngx_http_parse_uri(r) != NGX_OK) {
  623. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  624. "client sent invalid \":path\" header: \"%V\"",
  625. value);
  626. goto failed;
  627. }
  628. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  629. "http3 path \"%V\"", value);
  630. return NGX_OK;
  631. }
  632. if (name->len == 7 && ngx_strncmp(name->data, ":scheme", 7) == 0) {
  633. if (r->schema.len) {
  634. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  635. "client sent duplicate \":scheme\" header");
  636. goto failed;
  637. }
  638. if (value->len == 0) {
  639. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  640. "client sent empty \":scheme\" header");
  641. goto failed;
  642. }
  643. for (i = 0; i < value->len; i++) {
  644. ch = value->data[i];
  645. c = (u_char) (ch | 0x20);
  646. if (c >= 'a' && c <= 'z') {
  647. continue;
  648. }
  649. if (((ch >= '0' && ch <= '9')
  650. || ch == '+' || ch == '-' || ch == '.')
  651. && i > 0)
  652. {
  653. continue;
  654. }
  655. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  656. "client sent invalid \":scheme\" header: \"%V\"",
  657. value);
  658. goto failed;
  659. }
  660. r->schema = *value;
  661. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  662. "http3 schema \"%V\"", value);
  663. return NGX_OK;
  664. }
  665. if (name->len == 10 && ngx_strncmp(name->data, ":authority", 10) == 0) {
  666. if (r->host_start) {
  667. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  668. "client sent duplicate \":authority\" header");
  669. goto failed;
  670. }
  671. r->host_start = value->data;
  672. r->host_end = value->data + value->len;
  673. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  674. "http3 authority \"%V\"", value);
  675. return NGX_OK;
  676. }
  677. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  678. "client sent unknown pseudo-header \"%V\"", name);
  679. failed:
  680. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  681. return NGX_ERROR;
  682. }
  683. static ngx_int_t
  684. ngx_http_v3_init_pseudo_headers(ngx_http_request_t *r)
  685. {
  686. size_t len;
  687. u_char *p;
  688. ngx_int_t rc;
  689. ngx_str_t host;
  690. if (r->request_line.len) {
  691. return NGX_OK;
  692. }
  693. if (r->method_name.len == 0) {
  694. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  695. "client sent no \":method\" header");
  696. goto failed;
  697. }
  698. if (r->schema.len == 0) {
  699. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  700. "client sent no \":scheme\" header");
  701. goto failed;
  702. }
  703. if (r->uri_start == NULL) {
  704. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  705. "client sent no \":path\" header");
  706. goto failed;
  707. }
  708. len = r->method_name.len + 1
  709. + (r->uri_end - r->uri_start) + 1
  710. + sizeof("HTTP/3.0") - 1;
  711. p = ngx_pnalloc(r->pool, len);
  712. if (p == NULL) {
  713. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  714. return NGX_ERROR;
  715. }
  716. r->request_line.data = p;
  717. p = ngx_cpymem(p, r->method_name.data, r->method_name.len);
  718. *p++ = ' ';
  719. p = ngx_cpymem(p, r->uri_start, r->uri_end - r->uri_start);
  720. *p++ = ' ';
  721. p = ngx_cpymem(p, "HTTP/3.0", sizeof("HTTP/3.0") - 1);
  722. r->request_line.len = p - r->request_line.data;
  723. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  724. "http3 request line: \"%V\"", &r->request_line);
  725. ngx_str_set(&r->http_protocol, "HTTP/3.0");
  726. if (ngx_http_process_request_uri(r) != NGX_OK) {
  727. return NGX_ERROR;
  728. }
  729. if (r->host_end) {
  730. host.len = r->host_end - r->host_start;
  731. host.data = r->host_start;
  732. rc = ngx_http_validate_host(&host, r->pool, 0);
  733. if (rc == NGX_DECLINED) {
  734. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  735. "client sent invalid host in request line");
  736. goto failed;
  737. }
  738. if (rc == NGX_ERROR) {
  739. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  740. return NGX_ERROR;
  741. }
  742. if (ngx_http_set_virtual_server(r, &host) == NGX_ERROR) {
  743. return NGX_ERROR;
  744. }
  745. r->headers_in.server = host;
  746. }
  747. if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
  748. sizeof(ngx_table_elt_t))
  749. != NGX_OK)
  750. {
  751. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  752. return NGX_ERROR;
  753. }
  754. return NGX_OK;
  755. failed:
  756. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  757. return NGX_ERROR;
  758. }
  759. static ngx_int_t
  760. ngx_http_v3_process_request_header(ngx_http_request_t *r)
  761. {
  762. ssize_t n;
  763. ngx_buf_t *b;
  764. ngx_connection_t *c;
  765. ngx_http_v3_session_t *h3c;
  766. ngx_http_v3_srv_conf_t *h3scf;
  767. c = r->connection;
  768. if (ngx_http_v3_init_pseudo_headers(r) != NGX_OK) {
  769. return NGX_ERROR;
  770. }
  771. h3c = ngx_http_v3_get_session(c);
  772. h3scf = ngx_http_get_module_srv_conf(r, ngx_http_v3_module);
  773. if ((h3c->hq && !h3scf->enable_hq) || (!h3c->hq && !h3scf->enable)) {
  774. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  775. "client attempted to request the server name "
  776. "for which the negotiated protocol is disabled");
  777. ngx_http_finalize_request(r, NGX_HTTP_MISDIRECTED_REQUEST);
  778. return NGX_ERROR;
  779. }
  780. if (ngx_http_v3_construct_cookie_header(r) != NGX_OK) {
  781. return NGX_ERROR;
  782. }
  783. if (r->headers_in.server.len == 0) {
  784. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  785. "client sent neither \":authority\" nor \"Host\" header");
  786. goto failed;
  787. }
  788. if (r->headers_in.host) {
  789. if (r->headers_in.host->value.len != r->headers_in.server.len
  790. || ngx_memcmp(r->headers_in.host->value.data,
  791. r->headers_in.server.data,
  792. r->headers_in.server.len)
  793. != 0)
  794. {
  795. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  796. "client sent \":authority\" and \"Host\" headers "
  797. "with different values");
  798. goto failed;
  799. }
  800. }
  801. if (r->headers_in.content_length) {
  802. r->headers_in.content_length_n =
  803. ngx_atoof(r->headers_in.content_length->value.data,
  804. r->headers_in.content_length->value.len);
  805. if (r->headers_in.content_length_n == NGX_ERROR) {
  806. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  807. "client sent invalid \"Content-Length\" header");
  808. goto failed;
  809. }
  810. } else {
  811. b = r->header_in;
  812. n = b->last - b->pos;
  813. if (n == 0) {
  814. n = c->recv(c, b->start, b->end - b->start);
  815. if (n == NGX_ERROR) {
  816. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  817. return NGX_ERROR;
  818. }
  819. if (n > 0) {
  820. b->pos = b->start;
  821. b->last = b->start + n;
  822. }
  823. }
  824. if (n != 0) {
  825. r->headers_in.chunked = 1;
  826. }
  827. }
  828. if (r->method == NGX_HTTP_CONNECT) {
  829. ngx_log_error(NGX_LOG_INFO, c->log, 0, "client sent CONNECT method");
  830. ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  831. return NGX_ERROR;
  832. }
  833. if (r->method == NGX_HTTP_TRACE) {
  834. ngx_log_error(NGX_LOG_INFO, c->log, 0, "client sent TRACE method");
  835. ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  836. return NGX_ERROR;
  837. }
  838. return NGX_OK;
  839. failed:
  840. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  841. return NGX_ERROR;
  842. }
  843. static ngx_int_t
  844. ngx_http_v3_cookie(ngx_http_request_t *r, ngx_str_t *value)
  845. {
  846. ngx_str_t *val;
  847. ngx_array_t *cookies;
  848. cookies = r->v3_parse->cookies;
  849. if (cookies == NULL) {
  850. cookies = ngx_array_create(r->pool, 2, sizeof(ngx_str_t));
  851. if (cookies == NULL) {
  852. return NGX_ERROR;
  853. }
  854. r->v3_parse->cookies = cookies;
  855. }
  856. val = ngx_array_push(cookies);
  857. if (val == NULL) {
  858. return NGX_ERROR;
  859. }
  860. *val = *value;
  861. return NGX_OK;
  862. }
  863. static ngx_int_t
  864. ngx_http_v3_construct_cookie_header(ngx_http_request_t *r)
  865. {
  866. u_char *buf, *p, *end;
  867. size_t len;
  868. ngx_str_t *vals;
  869. ngx_uint_t i;
  870. ngx_array_t *cookies;
  871. ngx_table_elt_t *h;
  872. ngx_http_header_t *hh;
  873. ngx_http_core_main_conf_t *cmcf;
  874. static ngx_str_t cookie = ngx_string("cookie");
  875. cookies = r->v3_parse->cookies;
  876. if (cookies == NULL) {
  877. return NGX_OK;
  878. }
  879. vals = cookies->elts;
  880. i = 0;
  881. len = 0;
  882. do {
  883. len += vals[i].len + 2;
  884. } while (++i != cookies->nelts);
  885. len -= 2;
  886. buf = ngx_pnalloc(r->pool, len + 1);
  887. if (buf == NULL) {
  888. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  889. return NGX_ERROR;
  890. }
  891. p = buf;
  892. end = buf + len;
  893. for (i = 0; /* void */ ; i++) {
  894. p = ngx_cpymem(p, vals[i].data, vals[i].len);
  895. if (p == end) {
  896. *p = '\0';
  897. break;
  898. }
  899. *p++ = ';'; *p++ = ' ';
  900. }
  901. h = ngx_list_push(&r->headers_in.headers);
  902. if (h == NULL) {
  903. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  904. return NGX_ERROR;
  905. }
  906. h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
  907. ngx_hash('c', 'o'), 'o'), 'k'), 'i'), 'e');
  908. h->key.len = cookie.len;
  909. h->key.data = cookie.data;
  910. h->value.len = len;
  911. h->value.data = buf;
  912. h->lowcase_key = cookie.data;
  913. cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  914. hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
  915. h->lowcase_key, h->key.len);
  916. if (hh == NULL) {
  917. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  918. return NGX_ERROR;
  919. }
  920. if (hh->handler(r, h, hh->offset) != NGX_OK) {
  921. /*
  922. * request has been finalized already
  923. * in ngx_http_process_multi_header_lines()
  924. */
  925. return NGX_ERROR;
  926. }
  927. return NGX_OK;
  928. }
  929. ngx_int_t
  930. ngx_http_v3_read_request_body(ngx_http_request_t *r)
  931. {
  932. size_t preread;
  933. ngx_int_t rc;
  934. ngx_chain_t *cl, out;
  935. ngx_http_request_body_t *rb;
  936. ngx_http_core_loc_conf_t *clcf;
  937. rb = r->request_body;
  938. preread = r->header_in->last - r->header_in->pos;
  939. if (preread) {
  940. /* there is the pre-read part of the request body */
  941. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  942. "http3 client request body preread %uz", preread);
  943. out.buf = r->header_in;
  944. out.next = NULL;
  945. cl = &out;
  946. } else {
  947. cl = NULL;
  948. }
  949. rc = ngx_http_v3_request_body_filter(r, cl);
  950. if (rc != NGX_OK) {
  951. return rc;
  952. }
  953. if (rb->rest == 0 && rb->last_saved) {
  954. /* the whole request body was pre-read */
  955. r->request_body_no_buffering = 0;
  956. rb->post_handler(r);
  957. return NGX_OK;
  958. }
  959. if (rb->rest < 0) {
  960. ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  961. "negative request body rest");
  962. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  963. }
  964. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  965. rb->buf = ngx_create_temp_buf(r->pool, clcf->client_body_buffer_size);
  966. if (rb->buf == NULL) {
  967. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  968. }
  969. r->read_event_handler = ngx_http_v3_read_client_request_body_handler;
  970. r->write_event_handler = ngx_http_request_empty_handler;
  971. return ngx_http_v3_do_read_client_request_body(r);
  972. }
  973. static void
  974. ngx_http_v3_read_client_request_body_handler(ngx_http_request_t *r)
  975. {
  976. ngx_int_t rc;
  977. if (r->connection->read->timedout) {
  978. r->connection->timedout = 1;
  979. ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  980. return;
  981. }
  982. rc = ngx_http_v3_do_read_client_request_body(r);
  983. if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  984. ngx_http_finalize_request(r, rc);
  985. }
  986. }
  987. ngx_int_t
  988. ngx_http_v3_read_unbuffered_request_body(ngx_http_request_t *r)
  989. {
  990. ngx_int_t rc;
  991. if (r->connection->read->timedout) {
  992. r->connection->timedout = 1;
  993. return NGX_HTTP_REQUEST_TIME_OUT;
  994. }
  995. rc = ngx_http_v3_do_read_client_request_body(r);
  996. if (rc == NGX_OK) {
  997. r->reading_body = 0;
  998. }
  999. return rc;
  1000. }
  1001. static ngx_int_t
  1002. ngx_http_v3_do_read_client_request_body(ngx_http_request_t *r)
  1003. {
  1004. off_t rest;
  1005. size_t size;
  1006. ssize_t n;
  1007. ngx_int_t rc;
  1008. ngx_uint_t flush;
  1009. ngx_chain_t out;
  1010. ngx_connection_t *c;
  1011. ngx_http_request_body_t *rb;
  1012. ngx_http_core_loc_conf_t *clcf;
  1013. c = r->connection;
  1014. rb = r->request_body;
  1015. flush = 1;
  1016. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1017. "http3 read client request body");
  1018. for ( ;; ) {
  1019. for ( ;; ) {
  1020. if (rb->rest == 0) {
  1021. break;
  1022. }
  1023. if (rb->buf->last == rb->buf->end) {
  1024. /* update chains */
  1025. rc = ngx_http_v3_request_body_filter(r, NULL);
  1026. if (rc != NGX_OK) {
  1027. return rc;
  1028. }
  1029. if (rb->busy != NULL) {
  1030. if (r->request_body_no_buffering) {
  1031. if (c->read->timer_set) {
  1032. ngx_del_timer(c->read);
  1033. }
  1034. if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  1035. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1036. }
  1037. return NGX_AGAIN;
  1038. }
  1039. if (rb->filter_need_buffering) {
  1040. clcf = ngx_http_get_module_loc_conf(r,
  1041. ngx_http_core_module);
  1042. ngx_add_timer(c->read, clcf->client_body_timeout);
  1043. if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  1044. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1045. }
  1046. return NGX_AGAIN;
  1047. }
  1048. ngx_log_error(NGX_LOG_ALERT, c->log, 0,
  1049. "busy buffers after request body flush");
  1050. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1051. }
  1052. flush = 0;
  1053. rb->buf->pos = rb->buf->start;
  1054. rb->buf->last = rb->buf->start;
  1055. }
  1056. size = rb->buf->end - rb->buf->last;
  1057. rest = rb->rest - (rb->buf->last - rb->buf->pos);
  1058. if ((off_t) size > rest) {
  1059. size = (size_t) rest;
  1060. }
  1061. if (size == 0) {
  1062. break;
  1063. }
  1064. n = c->recv(c, rb->buf->last, size);
  1065. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1066. "http3 client request body recv %z", n);
  1067. if (n == NGX_AGAIN) {
  1068. break;
  1069. }
  1070. if (n == 0) {
  1071. rb->buf->last_buf = 1;
  1072. }
  1073. if (n == NGX_ERROR) {
  1074. c->error = 1;
  1075. return NGX_HTTP_BAD_REQUEST;
  1076. }
  1077. rb->buf->last += n;
  1078. /* pass buffer to request body filter chain */
  1079. flush = 0;
  1080. out.buf = rb->buf;
  1081. out.next = NULL;
  1082. rc = ngx_http_v3_request_body_filter(r, &out);
  1083. if (rc != NGX_OK) {
  1084. return rc;
  1085. }
  1086. if (rb->rest == 0) {
  1087. break;
  1088. }
  1089. if (rb->buf->last < rb->buf->end) {
  1090. break;
  1091. }
  1092. }
  1093. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1094. "http3 client request body rest %O", rb->rest);
  1095. if (flush) {
  1096. rc = ngx_http_v3_request_body_filter(r, NULL);
  1097. if (rc != NGX_OK) {
  1098. return rc;
  1099. }
  1100. }
  1101. if (rb->rest == 0 && rb->last_saved) {
  1102. break;
  1103. }
  1104. if (!c->read->ready || rb->rest == 0) {
  1105. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1106. ngx_add_timer(c->read, clcf->client_body_timeout);
  1107. if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  1108. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1109. }
  1110. return NGX_AGAIN;
  1111. }
  1112. }
  1113. if (c->read->timer_set) {
  1114. ngx_del_timer(c->read);
  1115. }
  1116. if (!r->request_body_no_buffering) {
  1117. r->read_event_handler = ngx_http_block_reading;
  1118. rb->post_handler(r);
  1119. }
  1120. return NGX_OK;
  1121. }
  1122. static ngx_int_t
  1123. ngx_http_v3_request_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
  1124. {
  1125. off_t max;
  1126. size_t size;
  1127. u_char *p;
  1128. ngx_int_t rc;
  1129. ngx_buf_t *b;
  1130. ngx_uint_t last;
  1131. ngx_chain_t *cl, *out, *tl, **ll;
  1132. ngx_http_v3_session_t *h3c;
  1133. ngx_http_request_body_t *rb;
  1134. ngx_http_core_loc_conf_t *clcf;
  1135. ngx_http_core_srv_conf_t *cscf;
  1136. ngx_http_v3_parse_data_t *st;
  1137. rb = r->request_body;
  1138. st = &r->v3_parse->body;
  1139. h3c = ngx_http_v3_get_session(r->connection);
  1140. if (rb->rest == -1) {
  1141. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1142. "http3 request body filter");
  1143. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  1144. rb->rest = cscf->large_client_header_buffers.size;
  1145. }
  1146. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1147. max = r->headers_in.content_length_n;
  1148. if (max == -1 && clcf->client_max_body_size) {
  1149. max = clcf->client_max_body_size;
  1150. }
  1151. out = NULL;
  1152. ll = &out;
  1153. last = 0;
  1154. for (cl = in; cl; cl = cl->next) {
  1155. ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
  1156. "http3 body buf "
  1157. "t:%d f:%d %p, pos %p, size: %z file: %O, size: %O",
  1158. cl->buf->temporary, cl->buf->in_file,
  1159. cl->buf->start, cl->buf->pos,
  1160. cl->buf->last - cl->buf->pos,
  1161. cl->buf->file_pos,
  1162. cl->buf->file_last - cl->buf->file_pos);
  1163. if (cl->buf->last_buf) {
  1164. last = 1;
  1165. }
  1166. b = NULL;
  1167. while (cl->buf->pos < cl->buf->last) {
  1168. if (st->length == 0) {
  1169. p = cl->buf->pos;
  1170. rc = ngx_http_v3_parse_data(r->connection, st, cl->buf);
  1171. r->request_length += cl->buf->pos - p;
  1172. h3c->total_bytes += cl->buf->pos - p;
  1173. if (ngx_http_v3_check_flood(r->connection) != NGX_OK) {
  1174. return NGX_HTTP_CLOSE;
  1175. }
  1176. if (rc == NGX_AGAIN) {
  1177. continue;
  1178. }
  1179. if (rc == NGX_DONE) {
  1180. last = 1;
  1181. goto done;
  1182. }
  1183. if (rc > 0) {
  1184. ngx_quic_reset_stream(r->connection, rc);
  1185. ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1186. "client sent invalid body");
  1187. return NGX_HTTP_BAD_REQUEST;
  1188. }
  1189. if (rc == NGX_ERROR) {
  1190. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1191. }
  1192. /* rc == NGX_OK */
  1193. if (max != -1 && (uint64_t) (max - rb->received) < st->length) {
  1194. if (r->headers_in.content_length_n != -1) {
  1195. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1196. "client intended to send body data "
  1197. "larger than declared");
  1198. return NGX_HTTP_BAD_REQUEST;
  1199. }
  1200. ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1201. "client intended to send too large "
  1202. "body: %O+%ui bytes",
  1203. rb->received, st->length);
  1204. return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
  1205. }
  1206. continue;
  1207. }
  1208. if (b
  1209. && st->length <= 128
  1210. && (uint64_t) (cl->buf->last - cl->buf->pos) >= st->length)
  1211. {
  1212. rb->received += st->length;
  1213. r->request_length += st->length;
  1214. h3c->total_bytes += st->length;
  1215. h3c->payload_bytes += st->length;
  1216. if (st->length < 8) {
  1217. while (st->length) {
  1218. *b->last++ = *cl->buf->pos++;
  1219. st->length--;
  1220. }
  1221. } else {
  1222. ngx_memmove(b->last, cl->buf->pos, st->length);
  1223. b->last += st->length;
  1224. cl->buf->pos += st->length;
  1225. st->length = 0;
  1226. }
  1227. continue;
  1228. }
  1229. tl = ngx_chain_get_free_buf(r->pool, &rb->free);
  1230. if (tl == NULL) {
  1231. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1232. }
  1233. b = tl->buf;
  1234. ngx_memzero(b, sizeof(ngx_buf_t));
  1235. b->temporary = 1;
  1236. b->tag = (ngx_buf_tag_t) &ngx_http_read_client_request_body;
  1237. b->start = cl->buf->pos;
  1238. b->pos = cl->buf->pos;
  1239. b->last = cl->buf->last;
  1240. b->end = cl->buf->end;
  1241. b->flush = r->request_body_no_buffering;
  1242. *ll = tl;
  1243. ll = &tl->next;
  1244. size = cl->buf->last - cl->buf->pos;
  1245. if (size > st->length) {
  1246. cl->buf->pos += (size_t) st->length;
  1247. rb->received += st->length;
  1248. r->request_length += st->length;
  1249. h3c->total_bytes += st->length;
  1250. h3c->payload_bytes += st->length;
  1251. st->length = 0;
  1252. } else {
  1253. st->length -= size;
  1254. rb->received += size;
  1255. r->request_length += size;
  1256. h3c->total_bytes += size;
  1257. h3c->payload_bytes += size;
  1258. cl->buf->pos = cl->buf->last;
  1259. }
  1260. b->last = cl->buf->pos;
  1261. }
  1262. }
  1263. done:
  1264. if (last) {
  1265. if (st->length > 0) {
  1266. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1267. "client prematurely closed stream");
  1268. r->connection->error = 1;
  1269. return NGX_HTTP_BAD_REQUEST;
  1270. }
  1271. if (r->headers_in.content_length_n == -1) {
  1272. r->headers_in.content_length_n = rb->received;
  1273. } else if (r->headers_in.content_length_n != rb->received) {
  1274. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1275. "client sent less body data than expected: "
  1276. "%O out of %O bytes of request body received",
  1277. rb->received, r->headers_in.content_length_n);
  1278. return NGX_HTTP_BAD_REQUEST;
  1279. }
  1280. rb->rest = 0;
  1281. tl = ngx_chain_get_free_buf(r->pool, &rb->free);
  1282. if (tl == NULL) {
  1283. return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1284. }
  1285. b = tl->buf;
  1286. ngx_memzero(b, sizeof(ngx_buf_t));
  1287. b->last_buf = 1;
  1288. *ll = tl;
  1289. } else {
  1290. /* set rb->rest, amount of data we want to see next time */
  1291. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  1292. rb->rest = (off_t) cscf->large_client_header_buffers.size;
  1293. }
  1294. rc = ngx_http_top_request_body_filter(r, out);
  1295. ngx_chain_update_chains(r->pool, &rb->free, &rb->busy, &out,
  1296. (ngx_buf_tag_t) &ngx_http_read_client_request_body);
  1297. return rc;
  1298. }