src/http/v3/ngx_http_v3_request.c - nginx-1.31.3 nginx/ @ 42f8df65b

Global variables defined

Data types defined

Functions defined

Source code


  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.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  505.         if (r->headers_in.count++ >= cscf->max_headers) {
  506.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  507.                           "client sent too many header lines");
  508.             ngx_http_finalize_request(r, NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
  509.             return NGX_ERROR;
  510.         }

  511.         h = ngx_list_push(&r->headers_in.headers);
  512.         if (h == NULL) {
  513.             ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  514.             return NGX_ERROR;
  515.         }

  516.         h->key = *name;
  517.         h->value = *value;
  518.         h->lowcase_key = h->key.data;
  519.         h->hash = ngx_hash_key(h->key.data, h->key.len);

  520.         cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

  521.         hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
  522.                            h->lowcase_key, h->key.len);

  523.         if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
  524.             return NGX_ERROR;
  525.         }
  526.     }

  527.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  528.                    "http3 header: \"%V: %V\"", name, value);
  529.     return NGX_OK;
  530. }


  531. static ngx_int_t
  532. ngx_http_v3_validate_header(ngx_http_request_t *r, ngx_str_t *name,
  533.     ngx_str_t *value)
  534. {
  535.     u_char                     ch;
  536.     ngx_uint_t                 i;
  537.     ngx_http_core_srv_conf_t  *cscf;

  538.     r->invalid_header = 0;

  539.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  540.     for (i = (name->data[0] == ':'); i != name->len; i++) {
  541.         ch = name->data[i];

  542.         if ((ch >= 'a' && ch <= 'z')
  543.             || (ch == '-')
  544.             || (ch >= '0' && ch <= '9')
  545.             || (ch == '_' && cscf->underscores_in_headers))
  546.         {
  547.             continue;
  548.         }

  549.         if (ch <= 0x20 || ch == 0x7f || ch == ':'
  550.             || (ch >= 'A' && ch <= 'Z'))
  551.         {
  552.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  553.                           "client sent invalid header name: \"%V\"", name);

  554.             return NGX_ERROR;
  555.         }

  556.         r->invalid_header = 1;
  557.     }

  558.     for (i = 0; i != value->len; i++) {
  559.         ch = value->data[i];

  560.         if (ch == '\0' || ch == LF || ch == CR) {
  561.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  562.                           "client sent header \"%V\" with "
  563.                           "invalid value: \"%V\"", name, value);

  564.             return NGX_ERROR;
  565.         }
  566.     }

  567.     return NGX_OK;
  568. }


  569. static ngx_int_t
  570. ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name,
  571.     ngx_str_t *value)
  572. {
  573.     u_char      ch, c;
  574.     ngx_uint_t  i;

  575.     if (r->request_line.len) {
  576.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  577.                       "client sent out of order pseudo-headers");
  578.         goto failed;
  579.     }

  580.     if (name->len == 7 && ngx_strncmp(name->data, ":method", 7) == 0) {

  581.         if (r->method_name.len) {
  582.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  583.                           "client sent duplicate \":method\" header");
  584.             goto failed;
  585.         }

  586.         if (value->len == 0) {
  587.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  588.                           "client sent empty \":method\" header");
  589.             goto failed;
  590.         }

  591.         r->method_name = *value;

  592.         for (i = 0; i < sizeof(ngx_http_v3_methods)
  593.                         / sizeof(ngx_http_v3_methods[0]); i++)
  594.         {
  595.             if (value->len == ngx_http_v3_methods[i].name.len
  596.                 && ngx_strncmp(value->data,
  597.                                ngx_http_v3_methods[i].name.data, value->len)
  598.                    == 0)
  599.             {
  600.                 r->method = ngx_http_v3_methods[i].method;
  601.                 break;
  602.             }
  603.         }

  604.         for (i = 0; i < value->len; i++) {
  605.             ch = value->data[i];

  606.             if ((ch < 'A' || ch > 'Z') && ch != '_' && ch != '-') {
  607.                 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  608.                               "client sent invalid method: \"%V\"", value);
  609.                 goto failed;
  610.             }
  611.         }

  612.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  613.                        "http3 method \"%V\" %ui", value, r->method);
  614.         return NGX_OK;
  615.     }

  616.     if (name->len == 5 && ngx_strncmp(name->data, ":path", 5) == 0) {

  617.         if (r->uri_start) {
  618.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  619.                           "client sent duplicate \":path\" header");
  620.             goto failed;
  621.         }

  622.         if (value->len == 0) {
  623.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  624.                           "client sent empty \":path\" header");
  625.             goto failed;
  626.         }

  627.         r->uri_start = value->data;
  628.         r->uri_end = value->data + value->len;

  629.         if (ngx_http_parse_uri(r) != NGX_OK) {
  630.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  631.                           "client sent invalid \":path\" header: \"%V\"",
  632.                           value);
  633.             goto failed;
  634.         }

  635.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  636.                        "http3 path \"%V\"", value);
  637.         return NGX_OK;
  638.     }

  639.     if (name->len == 7 && ngx_strncmp(name->data, ":scheme", 7) == 0) {

  640.         if (r->schema.len) {
  641.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  642.                           "client sent duplicate \":scheme\" header");
  643.             goto failed;
  644.         }

  645.         if (value->len == 0) {
  646.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  647.                           "client sent empty \":scheme\" header");
  648.             goto failed;
  649.         }

  650.         for (i = 0; i < value->len; i++) {
  651.             ch = value->data[i];

  652.             c = (u_char) (ch | 0x20);
  653.             if (c >= 'a' && c <= 'z') {
  654.                 continue;
  655.             }

  656.             if (((ch >= '0' && ch <= '9')
  657.                  || ch == '+' || ch == '-' || ch == '.')
  658.                 && i > 0)
  659.             {
  660.                 continue;
  661.             }

  662.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  663.                           "client sent invalid \":scheme\" header: \"%V\"",
  664.                           value);
  665.             goto failed;
  666.         }

  667.         r->schema = *value;

  668.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  669.                        "http3 schema \"%V\"", value);
  670.         return NGX_OK;
  671.     }

  672.     if (name->len == 10 && ngx_strncmp(name->data, ":authority", 10) == 0) {

  673.         if (r->host_start) {
  674.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  675.                           "client sent duplicate \":authority\" header");
  676.             goto failed;
  677.         }

  678.         r->host_start = value->data;
  679.         r->host_end = value->data + value->len;

  680.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  681.                        "http3 authority \"%V\"", value);
  682.         return NGX_OK;
  683.     }

  684.     ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  685.                   "client sent unknown pseudo-header \"%V\"", name);

  686. failed:

  687.     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  688.     return NGX_ERROR;
  689. }


  690. static ngx_int_t
  691. ngx_http_v3_init_pseudo_headers(ngx_http_request_t *r)
  692. {
  693.     size_t      len;
  694.     u_char     *p;
  695.     ngx_int_t   rc;
  696.     ngx_str_t   host;
  697.     in_port_t   port;

  698.     if (r->request_line.len) {
  699.         return NGX_OK;
  700.     }

  701.     if (r->method_name.len == 0) {
  702.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  703.                       "client sent no \":method\" header");
  704.         goto failed;
  705.     }

  706.     if (r->schema.len == 0) {
  707.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  708.                       "client sent no \":scheme\" header");
  709.         goto failed;
  710.     }

  711.     if (r->uri_start == NULL) {
  712.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  713.                       "client sent no \":path\" header");
  714.         goto failed;
  715.     }

  716.     len = r->method_name.len + 1
  717.           + (r->uri_end - r->uri_start) + 1
  718.           + sizeof("HTTP/3.0") - 1;

  719.     p = ngx_pnalloc(r->pool, len);
  720.     if (p == NULL) {
  721.         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  722.         return NGX_ERROR;
  723.     }

  724.     r->request_line.data = p;

  725.     p = ngx_cpymem(p, r->method_name.data, r->method_name.len);
  726.     *p++ = ' ';
  727.     p = ngx_cpymem(p, r->uri_start, r->uri_end - r->uri_start);
  728.     *p++ = ' ';
  729.     p = ngx_cpymem(p, "HTTP/3.0", sizeof("HTTP/3.0") - 1);

  730.     r->request_line.len = p - r->request_line.data;

  731.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  732.                    "http3 request line: \"%V\"", &r->request_line);

  733.     ngx_str_set(&r->http_protocol, "HTTP/3.0");

  734.     if (ngx_http_process_request_uri(r) != NGX_OK) {
  735.         return NGX_ERROR;
  736.     }

  737.     if (r->host_end) {

  738.         host.len = r->host_end - r->host_start;
  739.         host.data = r->host_start;

  740.         rc = ngx_http_validate_host(&host, &port, r->pool, 0);

  741.         if (rc == NGX_DECLINED) {
  742.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  743.                           "client sent invalid \":authority\" header");
  744.             goto failed;
  745.         }

  746.         if (rc == NGX_ERROR) {
  747.             ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  748.             return NGX_ERROR;
  749.         }

  750.         if (ngx_http_set_virtual_server(r, &host) == NGX_ERROR) {
  751.             return NGX_ERROR;
  752.         }

  753.         r->headers_in.server = host;
  754.         r->port = port;
  755.     }

  756.     if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
  757.                       sizeof(ngx_table_elt_t))
  758.         != NGX_OK)
  759.     {
  760.         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  761.         return NGX_ERROR;
  762.     }

  763.     return NGX_OK;

  764. failed:

  765.     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  766.     return NGX_ERROR;
  767. }


  768. static ngx_int_t
  769. ngx_http_v3_process_request_header(ngx_http_request_t *r)
  770. {
  771.     ssize_t                  n;
  772.     ngx_buf_t               *b;
  773.     ngx_str_t                host;
  774.     ngx_connection_t        *c;
  775.     ngx_http_v3_session_t   *h3c;
  776.     ngx_http_v3_srv_conf_t  *h3scf;

  777.     c = r->connection;

  778.     if (r->headers_in.connection) {
  779.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  780.                       "client sent \"Connection\" header");
  781.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  782.         return NGX_ERROR;
  783.     }

  784.     if (r->headers_in.keep_alive) {
  785.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  786.                       "client sent \"Keep-Alive\" header");
  787.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  788.         return NGX_ERROR;
  789.     }

  790.     if (r->headers_in.transfer_encoding) {
  791.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  792.                       "client sent \"Transfer-Encoding\" header");
  793.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  794.         return NGX_ERROR;
  795.     }

  796.     if (r->headers_in.upgrade) {
  797.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  798.                       "client sent \"Upgrade\" header");
  799.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  800.         return NGX_ERROR;
  801.     }

  802.     if (r->headers_in.te
  803.         && (r->headers_in.te->next
  804.             || r->headers_in.te->value.len != 8
  805.             || ngx_strncasecmp(r->headers_in.te->value.data,
  806.                                (u_char *) "trailers", 8) != 0))
  807.     {
  808.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  809.                       "client sent invalid \"TE\" header");
  810.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  811.         return NGX_ERROR;
  812.     }

  813.     if (ngx_http_v3_init_pseudo_headers(r) != NGX_OK) {
  814.         return NGX_ERROR;
  815.     }

  816.     h3c = ngx_http_v3_get_session(c);
  817.     h3scf = ngx_http_get_module_srv_conf(r, ngx_http_v3_module);

  818.     if ((h3c->hq && !h3scf->enable_hq) || (!h3c->hq && !h3scf->enable)) {
  819.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  820.                       "client attempted to request the server name "
  821.                       "for which the negotiated protocol is disabled");
  822.         ngx_http_finalize_request(r, NGX_HTTP_MISDIRECTED_REQUEST);
  823.         return NGX_ERROR;
  824.     }

  825.     if (ngx_http_v3_construct_cookie_header(r) != NGX_OK) {
  826.         return NGX_ERROR;
  827.     }

  828.     if (r->headers_in.server.len == 0) {
  829.         ngx_log_error(NGX_LOG_INFO, c->log, 0,
  830.                       "client sent neither \":authority\" nor \"Host\" header");
  831.         goto failed;
  832.     }

  833.     if (r->headers_in.host && r->host_end) {

  834.         host.len = r->host_end - r->host_start;
  835.         host.data = r->host_start;

  836.         if (r->headers_in.host->value.len != host.len
  837.             || ngx_memcmp(r->headers_in.host->value.data, host.data, host.len)
  838.                != 0)
  839.         {
  840.             ngx_log_error(NGX_LOG_INFO, c->log, 0,
  841.                           "client sent \":authority\" and \"Host\" headers "
  842.                           "with different values");
  843.             goto failed;
  844.         }
  845.     }

  846.     if (r->headers_in.content_length) {
  847.         r->headers_in.content_length_n =
  848.                             ngx_atoof(r->headers_in.content_length->value.data,
  849.                                       r->headers_in.content_length->value.len);

  850.         if (r->headers_in.content_length_n == NGX_ERROR) {
  851.             ngx_log_error(NGX_LOG_INFO, c->log, 0,
  852.                           "client sent invalid \"Content-Length\" header");
  853.             goto failed;
  854.         }

  855.     } else {
  856.         b = r->header_in;
  857.         n = b->last - b->pos;

  858.         if (n == 0) {
  859.             n = c->recv(c, b->start, b->end - b->start);

  860.             if (n == NGX_ERROR) {
  861.                 ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  862.                 return NGX_ERROR;
  863.             }

  864.             if (n > 0) {
  865.                 b->pos = b->start;
  866.                 b->last = b->start + n;
  867.             }
  868.         }

  869.         if (n != 0) {
  870.             r->headers_in.chunked = 1;
  871.         }
  872.     }

  873.     if (r->method == NGX_HTTP_CONNECT) {
  874.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client sent CONNECT method");
  875.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  876.         return NGX_ERROR;
  877.     }

  878.     if (r->method == NGX_HTTP_TRACE) {
  879.         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client sent TRACE method");
  880.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  881.         return NGX_ERROR;
  882.     }

  883.     return NGX_OK;

  884. failed:

  885.     ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  886.     return NGX_ERROR;
  887. }


  888. static ngx_int_t
  889. ngx_http_v3_cookie(ngx_http_request_t *r, ngx_str_t *value)
  890. {
  891.     ngx_str_t    *val;
  892.     ngx_array_t  *cookies;

  893.     cookies = r->v3_parse->cookies;

  894.     if (cookies == NULL) {
  895.         cookies = ngx_array_create(r->pool, 2, sizeof(ngx_str_t));
  896.         if (cookies == NULL) {
  897.             return NGX_ERROR;
  898.         }

  899.         r->v3_parse->cookies = cookies;
  900.     }

  901.     val = ngx_array_push(cookies);
  902.     if (val == NULL) {
  903.         return NGX_ERROR;
  904.     }

  905.     *val = *value;

  906.     return NGX_OK;
  907. }


  908. static ngx_int_t
  909. ngx_http_v3_construct_cookie_header(ngx_http_request_t *r)
  910. {
  911.     u_char                     *buf, *p, *end;
  912.     size_t                      len;
  913.     ngx_str_t                  *vals;
  914.     ngx_uint_t                  i;
  915.     ngx_array_t                *cookies;
  916.     ngx_table_elt_t            *h;
  917.     ngx_http_header_t          *hh;
  918.     ngx_http_core_main_conf_t  *cmcf;

  919.     static ngx_str_t cookie = ngx_string("cookie");

  920.     cookies = r->v3_parse->cookies;

  921.     if (cookies == NULL) {
  922.         return NGX_OK;
  923.     }

  924.     vals = cookies->elts;

  925.     i = 0;
  926.     len = 0;

  927.     do {
  928.         len += vals[i].len + 2;
  929.     } while (++i != cookies->nelts);

  930.     len -= 2;

  931.     buf = ngx_pnalloc(r->pool, len + 1);
  932.     if (buf == NULL) {
  933.         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  934.         return NGX_ERROR;
  935.     }

  936.     p = buf;
  937.     end = buf + len;

  938.     for (i = 0; /* void */ ; i++) {

  939.         p = ngx_cpymem(p, vals[i].data, vals[i].len);

  940.         if (p == end) {
  941.             *p = '\0';
  942.             break;
  943.         }

  944.         *p++ = ';'; *p++ = ' ';
  945.     }

  946.     h = ngx_list_push(&r->headers_in.headers);
  947.     if (h == NULL) {
  948.         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  949.         return NGX_ERROR;
  950.     }

  951.     h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
  952.                                     ngx_hash('c', 'o'), 'o'), 'k'), 'i'), 'e');

  953.     h->key.len = cookie.len;
  954.     h->key.data = cookie.data;

  955.     h->value.len = len;
  956.     h->value.data = buf;

  957.     h->lowcase_key = cookie.data;

  958.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

  959.     hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
  960.                        h->lowcase_key, h->key.len);

  961.     if (hh == NULL) {
  962.         ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  963.         return NGX_ERROR;
  964.     }

  965.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  966.         /*
  967.          * request has been finalized already
  968.          * in ngx_http_process_header_line()
  969.          */
  970.         return NGX_ERROR;
  971.     }

  972.     return NGX_OK;
  973. }


  974. ngx_int_t
  975. ngx_http_v3_read_request_body(ngx_http_request_t *r)
  976. {
  977.     size_t                     preread;
  978.     ngx_int_t                  rc;
  979.     ngx_chain_t               *cl, out;
  980.     ngx_http_request_body_t   *rb;
  981.     ngx_http_core_loc_conf_t  *clcf;

  982.     rb = r->request_body;

  983.     preread = r->header_in->last - r->header_in->pos;

  984.     if (preread) {

  985.         /* there is the pre-read part of the request body */

  986.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  987.                        "http3 client request body preread %uz", preread);

  988.         out.buf = r->header_in;
  989.         out.next = NULL;
  990.         cl = &out;

  991.     } else {
  992.         cl = NULL;
  993.     }

  994.     rc = ngx_http_v3_request_body_filter(r, cl);
  995.     if (rc != NGX_OK) {
  996.         return rc;
  997.     }

  998.     if (rb->rest == 0 && rb->last_saved) {
  999.         /* the whole request body was pre-read */
  1000.         r->request_body_no_buffering = 0;
  1001.         rb->post_handler(r);
  1002.         return NGX_OK;
  1003.     }

  1004.     if (rb->rest < 0) {
  1005.         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  1006.                       "negative request body rest");
  1007.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1008.     }

  1009.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  1010.     rb->buf = ngx_create_temp_buf(r->pool, clcf->client_body_buffer_size);
  1011.     if (rb->buf == NULL) {
  1012.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1013.     }

  1014.     r->read_event_handler = ngx_http_v3_read_client_request_body_handler;
  1015.     r->write_event_handler = ngx_http_request_empty_handler;

  1016.     return ngx_http_v3_do_read_client_request_body(r);
  1017. }


  1018. static void
  1019. ngx_http_v3_read_client_request_body_handler(ngx_http_request_t *r)
  1020. {
  1021.     ngx_int_t  rc;

  1022.     if (r->connection->read->timedout) {
  1023.         r->connection->timedout = 1;
  1024.         ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  1025.         return;
  1026.     }

  1027.     rc = ngx_http_v3_do_read_client_request_body(r);

  1028.     if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
  1029.         ngx_http_finalize_request(r, rc);
  1030.     }
  1031. }


  1032. ngx_int_t
  1033. ngx_http_v3_read_unbuffered_request_body(ngx_http_request_t *r)
  1034. {
  1035.     ngx_int_t  rc;

  1036.     if (r->connection->read->timedout) {
  1037.         r->connection->timedout = 1;
  1038.         return NGX_HTTP_REQUEST_TIME_OUT;
  1039.     }

  1040.     rc = ngx_http_v3_do_read_client_request_body(r);

  1041.     if (rc == NGX_OK) {
  1042.         r->reading_body = 0;
  1043.     }

  1044.     return rc;
  1045. }


  1046. static ngx_int_t
  1047. ngx_http_v3_do_read_client_request_body(ngx_http_request_t *r)
  1048. {
  1049.     off_t                      rest;
  1050.     size_t                     size;
  1051.     ssize_t                    n;
  1052.     ngx_int_t                  rc;
  1053.     ngx_uint_t                 flush;
  1054.     ngx_chain_t                out;
  1055.     ngx_connection_t          *c;
  1056.     ngx_http_request_body_t   *rb;
  1057.     ngx_http_core_loc_conf_t  *clcf;

  1058.     c = r->connection;
  1059.     rb = r->request_body;
  1060.     flush = 1;

  1061.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1062.                    "http3 read client request body");

  1063.     for ( ;; ) {
  1064.         for ( ;; ) {
  1065.             if (rb->rest == 0) {
  1066.                 break;
  1067.             }

  1068.             if (rb->buf->last == rb->buf->end) {

  1069.                 /* update chains */

  1070.                 rc = ngx_http_v3_request_body_filter(r, NULL);

  1071.                 if (rc != NGX_OK) {
  1072.                     return rc;
  1073.                 }

  1074.                 if (rb->busy != NULL) {
  1075.                     if (r->request_body_no_buffering) {
  1076.                         if (c->read->timer_set) {
  1077.                             ngx_del_timer(c->read);
  1078.                         }

  1079.                         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  1080.                             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1081.                         }

  1082.                         return NGX_AGAIN;
  1083.                     }

  1084.                     if (rb->filter_need_buffering) {
  1085.                         clcf = ngx_http_get_module_loc_conf(r,
  1086.                                                          ngx_http_core_module);
  1087.                         ngx_add_timer(c->read, clcf->client_body_timeout);

  1088.                         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  1089.                             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1090.                         }

  1091.                         return NGX_AGAIN;
  1092.                     }

  1093.                     ngx_log_error(NGX_LOG_ALERT, c->log, 0,
  1094.                                   "busy buffers after request body flush");

  1095.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1096.                 }

  1097.                 flush = 0;
  1098.                 rb->buf->pos = rb->buf->start;
  1099.                 rb->buf->last = rb->buf->start;
  1100.             }

  1101.             size = rb->buf->end - rb->buf->last;
  1102.             rest = rb->rest - (rb->buf->last - rb->buf->pos);

  1103.             if ((off_t) size > rest) {
  1104.                 size = (size_t) rest;
  1105.             }

  1106.             if (size == 0) {
  1107.                 break;
  1108.             }

  1109.             n = c->recv(c, rb->buf->last, size);

  1110.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1111.                            "http3 client request body recv %z", n);

  1112.             if (n == NGX_AGAIN) {
  1113.                 break;
  1114.             }

  1115.             if (n == 0) {
  1116.                 rb->buf->last_buf = 1;
  1117.             }

  1118.             if (n == NGX_ERROR) {
  1119.                 c->error = 1;
  1120.                 return NGX_HTTP_BAD_REQUEST;
  1121.             }

  1122.             rb->buf->last += n;

  1123.             /* pass buffer to request body filter chain */

  1124.             flush = 0;
  1125.             out.buf = rb->buf;
  1126.             out.next = NULL;

  1127.             rc = ngx_http_v3_request_body_filter(r, &out);

  1128.             if (rc != NGX_OK) {
  1129.                 return rc;
  1130.             }

  1131.             if (rb->rest == 0) {
  1132.                 break;
  1133.             }

  1134.             if (rb->buf->last < rb->buf->end) {
  1135.                 break;
  1136.             }
  1137.         }

  1138.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1139.                        "http3 client request body rest %O", rb->rest);

  1140.         if (flush) {
  1141.             rc = ngx_http_v3_request_body_filter(r, NULL);

  1142.             if (rc != NGX_OK) {
  1143.                 return rc;
  1144.             }
  1145.         }

  1146.         if (rb->rest == 0 && rb->last_saved) {
  1147.             break;
  1148.         }

  1149.         if (!c->read->ready || rb->rest == 0) {

  1150.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1151.             ngx_add_timer(c->read, clcf->client_body_timeout);

  1152.             if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  1153.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1154.             }

  1155.             return NGX_AGAIN;
  1156.         }
  1157.     }

  1158.     if (c->read->timer_set) {
  1159.         ngx_del_timer(c->read);
  1160.     }

  1161.     if (!r->request_body_no_buffering) {
  1162.         r->read_event_handler = ngx_http_block_reading;
  1163.         rb->post_handler(r);
  1164.     }

  1165.     return NGX_OK;
  1166. }


  1167. static ngx_int_t
  1168. ngx_http_v3_request_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
  1169. {
  1170.     off_t                      max;
  1171.     size_t                     size;
  1172.     u_char                    *p;
  1173.     ngx_int_t                  rc;
  1174.     ngx_buf_t                 *b;
  1175.     ngx_uint_t                 last;
  1176.     ngx_chain_t               *cl, *out, *tl, **ll;
  1177.     ngx_http_v3_session_t     *h3c;
  1178.     ngx_http_request_body_t   *rb;
  1179.     ngx_http_core_loc_conf_t  *clcf;
  1180.     ngx_http_core_srv_conf_t  *cscf;
  1181.     ngx_http_v3_parse_data_t  *st;

  1182.     rb = r->request_body;
  1183.     st = &r->v3_parse->body;

  1184.     h3c = ngx_http_v3_get_session(r->connection);

  1185.     if (rb->rest == -1) {

  1186.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1187.                        "http3 request body filter");

  1188.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  1189.         rb->rest = cscf->large_client_header_buffers.size;
  1190.     }

  1191.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  1192.     max = r->headers_in.content_length_n;

  1193.     if (max == -1 && clcf->client_max_body_size) {
  1194.         max = clcf->client_max_body_size;
  1195.     }

  1196.     out = NULL;
  1197.     ll = &out;
  1198.     last = 0;

  1199.     for (cl = in; cl; cl = cl->next) {

  1200.         ngx_log_debug7(NGX_LOG_DEBUG_EVENT, r->connection->log, 0,
  1201.                        "http3 body buf "
  1202.                        "t:%d f:%d %p, pos %p, size: %z file: %O, size: %O",
  1203.                        cl->buf->temporary, cl->buf->in_file,
  1204.                        cl->buf->start, cl->buf->pos,
  1205.                        cl->buf->last - cl->buf->pos,
  1206.                        cl->buf->file_pos,
  1207.                        cl->buf->file_last - cl->buf->file_pos);

  1208.         if (cl->buf->last_buf) {
  1209.             last = 1;
  1210.         }

  1211.         b = NULL;

  1212.         while (cl->buf->pos < cl->buf->last) {

  1213.             if (st->length == 0) {
  1214.                 p = cl->buf->pos;

  1215.                 rc = ngx_http_v3_parse_data(r->connection, st, cl->buf);

  1216.                 r->request_length += cl->buf->pos - p;
  1217.                 h3c->total_bytes += cl->buf->pos - p;

  1218.                 if (ngx_http_v3_check_flood(r->connection) != NGX_OK) {
  1219.                     return NGX_HTTP_CLOSE;
  1220.                 }

  1221.                 if (rc == NGX_AGAIN) {
  1222.                     continue;
  1223.                 }

  1224.                 if (rc == NGX_DONE) {
  1225.                     last = 1;
  1226.                     goto done;
  1227.                 }

  1228.                 if (rc > 0) {
  1229.                     ngx_quic_reset_stream(r->connection, rc);
  1230.                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1231.                                   "client sent invalid body");
  1232.                     return NGX_HTTP_BAD_REQUEST;
  1233.                 }

  1234.                 if (rc == NGX_ERROR) {
  1235.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1236.                 }

  1237.                 /* rc == NGX_OK */

  1238.                 if (max != -1 && (uint64_t) (max - rb->received) < st->length) {

  1239.                     if (r->headers_in.content_length_n != -1) {
  1240.                         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1241.                                       "client intended to send body data "
  1242.                                       "larger than declared");

  1243.                         return NGX_HTTP_BAD_REQUEST;
  1244.                     }

  1245.                     ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  1246.                                   "client intended to send too large "
  1247.                                   "body: %O+%ui bytes",
  1248.                                   rb->received, st->length);

  1249.                     return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
  1250.                 }

  1251.                 continue;
  1252.             }

  1253.             if (b
  1254.                 && st->length <= 128
  1255.                 && (uint64_t) (cl->buf->last - cl->buf->pos) >= st->length)
  1256.             {
  1257.                 rb->received += st->length;
  1258.                 r->request_length += st->length;
  1259.                 h3c->total_bytes += st->length;
  1260.                 h3c->payload_bytes += st->length;

  1261.                 if (st->length < 8) {

  1262.                     while (st->length) {
  1263.                         *b->last++ = *cl->buf->pos++;
  1264.                         st->length--;
  1265.                     }

  1266.                 } else {
  1267.                     ngx_memmove(b->last, cl->buf->pos, st->length);
  1268.                     b->last += st->length;
  1269.                     cl->buf->pos += st->length;
  1270.                     st->length = 0;
  1271.                 }

  1272.                 continue;
  1273.             }

  1274.             tl = ngx_chain_get_free_buf(r->pool, &rb->free);
  1275.             if (tl == NULL) {
  1276.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1277.             }

  1278.             b = tl->buf;

  1279.             ngx_memzero(b, sizeof(ngx_buf_t));

  1280.             b->temporary = 1;
  1281.             b->tag = (ngx_buf_tag_t) &ngx_http_read_client_request_body;
  1282.             b->start = cl->buf->pos;
  1283.             b->pos = cl->buf->pos;
  1284.             b->last = cl->buf->last;
  1285.             b->end = cl->buf->end;
  1286.             b->flush = r->request_body_no_buffering;

  1287.             *ll = tl;
  1288.             ll = &tl->next;

  1289.             size = cl->buf->last - cl->buf->pos;

  1290.             if (size > st->length) {
  1291.                 cl->buf->pos += (size_t) st->length;
  1292.                 rb->received += st->length;
  1293.                 r->request_length += st->length;
  1294.                 h3c->total_bytes += st->length;
  1295.                 h3c->payload_bytes += st->length;
  1296.                 st->length = 0;

  1297.             } else {
  1298.                 st->length -= size;
  1299.                 rb->received += size;
  1300.                 r->request_length += size;
  1301.                 h3c->total_bytes += size;
  1302.                 h3c->payload_bytes += size;
  1303.                 cl->buf->pos = cl->buf->last;
  1304.             }

  1305.             b->last = cl->buf->pos;
  1306.         }
  1307.     }

  1308. done:

  1309.     if (last) {

  1310.         if (st->length > 0) {
  1311.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1312.                           "client prematurely closed stream");
  1313.             r->connection->error = 1;
  1314.             return NGX_HTTP_BAD_REQUEST;
  1315.         }

  1316.         if (r->headers_in.content_length_n == -1) {
  1317.             r->headers_in.content_length_n = rb->received;

  1318.         } else if (r->headers_in.content_length_n != rb->received) {
  1319.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1320.                           "client sent less body data than expected: "
  1321.                           "%O out of %O bytes of request body received",
  1322.                           rb->received, r->headers_in.content_length_n);
  1323.             return NGX_HTTP_BAD_REQUEST;
  1324.         }

  1325.         rb->rest = 0;

  1326.         tl = ngx_chain_get_free_buf(r->pool, &rb->free);
  1327.         if (tl == NULL) {
  1328.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  1329.         }

  1330.         b = tl->buf;

  1331.         ngx_memzero(b, sizeof(ngx_buf_t));

  1332.         b->last_buf = 1;

  1333.         *ll = tl;

  1334.     } else {

  1335.         /* set rb->rest, amount of data we want to see next time */

  1336.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  1337.         rb->rest = (off_t) cscf->large_client_header_buffers.size;
  1338.     }

  1339.     rc = ngx_http_top_request_body_filter(r, out);

  1340.     ngx_chain_update_chains(r->pool, &rb->free, &rb->busy, &out,
  1341.                             (ngx_buf_tag_t) &ngx_http_read_client_request_body);

  1342.     return rc;
  1343. }