src/http/v2/ngx_http_v2.c - nginx-1.31.9 nginx/ @ 2a3a19c6f

Global variables defined

Data types defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. * Copyright (C) Valentin V. Bartenev
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8. #include <ngx_http_v2_module.h>


  9. /* errors */
  10. ‌#define NGX_HTTP_V2_NO_ERROR                     0x0
  11. ‌#define NGX_HTTP_V2_PROTOCOL_ERROR               0x1
  12. ‌#define NGX_HTTP_V2_INTERNAL_ERROR               0x2
  13. ‌#define NGX_HTTP_V2_FLOW_CTRL_ERROR              0x3
  14. ‌#define NGX_HTTP_V2_SETTINGS_TIMEOUT             0x4
  15. ‌#define NGX_HTTP_V2_STREAM_CLOSED                0x5
  16. ‌#define NGX_HTTP_V2_SIZE_ERROR                   0x6
  17. ‌#define NGX_HTTP_V2_REFUSED_STREAM               0x7
  18. ‌#define NGX_HTTP_V2_CANCEL                       0x8
  19. ‌#define NGX_HTTP_V2_COMP_ERROR                   0x9
  20. ‌#define NGX_HTTP_V2_CONNECT_ERROR                0xa
  21. ‌#define NGX_HTTP_V2_ENHANCE_YOUR_CALM            0xb
  22. ‌#define NGX_HTTP_V2_INADEQUATE_SECURITY          0xc
  23. ‌#define NGX_HTTP_V2_HTTP_1_1_REQUIRED            0xd

  24. /* frame sizes */
  25. ‌#define NGX_HTTP_V2_SETTINGS_ACK_SIZE            0
  26. ‌#define NGX_HTTP_V2_RST_STREAM_SIZE              4
  27. ‌#define NGX_HTTP_V2_PRIORITY_SIZE                5
  28. ‌#define NGX_HTTP_V2_PING_SIZE                    8
  29. ‌#define NGX_HTTP_V2_GOAWAY_SIZE                  8
  30. ‌#define NGX_HTTP_V2_WINDOW_UPDATE_SIZE           4

  31. ‌#define NGX_HTTP_V2_SETTINGS_PARAM_SIZE          6

  32. /* settings fields */
  33. ‌#define NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING    0x1
  34. ‌#define NGX_HTTP_V2_ENABLE_PUSH_SETTING          0x2
  35. ‌#define NGX_HTTP_V2_MAX_STREAMS_SETTING          0x3
  36. ‌#define NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING     0x4
  37. ‌#define NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING       0x5
  38. ‌#define NGX_HTTP_V2_NO_PRIORITIES_SETTING        0x9

  39. ‌#define NGX_HTTP_V2_FRAME_BUFFER_SIZE            24


  40. static void ngx_http_v2_read_handler(ngx_event_t *rev);
  41. static void ngx_http_v2_write_handler(ngx_event_t *wev);
  42. static void ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c);
  43. static void ngx_http_v2_lingering_close(ngx_connection_t *c);
  44. static void ngx_http_v2_lingering_close_handler(ngx_event_t *rev);

  45. static u_char *ngx_http_v2_state_preface(ngx_http_v2_connection_t *h2c,
  46.     u_char *pos, u_char *end);
  47. static u_char *ngx_http_v2_state_preface_end(ngx_http_v2_connection_t *h2c,
  48.     u_char *pos, u_char *end);
  49. static u_char *ngx_http_v2_state_head(ngx_http_v2_connection_t *h2c,
  50.     u_char *pos, u_char *end);
  51. static u_char *ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c,
  52.     u_char *pos, u_char *end);
  53. static u_char *ngx_http_v2_state_read_data(ngx_http_v2_connection_t *h2c,
  54.     u_char *pos, u_char *end);
  55. static u_char *ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c,
  56.     u_char *pos, u_char *end);
  57. static u_char *ngx_http_v2_state_header_block(ngx_http_v2_connection_t *h2c,
  58.     u_char *pos, u_char *end);
  59. static u_char *ngx_http_v2_state_field_len(ngx_http_v2_connection_t *h2c,
  60.     u_char *pos, u_char *end);
  61. static u_char *ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c,
  62.     u_char *pos, u_char *end);
  63. static u_char *ngx_http_v2_state_field_raw(ngx_http_v2_connection_t *h2c,
  64.     u_char *pos, u_char *end);
  65. static u_char *ngx_http_v2_state_field_skip(ngx_http_v2_connection_t *h2c,
  66.     u_char *pos, u_char *end);
  67. static u_char *ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c,
  68.     u_char *pos, u_char *end);
  69. static u_char *ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c,
  70.     u_char *pos, u_char *end);
  71. static u_char *ngx_http_v2_handle_continuation(ngx_http_v2_connection_t *h2c,
  72.     u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
  73. static u_char *ngx_http_v2_state_priority(ngx_http_v2_connection_t *h2c,
  74.     u_char *pos, u_char *end);
  75. static u_char *ngx_http_v2_state_priority_update(ngx_http_v2_connection_t *h2c,
  76.     u_char *pos, u_char *end);
  77. static u_char *ngx_http_v2_state_rst_stream(ngx_http_v2_connection_t *h2c,
  78.     u_char *pos, u_char *end);
  79. static u_char *ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c,
  80.     u_char *pos, u_char *end);
  81. static u_char *ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c,
  82.     u_char *pos, u_char *end);
  83. static u_char *ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c,
  84.     u_char *pos, u_char *end);
  85. static u_char *ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c,
  86.     u_char *pos, u_char *end);
  87. static u_char *ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c,
  88.     u_char *pos, u_char *end);
  89. static u_char *ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c,
  90.     u_char *pos, u_char *end);
  91. static u_char *ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c,
  92.     u_char *pos, u_char *end);
  93. static u_char *ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c,
  94.     u_char *pos, u_char *end);
  95. static u_char *ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c,
  96.     u_char *pos, u_char *end);
  97. static u_char *ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c,
  98.     u_char *pos, u_char *end);
  99. static u_char *ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c,
  100.     u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
  101. static u_char *ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c,
  102.     u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
  103. static u_char *ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
  104.     ngx_uint_t err);

  105. static ngx_int_t ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c,
  106.     u_char **pos, u_char *end, ngx_uint_t prefix);

  107. static ngx_http_v2_stream_t *ngx_http_v2_create_stream(
  108.     ngx_http_v2_connection_t *h2c);
  109. static ngx_http_v2_node_t *ngx_http_v2_get_node_by_id(
  110.     ngx_http_v2_connection_t *h2c, ngx_uint_t sid, ngx_uint_t alloc);
  111. static ngx_http_v2_node_t *ngx_http_v2_get_closed_node(
  112.     ngx_http_v2_connection_t *h2c);
  113. ‌#define ngx_http_v2_index_size(h2scf)  (h2scf->streams_index_mask + 1)
  114. ‌#define ngx_http_v2_index(h2scf, sid)  ((sid >> 1) & h2scf->streams_index_mask)

  115. static ngx_int_t ngx_http_v2_send_settings(ngx_http_v2_connection_t *h2c);
  116. static ngx_int_t ngx_http_v2_settings_frame_handler(
  117.     ngx_http_v2_connection_t *h2c, ngx_http_v2_out_frame_t *frame);
  118. static ngx_int_t ngx_http_v2_send_window_update(ngx_http_v2_connection_t *h2c,
  119.     ngx_uint_t sid, size_t window);
  120. static ngx_int_t ngx_http_v2_send_rst_stream(ngx_http_v2_connection_t *h2c,
  121.     ngx_uint_t sid, ngx_uint_t status);
  122. static ngx_int_t ngx_http_v2_send_goaway(ngx_http_v2_connection_t *h2c,
  123.     ngx_uint_t status);

  124. static ngx_http_v2_out_frame_t *ngx_http_v2_get_frame(
  125.     ngx_http_v2_connection_t *h2c, size_t length, ngx_uint_t type,
  126.     u_char flags, ngx_uint_t sid);
  127. static ngx_int_t ngx_http_v2_frame_handler(ngx_http_v2_connection_t *h2c,
  128.     ngx_http_v2_out_frame_t *frame);

  129. static ngx_int_t ngx_http_v2_validate_header(ngx_http_request_t *r,
  130.     ngx_http_v2_header_t *header);
  131. static ngx_int_t ngx_http_v2_pseudo_header(ngx_http_request_t *r,
  132.     ngx_http_v2_header_t *header);
  133. static ngx_int_t ngx_http_v2_parse_path(ngx_http_request_t *r,
  134.     ngx_str_t *value);
  135. static ngx_int_t ngx_http_v2_parse_method(ngx_http_request_t *r,
  136.     ngx_str_t *value);
  137. static ngx_int_t ngx_http_v2_parse_scheme(ngx_http_request_t *r,
  138.     ngx_str_t *value);
  139. static ngx_int_t ngx_http_v2_parse_authority(ngx_http_request_t *r,
  140.     ngx_str_t *value);
  141. static ngx_int_t ngx_http_v2_construct_request_line(ngx_http_request_t *r);
  142. static ngx_int_t ngx_http_v2_cookie(ngx_http_request_t *r,
  143.     ngx_http_v2_header_t *header);
  144. static ngx_int_t ngx_http_v2_construct_cookie_header(ngx_http_request_t *r);
  145. static ngx_int_t ngx_http_v2_priority(ngx_http_request_t *r,
  146.     ngx_http_v2_header_t *header);
  147. static ngx_int_t ngx_http_v2_process_priority(ngx_http_request_t *r);
  148. static ngx_int_t ngx_http_v2_construct_host_header(ngx_http_request_t *r);
  149. static void ngx_http_v2_run_request(ngx_http_request_t *r);
  150. static ngx_int_t ngx_http_v2_process_request_body(ngx_http_request_t *r,
  151.     u_char *pos, size_t size, ngx_uint_t last, ngx_uint_t flush);
  152. static ngx_int_t ngx_http_v2_filter_request_body(ngx_http_request_t *r);
  153. static void ngx_http_v2_read_client_request_body_handler(ngx_http_request_t *r);

  154. static ngx_int_t ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
  155.     ngx_http_v2_stream_t *stream, ngx_uint_t status);
  156. static void ngx_http_v2_close_stream_handler(ngx_event_t *ev);
  157. static void ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev);
  158. static void ngx_http_v2_handle_connection_handler(ngx_event_t *rev);
  159. static void ngx_http_v2_idle_handler(ngx_event_t *rev);
  160. static void ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
  161.     ngx_uint_t status);

  162. static ngx_int_t ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c,
  163.     ssize_t delta);

  164. static void ngx_http_v2_pool_cleanup(void *data);


  165. ‌static ngx_http_v2_handler_pt ngx_http_v2_frame_states[] = {
  166.     ngx_http_v2_state_data,               /* NGX_HTTP_V2_DATA_FRAME */
  167.     ngx_http_v2_state_headers,            /* NGX_HTTP_V2_HEADERS_FRAME */
  168.     ngx_http_v2_state_priority,           /* NGX_HTTP_V2_PRIORITY_FRAME */
  169.     ngx_http_v2_state_rst_stream,         /* NGX_HTTP_V2_RST_STREAM_FRAME */
  170.     ngx_http_v2_state_settings,           /* NGX_HTTP_V2_SETTINGS_FRAME */
  171.     ngx_http_v2_state_push_promise,       /* NGX_HTTP_V2_PUSH_PROMISE_FRAME */
  172.     ngx_http_v2_state_ping,               /* NGX_HTTP_V2_PING_FRAME */
  173.     ngx_http_v2_state_goaway,             /* NGX_HTTP_V2_GOAWAY_FRAME */
  174.     ngx_http_v2_state_window_update,      /* NGX_HTTP_V2_WINDOW_UPDATE_FRAME */
  175.     ngx_http_v2_state_continuation        /* NGX_HTTP_V2_CONTINUATION_FRAME */
  176. };

  177. ‌#define NGX_HTTP_V2_FRAME_STATES                                              \
  178.     (sizeof(ngx_http_v2_frame_states) / sizeof(ngx_http_v2_handler_pt))


  179. void
  180. ‌ngx_http_v2_init(ngx_event_t *rev)
  181. {
  182.     u_char                    *p, *end;
  183.     ngx_connection_t          *c;
  184.     ngx_pool_cleanup_t        *cln;
  185.     ngx_http_connection_t     *hc;
  186.     ngx_http_v2_srv_conf_t    *h2scf;
  187.     ngx_http_v2_main_conf_t   *h2mcf;
  188.     ngx_http_v2_connection_t  *h2c;
  189.     ngx_http_core_srv_conf_t  *cscf;

  190.     c = rev->data;
  191.     hc = c->data;

  192.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "init http2 connection");

  193.     c->log->action = "processing HTTP/2 connection";

  194.     h2mcf = ngx_http_get_module_main_conf(hc->conf_ctx, ngx_http_v2_module);

  195.     if (h2mcf->recv_buffer == NULL) {
  196.         h2mcf->recv_buffer = ngx_palloc(ngx_cycle->pool,
  197.                                         h2mcf->recv_buffer_size);
  198.         if (h2mcf->recv_buffer == NULL) {
  199.             ngx_http_close_connection(c);
  200.             return;
  201.         }
  202.     }

  203.     h2c = ngx_pcalloc(c->pool, sizeof(ngx_http_v2_connection_t));
  204.     if (h2c == NULL) {
  205.         ngx_http_close_connection(c);
  206.         return;
  207.     }

  208.     h2c->connection = c;
  209.     h2c->http_connection = hc;

  210.     h2c->send_window = NGX_HTTP_V2_DEFAULT_WINDOW;
  211.     h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;

  212.     h2c->init_window = NGX_HTTP_V2_DEFAULT_WINDOW;

  213.     h2c->frame_size = NGX_HTTP_V2_DEFAULT_FRAME_SIZE;

  214.     h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);

  215.     h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
  216.     if (h2c->pool == NULL) {
  217.         ngx_http_close_connection(c);
  218.         return;
  219.     }

  220.     cln = ngx_pool_cleanup_add(c->pool, 0);
  221.     if (cln == NULL) {
  222.         ngx_http_close_connection(c);
  223.         return;
  224.     }

  225.     cln->handler = ngx_http_v2_pool_cleanup;
  226.     cln->data = h2c;

  227.     h2c->streams_index = ngx_pcalloc(c->pool, ngx_http_v2_index_size(h2scf)
  228.                                               * sizeof(ngx_http_v2_node_t *));
  229.     if (h2c->streams_index == NULL) {
  230.         ngx_http_close_connection(c);
  231.         return;
  232.     }

  233.     if (ngx_http_v2_send_settings(h2c) == NGX_ERROR) {
  234.         ngx_http_close_connection(c);
  235.         return;
  236.     }

  237.     if (ngx_http_v2_send_window_update(h2c, 0, NGX_HTTP_V2_MAX_WINDOW
  238.                                                - NGX_HTTP_V2_DEFAULT_WINDOW)
  239.         == NGX_ERROR)
  240.     {
  241.         ngx_http_close_connection(c);
  242.         return;
  243.     }

  244.     h2c->state.handler = ngx_http_v2_state_preface;

  245.     ngx_queue_init(&h2c->waiting);
  246.     ngx_queue_init(&h2c->closed);

  247.     c->data = h2c;

  248.     if (ngx_exiting) {
  249.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  250.         return;
  251.     }

  252.     rev->handler = ngx_http_v2_read_handler;
  253.     c->write->handler = ngx_http_v2_write_handler;

  254.     if (!rev->timer_set) {
  255.         cscf = ngx_http_get_module_srv_conf(hc->conf_ctx,
  256.                                             ngx_http_core_module);
  257.         ngx_add_timer(rev, cscf->client_header_timeout);
  258.     }

  259.     c->idle = 1;
  260.     ngx_reusable_connection(c, 0);

  261.     if (c->buffer) {
  262.         p = c->buffer->pos;
  263.         end = c->buffer->last;

  264.         do {
  265.             p = h2c->state.handler(h2c, p, end);

  266.             if (p == NULL) {
  267.                 return;
  268.             }

  269.         } while (p != end);

  270.         h2c->total_bytes += p - c->buffer->pos;
  271.         c->buffer->pos = p;
  272.     }

  273.     ngx_http_v2_read_handler(rev);
  274. }


  275. static void
  276. ‌ngx_http_v2_read_handler(ngx_event_t *rev)
  277. {
  278.     u_char                    *p, *end;
  279.     size_t                     available;
  280.     ssize_t                    n;
  281.     ngx_connection_t          *c;
  282.     ngx_http_v2_main_conf_t   *h2mcf;
  283.     ngx_http_v2_connection_t  *h2c;

  284.     c = rev->data;
  285.     h2c = c->data;

  286.     if (rev->timedout) {
  287.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  288.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  289.         return;
  290.     }

  291.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 read handler");

  292.     h2c->blocked = 1;
  293.     h2c->new_streams = 0;

  294.     if (c->close) {
  295.         c->close = 0;

  296.         if (c->error) {
  297.             ngx_http_v2_finalize_connection(h2c, 0);
  298.             return;
  299.         }

  300.         if (!h2c->processing) {
  301.             ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  302.             return;
  303.         }

  304.         if (!h2c->goaway) {
  305.             h2c->goaway = 1;

  306.             if (ngx_http_v2_send_goaway(h2c, NGX_HTTP_V2_NO_ERROR)
  307.                 == NGX_ERROR)
  308.             {
  309.                 ngx_http_v2_finalize_connection(h2c, 0);
  310.                 return;
  311.             }

  312.             if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  313.                 ngx_http_v2_finalize_connection(h2c, 0);
  314.                 return;
  315.             }
  316.         }

  317.         h2c->blocked = 0;

  318.         return;
  319.     }

  320.     h2mcf = ngx_http_get_module_main_conf(h2c->http_connection->conf_ctx,
  321.                                           ngx_http_v2_module);

  322.     available = h2mcf->recv_buffer_size - NGX_HTTP_V2_STATE_BUFFER_SIZE;

  323.     do {
  324.         p = h2mcf->recv_buffer;
  325.         end = ngx_cpymem(p, h2c->state.buffer, h2c->state.buffer_used);

  326.         n = c->recv(c, end, available);

  327.         if (n == NGX_AGAIN) {
  328.             break;
  329.         }

  330.         if (n == 0 && (h2c->state.incomplete || h2c->processing)) {
  331.             ngx_log_error(NGX_LOG_INFO, c->log, 0,
  332.                           "client prematurely closed connection");
  333.         }

  334.         if (n == 0 || n == NGX_ERROR) {
  335.             c->error = 1;
  336.             ngx_http_v2_finalize_connection(h2c, 0);
  337.             return;
  338.         }

  339.         end += n;

  340.         h2c->state.buffer_used = 0;
  341.         h2c->state.incomplete = 0;

  342.         do {
  343.             p = h2c->state.handler(h2c, p, end);

  344.             if (p == NULL) {
  345.                 return;
  346.             }

  347.         } while (p != end);

  348.         h2c->total_bytes += n;

  349.         if (h2c->total_bytes / 8 > h2c->payload_bytes + 1048576) {
  350.             ngx_log_error(NGX_LOG_INFO, c->log, 0, "http2 flood detected");
  351.             ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  352.             return;
  353.         }

  354.     } while (rev->ready);

  355.     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  356.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  357.         return;
  358.     }

  359.     if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  360.         ngx_http_v2_finalize_connection(h2c, 0);
  361.         return;
  362.     }

  363.     h2c->blocked = 0;

  364.     ngx_http_v2_handle_connection(h2c);
  365. }


  366. static void
  367. ‌ngx_http_v2_write_handler(ngx_event_t *wev)
  368. {
  369.     ngx_int_t                  rc;
  370.     ngx_connection_t          *c;
  371.     ngx_http_v2_connection_t  *h2c;

  372.     c = wev->data;
  373.     h2c = c->data;

  374.     if (wev->timedout) {
  375.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
  376.                        "http2 write event timed out");
  377.         c->error = 1;
  378.         c->timedout = 1;
  379.         ngx_http_v2_finalize_connection(h2c, 0);
  380.         return;
  381.     }

  382.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 write handler");

  383.     if (h2c->last_out == NULL && !c->buffered) {

  384.         if (wev->timer_set) {
  385.             ngx_del_timer(wev);
  386.         }

  387.         ngx_http_v2_handle_connection(h2c);
  388.         return;
  389.     }

  390.     h2c->blocked = 1;

  391.     rc = ngx_http_v2_send_output_queue(h2c);

  392.     if (rc == NGX_ERROR) {
  393.         ngx_http_v2_finalize_connection(h2c, 0);
  394.         return;
  395.     }

  396.     h2c->blocked = 0;

  397.     if (rc == NGX_AGAIN) {
  398.         return;
  399.     }

  400.     ngx_http_v2_handle_connection(h2c);
  401. }


  402. ngx_int_t
  403. ‌ngx_http_v2_send_output_queue(ngx_http_v2_connection_t *h2c)
  404. {
  405.     int                        tcp_nodelay;
  406.     ngx_chain_t               *cl;
  407.     ngx_event_t               *wev;
  408.     ngx_connection_t          *c;
  409.     ngx_http_v2_out_frame_t   *out, *frame, *fn;
  410.     ngx_http_core_loc_conf_t  *clcf;

  411.     c = h2c->connection;
  412.     wev = c->write;

  413.     if (c->error) {
  414.         goto error;
  415.     }

  416.     if (!wev->ready) {
  417.         return NGX_AGAIN;
  418.     }

  419.     cl = NULL;
  420.     out = NULL;

  421.     for (frame = h2c->last_out; frame; frame = fn) {
  422.         frame->last->next = cl;
  423.         cl = frame->first;

  424.         fn = frame->next;
  425.         frame->next = out;
  426.         out = frame;

  427.         ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
  428.                        "http2 frame out: %p sid:%ui bl:%d len:%uz",
  429.                        out, out->stream ? out->stream->node->id : 0,
  430.                        out->blocked, out->length);
  431.     }

  432.     cl = c->send_chain(c, cl, 0);

  433.     if (cl == NGX_CHAIN_ERROR) {
  434.         goto error;
  435.     }

  436.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  437.                                         ngx_http_core_module);

  438.     if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
  439.         goto error;
  440.     }

  441.     if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
  442.         if (ngx_tcp_push(c->fd) == -1) {
  443.             ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
  444.             goto error;
  445.         }

  446.         c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
  447.         tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;

  448.     } else {
  449.         tcp_nodelay = 1;
  450.     }

  451.     if (tcp_nodelay && clcf->tcp_nodelay && ngx_tcp_nodelay(c) != NGX_OK) {
  452.         goto error;
  453.     }

  454.     for ( /* void */ ; out; out = fn) {
  455.         fn = out->next;

  456.         if (out->handler(h2c, out) != NGX_OK) {
  457.             out->blocked = 1;
  458.             break;
  459.         }

  460.         ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
  461.                        "http2 frame sent: %p sid:%ui bl:%d len:%uz",
  462.                        out, out->stream ? out->stream->node->id : 0,
  463.                        out->blocked, out->length);
  464.     }

  465.     frame = NULL;

  466.     for ( /* void */ ; out; out = fn) {
  467.         fn = out->next;
  468.         out->next = frame;
  469.         frame = out;
  470.     }

  471.     h2c->last_out = frame;

  472.     if (!wev->ready) {
  473.         ngx_add_timer(wev, clcf->send_timeout);
  474.         return NGX_AGAIN;
  475.     }

  476.     if (wev->timer_set) {
  477.         ngx_del_timer(wev);
  478.     }

  479.     return NGX_OK;

  480. error:

  481.     c->error = 1;

  482.     if (!h2c->blocked) {
  483.         ngx_post_event(wev, &ngx_posted_events);
  484.     }

  485.     return NGX_ERROR;
  486. }


  487. static void
  488. ‌ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c)
  489. {
  490.     ngx_int_t                  rc;
  491.     ngx_connection_t          *c;
  492.     ngx_http_core_loc_conf_t  *clcf;

  493.     if (h2c->last_out || h2c->processing) {
  494.         return;
  495.     }

  496.     c = h2c->connection;

  497.     if (c->error) {
  498.         ngx_http_close_connection(c);
  499.         return;
  500.     }

  501.     if (c->buffered) {
  502.         h2c->blocked = 1;

  503.         rc = ngx_http_v2_send_output_queue(h2c);

  504.         h2c->blocked = 0;

  505.         if (rc == NGX_ERROR) {
  506.             ngx_http_close_connection(c);
  507.             return;
  508.         }

  509.         if (rc == NGX_AGAIN) {
  510.             return;
  511.         }

  512.         /* rc == NGX_OK */
  513.     }

  514.     if (h2c->goaway) {
  515.         ngx_http_v2_lingering_close(c);
  516.         return;
  517.     }

  518.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  519.                                         ngx_http_core_module);

  520.     if (!c->read->timer_set) {
  521.         ngx_add_timer(c->read, clcf->keepalive_timeout);
  522.     }

  523.     ngx_reusable_connection(c, 1);

  524.     if (h2c->state.incomplete) {
  525.         return;
  526.     }

  527.     ngx_destroy_pool(h2c->pool);

  528.     h2c->pool = NULL;
  529.     h2c->free_frames = NULL;
  530.     h2c->frames = 0;
  531.     h2c->free_fake_connections = NULL;

  532. #if (NGX_HTTP_SSL)
  533.     if (c->ssl) {
  534.         ngx_ssl_free_buffer(c);
  535.     }
  536. #endif

  537.     c->destroyed = 1;

  538.     c->write->handler = ngx_http_empty_handler;
  539.     c->read->handler = ngx_http_v2_idle_handler;

  540.     if (c->write->timer_set) {
  541.         ngx_del_timer(c->write);
  542.     }
  543. }


  544. static void
  545. ‌ngx_http_v2_lingering_close(ngx_connection_t *c)
  546. {
  547.     ngx_event_t               *rev, *wev;
  548.     ngx_http_v2_connection_t  *h2c;
  549.     ngx_http_core_loc_conf_t  *clcf;

  550.     h2c = c->data;

  551.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  552.                                         ngx_http_core_module);

  553.     if (clcf->lingering_close == NGX_HTTP_LINGERING_OFF) {
  554.         ngx_http_close_connection(c);
  555.         return;
  556.     }

  557.     if (h2c->lingering_time == 0) {
  558.         h2c->lingering_time = ngx_time()
  559.                               + (time_t) (clcf->lingering_time / 1000);
  560.     }

  561. #if (NGX_HTTP_SSL)
  562.     if (c->ssl) {
  563.         ngx_int_t  rc;

  564.         rc = ngx_ssl_shutdown(c);

  565.         if (rc == NGX_ERROR) {
  566.             ngx_http_close_connection(c);
  567.             return;
  568.         }

  569.         if (rc == NGX_AGAIN) {
  570.             c->ssl->handler = ngx_http_v2_lingering_close;
  571.             return;
  572.         }
  573.     }
  574. #endif

  575.     rev = c->read;
  576.     rev->handler = ngx_http_v2_lingering_close_handler;

  577.     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  578.         ngx_http_close_connection(c);
  579.         return;
  580.     }

  581.     wev = c->write;
  582.     wev->handler = ngx_http_empty_handler;

  583.     if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
  584.         if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
  585.             ngx_http_close_connection(c);
  586.             return;
  587.         }
  588.     }

  589.     if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
  590.         ngx_connection_error(c, ngx_socket_errno,
  591.                              ngx_shutdown_socket_n " failed");
  592.         ngx_http_close_connection(c);
  593.         return;
  594.     }

  595.     c->close = 0;
  596.     ngx_reusable_connection(c, 1);

  597.     ngx_add_timer(rev, clcf->lingering_timeout);

  598.     if (rev->ready) {
  599.         ngx_http_v2_lingering_close_handler(rev);
  600.     }
  601. }


  602. static void
  603. ‌ngx_http_v2_lingering_close_handler(ngx_event_t *rev)
  604. {
  605.     ssize_t                    n;
  606.     ngx_msec_t                 timer;
  607.     ngx_connection_t          *c;
  608.     ngx_http_core_loc_conf_t  *clcf;
  609.     ngx_http_v2_connection_t  *h2c;
  610.     u_char                     buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];

  611.     c = rev->data;
  612.     h2c = c->data;

  613.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
  614.                    "http2 lingering close handler");

  615.     if (rev->timedout || c->close) {
  616.         ngx_http_close_connection(c);
  617.         return;
  618.     }

  619.     timer = (ngx_msec_t) h2c->lingering_time - (ngx_msec_t) ngx_time();
  620.     if ((ngx_msec_int_t) timer <= 0) {
  621.         ngx_http_close_connection(c);
  622.         return;
  623.     }

  624.     do {
  625.         n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);

  626.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %z", n);

  627.         if (n == NGX_AGAIN) {
  628.             break;
  629.         }

  630.         if (n == NGX_ERROR || n == 0) {
  631.             ngx_http_close_connection(c);
  632.             return;
  633.         }

  634.     } while (rev->ready);

  635.     if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  636.         ngx_http_close_connection(c);
  637.         return;
  638.     }

  639.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  640.                                         ngx_http_core_module);
  641.     timer *= 1000;

  642.     if (timer > clcf->lingering_timeout) {
  643.         timer = clcf->lingering_timeout;
  644.     }

  645.     ngx_add_timer(rev, timer);
  646. }


  647. static u_char *
  648. ‌ngx_http_v2_state_preface(ngx_http_v2_connection_t *h2c, u_char *pos,
  649.     u_char *end)
  650. {
  651.     static const u_char preface[] = NGX_HTTP_V2_PREFACE_START;

  652.     if ((size_t) (end - pos) < sizeof(preface) - 1) {
  653.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_preface);
  654.     }

  655.     if (ngx_memcmp(pos, preface, sizeof(preface) - 1) != 0) {
  656.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  657.                       "invalid connection preface");

  658.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  659.     }

  660.     return ngx_http_v2_state_preface_end(h2c, pos + sizeof(preface) - 1, end);
  661. }


  662. static u_char *
  663. ‌ngx_http_v2_state_preface_end(ngx_http_v2_connection_t *h2c, u_char *pos,
  664.     u_char *end)
  665. {
  666.     static const u_char preface[] = NGX_HTTP_V2_PREFACE_END;

  667.     if ((size_t) (end - pos) < sizeof(preface) - 1) {
  668.         return ngx_http_v2_state_save(h2c, pos, end,
  669.                                       ngx_http_v2_state_preface_end);
  670.     }

  671.     if (ngx_memcmp(pos, preface, sizeof(preface) - 1) != 0) {
  672.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  673.                       "invalid connection preface");

  674.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  675.     }

  676.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  677.                    "http2 preface verified");

  678.     return ngx_http_v2_state_head(h2c, pos + sizeof(preface) - 1, end);
  679. }


  680. static u_char *
  681. ‌ngx_http_v2_state_head(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  682. {
  683.     uint32_t    head;
  684.     ngx_uint_t  type;

  685.     if (end - pos < NGX_HTTP_V2_FRAME_HEADER_SIZE) {
  686.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_head);
  687.     }

  688.     head = ngx_http_v2_parse_uint32(pos);

  689.     h2c->state.length = ngx_http_v2_parse_length(head);
  690.     h2c->state.flags = pos[4];

  691.     h2c->state.sid = ngx_http_v2_parse_sid(&pos[5]);

  692.     pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;

  693.     type = ngx_http_v2_parse_type(head);

  694.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  695.                    "http2 frame type:%ui f:%Xd l:%uz sid:%ui",
  696.                    type, h2c->state.flags, h2c->state.length, h2c->state.sid);

  697.     if (type >= NGX_HTTP_V2_FRAME_STATES) {

  698.         if (type == NGX_HTTP_V2_PRIORITY_UPDATE_FRAME) {
  699.             return ngx_http_v2_state_priority_update(h2c, pos, end);
  700.         }

  701.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  702.                       "client sent frame with unknown type %ui", type);
  703.         return ngx_http_v2_state_skip(h2c, pos, end);
  704.     }

  705.     return ngx_http_v2_frame_states[type](h2c, pos, end);
  706. }


  707. static u_char *
  708. ‌ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  709. {
  710.     size_t                 size;
  711.     ngx_http_v2_node_t    *node;
  712.     ngx_http_v2_stream_t  *stream;

  713.     size = h2c->state.length;

  714.     if (h2c->state.flags & NGX_HTTP_V2_PADDED_FLAG) {

  715.         if (h2c->state.length == 0) {
  716.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  717.                           "client sent padded DATA frame "
  718.                           "with incorrect length: 0");

  719.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  720.         }

  721.         if (end - pos == 0) {
  722.             return ngx_http_v2_state_save(h2c, pos, end,
  723.                                           ngx_http_v2_state_data);
  724.         }

  725.         h2c->state.padding = *pos++;

  726.         if (h2c->state.padding >= size) {
  727.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  728.                           "client sent padded DATA frame "
  729.                           "with incorrect length: %uz, padding: %uz",
  730.                           size, h2c->state.padding);

  731.             return ngx_http_v2_connection_error(h2c,
  732.                                                 NGX_HTTP_V2_PROTOCOL_ERROR);
  733.         }

  734.         h2c->state.length -= 1 + h2c->state.padding;
  735.     }

  736.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  737.                    "http2 DATA frame");

  738.     if (h2c->state.sid == 0) {
  739.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  740.                       "client sent DATA frame with incorrect identifier");

  741.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  742.     }

  743.     if (size > h2c->recv_window) {
  744.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  745.                       "client violated connection flow control: "
  746.                       "received DATA frame length %uz, available window %uz",
  747.                       size, h2c->recv_window);

  748.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
  749.     }

  750.     h2c->recv_window -= size;

  751.     if (h2c->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4) {

  752.         if (ngx_http_v2_send_window_update(h2c, 0, NGX_HTTP_V2_MAX_WINDOW
  753.                                                    - h2c->recv_window)
  754.             == NGX_ERROR)
  755.         {
  756.             return ngx_http_v2_connection_error(h2c,
  757.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  758.         }

  759.         h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;
  760.     }

  761.     node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);

  762.     if (node == NULL || node->stream == NULL) {
  763.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  764.                        "unknown http2 stream");

  765.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  766.     }

  767.     stream = node->stream;

  768.     if (size > stream->recv_window) {
  769.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  770.                       "client violated flow control for stream %ui: "
  771.                       "received DATA frame length %uz, available window %uz",
  772.                       node->id, size, stream->recv_window);

  773.         if (ngx_http_v2_terminate_stream(h2c, stream,
  774.                                          NGX_HTTP_V2_FLOW_CTRL_ERROR)
  775.             == NGX_ERROR)
  776.         {
  777.             return ngx_http_v2_connection_error(h2c,
  778.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  779.         }

  780.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  781.     }

  782.     stream->recv_window -= size;

  783.     if (stream->no_flow_control
  784.         && stream->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4)
  785.     {
  786.         if (ngx_http_v2_send_window_update(h2c, node->id,
  787.                                            NGX_HTTP_V2_MAX_WINDOW
  788.                                            - stream->recv_window)
  789.             == NGX_ERROR)
  790.         {
  791.             return ngx_http_v2_connection_error(h2c,
  792.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  793.         }

  794.         stream->recv_window = NGX_HTTP_V2_MAX_WINDOW;
  795.     }

  796.     if (stream->in_closed) {
  797.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  798.                       "client sent DATA frame for half-closed stream %ui",
  799.                       node->id);

  800.         if (ngx_http_v2_terminate_stream(h2c, stream,
  801.                                          NGX_HTTP_V2_STREAM_CLOSED)
  802.             == NGX_ERROR)
  803.         {
  804.             return ngx_http_v2_connection_error(h2c,
  805.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  806.         }

  807.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  808.     }

  809.     h2c->state.stream = stream;

  810.     return ngx_http_v2_state_read_data(h2c, pos, end);
  811. }


  812. static u_char *
  813. ‌ngx_http_v2_state_read_data(ngx_http_v2_connection_t *h2c, u_char *pos,
  814.     u_char *end)
  815. {
  816.     size_t                   size;
  817.     ngx_buf_t               *buf;
  818.     ngx_int_t                rc;
  819.     ngx_connection_t        *fc;
  820.     ngx_http_request_t      *r;
  821.     ngx_http_v2_stream_t    *stream;
  822.     ngx_http_v2_srv_conf_t  *h2scf;

  823.     stream = h2c->state.stream;

  824.     if (stream == NULL) {
  825.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  826.     }

  827.     if (stream->skip_data) {
  828.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  829.                        "skipping http2 DATA frame");

  830.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  831.     }

  832.     r = stream->request;
  833.     fc = r->connection;

  834.     if (r->reading_body && !r->request_body_no_buffering) {
  835.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  836.                        "skipping http2 DATA frame");

  837.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  838.     }

  839.     if (r->headers_in.content_length_n < 0 && !r->headers_in.chunked) {
  840.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  841.                        "skipping http2 DATA frame");

  842.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  843.     }

  844.     size = end - pos;

  845.     if (size >= h2c->state.length) {
  846.         size = h2c->state.length;
  847.         stream->in_closed = h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG;
  848.     }

  849.     h2c->payload_bytes += size;

  850.     if (r->request_body) {
  851.         rc = ngx_http_v2_process_request_body(r, pos, size,
  852.                                               stream->in_closed, 0);

  853.         if (rc != NGX_OK && rc != NGX_AGAIN) {
  854.             stream->skip_data = 1;
  855.             ngx_http_finalize_request(r, rc);
  856.         }

  857.         ngx_http_run_posted_requests(fc);

  858.     } else if (size) {
  859.         buf = stream->preread;

  860.         if (buf == NULL) {
  861.             h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

  862.             buf = ngx_create_temp_buf(r->pool, h2scf->preread_size);
  863.             if (buf == NULL) {
  864.                 return ngx_http_v2_connection_error(h2c,
  865.                                                     NGX_HTTP_V2_INTERNAL_ERROR);
  866.             }

  867.             stream->preread = buf;
  868.         }

  869.         if (size > (size_t) (buf->end - buf->last)) {
  870.             ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  871.                           "http2 preread buffer overflow");
  872.             return ngx_http_v2_connection_error(h2c,
  873.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  874.         }

  875.         buf->last = ngx_cpymem(buf->last, pos, size);
  876.     }

  877.     pos += size;
  878.     h2c->state.length -= size;

  879.     if (h2c->state.length) {
  880.         return ngx_http_v2_state_save(h2c, pos, end,
  881.                                       ngx_http_v2_state_read_data);
  882.     }

  883.     if (h2c->state.padding) {
  884.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  885.     }

  886.     return ngx_http_v2_state_complete(h2c, pos, end);
  887. }


  888. static u_char *
  889. ‌ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c, u_char *pos,
  890.     u_char *end)
  891. {
  892.     size_t                     size;
  893.     ngx_uint_t                 padded, priority;
  894.     ngx_uint_t                 status;
  895.     ngx_http_v2_node_t        *node;
  896.     ngx_http_v2_stream_t      *stream;
  897.     ngx_http_v2_srv_conf_t    *h2scf;
  898.     ngx_http_core_srv_conf_t  *cscf;
  899.     ngx_http_core_loc_conf_t  *clcf;

  900.     padded = h2c->state.flags & NGX_HTTP_V2_PADDED_FLAG;
  901.     priority = h2c->state.flags & NGX_HTTP_V2_PRIORITY_FLAG;

  902.     size = 0;

  903.     if (padded) {
  904.         size++;
  905.     }

  906.     if (priority) {
  907.         size += sizeof(uint32_t) + 1;
  908.     }

  909.     if (h2c->state.length < size) {
  910.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  911.                       "client sent HEADERS frame with incorrect length %uz",
  912.                       h2c->state.length);

  913.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  914.     }

  915.     if (h2c->state.length == size) {
  916.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  917.                       "client sent HEADERS frame with empty header block");

  918.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  919.     }

  920.     if (h2c->goaway) {
  921.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  922.                        "skipping http2 HEADERS frame");
  923.         return ngx_http_v2_state_skip(h2c, pos, end);
  924.     }

  925.     if ((size_t) (end - pos) < size) {
  926.         return ngx_http_v2_state_save(h2c, pos, end,
  927.                                       ngx_http_v2_state_headers);
  928.     }

  929.     h2c->state.length -= size;

  930.     if (padded) {
  931.         h2c->state.padding = *pos++;

  932.         if (h2c->state.padding > h2c->state.length) {
  933.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  934.                           "client sent padded HEADERS frame "
  935.                           "with incorrect length: %uz, padding: %uz",
  936.                           h2c->state.length, h2c->state.padding);

  937.             return ngx_http_v2_connection_error(h2c,
  938.                                                 NGX_HTTP_V2_PROTOCOL_ERROR);
  939.         }

  940.         h2c->state.length -= h2c->state.padding;
  941.     }

  942.     if (priority) {
  943.         /* skip the ignored RFC 7540 priority data */
  944.         pos += sizeof(uint32_t) + 1;
  945.     }

  946.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  947.                    "http2 HEADERS frame sid:%ui", h2c->state.sid);

  948.     if (h2c->state.sid % 2 == 0 || h2c->state.sid <= h2c->last_sid) {
  949.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  950.                       "client sent HEADERS frame with incorrect identifier "
  951.                       "%ui, the last was %ui", h2c->state.sid, h2c->last_sid);

  952.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  953.     }

  954.     h2c->last_sid = h2c->state.sid;

  955.     h2c->state.pool = ngx_create_pool(1024, h2c->connection->log);
  956.     if (h2c->state.pool == NULL) {
  957.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  958.     }

  959.     cscf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  960.                                         ngx_http_core_module);

  961.     h2c->state.header_limit = cscf->large_client_header_buffers.size
  962.                               * cscf->large_client_header_buffers.num;

  963.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  964.                                          ngx_http_v2_module);

  965.     if (h2c->processing >= h2scf->concurrent_streams) {
  966.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  967.                       "concurrent streams exceeded %ui", h2c->processing);

  968.         status = NGX_HTTP_V2_REFUSED_STREAM;
  969.         goto rst_stream;
  970.     }

  971.     if (h2c->new_streams++ >= 2 * h2scf->concurrent_streams) {
  972.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  973.                       "client sent too many streams at once");

  974.         status = NGX_HTTP_V2_REFUSED_STREAM;
  975.         goto rst_stream;
  976.     }

  977.     if (!h2c->settings_ack
  978.         && !(h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG)
  979.         && h2scf->preread_size < NGX_HTTP_V2_DEFAULT_WINDOW)
  980.     {
  981.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  982.                       "client sent stream with data "
  983.                       "before settings were acknowledged");

  984.         status = NGX_HTTP_V2_REFUSED_STREAM;
  985.         goto rst_stream;
  986.     }

  987.     node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 1);

  988.     if (node == NULL) {
  989.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  990.     }

  991.     if (node->priority_set) {
  992.         ngx_queue_remove(&node->reuse);
  993.         h2c->closed_nodes--;
  994.     }

  995.     stream = ngx_http_v2_create_stream(h2c);
  996.     if (stream == NULL) {
  997.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  998.     }

  999.     h2c->state.stream = stream;

  1000.     stream->pool = h2c->state.pool;
  1001.     h2c->state.keep_pool = 1;

  1002.     stream->request->request_length = h2c->state.length;

  1003.     stream->in_closed = h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG;
  1004.     stream->node = node;

  1005.     node->stream = stream;

  1006.     /* a PRIORITY_UPDATE may have arrived before HEADERS */

  1007.     if (node->priority_set) {
  1008.         stream->priority.client = node->priority;
  1009.         node->priority_set = 0;
  1010.         stream->priority_update = 1;

  1011.         ngx_http_priority_state_update(&stream->priority);

  1012.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1013.                        "http2 buffered priority: u=%ud i=%ud",
  1014.                        stream->priority.effective.urgency,
  1015.                        stream->priority.effective.incremental);
  1016.     }

  1017.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  1018.                                         ngx_http_core_module);

  1019.     if (clcf->keepalive_timeout == 0
  1020.         || h2c->connection->requests >= clcf->keepalive_requests
  1021.         || ngx_current_msec - h2c->connection->start_time
  1022.            > clcf->keepalive_time)
  1023.     {
  1024.         h2c->goaway = 1;

  1025.         if (ngx_http_v2_send_goaway(h2c, NGX_HTTP_V2_NO_ERROR) == NGX_ERROR) {
  1026.             return ngx_http_v2_connection_error(h2c,
  1027.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1028.         }
  1029.     }

  1030.     return ngx_http_v2_state_header_block(h2c, pos, end);

  1031. rst_stream:

  1032.     if (h2c->refused_streams++ > ngx_max(h2scf->concurrent_streams, 100)) {
  1033.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1034.                       "client sent too many refused streams");
  1035.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_NO_ERROR);
  1036.     }

  1037.     if (ngx_http_v2_send_rst_stream(h2c, h2c->state.sid, status) != NGX_OK) {
  1038.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1039.     }

  1040.     return ngx_http_v2_state_header_block(h2c, pos, end);
  1041. }


  1042. static u_char *
  1043. ‌ngx_http_v2_state_header_block(ngx_http_v2_connection_t *h2c, u_char *pos,
  1044.     u_char *end)
  1045. {
  1046.     u_char      ch;
  1047.     ngx_int_t   value;
  1048.     ngx_uint_t  indexed, size_update, prefix;

  1049.     if (end - pos < 1) {
  1050.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1051.                                               ngx_http_v2_state_header_block);
  1052.     }

  1053.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)
  1054.         && h2c->state.length < NGX_HTTP_V2_INT_OCTETS)
  1055.     {
  1056.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1057.                                                ngx_http_v2_state_header_block);
  1058.     }

  1059.     size_update = 0;
  1060.     indexed = 0;

  1061.     ch = *pos;

  1062.     if (ch >= (1 << 7)) {
  1063.         /* indexed header field */
  1064.         indexed = 1;
  1065.         prefix = ngx_http_v2_prefix(7);

  1066.     } else if (ch >= (1 << 6)) {
  1067.         /* literal header field with incremental indexing */
  1068.         h2c->state.index = 1;
  1069.         prefix = ngx_http_v2_prefix(6);

  1070.     } else if (ch >= (1 << 5)) {
  1071.         /* dynamic table size update */
  1072.         size_update = 1;
  1073.         prefix = ngx_http_v2_prefix(5);

  1074.     } else if (ch >= (1 << 4)) {
  1075.         /* literal header field never indexed */
  1076.         prefix = ngx_http_v2_prefix(4);

  1077.     } else {
  1078.         /* literal header field without indexing */
  1079.         prefix = ngx_http_v2_prefix(4);
  1080.     }

  1081.     value = ngx_http_v2_parse_int(h2c, &pos, end, prefix);

  1082.     if (value < 0) {
  1083.         if (value == NGX_AGAIN) {
  1084.             return ngx_http_v2_state_headers_save(h2c, pos, end,
  1085.                                                ngx_http_v2_state_header_block);
  1086.         }

  1087.         if (value == NGX_DECLINED) {
  1088.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1089.                           "client sent header block with too long %s value",
  1090.                           size_update ? "size update" : "header index");

  1091.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1092.         }

  1093.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1094.                       "client sent header block with incorrect length");

  1095.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1096.     }

  1097.     if (indexed) {
  1098.         if (ngx_http_v2_get_indexed_header(h2c, value, 0) != NGX_OK) {
  1099.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1100.         }

  1101.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1102.     }

  1103.     if (size_update) {
  1104.         if (ngx_http_v2_table_size(h2c, value) != NGX_OK) {
  1105.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1106.         }

  1107.         return ngx_http_v2_state_header_complete(h2c, pos, end);
  1108.     }

  1109.     if (value == 0) {
  1110.         h2c->state.parse_name = 1;

  1111.     } else if (ngx_http_v2_get_indexed_header(h2c, value, 1) != NGX_OK) {
  1112.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1113.     }

  1114.     h2c->state.parse_value = 1;

  1115.     return ngx_http_v2_state_field_len(h2c, pos, end);
  1116. }


  1117. static u_char *
  1118. ‌ngx_http_v2_state_field_len(ngx_http_v2_connection_t *h2c, u_char *pos,
  1119.     u_char *end)
  1120. {
  1121.     size_t                     alloc;
  1122.     ngx_int_t                  len;
  1123.     ngx_uint_t                 huff;
  1124.     ngx_http_core_srv_conf_t  *cscf;

  1125.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)
  1126.         && h2c->state.length < NGX_HTTP_V2_INT_OCTETS)
  1127.     {
  1128.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1129.                                                ngx_http_v2_state_field_len);
  1130.     }

  1131.     if (h2c->state.length < 1) {
  1132.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1133.                       "client sent header block with incorrect length");

  1134.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1135.     }

  1136.     if (end - pos < 1) {
  1137.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1138.                                               ngx_http_v2_state_field_len);
  1139.     }

  1140.     huff = *pos >> 7;
  1141.     len = ngx_http_v2_parse_int(h2c, &pos, end, ngx_http_v2_prefix(7));

  1142.     if (len < 0) {
  1143.         if (len == NGX_AGAIN) {
  1144.             return ngx_http_v2_state_headers_save(h2c, pos, end,
  1145.                                                   ngx_http_v2_state_field_len);
  1146.         }

  1147.         if (len == NGX_DECLINED) {
  1148.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1149.                         "client sent header field with too long length value");

  1150.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1151.         }

  1152.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1153.                       "client sent header block with incorrect length");

  1154.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1155.     }

  1156.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1157.                    "http2 %s string, len:%i",
  1158.                    huff ? "encoded" : "raw", len);

  1159.     cscf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  1160.                                         ngx_http_core_module);

  1161.     if ((size_t) len > cscf->large_client_header_buffers.size) {
  1162.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1163.                       "client sent too large header field");

  1164.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
  1165.     }

  1166.     h2c->state.field_rest = len;

  1167.     if (h2c->state.stream == NULL && !h2c->state.index) {
  1168.         return ngx_http_v2_state_field_skip(h2c, pos, end);
  1169.     }

  1170.     alloc = (huff ? len * 8 / 5 : len) + 1;

  1171.     h2c->state.field_start = ngx_pnalloc(h2c->state.pool, alloc);
  1172.     if (h2c->state.field_start == NULL) {
  1173.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1174.     }

  1175.     h2c->state.field_end = h2c->state.field_start;

  1176.     if (huff) {
  1177.         return ngx_http_v2_state_field_huff(h2c, pos, end);
  1178.     }

  1179.     return ngx_http_v2_state_field_raw(h2c, pos, end);
  1180. }


  1181. static u_char *
  1182. ‌ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c, u_char *pos,
  1183.     u_char *end)
  1184. {
  1185.     size_t  size;

  1186.     size = end - pos;

  1187.     if (size > h2c->state.field_rest) {
  1188.         size = h2c->state.field_rest;
  1189.     }

  1190.     if (size > h2c->state.length) {
  1191.         size = h2c->state.length;
  1192.     }

  1193.     h2c->state.length -= size;
  1194.     h2c->state.field_rest -= size;

  1195.     if (ngx_http_huff_decode(&h2c->state.field_state, pos, size,
  1196.                              &h2c->state.field_end,
  1197.                              h2c->state.field_rest == 0,
  1198.                              h2c->connection->log)
  1199.         != NGX_OK)
  1200.     {
  1201.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1202.                       "client sent invalid encoded header field");

  1203.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1204.     }

  1205.     pos += size;

  1206.     if (h2c->state.field_rest == 0) {
  1207.         *h2c->state.field_end = '\0';
  1208.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1209.     }

  1210.     if (h2c->state.length) {
  1211.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1212.                                               ngx_http_v2_state_field_huff);
  1213.     }

  1214.     if (h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG) {
  1215.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1216.                       "client sent header field with incorrect length");

  1217.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1218.     }

  1219.     return ngx_http_v2_handle_continuation(h2c, pos, end,
  1220.                                            ngx_http_v2_state_field_huff);
  1221. }


  1222. static u_char *
  1223. ‌ngx_http_v2_state_field_raw(ngx_http_v2_connection_t *h2c, u_char *pos,
  1224.     u_char *end)
  1225. {
  1226.     size_t  size;

  1227.     size = end - pos;

  1228.     if (size > h2c->state.field_rest) {
  1229.         size = h2c->state.field_rest;
  1230.     }

  1231.     if (size > h2c->state.length) {
  1232.         size = h2c->state.length;
  1233.     }

  1234.     h2c->state.length -= size;
  1235.     h2c->state.field_rest -= size;

  1236.     h2c->state.field_end = ngx_cpymem(h2c->state.field_end, pos, size);

  1237.     pos += size;

  1238.     if (h2c->state.field_rest == 0) {
  1239.         *h2c->state.field_end = '\0';
  1240.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1241.     }

  1242.     if (h2c->state.length) {
  1243.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1244.                                               ngx_http_v2_state_field_raw);
  1245.     }

  1246.     if (h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG) {
  1247.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1248.                       "client sent header field with incorrect length");

  1249.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1250.     }

  1251.     return ngx_http_v2_handle_continuation(h2c, pos, end,
  1252.                                            ngx_http_v2_state_field_raw);
  1253. }


  1254. static u_char *
  1255. ‌ngx_http_v2_state_field_skip(ngx_http_v2_connection_t *h2c, u_char *pos,
  1256.     u_char *end)
  1257. {
  1258.     size_t  size;

  1259.     size = end - pos;

  1260.     if (size > h2c->state.field_rest) {
  1261.         size = h2c->state.field_rest;
  1262.     }

  1263.     if (size > h2c->state.length) {
  1264.         size = h2c->state.length;
  1265.     }

  1266.     h2c->state.length -= size;
  1267.     h2c->state.field_rest -= size;

  1268.     pos += size;

  1269.     if (h2c->state.field_rest == 0) {
  1270.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1271.     }

  1272.     if (h2c->state.length) {
  1273.         return ngx_http_v2_state_save(h2c, pos, end,
  1274.                                       ngx_http_v2_state_field_skip);
  1275.     }

  1276.     if (h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG) {
  1277.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1278.                       "client sent header field with incorrect length");

  1279.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1280.     }

  1281.     return ngx_http_v2_handle_continuation(h2c, pos, end,
  1282.                                            ngx_http_v2_state_field_skip);
  1283. }


  1284. static u_char *
  1285. ‌ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c, u_char *pos,
  1286.     u_char *end)
  1287. {
  1288.     size_t                      len;
  1289.     ngx_int_t                   rc;
  1290.     ngx_table_elt_t            *h;
  1291.     ngx_connection_t           *fc;
  1292.     ngx_http_header_t          *hh;
  1293.     ngx_http_request_t         *r;
  1294.     ngx_http_v2_header_t       *header;
  1295.     ngx_http_core_srv_conf_t   *cscf;
  1296.     ngx_http_core_main_conf_t  *cmcf;

  1297.     static ngx_str_t cookie = ngx_string("cookie");
  1298.     static ngx_str_t priority = ngx_string("priority");

  1299.     header = &h2c->state.header;

  1300.     if (h2c->state.parse_name) {
  1301.         h2c->state.parse_name = 0;

  1302.         header->name.len = h2c->state.field_end - h2c->state.field_start;
  1303.         header->name.data = h2c->state.field_start;

  1304.         if (header->name.len == 0) {
  1305.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1306.                           "client sent zero header name length");

  1307.             return ngx_http_v2_connection_error(h2c,
  1308.                                                 NGX_HTTP_V2_PROTOCOL_ERROR);
  1309.         }

  1310.         return ngx_http_v2_state_field_len(h2c, pos, end);
  1311.     }

  1312.     if (h2c->state.parse_value) {
  1313.         h2c->state.parse_value = 0;

  1314.         header->value.len = h2c->state.field_end - h2c->state.field_start;
  1315.         header->value.data = h2c->state.field_start;
  1316.     }

  1317.     len = header->name.len + header->value.len;

  1318.     if (len > h2c->state.header_limit) {
  1319.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1320.                       "client sent too large header");

  1321.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
  1322.     }

  1323.     h2c->state.header_limit -= len;

  1324.     if (h2c->state.index) {
  1325.         if (ngx_http_v2_add_header(h2c, header) != NGX_OK) {
  1326.             return ngx_http_v2_connection_error(h2c,
  1327.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1328.         }

  1329.         h2c->state.index = 0;
  1330.     }

  1331.     if (h2c->state.stream == NULL) {
  1332.         return ngx_http_v2_state_header_complete(h2c, pos, end);
  1333.     }

  1334.     r = h2c->state.stream->request;
  1335.     fc = r->connection;

  1336.     /* TODO Optimization: validate headers while parsing. */
  1337.     if (ngx_http_v2_validate_header(r, header) != NGX_OK) {
  1338.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1339.         goto error;
  1340.     }

  1341.     if (header->name.data[0] == ':') {
  1342.         rc = ngx_http_v2_pseudo_header(r, header);

  1343.         if (rc == NGX_OK) {
  1344.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1345.                            "http2 header: \":%V: %V\"",
  1346.                            &header->name, &header->value);

  1347.             return ngx_http_v2_state_header_complete(h2c, pos, end);
  1348.         }

  1349.         if (rc == NGX_ABORT) {
  1350.             goto error;
  1351.         }

  1352.         if (rc == NGX_DECLINED) {
  1353.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1354.             goto error;
  1355.         }

  1356.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1357.     }

  1358.     if (ngx_http_v2_construct_request_line(r) != NGX_OK) {
  1359.         goto error;
  1360.     }

  1361.     if (r->invalid_header) {
  1362.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  1363.         if (cscf->ignore_invalid_headers) {
  1364.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1365.                           "client sent invalid header: \"%V\"", &header->name);

  1366.             return ngx_http_v2_state_header_complete(h2c, pos, end);
  1367.         }
  1368.     }

  1369.     if (header->name.len == priority.len
  1370.         && ngx_memcmp(header->name.data, priority.data, priority.len) == 0)
  1371.     {
  1372.         if (ngx_http_v2_priority(r, header) != NGX_OK) {
  1373.             return ngx_http_v2_connection_error(h2c,
  1374.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1375.         }
  1376.     }

  1377.     if (header->name.len == cookie.len
  1378.         && ngx_memcmp(header->name.data, cookie.data, cookie.len) == 0)
  1379.     {
  1380.         if (ngx_http_v2_cookie(r, header) != NGX_OK) {
  1381.             return ngx_http_v2_connection_error(h2c,
  1382.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1383.         }

  1384.     } else {
  1385.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  1386.         if (r->headers_in.count++ >= cscf->max_headers) {
  1387.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1388.                           "client sent too many header lines");
  1389.             ngx_http_finalize_request(r, NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
  1390.             goto error;
  1391.         }

  1392.         h = ngx_list_push(&r->headers_in.headers);
  1393.         if (h == NULL) {
  1394.             return ngx_http_v2_connection_error(h2c,
  1395.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1396.         }

  1397.         h->key.len = header->name.len;
  1398.         h->key.data = header->name.data;

  1399.         /*
  1400.          * TODO Optimization: precalculate hash
  1401.          * and handler for indexed headers.
  1402.          */
  1403.         h->hash = ngx_hash_key(h->key.data, h->key.len);

  1404.         h->value.len = header->value.len;
  1405.         h->value.data = header->value.data;

  1406.         h->lowcase_key = h->key.data;

  1407.         cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  1410.         if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
  1411.             goto error;
  1412.         }
  1413.     }

  1414.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1415.                    "http2 header: \"%V: %V\"",
  1416.                    &header->name, &header->value);

  1417.     return ngx_http_v2_state_header_complete(h2c, pos, end);

  1418. error:

  1419.     h2c->state.stream = NULL;

  1420.     ngx_http_run_posted_requests(fc);

  1421.     return ngx_http_v2_state_header_complete(h2c, pos, end);
  1422. }


  1423. static u_char *
  1424. ‌ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
  1425.     u_char *end)
  1426. {
  1427.     ngx_http_v2_stream_t  *stream;

  1428.     if (h2c->state.length) {
  1429.         if (end - pos > 0) {
  1430.             h2c->state.handler = ngx_http_v2_state_header_block;
  1431.             return pos;
  1432.         }

  1433.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1434.                                               ngx_http_v2_state_header_block);
  1435.     }

  1436.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)) {
  1437.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1438.                                              ngx_http_v2_state_header_complete);
  1439.     }

  1440.     stream = h2c->state.stream;

  1441.     if (stream) {
  1442.         ngx_http_v2_run_request(stream->request);
  1443.     }

  1444.     if (!h2c->state.keep_pool) {
  1445.         ngx_destroy_pool(h2c->state.pool);
  1446.     }

  1447.     h2c->state.pool = NULL;
  1448.     h2c->state.keep_pool = 0;

  1449.     if (h2c->state.padding) {
  1450.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  1451.     }

  1452.     return ngx_http_v2_state_complete(h2c, pos, end);
  1453. }


  1454. static u_char *
  1455. ‌ngx_http_v2_handle_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
  1456.     u_char *end, ngx_http_v2_handler_pt handler)
  1457. {
  1458.     u_char    *p;
  1459.     size_t     len, skip;
  1460.     uint32_t   head;

  1461.     len = h2c->state.length;

  1462.     if (h2c->state.padding && (size_t) (end - pos) > len) {
  1463.         skip = ngx_min(h2c->state.padding, (end - pos) - len);

  1464.         h2c->state.padding -= skip;

  1465.         p = pos;
  1466.         pos += skip;
  1467.         ngx_memmove(pos, p, len);
  1468.     }

  1469.     if ((size_t) (end - pos) < len + NGX_HTTP_V2_FRAME_HEADER_SIZE) {
  1470.         return ngx_http_v2_state_headers_save(h2c, pos, end, handler);
  1471.     }

  1472.     p = pos + len;

  1473.     head = ngx_http_v2_parse_uint32(p);

  1474.     if (ngx_http_v2_parse_type(head) != NGX_HTTP_V2_CONTINUATION_FRAME) {
  1475.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1476.              "client sent inappropriate frame while CONTINUATION was expected");

  1477.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1478.     }

  1479.     h2c->state.flags |= p[4];

  1480.     if (h2c->state.sid != ngx_http_v2_parse_sid(&p[5])) {
  1481.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1482.                     "client sent CONTINUATION frame with incorrect identifier");

  1483.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1484.     }

  1485.     p = pos;
  1486.     pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;

  1487.     ngx_memcpy(pos, p, len);

  1488.     len = ngx_http_v2_parse_length(head);

  1489.     h2c->state.length += len;

  1490.     if (h2c->state.stream) {
  1491.         h2c->state.stream->request->request_length += len;
  1492.     }

  1493.     h2c->state.handler = handler;
  1494.     return pos;
  1495. }


  1496. static u_char *
  1497. ‌ngx_http_v2_state_priority(ngx_http_v2_connection_t *h2c, u_char *pos,
  1498.     u_char *end)
  1499. {
  1500.     if (h2c->state.length != NGX_HTTP_V2_PRIORITY_SIZE) {
  1501.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1502.                       "client sent PRIORITY frame with incorrect length %uz",
  1503.                       h2c->state.length);

  1504.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1505.     }

  1506.     if (h2c->state.sid == 0) {
  1507.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1508.                       "client sent PRIORITY frame with incorrect identifier");

  1509.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1510.     }

  1511.     return ngx_http_v2_state_skip(h2c, pos, end);
  1512. }


  1513. static u_char *
  1514. ‌ngx_http_v2_state_priority_update(ngx_http_v2_connection_t *h2c, u_char *pos,
  1515.     u_char *end)
  1516. {
  1517.     size_t                 len;
  1518.     ngx_str_t              value;
  1519.     ngx_uint_t             sid;
  1520.     ngx_http_v2_node_t    *node;
  1521.     ngx_http_priority_t    priority;
  1522.     ngx_http_v2_stream_t  *stream;

  1523.     if (h2c->state.sid != 0) {
  1524.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1525.                       "client sent PRIORITY_UPDATE frame with incorrect "
  1526.                       "identifier");

  1527.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1528.     }

  1529.     if (h2c->state.length < 4) {
  1530.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1531.                       "client sent PRIORITY_UPDATE frame with incorrect "
  1532.                       "length %uz", h2c->state.length);

  1533.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1534.     }

  1535.     len = h2c->state.length;

  1536.     if ((size_t) (end - pos) < len) {

  1537.         if (len > NGX_HTTP_V2_STATE_BUFFER_SIZE) {
  1538.             return ngx_http_v2_state_skip(h2c, pos, end);
  1539.         }

  1540.         return ngx_http_v2_state_save(h2c, pos, end,
  1541.                                       ngx_http_v2_state_priority_update);
  1542.     }

  1543.     sid = ngx_http_v2_parse_sid(pos);

  1544.     if (sid % 2 == 0) {
  1545.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1546.                       "client sent PRIORITY_UPDATE frame with incorrect "
  1547.                       "prioritized stream %ui", sid);

  1548.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1549.     }

  1550.     value.data = pos + 4;
  1551.     value.len = len - 4;

  1552.     pos += len;

  1553.     if (ngx_http_priority_parse(&value, &priority) != NGX_OK) {
  1554.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1555.                       "client sent PRIORITY_UPDATE frame with malformed value "
  1556.                       "for stream %ui", sid);

  1557.         return ngx_http_v2_state_complete(h2c, pos, end);
  1558.     }

  1559.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1560.                    "http2 PRIORITY_UPDATE frame sid:%ui u=%ud i=%ud",
  1561.                    sid, priority.urgency, priority.incremental);

  1562.     /*
  1563.      * A PRIORITY_UPDATE conveys a complete set of parameters (RFC 9218,
  1564.      * Section 7): omitted parameters reset to their defaults, so the result
  1565.      * replaces any earlier signal rather than merging with it.
  1566.      */

  1567.     node = ngx_http_v2_get_node_by_id(h2c, sid, sid > h2c->last_sid);

  1568.     if (sid > h2c->last_sid) {
  1569.         /*
  1570.          * The stream is not created yet; park the node in h2c->closed holding
  1571.          * the buffered priority, bounded by the closed-node budget with the
  1572.          * oldest evicted first.  A node already buffered is moved to the tail.
  1573.          *
  1574.          * RFC 9218, Section 7.1, bounds idle prioritized streams together with
  1575.          * active ones and requires a connection error on overflow; here idle
  1576.          * signals are bounded on their own and the oldest is dropped instead,
  1577.          * which is deliberately more lenient.
  1578.          */

  1579.         if (node == NULL) {
  1580.             return ngx_http_v2_connection_error(h2c,
  1581.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1582.         }

  1583.         if (node->priority_set) {
  1584.             ngx_queue_remove(&node->reuse);

  1585.         } else {
  1586.             h2c->closed_nodes++;
  1587.         }

  1588.         ngx_queue_insert_tail(&h2c->closed, &node->reuse);

  1589.         node->priority = priority;
  1590.         node->priority_set = 1;

  1591.     } else if (node && node->stream) {
  1592.         stream = node->stream;
  1593.         stream->priority.client = priority;
  1594.         stream->priority_update = 1;
  1595.         ngx_http_priority_state_update(&stream->priority);
  1596.     }

  1597.     return ngx_http_v2_state_complete(h2c, pos, end);
  1598. }


  1599. static u_char *
  1600. ‌ngx_http_v2_state_rst_stream(ngx_http_v2_connection_t *h2c, u_char *pos,
  1601.     u_char *end)
  1602. {
  1603.     ngx_uint_t             status;
  1604.     ngx_event_t           *ev;
  1605.     ngx_connection_t      *fc;
  1606.     ngx_http_v2_node_t    *node;
  1607.     ngx_http_v2_stream_t  *stream;

  1608.     if (h2c->state.length != NGX_HTTP_V2_RST_STREAM_SIZE) {
  1609.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1610.                       "client sent RST_STREAM frame with incorrect length %uz",
  1611.                       h2c->state.length);

  1612.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1613.     }

  1614.     if (end - pos < NGX_HTTP_V2_RST_STREAM_SIZE) {
  1615.         return ngx_http_v2_state_save(h2c, pos, end,
  1616.                                       ngx_http_v2_state_rst_stream);
  1617.     }

  1618.     status = ngx_http_v2_parse_uint32(pos);

  1619.     pos += NGX_HTTP_V2_RST_STREAM_SIZE;

  1620.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1621.                    "http2 RST_STREAM frame, sid:%ui status:%ui",
  1622.                    h2c->state.sid, status);

  1623.     if (h2c->state.sid == 0) {
  1624.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1625.                       "client sent RST_STREAM frame with incorrect identifier");

  1626.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1627.     }

  1628.     node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);

  1629.     if (node == NULL || node->stream == NULL) {
  1630.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1631.                        "unknown http2 stream");

  1632.         return ngx_http_v2_state_complete(h2c, pos, end);
  1633.     }

  1634.     stream = node->stream;

  1635.     stream->in_closed = 1;
  1636.     stream->out_closed = 1;

  1637.     fc = stream->request->connection;
  1638.     fc->error = 1;

  1639.     switch (status) {

  1640.     case NGX_HTTP_V2_CANCEL:
  1641.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  1642.                       "client canceled stream %ui", h2c->state.sid);
  1643.         break;

  1644.     case NGX_HTTP_V2_INTERNAL_ERROR:
  1645.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  1646.                       "client terminated stream %ui due to internal error",
  1647.                       h2c->state.sid);
  1648.         break;

  1649.     default:
  1650.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  1651.                       "client terminated stream %ui with status %ui",
  1652.                       h2c->state.sid, status);
  1653.         break;
  1654.     }

  1655.     ev = fc->read;
  1656.     ev->handler(ev);

  1657.     return ngx_http_v2_state_complete(h2c, pos, end);
  1658. }


  1659. static u_char *
  1660. ‌ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c, u_char *pos,
  1661.     u_char *end)
  1662. {
  1663.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1664.                    "http2 SETTINGS frame");

  1665.     if (h2c->state.sid) {
  1666.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1667.                       "client sent SETTINGS frame with incorrect identifier");

  1668.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1669.     }

  1670.     if (h2c->state.flags & NGX_HTTP_V2_ACK_FLAG) {

  1671.         if (h2c->state.length != 0) {
  1672.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1673.                           "client sent SETTINGS frame with the ACK flag "
  1674.                           "and nonzero length");

  1675.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1676.         }

  1677.         h2c->settings_ack = 1;

  1678.         return ngx_http_v2_state_complete(h2c, pos, end);
  1679.     }

  1680.     if (h2c->state.length % NGX_HTTP_V2_SETTINGS_PARAM_SIZE) {
  1681.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1682.                       "client sent SETTINGS frame with incorrect length %uz",
  1683.                       h2c->state.length);

  1684.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1685.     }

  1686.     h2c->state.window_delta = 0;

  1687.     return ngx_http_v2_state_settings_params(h2c, pos, end);
  1688. }


  1689. static u_char *
  1690. ‌ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c, u_char *pos,
  1691.     u_char *end)
  1692. {
  1693.     ngx_uint_t                id, value;
  1694.     ngx_http_v2_out_frame_t  *frame;

  1695.     while (h2c->state.length) {
  1696.         if (end - pos < NGX_HTTP_V2_SETTINGS_PARAM_SIZE) {
  1697.             return ngx_http_v2_state_save(h2c, pos, end,
  1698.                                           ngx_http_v2_state_settings_params);
  1699.         }

  1700.         h2c->state.length -= NGX_HTTP_V2_SETTINGS_PARAM_SIZE;

  1701.         id = ngx_http_v2_parse_uint16(pos);
  1702.         value = ngx_http_v2_parse_uint32(&pos[2]);

  1703.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1704.                        "http2 setting %ui:%ui", id, value);

  1705.         switch (id) {

  1706.         case NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING:

  1707.             if (value > NGX_HTTP_V2_MAX_WINDOW) {
  1708.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1709.                               "client sent SETTINGS frame with incorrect "
  1710.                               "INITIAL_WINDOW_SIZE value %ui", value);

  1711.                 return ngx_http_v2_connection_error(h2c,
  1712.                                                   NGX_HTTP_V2_FLOW_CTRL_ERROR);
  1713.             }

  1714.             h2c->state.window_delta = (ssize_t) value
  1715.                                       - (ssize_t) h2c->init_window;
  1716.             break;

  1717.         case NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING:

  1718.             if (value > NGX_HTTP_V2_MAX_FRAME_SIZE
  1719.                 || value < NGX_HTTP_V2_DEFAULT_FRAME_SIZE)
  1720.             {
  1721.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1722.                               "client sent SETTINGS frame with incorrect "
  1723.                               "MAX_FRAME_SIZE value %ui", value);

  1724.                 return ngx_http_v2_connection_error(h2c,
  1725.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1726.             }

  1727.             h2c->frame_size = value;
  1728.             break;

  1729.         case NGX_HTTP_V2_ENABLE_PUSH_SETTING:

  1730.             if (value > 1) {
  1731.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1732.                               "client sent SETTINGS frame with incorrect "
  1733.                               "ENABLE_PUSH value %ui", value);

  1734.                 return ngx_http_v2_connection_error(h2c,
  1735.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1736.             }

  1737.             break;

  1738.         case NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING:

  1739.             h2c->table_update = 1;
  1740.             break;

  1741.         case NGX_HTTP_V2_NO_PRIORITIES_SETTING:

  1742.             if (value > 1) {
  1743.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1744.                               "client sent SETTINGS frame with incorrect "
  1745.                               "NO_RFC7540_PRIORITIES value %ui", value);

  1746.                 return ngx_http_v2_connection_error(h2c,
  1747.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1748.             }

  1749.             break;

  1750.         default:
  1751.             break;
  1752.         }

  1753.         pos += NGX_HTTP_V2_SETTINGS_PARAM_SIZE;
  1754.     }

  1755.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_SETTINGS_ACK_SIZE,
  1756.                                   NGX_HTTP_V2_SETTINGS_FRAME,
  1757.                                   NGX_HTTP_V2_ACK_FLAG, 0);
  1758.     if (frame == NULL) {
  1759.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1760.     }

  1761.     ngx_http_v2_queue_ordered_frame(h2c, frame);

  1762.     if (h2c->state.window_delta) {
  1763.         h2c->init_window += h2c->state.window_delta;

  1764.         if (ngx_http_v2_adjust_windows(h2c, h2c->state.window_delta) != NGX_OK)
  1765.         {
  1766.             return ngx_http_v2_connection_error(h2c,
  1767.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1768.         }

  1769.         h2c->state.window_delta = 0;
  1770.     }

  1771.     return ngx_http_v2_state_complete(h2c, pos, end);
  1772. }


  1773. static u_char *
  1774. ‌ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c, u_char *pos,
  1775.     u_char *end)
  1776. {
  1777.     ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1778.                   "client sent PUSH_PROMISE frame");

  1779.     return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1780. }


  1781. static u_char *
  1782. ‌ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  1783. {
  1784.     ngx_buf_t                *buf;
  1785.     ngx_http_v2_out_frame_t  *frame;

  1786.     if (h2c->state.length != NGX_HTTP_V2_PING_SIZE) {
  1787.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1788.                       "client sent PING frame with incorrect length %uz",
  1789.                       h2c->state.length);

  1790.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1791.     }

  1792.     if (end - pos < NGX_HTTP_V2_PING_SIZE) {
  1793.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_ping);
  1794.     }

  1795.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1796.                    "http2 PING frame");

  1797.     if (h2c->state.sid) {
  1798.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1799.                       "client sent PING frame with incorrect identifier");

  1800.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1801.     }

  1802.     if (h2c->state.flags & NGX_HTTP_V2_ACK_FLAG) {
  1803.         return ngx_http_v2_state_skip(h2c, pos, end);
  1804.     }

  1805.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_PING_SIZE,
  1806.                                   NGX_HTTP_V2_PING_FRAME,
  1807.                                   NGX_HTTP_V2_ACK_FLAG, 0);
  1808.     if (frame == NULL) {
  1809.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1810.     }

  1811.     buf = frame->first->buf;

  1812.     buf->last = ngx_cpymem(buf->last, pos, NGX_HTTP_V2_PING_SIZE);

  1813.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  1814.     return ngx_http_v2_state_complete(h2c, pos + NGX_HTTP_V2_PING_SIZE, end);
  1815. }


  1816. static u_char *
  1817. ‌ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c, u_char *pos,
  1818.     u_char *end)
  1819. {
  1820. #if (NGX_DEBUG)
  1821.     ngx_uint_t  last_sid, error;
  1822. #endif

  1823.     if (h2c->state.length < NGX_HTTP_V2_GOAWAY_SIZE) {
  1824.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1825.                       "client sent GOAWAY frame "
  1826.                       "with incorrect length %uz", h2c->state.length);

  1827.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1828.     }

  1829.     if (end - pos < NGX_HTTP_V2_GOAWAY_SIZE) {
  1830.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_goaway);
  1831.     }

  1832.     if (h2c->state.sid) {
  1833.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1834.                       "client sent GOAWAY frame with incorrect identifier");

  1835.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1836.     }

  1837. #if (NGX_DEBUG)
  1838.     h2c->state.length -= NGX_HTTP_V2_GOAWAY_SIZE;

  1839.     last_sid = ngx_http_v2_parse_sid(pos);
  1840.     error = ngx_http_v2_parse_uint32(&pos[4]);

  1841.     pos += NGX_HTTP_V2_GOAWAY_SIZE;

  1842.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1843.                    "http2 GOAWAY frame: last sid %ui, error %ui",
  1844.                    last_sid, error);
  1845. #endif

  1846.     return ngx_http_v2_state_skip(h2c, pos, end);
  1847. }


  1848. static u_char *
  1849. ‌ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos,
  1850.     u_char *end)
  1851. {
  1852.     size_t                 window;
  1853.     ngx_event_t           *wev;
  1854.     ngx_queue_t           *q;
  1855.     ngx_http_v2_node_t    *node;
  1856.     ngx_http_v2_stream_t  *stream;

  1857.     if (h2c->state.length != NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
  1858.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1859.                       "client sent WINDOW_UPDATE frame "
  1860.                       "with incorrect length %uz", h2c->state.length);

  1861.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1862.     }

  1863.     if (end - pos < NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
  1864.         return ngx_http_v2_state_save(h2c, pos, end,
  1865.                                       ngx_http_v2_state_window_update);
  1866.     }

  1867.     window = ngx_http_v2_parse_window(pos);

  1868.     pos += NGX_HTTP_V2_WINDOW_UPDATE_SIZE;

  1869.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1870.                    "http2 WINDOW_UPDATE frame sid:%ui window:%uz",
  1871.                    h2c->state.sid, window);

  1872.     if (window == 0) {
  1873.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1874.                       "client sent WINDOW_UPDATE frame "
  1875.                       "with incorrect window increment 0");

  1876.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1877.     }

  1878.     if (h2c->state.sid) {
  1879.         node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);

  1880.         if (node == NULL || node->stream == NULL) {
  1881.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1882.                            "unknown http2 stream");

  1883.             return ngx_http_v2_state_complete(h2c, pos, end);
  1884.         }

  1885.         stream = node->stream;

  1886.         if (window > (size_t) (NGX_HTTP_V2_MAX_WINDOW - stream->send_window)) {

  1887.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1888.                           "client violated flow control for stream %ui: "
  1889.                           "received WINDOW_UPDATE frame "
  1890.                           "with window increment %uz "
  1891.                           "not allowed for window %z",
  1892.                           h2c->state.sid, window, stream->send_window);

  1893.             if (ngx_http_v2_terminate_stream(h2c, stream,
  1894.                                              NGX_HTTP_V2_FLOW_CTRL_ERROR)
  1895.                 == NGX_ERROR)
  1896.             {
  1897.                 return ngx_http_v2_connection_error(h2c,
  1898.                                                     NGX_HTTP_V2_INTERNAL_ERROR);
  1899.             }

  1900.             return ngx_http_v2_state_complete(h2c, pos, end);
  1901.         }

  1902.         stream->send_window += window;

  1903.         if (stream->exhausted) {
  1904.             stream->exhausted = 0;

  1905.             wev = stream->request->connection->write;

  1906.             wev->active = 0;
  1907.             wev->ready = 1;

  1908.             if (!wev->delayed) {
  1909.                 wev->handler(wev);
  1910.             }
  1911.         }

  1912.         return ngx_http_v2_state_complete(h2c, pos, end);
  1913.     }

  1914.     if (window > NGX_HTTP_V2_MAX_WINDOW - h2c->send_window) {
  1915.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1916.                       "client violated connection flow control: "
  1917.                       "received WINDOW_UPDATE frame "
  1918.                       "with window increment %uz "
  1919.                       "not allowed for window %uz",
  1920.                       window, h2c->send_window);

  1921.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
  1922.     }

  1923.     h2c->send_window += window;

  1924.     while (!ngx_queue_empty(&h2c->waiting)) {
  1925.         q = ngx_queue_head(&h2c->waiting);

  1926.         ngx_queue_remove(q);

  1927.         stream = ngx_queue_data(q, ngx_http_v2_stream_t, queue);

  1928.         stream->waiting = 0;

  1929.         wev = stream->request->connection->write;

  1930.         wev->active = 0;
  1931.         wev->ready = 1;

  1932.         if (!wev->delayed) {
  1933.             wev->handler(wev);

  1934.             if (h2c->send_window == 0) {
  1935.                 break;
  1936.             }
  1937.         }
  1938.     }

  1939.     return ngx_http_v2_state_complete(h2c, pos, end);
  1940. }


  1941. static u_char *
  1942. ‌ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
  1943.     u_char *end)
  1944. {
  1945.     ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1946.                   "client sent unexpected CONTINUATION frame");

  1947.     return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1948. }


  1949. static u_char *
  1950. ‌ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
  1951.     u_char *end)
  1952. {
  1953.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1954.                    "http2 frame complete pos:%p end:%p", pos, end);

  1955.     if (pos > end) {
  1956.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  1957.                       "receive buffer overrun");

  1958.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1959.     }

  1960.     h2c->state.stream = NULL;
  1961.     h2c->state.handler = ngx_http_v2_state_head;

  1962.     return pos;
  1963. }


  1964. static u_char *
  1965. ‌ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c, u_char *pos,
  1966.     u_char *end)
  1967. {
  1968.     h2c->state.length += h2c->state.padding;
  1969.     h2c->state.padding = 0;

  1970.     return ngx_http_v2_state_skip(h2c, pos, end);
  1971. }


  1972. static u_char *
  1973. ‌ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  1974. {
  1975.     size_t  size;

  1976.     size = end - pos;

  1977.     if (size < h2c->state.length) {
  1978.         ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1979.                        "http2 frame skip %uz of %uz", size, h2c->state.length);

  1980.         h2c->state.length -= size;
  1981.         return ngx_http_v2_state_save(h2c, end, end, ngx_http_v2_state_skip);
  1982.     }

  1983.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1984.                    "http2 frame skip %uz", h2c->state.length);

  1985.     return ngx_http_v2_state_complete(h2c, pos + h2c->state.length, end);
  1986. }


  1987. static u_char *
  1988. ‌ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end,
  1989.     ngx_http_v2_handler_pt handler)
  1990. {
  1991.     size_t  size;

  1992.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1993.                    "http2 frame state save pos:%p end:%p handler:%p",
  1994.                    pos, end, handler);

  1995.     size = end - pos;

  1996.     if (size > NGX_HTTP_V2_STATE_BUFFER_SIZE) {
  1997.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  1998.                       "state buffer overflow: %uz bytes required", size);

  1999.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  2000.     }

  2001.     ngx_memcpy(h2c->state.buffer, pos, size);

  2002.     h2c->state.buffer_used = size;
  2003.     h2c->state.handler = handler;
  2004.     h2c->state.incomplete = 1;

  2005.     return end;
  2006. }


  2007. static u_char *
  2008. ‌ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c, u_char *pos,
  2009.     u_char *end, ngx_http_v2_handler_pt handler)
  2010. {
  2011.     ngx_event_t               *rev;
  2012.     ngx_http_request_t        *r;
  2013.     ngx_http_core_srv_conf_t  *cscf;

  2014.     if (h2c->state.stream) {
  2015.         r = h2c->state.stream->request;
  2016.         rev = r->connection->read;

  2017.         if (!rev->timer_set) {
  2018.             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  2019.             ngx_add_timer(rev, cscf->client_header_timeout);
  2020.         }
  2021.     }

  2022.     return ngx_http_v2_state_save(h2c, pos, end, handler);
  2023. }


  2024. static u_char *
  2025. ‌ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
  2026.     ngx_uint_t err)
  2027. {
  2028.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2029.                    "http2 state connection error");

  2030.     ngx_http_v2_finalize_connection(h2c, err);

  2031.     return NULL;
  2032. }


  2033. static ngx_int_t
  2034. ‌ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c, u_char **pos, u_char *end,
  2035.     ngx_uint_t prefix)
  2036. {
  2037.     u_char      *start, *p;
  2038.     ngx_uint_t   value, octet, shift;

  2039.     start = *pos;
  2040.     p = start;

  2041.     value = *p++ & prefix;

  2042.     if (value != prefix) {
  2043.         if (h2c->state.length == 0) {
  2044.             return NGX_ERROR;
  2045.         }

  2046.         h2c->state.length--;

  2047.         *pos = p;
  2048.         return value;
  2049.     }

  2050.     if (end - start > NGX_HTTP_V2_INT_OCTETS) {
  2051.         end = start + NGX_HTTP_V2_INT_OCTETS;
  2052.     }

  2053.     for (shift = 0; p != end; shift += 7) {
  2054.         octet = *p++;

  2055.         value += (octet & 0x7f) << shift;

  2056.         if (octet < 128) {
  2057.             if ((size_t) (p - start) > h2c->state.length) {
  2058.                 return NGX_ERROR;
  2059.             }

  2060.             h2c->state.length -= p - start;

  2061.             *pos = p;
  2062.             return value;
  2063.         }
  2064.     }

  2065.     if ((size_t) (end - start) >= h2c->state.length) {
  2066.         return NGX_ERROR;
  2067.     }

  2068.     if (end == start + NGX_HTTP_V2_INT_OCTETS) {
  2069.         return NGX_DECLINED;
  2070.     }

  2071.     return NGX_AGAIN;
  2072. }


  2073. static ngx_int_t
  2074. ‌ngx_http_v2_send_settings(ngx_http_v2_connection_t *h2c)
  2075. {
  2076.     size_t                    len;
  2077.     ngx_buf_t                *buf;
  2078.     ngx_chain_t              *cl;
  2079.     ngx_http_v2_srv_conf_t   *h2scf;
  2080.     ngx_http_v2_out_frame_t  *frame;

  2081.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2082.                    "http2 send SETTINGS frame");

  2083.     frame = ngx_palloc(h2c->pool, sizeof(ngx_http_v2_out_frame_t));
  2084.     if (frame == NULL) {
  2085.         return NGX_ERROR;
  2086.     }

  2087.     cl = ngx_alloc_chain_link(h2c->pool);
  2088.     if (cl == NULL) {
  2089.         return NGX_ERROR;
  2090.     }

  2091.     len = NGX_HTTP_V2_SETTINGS_PARAM_SIZE * 4;

  2092.     buf = ngx_create_temp_buf(h2c->pool, NGX_HTTP_V2_FRAME_HEADER_SIZE + len);
  2093.     if (buf == NULL) {
  2094.         return NGX_ERROR;
  2095.     }

  2096.     buf->last_buf = 1;

  2097.     cl->buf = buf;
  2098.     cl->next = NULL;

  2099.     frame->first = cl;
  2100.     frame->last = cl;
  2101.     frame->handler = ngx_http_v2_settings_frame_handler;
  2102.     frame->stream = NULL;
  2103. #if (NGX_DEBUG)
  2104.     frame->length = len;
  2105. #endif
  2106.     frame->blocked = 0;

  2107.     buf->last = ngx_http_v2_write_len_and_type(buf->last, len,
  2108.                                                NGX_HTTP_V2_SETTINGS_FRAME);

  2109.     *buf->last++ = NGX_HTTP_V2_NO_FLAG;

  2110.     buf->last = ngx_http_v2_write_sid(buf->last, 0);

  2111.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2112.                                          ngx_http_v2_module);

  2113.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2114.                                          NGX_HTTP_V2_MAX_STREAMS_SETTING);
  2115.     buf->last = ngx_http_v2_write_uint32(buf->last,
  2116.                                          h2scf->concurrent_streams);

  2117.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2118.                                          NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING);
  2119.     buf->last = ngx_http_v2_write_uint32(buf->last, h2scf->preread_size);

  2120.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2121.                                          NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING);
  2122.     buf->last = ngx_http_v2_write_uint32(buf->last,
  2123.                                          NGX_HTTP_V2_MAX_FRAME_SIZE);

  2124.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2125.                                          NGX_HTTP_V2_NO_PRIORITIES_SETTING);
  2126.     buf->last = ngx_http_v2_write_uint32(buf->last, 1);

  2127.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2128.     return NGX_OK;
  2129. }


  2130. static ngx_int_t
  2131. ‌ngx_http_v2_settings_frame_handler(ngx_http_v2_connection_t *h2c,
  2132.     ngx_http_v2_out_frame_t *frame)
  2133. {
  2134.     ngx_buf_t  *buf;

  2135.     buf = frame->first->buf;

  2136.     if (buf->pos != buf->last) {
  2137.         return NGX_AGAIN;
  2138.     }

  2139.     ngx_free_chain(h2c->pool, frame->first);

  2140.     return NGX_OK;
  2141. }


  2142. static ngx_int_t
  2143. ‌ngx_http_v2_send_window_update(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2144.     size_t window)
  2145. {
  2146.     ngx_buf_t                *buf;
  2147.     ngx_http_v2_out_frame_t  *frame;

  2148.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2149.                    "http2 send WINDOW_UPDATE frame sid:%ui, window:%uz",
  2150.                    sid, window);

  2151.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_WINDOW_UPDATE_SIZE,
  2152.                                   NGX_HTTP_V2_WINDOW_UPDATE_FRAME,
  2153.                                   NGX_HTTP_V2_NO_FLAG, sid);
  2154.     if (frame == NULL) {
  2155.         return NGX_ERROR;
  2156.     }

  2157.     buf = frame->first->buf;

  2158.     buf->last = ngx_http_v2_write_uint32(buf->last, window);

  2159.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2160.     return NGX_OK;
  2161. }


  2162. static ngx_int_t
  2163. ‌ngx_http_v2_send_rst_stream(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2164.     ngx_uint_t status)
  2165. {
  2166.     ngx_buf_t                *buf;
  2167.     ngx_http_v2_out_frame_t  *frame;

  2168.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2169.                    "http2 send RST_STREAM frame sid:%ui, status:%ui",
  2170.                    sid, status);

  2171.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_RST_STREAM_SIZE,
  2172.                                   NGX_HTTP_V2_RST_STREAM_FRAME,
  2173.                                   NGX_HTTP_V2_NO_FLAG, sid);
  2174.     if (frame == NULL) {
  2175.         return NGX_ERROR;
  2176.     }

  2177.     buf = frame->first->buf;

  2178.     buf->last = ngx_http_v2_write_uint32(buf->last, status);

  2179.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2180.     return NGX_OK;
  2181. }


  2182. static ngx_int_t
  2183. ‌ngx_http_v2_send_goaway(ngx_http_v2_connection_t *h2c, ngx_uint_t status)
  2184. {
  2185.     ngx_buf_t                *buf;
  2186.     ngx_http_v2_out_frame_t  *frame;

  2187.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2188.                    "http2 send GOAWAY frame: last sid %ui, error %ui",
  2189.                    h2c->last_sid, status);

  2190.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_GOAWAY_SIZE,
  2191.                                   NGX_HTTP_V2_GOAWAY_FRAME,
  2192.                                   NGX_HTTP_V2_NO_FLAG, 0);
  2193.     if (frame == NULL) {
  2194.         return NGX_ERROR;
  2195.     }

  2196.     buf = frame->first->buf;

  2197.     buf->last = ngx_http_v2_write_sid(buf->last, h2c->last_sid);
  2198.     buf->last = ngx_http_v2_write_uint32(buf->last, status);

  2199.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2200.     return NGX_OK;
  2201. }


  2202. static ngx_http_v2_out_frame_t *
  2203. ‌ngx_http_v2_get_frame(ngx_http_v2_connection_t *h2c, size_t length,
  2204.     ngx_uint_t type, u_char flags, ngx_uint_t sid)
  2205. {
  2206.     ngx_buf_t                *buf;
  2207.     ngx_pool_t               *pool;
  2208.     ngx_http_v2_out_frame_t  *frame;

  2209.     frame = h2c->free_frames;

  2210.     if (frame) {
  2211.         h2c->free_frames = frame->next;

  2212.         buf = frame->first->buf;
  2213.         buf->pos = buf->start;

  2214.         frame->blocked = 0;

  2215.     } else if (h2c->frames < 10000) {
  2216.         pool = h2c->pool ? h2c->pool : h2c->connection->pool;

  2217.         frame = ngx_pcalloc(pool, sizeof(ngx_http_v2_out_frame_t));
  2218.         if (frame == NULL) {
  2219.             return NULL;
  2220.         }

  2221.         frame->first = ngx_alloc_chain_link(pool);
  2222.         if (frame->first == NULL) {
  2223.             return NULL;
  2224.         }

  2225.         buf = ngx_create_temp_buf(pool, NGX_HTTP_V2_FRAME_BUFFER_SIZE);
  2226.         if (buf == NULL) {
  2227.             return NULL;
  2228.         }

  2229.         buf->last_buf = 1;

  2230.         frame->first->buf = buf;
  2231.         frame->last = frame->first;

  2232.         frame->handler = ngx_http_v2_frame_handler;

  2233.         h2c->frames++;

  2234.     } else {
  2235.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  2236.                       "http2 flood detected");

  2237.         h2c->connection->error = 1;
  2238.         return NULL;
  2239.     }

  2240. #if (NGX_DEBUG)
  2241.     if (length > NGX_HTTP_V2_FRAME_BUFFER_SIZE - NGX_HTTP_V2_FRAME_HEADER_SIZE)
  2242.     {
  2243.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  2244.                       "requested control frame is too large: %uz", length);
  2245.         return NULL;
  2246.     }
  2247. #endif

  2248.     frame->length = length;

  2249.     buf->last = ngx_http_v2_write_len_and_type(buf->pos, length, type);

  2250.     *buf->last++ = flags;

  2251.     buf->last = ngx_http_v2_write_sid(buf->last, sid);

  2252.     return frame;
  2253. }


  2254. static ngx_int_t
  2255. ‌ngx_http_v2_frame_handler(ngx_http_v2_connection_t *h2c,
  2256.     ngx_http_v2_out_frame_t *frame)
  2257. {
  2258.     ngx_buf_t  *buf;

  2259.     buf = frame->first->buf;

  2260.     if (buf->pos != buf->last) {
  2261.         return NGX_AGAIN;
  2262.     }

  2263.     frame->next = h2c->free_frames;
  2264.     h2c->free_frames = frame;

  2265.     h2c->total_bytes += NGX_HTTP_V2_FRAME_HEADER_SIZE + frame->length;

  2266.     return NGX_OK;
  2267. }


  2268. static ngx_http_v2_stream_t *
  2269. ‌ngx_http_v2_create_stream(ngx_http_v2_connection_t *h2c)
  2270. {
  2271.     ngx_log_t                 *log;
  2272.     ngx_event_t               *rev, *wev;
  2273.     ngx_connection_t          *fc;
  2274.     ngx_http_log_ctx_t        *ctx;
  2275.     ngx_http_request_t        *r;
  2276.     ngx_http_v2_stream_t      *stream;
  2277.     ngx_http_v2_srv_conf_t    *h2scf;
  2278.     ngx_http_core_srv_conf_t  *cscf;

  2279.     fc = h2c->free_fake_connections;

  2280.     if (fc) {
  2281.         h2c->free_fake_connections = fc->data;

  2282.         rev = fc->read;
  2283.         wev = fc->write;
  2284.         log = fc->log;
  2285.         ctx = log->data;

  2286.     } else {
  2287.         fc = ngx_palloc(h2c->pool, sizeof(ngx_connection_t));
  2288.         if (fc == NULL) {
  2289.             return NULL;
  2290.         }

  2291.         rev = ngx_palloc(h2c->pool, sizeof(ngx_event_t));
  2292.         if (rev == NULL) {
  2293.             return NULL;
  2294.         }

  2295.         wev = ngx_palloc(h2c->pool, sizeof(ngx_event_t));
  2296.         if (wev == NULL) {
  2297.             return NULL;
  2298.         }

  2299.         log = ngx_palloc(h2c->pool, sizeof(ngx_log_t));
  2300.         if (log == NULL) {
  2301.             return NULL;
  2302.         }

  2303.         ctx = ngx_palloc(h2c->pool, sizeof(ngx_http_log_ctx_t));
  2304.         if (ctx == NULL) {
  2305.             return NULL;
  2306.         }

  2307.         ctx->connection = fc;
  2308.         ctx->request = NULL;
  2309.         ctx->current_request = NULL;
  2310.     }

  2311.     ngx_memcpy(log, h2c->connection->log, sizeof(ngx_log_t));

  2312.     log->data = ctx;
  2313.     log->action = "reading client request headers";

  2314.     ngx_memzero(rev, sizeof(ngx_event_t));

  2315.     rev->data = fc;
  2316.     rev->ready = 1;
  2317.     rev->handler = ngx_http_v2_close_stream_handler;
  2318.     rev->log = log;

  2319.     ngx_memcpy(wev, rev, sizeof(ngx_event_t));

  2320.     wev->write = 1;

  2321.     ngx_memcpy(fc, h2c->connection, sizeof(ngx_connection_t));

  2322.     fc->data = h2c->http_connection;
  2323.     fc->read = rev;
  2324.     fc->write = wev;
  2325.     fc->sent = 0;
  2326.     fc->log = log;
  2327.     fc->buffered = 0;
  2328.     fc->sndlowat = 1;
  2329.     fc->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;

  2330.     r = ngx_http_create_request(fc);
  2331.     if (r == NULL) {
  2332.         return NULL;
  2333.     }

  2334.     ngx_str_set(&r->http_protocol, "HTTP/2.0");

  2335.     r->http_version = NGX_HTTP_VERSION_20;
  2336.     r->valid_location = 1;

  2337.     fc->data = r;
  2338.     h2c->connection->requests++;

  2339.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  2340.     r->header_in = ngx_create_temp_buf(r->pool,
  2341.                                        cscf->client_header_buffer_size);
  2342.     if (r->header_in == NULL) {
  2343.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2344.         return NULL;
  2345.     }

  2346.     if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
  2347.                       sizeof(ngx_table_elt_t))
  2348.         != NGX_OK)
  2349.     {
  2350.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2351.         return NULL;
  2352.     }

  2353.     r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;

  2354.     stream = ngx_pcalloc(r->pool, sizeof(ngx_http_v2_stream_t));
  2355.     if (stream == NULL) {
  2356.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2357.         return NULL;
  2358.     }

  2359.     r->stream = stream;

  2360.     stream->request = r;
  2361.     stream->connection = h2c;

  2362.     ngx_http_priority_state_init(&stream->priority);

  2363.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

  2364.     stream->send_window = h2c->init_window;
  2365.     stream->recv_window = h2scf->preread_size;

  2366.     h2c->processing++;

  2367.     if (h2c->connection->read->timer_set) {
  2368.         ngx_del_timer(h2c->connection->read);
  2369.     }

  2370.     return stream;
  2371. }


  2372. static ngx_http_v2_node_t *
  2373. ‌ngx_http_v2_get_node_by_id(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2374.     ngx_uint_t alloc)
  2375. {
  2376.     ngx_uint_t               index;
  2377.     ngx_http_v2_node_t      *node;
  2378.     ngx_http_v2_srv_conf_t  *h2scf;

  2379.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2380.                                          ngx_http_v2_module);

  2381.     index = ngx_http_v2_index(h2scf, sid);

  2382.     for (node = h2c->streams_index[index]; node; node = node->index) {

  2383.         if (node->id == sid) {
  2384.             return node;
  2385.         }
  2386.     }

  2387.     if (!alloc) {
  2388.         return NULL;
  2389.     }

  2390.     if (h2c->closed_nodes < 32) {
  2391.         node = ngx_pcalloc(h2c->connection->pool, sizeof(ngx_http_v2_node_t));
  2392.         if (node == NULL) {
  2393.             return NULL;
  2394.         }

  2395.     } else {
  2396.         node = ngx_http_v2_get_closed_node(h2c);
  2397.     }

  2398.     node->id = sid;

  2399.     node->index = h2c->streams_index[index];
  2400.     h2c->streams_index[index] = node;

  2401.     return node;
  2402. }


  2403. static ngx_http_v2_node_t *
  2404. ‌ngx_http_v2_get_closed_node(ngx_http_v2_connection_t *h2c)
  2405. {
  2406.     ngx_queue_t             *q;
  2407.     ngx_http_v2_node_t      *node, **next, *n;
  2408.     ngx_http_v2_srv_conf_t  *h2scf;

  2409.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2410.                                          ngx_http_v2_module);

  2411.     h2c->closed_nodes--;

  2412.     q = ngx_queue_head(&h2c->closed);

  2413.     ngx_queue_remove(q);

  2414.     node = ngx_queue_data(q, ngx_http_v2_node_t, reuse);

  2415.     next = &h2c->streams_index[ngx_http_v2_index(h2scf, node->id)];

  2416.     for ( ;; ) {
  2417.         n = *next;

  2418.         if (n == node) {
  2419.             *next = n->index;
  2420.             break;
  2421.         }

  2422.         next = &n->index;
  2423.     }

  2424.     ngx_memzero(node, sizeof(ngx_http_v2_node_t));

  2425.     return node;
  2426. }


  2427. static ngx_int_t
  2428. ‌ngx_http_v2_validate_header(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2429. {
  2430.     u_char                     ch;
  2431.     ngx_uint_t                 i;
  2432.     ngx_http_core_srv_conf_t  *cscf;

  2433.     r->invalid_header = 0;

  2434.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

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

  2437.         if ((ch >= 'a' && ch <= 'z')
  2438.             || (ch == '-')
  2439.             || (ch >= '0' && ch <= '9')
  2440.             || (ch == '_' && cscf->underscores_in_headers))
  2441.         {
  2442.             continue;
  2443.         }

  2444.         if (ch <= 0x20 || ch == 0x7f || ch == ':'
  2445.             || (ch >= 'A' && ch <= 'Z'))
  2446.         {
  2447.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2448.                           "client sent invalid header name: \"%V\"",
  2449.                           &header->name);

  2450.             return NGX_ERROR;
  2451.         }

  2452.         r->invalid_header = 1;
  2453.     }

  2454.     for (i = 0; i != header->value.len; i++) {
  2455.         ch = header->value.data[i];

  2456.         if (ch == '\0' || ch == LF || ch == CR) {
  2457.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2458.                           "client sent header \"%V\" with "
  2459.                           "invalid value: \"%V\"",
  2460.                           &header->name, &header->value);

  2461.             return NGX_ERROR;
  2462.         }
  2463.     }

  2464.     return NGX_OK;
  2465. }


  2466. static ngx_int_t
  2467. ‌ngx_http_v2_pseudo_header(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2468. {
  2469.     header->name.len--;
  2470.     header->name.data++;

  2471.     if (r->request_line.len) {
  2472.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2473.                       "client sent out of order pseudo-headers");

  2474.         return NGX_DECLINED;
  2475.     }

  2476.     switch (header->name.len) {
  2477.     case 4:
  2478.         if (ngx_memcmp(header->name.data, "path", sizeof("path") - 1)
  2479.             == 0)
  2480.         {
  2481.             return ngx_http_v2_parse_path(r, &header->value);
  2482.         }

  2483.         break;

  2484.     case 6:
  2485.         if (ngx_memcmp(header->name.data, "method", sizeof("method") - 1)
  2486.             == 0)
  2487.         {
  2488.             return ngx_http_v2_parse_method(r, &header->value);
  2489.         }

  2490.         if (ngx_memcmp(header->name.data, "scheme", sizeof("scheme") - 1)
  2491.             == 0)
  2492.         {
  2493.             return ngx_http_v2_parse_scheme(r, &header->value);
  2494.         }

  2495.         break;

  2496.     case 9:
  2497.         if (ngx_memcmp(header->name.data, "authority", sizeof("authority") - 1)
  2498.             == 0)
  2499.         {
  2500.             return ngx_http_v2_parse_authority(r, &header->value);
  2501.         }

  2502.         break;
  2503.     }

  2504.     ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2505.                   "client sent unknown pseudo-header \":%V\"",
  2506.                   &header->name);

  2507.     return NGX_DECLINED;
  2508. }


  2509. static ngx_int_t
  2510. ‌ngx_http_v2_parse_path(ngx_http_request_t *r, ngx_str_t *value)
  2511. {
  2512.     if (r->unparsed_uri.len) {
  2513.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2514.                       "client sent duplicate :path header");

  2515.         return NGX_DECLINED;
  2516.     }

  2517.     if (value->len == 0) {
  2518.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2519.                       "client sent empty :path header");

  2520.         return NGX_DECLINED;
  2521.     }

  2522.     r->uri_start = value->data;
  2523.     r->uri_end = value->data + value->len;

  2524.     if (ngx_http_parse_uri(r) != NGX_OK) {
  2525.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2526.                       "client sent invalid :path header: \"%V\"", value);

  2527.         return NGX_DECLINED;
  2528.     }

  2529.     if (ngx_http_process_request_uri(r) != NGX_OK) {
  2530.         /*
  2531.          * request has been finalized already
  2532.          * in ngx_http_process_request_uri()
  2533.          */
  2534.         return NGX_ABORT;
  2535.     }

  2536.     return NGX_OK;
  2537. }


  2538. static ngx_int_t
  2539. ‌ngx_http_v2_parse_method(ngx_http_request_t *r, ngx_str_t *value)
  2540. {
  2541.     size_t         k, len;
  2542.     ngx_uint_t     n;
  2543.     const u_char  *p, *m;

  2544.     /*
  2545.      * This array takes less than 256 sequential bytes,
  2546.      * and if typical CPU cache line size is 64 bytes,
  2547.      * it is prefetched for 4 load operations.
  2548.      */
  2549. ‌    static const struct {
  2550.         u_char            len;
  2551.         const u_char      method[11];
  2552.         uint32_t          value;
  2553.     } tests[] = {
  2554.         { 3, "GET",       NGX_HTTP_GET },
  2555.         { 4, "POST",      NGX_HTTP_POST },
  2556.         { 4, "HEAD",      NGX_HTTP_HEAD },
  2557.         { 7, "OPTIONS",   NGX_HTTP_OPTIONS },
  2558.         { 8, "PROPFIND",  NGX_HTTP_PROPFIND },
  2559.         { 3, "PUT",       NGX_HTTP_PUT },
  2560.         { 5, "MKCOL",     NGX_HTTP_MKCOL },
  2561.         { 6, "DELETE",    NGX_HTTP_DELETE },
  2562.         { 4, "COPY",      NGX_HTTP_COPY },
  2563.         { 4, "MOVE",      NGX_HTTP_MOVE },
  2564.         { 9, "PROPPATCH", NGX_HTTP_PROPPATCH },
  2565.         { 4, "LOCK",      NGX_HTTP_LOCK },
  2566.         { 6, "UNLOCK",    NGX_HTTP_UNLOCK },
  2567.         { 5, "PATCH",     NGX_HTTP_PATCH },
  2568.         { 5, "TRACE",     NGX_HTTP_TRACE },
  2569.         { 7, "CONNECT",   NGX_HTTP_CONNECT }
  2570.     }, *test;

  2571.     if (r->method_name.len) {
  2572.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2573.                       "client sent duplicate :method header");

  2574.         return NGX_DECLINED;
  2575.     }

  2576.     if (value->len == 0) {
  2577.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2578.                       "client sent empty :method header");

  2579.         return NGX_DECLINED;
  2580.     }

  2581.     r->method_name.len = value->len;
  2582.     r->method_name.data = value->data;

  2583.     len = r->method_name.len;
  2584.     n = sizeof(tests) / sizeof(tests[0]);
  2585.     test = tests;

  2586.     do {
  2587.         if (len == test->len) {
  2588.             p = r->method_name.data;
  2589.             m = test->method;
  2590.             k = len;

  2591.             do {
  2592.                 if (*p++ != *m++) {
  2593.                     goto next;
  2594.                 }
  2595.             } while (--k);

  2596.             r->method = test->value;
  2597.             return NGX_OK;
  2598.         }

  2599.     next:
  2600.         test++;

  2601.     } while (--n);

  2602.     p = r->method_name.data;

  2603.     do {
  2604.         if ((*p < 'A' || *p > 'Z') && *p != '_' && *p != '-') {
  2605.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2606.                           "client sent invalid method: \"%V\"",
  2607.                           &r->method_name);

  2608.             return NGX_DECLINED;
  2609.         }

  2610.         p++;

  2611.     } while (--len);

  2612.     return NGX_OK;
  2613. }


  2614. static ngx_int_t
  2615. ‌ngx_http_v2_parse_scheme(ngx_http_request_t *r, ngx_str_t *value)
  2616. {
  2617.     u_char      c, ch;
  2618.     ngx_uint_t  i;

  2619.     if (r->schema.len) {
  2620.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2621.                       "client sent duplicate :scheme header");

  2622.         return NGX_DECLINED;
  2623.     }

  2624.     if (value->len == 0) {
  2625.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2626.                       "client sent empty :scheme header");

  2627.         return NGX_DECLINED;
  2628.     }

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

  2631.         c = (u_char) (ch | 0x20);
  2632.         if (c >= 'a' && c <= 'z') {
  2633.             continue;
  2634.         }

  2635.         if (((ch >= '0' && ch <= '9') || ch == '+' || ch == '-' || ch == '.')
  2636.             && i > 0)
  2637.         {
  2638.             continue;
  2639.         }

  2640.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2641.                       "client sent invalid :scheme header: \"%V\"", value);

  2642.         return NGX_DECLINED;
  2643.     }

  2644.     r->schema = *value;

  2645.     return NGX_OK;
  2646. }


  2647. static ngx_int_t
  2648. ‌ngx_http_v2_parse_authority(ngx_http_request_t *r, ngx_str_t *value)
  2649. {
  2650.     ngx_int_t  rc;
  2651.     in_port_t  port;

  2652.     if (r->host_start) {
  2653.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2654.                       "client sent duplicate \":authority\" header");
  2655.         return NGX_DECLINED;
  2656.     }

  2657.     r->host_start = value->data;
  2658.     r->host_end = value->data + value->len;

  2659.     rc = ngx_http_validate_host(value, &port, r->pool, 0);

  2660.     if (rc == NGX_DECLINED) {
  2661.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2662.                       "client sent invalid \":authority\" header");
  2663.         return NGX_DECLINED;
  2664.     }

  2665.     if (rc == NGX_ERROR) {
  2666.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2667.         return NGX_ABORT;
  2668.     }

  2669.     if (ngx_http_set_virtual_server(r, value) == NGX_ERROR) {
  2670.         /*
  2671.          * request has been finalized already
  2672.          * in ngx_http_set_virtual_server()
  2673.          */
  2674.         return NGX_ABORT;
  2675.     }

  2676.     r->headers_in.server = *value;
  2677.     r->port = port;

  2678.     return NGX_OK;
  2679. }


  2680. static ngx_int_t
  2681. ‌ngx_http_v2_construct_request_line(ngx_http_request_t *r)
  2682. {
  2683.     u_char  *p;

  2684.     static const u_char ending[] = " HTTP/2.0";

  2685.     if (r->request_line.len) {
  2686.         return NGX_OK;
  2687.     }

  2688.     if (r->method_name.len == 0
  2689.         || r->schema.len == 0
  2690.         || r->unparsed_uri.len == 0)
  2691.     {
  2692.         if (r->method_name.len == 0) {
  2693.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2694.                           "client sent no :method header");

  2695.         } else if (r->schema.len == 0) {
  2696.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2697.                           "client sent no :scheme header");

  2698.         } else {
  2699.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2700.                           "client sent no :path header");
  2701.         }

  2702.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2703.         return NGX_ERROR;
  2704.     }

  2705.     r->request_line.len = r->method_name.len + 1
  2706.                           + r->unparsed_uri.len
  2707.                           + sizeof(ending) - 1;

  2708.     p = ngx_pnalloc(r->pool, r->request_line.len + 1);
  2709.     if (p == NULL) {
  2710.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2711.         return NGX_ERROR;
  2712.     }

  2713.     r->request_line.data = p;

  2714.     p = ngx_cpymem(p, r->method_name.data, r->method_name.len);

  2715.     *p++ = ' ';

  2716.     p = ngx_cpymem(p, r->unparsed_uri.data, r->unparsed_uri.len);

  2717.     ngx_memcpy(p, ending, sizeof(ending));

  2718.     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2719.                    "http2 request line: \"%V\"", &r->request_line);

  2720.     return NGX_OK;
  2721. }


  2722. static ngx_int_t
  2723. ‌ngx_http_v2_cookie(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2724. {
  2725.     ngx_str_t    *val;
  2726.     ngx_array_t  *cookies;

  2727.     cookies = r->stream->cookies;

  2728.     if (cookies == NULL) {
  2729.         cookies = ngx_array_create(r->pool, 2, sizeof(ngx_str_t));
  2730.         if (cookies == NULL) {
  2731.             return NGX_ERROR;
  2732.         }

  2733.         r->stream->cookies = cookies;
  2734.     }

  2735.     val = ngx_array_push(cookies);
  2736.     if (val == NULL) {
  2737.         return NGX_ERROR;
  2738.     }

  2739.     val->len = header->value.len;
  2740.     val->data = header->value.data;

  2741.     return NGX_OK;
  2742. }


  2743. static ngx_int_t
  2744. ‌ngx_http_v2_construct_cookie_header(ngx_http_request_t *r)
  2745. {
  2746.     u_char                     *buf, *p, *end;
  2747.     size_t                      len;
  2748.     ngx_str_t                  *vals;
  2749.     ngx_uint_t                  i;
  2750.     ngx_array_t                *cookies;
  2751.     ngx_table_elt_t            *h;
  2752.     ngx_http_header_t          *hh;
  2753.     ngx_http_core_main_conf_t  *cmcf;

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

  2755.     cookies = r->stream->cookies;

  2756.     if (cookies == NULL) {
  2757.         return NGX_OK;
  2758.     }

  2759.     vals = cookies->elts;

  2760.     i = 0;
  2761.     len = 0;

  2762.     do {
  2763.         len += vals[i].len + 2;
  2764.     } while (++i != cookies->nelts);

  2765.     len -= 2;

  2766.     buf = ngx_pnalloc(r->pool, len + 1);
  2767.     if (buf == NULL) {
  2768.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2769.         return NGX_ERROR;
  2770.     }

  2771.     p = buf;
  2772.     end = buf + len;

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

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

  2775.         if (p == end) {
  2776.             *p = '\0';
  2777.             break;
  2778.         }

  2779.         *p++ = ';'; *p++ = ' ';
  2780.     }

  2781.     h = ngx_list_push(&r->headers_in.headers);
  2782.     if (h == NULL) {
  2783.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2784.         return NGX_ERROR;
  2785.     }

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

  2788.     h->key.len = cookie.len;
  2789.     h->key.data = cookie.data;

  2790.     h->value.len = len;
  2791.     h->value.data = buf;

  2792.     h->lowcase_key = cookie.data;

  2793.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  2796.     if (hh == NULL) {
  2797.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2798.         return NGX_ERROR;
  2799.     }

  2800.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  2801.         /*
  2802.          * request has been finalized already
  2803.          * in ngx_http_process_header_line()
  2804.          */
  2805.         return NGX_ERROR;
  2806.     }

  2807.     return NGX_OK;
  2808. }


  2809. static ngx_int_t
  2810. ‌ngx_http_v2_priority(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2811. {
  2812.     ngx_str_t    *val;
  2813.     ngx_array_t  *priorities;

  2814.     priorities = r->stream->priorities;

  2815.     if (priorities == NULL) {
  2816.         priorities = ngx_array_create(r->pool, 1, sizeof(ngx_str_t));
  2817.         if (priorities == NULL) {
  2818.             return NGX_ERROR;
  2819.         }

  2820.         r->stream->priorities = priorities;
  2821.     }

  2822.     val = ngx_array_push(priorities);
  2823.     if (val == NULL) {
  2824.         return NGX_ERROR;
  2825.     }

  2826.     val->len = header->value.len;
  2827.     val->data = header->value.data;

  2828.     return NGX_OK;
  2829. }


  2830. static ngx_int_t
  2831. ‌ngx_http_v2_process_priority(ngx_http_request_t *r)
  2832. {
  2833.     u_char               *p;
  2834.     size_t                len;
  2835.     ngx_str_t             value, *vals;
  2836.     ngx_uint_t            i;
  2837.     ngx_array_t          *priorities;
  2838.     ngx_http_priority_t   priority;

  2839.     priorities = r->stream->priorities;

  2840.     if (priorities == NULL) {
  2841.         return NGX_OK;
  2842.     }

  2843.     /*
  2844.      * RFC 9218, Section 7: a PRIORITY_UPDATE frame overrides the Priority
  2845.      * header field, so ignore the header once a frame has been applied.
  2846.      */

  2847.     if (r->stream->priority_update) {
  2848.         return NGX_OK;
  2849.     }

  2850.     vals = priorities->elts;

  2851.     if (priorities->nelts == 1) {
  2852.         value = vals[0];

  2853.     } else {

  2854.         /* combine the field lines into a single value, "v1, v2, ..." */

  2855.         len = 0;

  2856.         for (i = 0; i < priorities->nelts; i++) {
  2857.             len += vals[i].len + 2;
  2858.         }

  2859.         len -= 2;

  2860.         p = ngx_pnalloc(r->pool, len);
  2861.         if (p == NULL) {
  2862.             ngx_http_v2_close_stream(r->stream,
  2863.                                      NGX_HTTP_INTERNAL_SERVER_ERROR);
  2864.             return NGX_ERROR;
  2865.         }

  2866.         value.data = p;
  2867.         value.len = len;

  2868.         for (i = 0; /* void */ ; i++) {
  2869.             p = ngx_cpymem(p, vals[i].data, vals[i].len);

  2870.             if (i == priorities->nelts - 1) {
  2871.                 break;
  2872.             }

  2873.             *p++ = ','; *p++ = ' ';
  2874.         }
  2875.     }

  2876.     if (ngx_http_priority_parse(&value, &priority) != NGX_OK) {
  2877.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2878.                        "http2 ignoring malformed priority header");
  2879.         return NGX_OK;
  2880.     }

  2881.     r->stream->priority.client = priority;
  2882.     ngx_http_priority_state_update(&r->stream->priority);

  2883.     return NGX_OK;
  2884. }


  2885. static ngx_int_t
  2886. ‌ngx_http_v2_construct_host_header(ngx_http_request_t *r)
  2887. {
  2888.     ngx_table_elt_t            *h;
  2889.     ngx_http_header_t          *hh;
  2890.     ngx_http_core_main_conf_t  *cmcf;

  2891.     static ngx_str_t host = ngx_string("host");

  2892.     h = ngx_list_push(&r->headers_in.headers);
  2893.     if (h == NULL) {
  2894.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2895.         return NGX_ERROR;
  2896.     }

  2897.     h->hash = ngx_hash(ngx_hash(ngx_hash('h', 'o'), 's'), 't');

  2898.     h->key.len = host.len;
  2899.     h->key.data = host.data;

  2900.     h->value.len = r->host_end - r->host_start;
  2901.     h->value.data = r->host_start;

  2902.     h->lowcase_key = host.data;

  2903.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  2906.     if (hh == NULL) {
  2907.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2908.         return NGX_ERROR;
  2909.     }

  2910.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  2911.         /*
  2912.          * request has been finalized already
  2913.          * in ngx_http_process_host()
  2914.          */
  2915.         return NGX_ERROR;
  2916.     }

  2917.     return NGX_OK;
  2918. }


  2919. static void
  2920. ‌ngx_http_v2_run_request(ngx_http_request_t *r)
  2921. {
  2922.     ngx_str_t                  host;
  2923.     ngx_connection_t          *fc;
  2924.     ngx_http_v2_srv_conf_t    *h2scf;
  2925.     ngx_http_v2_connection_t  *h2c;

  2926.     fc = r->connection;

  2927.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

  2928.     if (!h2scf->enable && !r->http_connection->addr_conf->http2) {
  2929.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2930.                       "client attempted to request the server name "
  2931.                       "for which the negotiated protocol is disabled");

  2932.         ngx_http_finalize_request(r, NGX_HTTP_MISDIRECTED_REQUEST);
  2933.         goto failed;
  2934.     }

  2935.     if (ngx_http_v2_construct_request_line(r) != NGX_OK) {
  2936.         goto failed;
  2937.     }

  2938.     if (ngx_http_v2_construct_cookie_header(r) != NGX_OK) {
  2939.         goto failed;
  2940.     }

  2941.     if (ngx_http_v2_process_priority(r) != NGX_OK) {
  2942.         goto failed;
  2943.     }

  2944.     r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;

  2945.     if (r->headers_in.connection) {
  2946.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2947.                       "client sent \"Connection\" header");
  2948.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2949.         goto failed;
  2950.     }

  2951.     if (r->headers_in.keep_alive) {
  2952.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2953.                       "client sent \"Keep-Alive\" header");
  2954.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2955.         goto failed;
  2956.     }

  2957.     if (r->headers_in.transfer_encoding) {
  2958.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2959.                       "client sent \"Transfer-Encoding\" header");
  2960.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2961.         goto failed;
  2962.     }

  2963.     if (r->headers_in.upgrade) {
  2964.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2965.                       "client sent \"Upgrade\" header");
  2966.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2967.         goto failed;
  2968.     }

  2969.     if (r->headers_in.te
  2970.         && (r->headers_in.te->next
  2971.             || r->headers_in.te->value.len != 8
  2972.             || ngx_strncasecmp(r->headers_in.te->value.data,
  2973.                                (u_char *) "trailers", 8) != 0))
  2974.     {
  2975.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2976.                       "client sent invalid \"TE\" header");
  2977.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2978.         goto failed;
  2979.     }

  2980.     if (r->headers_in.server.len == 0) {
  2981.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2982.                       "client sent neither \":authority\" nor \"Host\" header");
  2983.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2984.         goto failed;
  2985.     }

  2986.     if (r->host_end) {

  2987.         host.len = r->host_end - r->host_start;
  2988.         host.data = r->host_start;

  2989.         if (r->headers_in.host) {
  2990.             if (r->headers_in.host->value.len != host.len
  2991.                 || ngx_memcmp(r->headers_in.host->value.data, host.data,
  2992.                               host.len)
  2993.                    != 0)
  2994.             {
  2995.                 ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2996.                               "client sent \":authority\" and \"Host\" headers "
  2997.                               "with different values");
  2998.                 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2999.                 goto failed;
  3000.             }

  3001.         } else {
  3002.             /* compatibility for $http_host */

  3003.             if (ngx_http_v2_construct_host_header(r) != NGX_OK) {
  3004.                 goto failed;
  3005.             }
  3006.         }
  3007.     }

  3008.     if (r->headers_in.content_length) {
  3009.         r->headers_in.content_length_n =
  3010.                             ngx_atoof(r->headers_in.content_length->value.data,
  3011.                                       r->headers_in.content_length->value.len);

  3012.         if (r->headers_in.content_length_n == NGX_ERROR) {
  3013.             ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  3014.                           "client sent invalid \"Content-Length\" header");
  3015.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  3016.             goto failed;
  3017.         }

  3018.         if (r->headers_in.content_length_n > 0 && r->stream->in_closed) {
  3019.             ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  3020.                           "client prematurely closed stream");

  3021.             r->stream->skip_data = 1;

  3022.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  3023.             goto failed;
  3024.         }

  3025.     } else if (!r->stream->in_closed) {
  3026.         r->headers_in.chunked = 1;
  3027.     }

  3028.     if (r->method == NGX_HTTP_CONNECT) {
  3029.         ngx_log_error(NGX_LOG_INFO, fc->log, 0, "client sent CONNECT method");
  3030.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  3031.         goto failed;
  3032.     }

  3033.     if (r->method == NGX_HTTP_TRACE) {
  3034.         ngx_log_error(NGX_LOG_INFO, fc->log, 0, "client sent TRACE method");
  3035.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  3036.         goto failed;
  3037.     }

  3038.     h2c = r->stream->connection;

  3039.     h2c->payload_bytes += r->request_length;

  3040.     ngx_http_process_request(r);

  3041. failed:

  3042.     ngx_http_run_posted_requests(fc);
  3043. }


  3044. ngx_int_t
  3045. ‌ngx_http_v2_read_request_body(ngx_http_request_t *r)
  3046. {
  3047.     off_t                      len;
  3048.     size_t                     size;
  3049.     ngx_buf_t                 *buf;
  3050.     ngx_int_t                  rc;
  3051.     ngx_http_v2_stream_t      *stream;
  3052.     ngx_http_v2_srv_conf_t    *h2scf;
  3053.     ngx_http_request_body_t   *rb;
  3054.     ngx_http_core_loc_conf_t  *clcf;
  3055.     ngx_http_v2_connection_t  *h2c;

  3056.     stream = r->stream;
  3057.     rb = r->request_body;

  3058.     if (stream->skip_data) {
  3059.         r->request_body_no_buffering = 0;
  3060.         rb->post_handler(r);
  3061.         return NGX_OK;
  3062.     }

  3063.     rb->rest = 1;

  3064.     /* set rb->filter_need_buffering */

  3065.     rc = ngx_http_top_request_body_filter(r, NULL);

  3066.     if (rc != NGX_OK) {
  3067.         stream->skip_data = 1;
  3068.         return rc;
  3069.     }

  3070.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);
  3071.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  3072.     len = r->headers_in.content_length_n;

  3073.     if (len < 0 || len > (off_t) clcf->client_body_buffer_size) {
  3074.         len = clcf->client_body_buffer_size;

  3075.     } else {
  3076.         len++;
  3077.     }

  3078.     if (r->request_body_no_buffering || rb->filter_need_buffering) {

  3079.         /*
  3080.          * We need a room to store data up to the stream's initial window size,
  3081.          * at least until this window will be exhausted.
  3082.          */

  3083.         if (len < (off_t) h2scf->preread_size) {
  3084.             len = h2scf->preread_size;
  3085.         }

  3086.         if (len > NGX_HTTP_V2_MAX_WINDOW) {
  3087.             len = NGX_HTTP_V2_MAX_WINDOW;
  3088.         }
  3089.     }

  3090.     rb->buf = ngx_create_temp_buf(r->pool, (size_t) len);

  3091.     if (rb->buf == NULL) {
  3092.         stream->skip_data = 1;
  3093.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3094.     }

  3095.     buf = stream->preread;

  3096.     if (stream->in_closed) {
  3097.         if (!rb->filter_need_buffering) {
  3098.             r->request_body_no_buffering = 0;
  3099.         }

  3100.         if (buf) {
  3101.             rc = ngx_http_v2_process_request_body(r, buf->pos,
  3102.                                                   buf->last - buf->pos, 1, 0);
  3103.             ngx_pfree(r->pool, buf->start);

  3104.         } else {
  3105.             rc = ngx_http_v2_process_request_body(r, NULL, 0, 1, 0);
  3106.         }

  3107.         if (rc != NGX_AGAIN) {
  3108.             return rc;
  3109.         }

  3110.         r->read_event_handler = ngx_http_v2_read_client_request_body_handler;
  3111.         r->write_event_handler = ngx_http_request_empty_handler;

  3112.         return NGX_AGAIN;
  3113.     }

  3114.     if (buf) {
  3115.         rc = ngx_http_v2_process_request_body(r, buf->pos,
  3116.                                               buf->last - buf->pos, 0, 0);

  3117.         ngx_pfree(r->pool, buf->start);

  3118.         if (rc != NGX_OK && rc != NGX_AGAIN) {
  3119.             stream->skip_data = 1;
  3120.             return rc;
  3121.         }
  3122.     }

  3123.     if (r->request_body_no_buffering || rb->filter_need_buffering) {
  3124.         size = (size_t) len - h2scf->preread_size;

  3125.     } else {
  3126.         stream->no_flow_control = 1;
  3127.         size = NGX_HTTP_V2_MAX_WINDOW - stream->recv_window;
  3128.     }

  3129.     if (size) {
  3130.         if (ngx_http_v2_send_window_update(stream->connection,
  3131.                                            stream->node->id, size)
  3132.             == NGX_ERROR)
  3133.         {
  3134.             stream->skip_data = 1;
  3135.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3136.         }

  3137.         h2c = stream->connection;

  3138.         if (!h2c->blocked) {
  3139.             if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3140.                 stream->skip_data = 1;
  3141.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3142.             }
  3143.         }

  3144.         stream->recv_window += size;
  3145.     }

  3146.     if (!buf) {
  3147.         ngx_add_timer(r->connection->read, clcf->client_body_timeout);
  3148.     }

  3149.     r->read_event_handler = ngx_http_v2_read_client_request_body_handler;
  3150.     r->write_event_handler = ngx_http_request_empty_handler;

  3151.     return NGX_AGAIN;
  3152. }


  3153. static ngx_int_t
  3154. ‌ngx_http_v2_process_request_body(ngx_http_request_t *r, u_char *pos,
  3155.     size_t size, ngx_uint_t last, ngx_uint_t flush)
  3156. {
  3157.     size_t                     n;
  3158.     ngx_int_t                  rc;
  3159.     ngx_connection_t          *fc;
  3160.     ngx_http_request_body_t   *rb;
  3161.     ngx_http_core_loc_conf_t  *clcf;

  3162.     fc = r->connection;
  3163.     rb = r->request_body;

  3164.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3165.                    "http2 process request body");

  3166.     if (size == 0 && !last && !flush) {
  3167.         return NGX_AGAIN;
  3168.     }

  3169.     for ( ;; ) {
  3170.         for ( ;; ) {
  3171.             if (rb->buf->last == rb->buf->end && size) {

  3172.                 if (r->request_body_no_buffering) {

  3173.                     /* should never happen due to flow control */

  3174.                     ngx_log_error(NGX_LOG_ALERT, fc->log, 0,
  3175.                                   "no space in http2 body buffer");

  3176.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3177.                 }

  3178.                 /* update chains */

  3179.                 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3180.                                "http2 body update chains");

  3181.                 rc = ngx_http_v2_filter_request_body(r);

  3182.                 if (rc != NGX_OK) {
  3183.                     return rc;
  3184.                 }

  3185.                 if (rb->busy != NULL) {
  3186.                     ngx_log_error(NGX_LOG_ALERT, fc->log, 0,
  3187.                                   "busy buffers after request body flush");
  3188.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3189.                 }

  3190.                 rb->buf->pos = rb->buf->start;
  3191.                 rb->buf->last = rb->buf->start;
  3192.             }

  3193.             /* copy body data to the buffer */

  3194.             n = rb->buf->end - rb->buf->last;

  3195.             if (n > size) {
  3196.                 n = size;
  3197.             }

  3198.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3199.                            "http2 request body recv %uz", n);

  3200.             if (n > 0) {
  3201.                 rb->buf->last = ngx_cpymem(rb->buf->last, pos, n);
  3202.                 pos += n;
  3203.                 size -= n;
  3204.             }

  3205.             if (size == 0 && last) {
  3206.                 rb->rest = 0;
  3207.             }

  3208.             if (size == 0) {
  3209.                 break;
  3210.             }
  3211.         }

  3212.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3213.                        "http2 request body rest %O", rb->rest);

  3214.         if (flush) {
  3215.             rc = ngx_http_v2_filter_request_body(r);

  3216.             if (rc != NGX_OK) {
  3217.                 return rc;
  3218.             }
  3219.         }

  3220.         if (rb->rest == 0 && rb->last_saved) {
  3221.             break;
  3222.         }

  3223.         if (size == 0) {
  3224.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  3225.             ngx_add_timer(fc->read, clcf->client_body_timeout);

  3226.             if (!flush) {
  3227.                 ngx_post_event(fc->read, &ngx_posted_events);
  3228.             }

  3229.             return NGX_AGAIN;
  3230.         }
  3231.     }

  3232.     if (fc->read->timer_set) {
  3233.         ngx_del_timer(fc->read);
  3234.     }

  3235.     if (r->request_body_no_buffering) {
  3236.         if (!flush) {
  3237.             ngx_post_event(fc->read, &ngx_posted_events);
  3238.         }

  3239.         return NGX_OK;
  3240.     }

  3241.     if (r->headers_in.chunked) {
  3242.         r->headers_in.content_length_n = rb->received;
  3243.     }

  3244.     r->read_event_handler = ngx_http_block_reading;
  3245.     rb->post_handler(r);

  3246.     return NGX_OK;
  3247. }


  3248. static ngx_int_t
  3249. ‌ngx_http_v2_filter_request_body(ngx_http_request_t *r)
  3250. {
  3251.     ngx_buf_t                 *b, *buf;
  3252.     ngx_int_t                  rc;
  3253.     ngx_chain_t               *cl;
  3254.     ngx_http_request_body_t   *rb;
  3255.     ngx_http_core_loc_conf_t  *clcf;

  3256.     rb = r->request_body;
  3257.     buf = rb->buf;

  3258.     if (buf->pos == buf->last && (rb->rest || rb->last_sent)) {
  3259.         cl = NULL;
  3260.         goto update;
  3261.     }

  3262.     cl = ngx_chain_get_free_buf(r->pool, &rb->free);
  3263.     if (cl == NULL) {
  3264.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3265.     }

  3266.     b = cl->buf;

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

  3268.     if (buf->pos != buf->last) {
  3269.         r->request_length += buf->last - buf->pos;
  3270.         rb->received += buf->last - buf->pos;

  3271.         if (r->headers_in.content_length_n != -1) {
  3272.             if (rb->received > r->headers_in.content_length_n) {
  3273.                 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  3274.                               "client intended to send body data "
  3275.                               "larger than declared");

  3276.                 return NGX_HTTP_BAD_REQUEST;
  3277.             }

  3278.         } else {
  3279.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  3280.             if (clcf->client_max_body_size
  3281.                 && rb->received > clcf->client_max_body_size)
  3282.             {
  3283.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  3284.                               "client intended to send too large chunked body: "
  3285.                               "%O bytes", rb->received);

  3286.                 return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
  3287.             }
  3288.         }

  3289.         b->temporary = 1;
  3290.         b->pos = buf->pos;
  3291.         b->last = buf->last;
  3292.         b->start = b->pos;
  3293.         b->end = b->last;

  3294.         buf->pos = buf->last;
  3295.     }

  3296.     if (!rb->rest) {
  3297.         if (r->headers_in.content_length_n != -1
  3298.             && r->headers_in.content_length_n != rb->received)
  3299.         {
  3300.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  3301.                           "client prematurely closed stream: "
  3302.                           "only %O out of %O bytes of request body received",
  3303.                           rb->received, r->headers_in.content_length_n);

  3304.             return NGX_HTTP_BAD_REQUEST;
  3305.         }

  3306.         b->last_buf = 1;
  3307.         rb->last_sent = 1;
  3308.     }

  3309.     b->tag = (ngx_buf_tag_t) &ngx_http_v2_filter_request_body;
  3310.     b->flush = r->request_body_no_buffering;

  3311. update:

  3312.     rc = ngx_http_top_request_body_filter(r, cl);

  3313.     ngx_chain_update_chains(r->pool, &rb->free, &rb->busy, &cl,
  3314.                             (ngx_buf_tag_t) &ngx_http_v2_filter_request_body);

  3315.     return rc;
  3316. }


  3317. static void
  3318. ‌ngx_http_v2_read_client_request_body_handler(ngx_http_request_t *r)
  3319. {
  3320.     size_t                     window;
  3321.     ngx_buf_t                 *buf;
  3322.     ngx_int_t                  rc;
  3323.     ngx_connection_t          *fc;
  3324.     ngx_http_v2_stream_t      *stream;
  3325.     ngx_http_v2_connection_t  *h2c;

  3326.     fc = r->connection;

  3327.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3328.                    "http2 read client request body handler");

  3329.     if (fc->read->timedout) {
  3330.         ngx_log_error(NGX_LOG_INFO, fc->log, NGX_ETIMEDOUT, "client timed out");

  3331.         fc->timedout = 1;
  3332.         r->stream->skip_data = 1;

  3333.         ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  3334.         return;
  3335.     }

  3336.     if (fc->error) {
  3337.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  3338.                       "client prematurely closed stream");

  3339.         r->stream->skip_data = 1;

  3340.         ngx_http_finalize_request(r, NGX_HTTP_CLIENT_CLOSED_REQUEST);
  3341.         return;
  3342.     }

  3343.     rc = ngx_http_v2_process_request_body(r, NULL, 0, r->stream->in_closed, 1);

  3344.     if (rc != NGX_OK && rc != NGX_AGAIN) {
  3345.         r->stream->skip_data = 1;
  3346.         ngx_http_finalize_request(r, rc);
  3347.         return;
  3348.     }

  3349.     if (rc == NGX_OK) {
  3350.         return;
  3351.     }

  3352.     if (r->stream->no_flow_control) {
  3353.         return;
  3354.     }

  3355.     if (r->request_body->rest == 0) {
  3356.         return;
  3357.     }

  3358.     if (r->request_body->busy != NULL) {
  3359.         return;
  3360.     }

  3361.     stream = r->stream;
  3362.     h2c = stream->connection;

  3363.     buf = r->request_body->buf;

  3364.     buf->pos = buf->start;
  3365.     buf->last = buf->start;

  3366.     window = buf->end - buf->start;

  3367.     if (h2c->state.stream == stream) {
  3368.         window -= h2c->state.length;
  3369.     }

  3370.     if (window <= stream->recv_window) {
  3371.         if (window < stream->recv_window) {
  3372.             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  3373.                           "http2 negative window update");

  3374.             stream->skip_data = 1;

  3375.             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3376.             return;
  3377.         }

  3378.         return;
  3379.     }

  3380.     if (ngx_http_v2_send_window_update(h2c, stream->node->id,
  3381.                                        window - stream->recv_window)
  3382.         == NGX_ERROR)
  3383.     {
  3384.         stream->skip_data = 1;
  3385.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3386.         return;
  3387.     }

  3388.     stream->recv_window = window;

  3389.     if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3390.         stream->skip_data = 1;
  3391.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3392.         return;
  3393.     }
  3394. }


  3395. ngx_int_t
  3396. ‌ngx_http_v2_read_unbuffered_request_body(ngx_http_request_t *r)
  3397. {
  3398.     size_t                     window;
  3399.     ngx_buf_t                 *buf;
  3400.     ngx_int_t                  rc;
  3401.     ngx_connection_t          *fc;
  3402.     ngx_http_v2_stream_t      *stream;
  3403.     ngx_http_v2_connection_t  *h2c;

  3404.     stream = r->stream;
  3405.     fc = r->connection;

  3406.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3407.                    "http2 read unbuffered request body");

  3408.     if (fc->read->timedout) {
  3409.         if (stream->recv_window) {
  3410.             stream->skip_data = 1;
  3411.             fc->timedout = 1;

  3412.             return NGX_HTTP_REQUEST_TIME_OUT;
  3413.         }

  3414.         fc->read->timedout = 0;
  3415.     }

  3416.     if (fc->error) {
  3417.         stream->skip_data = 1;
  3418.         return NGX_HTTP_BAD_REQUEST;
  3419.     }

  3420.     rc = ngx_http_v2_process_request_body(r, NULL, 0, r->stream->in_closed, 1);

  3421.     if (rc != NGX_OK && rc != NGX_AGAIN) {
  3422.         stream->skip_data = 1;
  3423.         return rc;
  3424.     }

  3425.     if (rc == NGX_OK) {
  3426.         return NGX_OK;
  3427.     }

  3428.     if (r->request_body->rest == 0) {
  3429.         return NGX_AGAIN;
  3430.     }

  3431.     if (r->request_body->busy != NULL) {
  3432.         return NGX_AGAIN;
  3433.     }

  3434.     buf = r->request_body->buf;

  3435.     buf->pos = buf->start;
  3436.     buf->last = buf->start;

  3437.     window = buf->end - buf->start;
  3438.     h2c = stream->connection;

  3439.     if (h2c->state.stream == stream) {
  3440.         window -= h2c->state.length;
  3441.     }

  3442.     if (window <= stream->recv_window) {
  3443.         if (window < stream->recv_window) {
  3444.             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  3445.                           "http2 negative window update");
  3446.             stream->skip_data = 1;
  3447.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3448.         }

  3449.         return NGX_AGAIN;
  3450.     }

  3451.     if (ngx_http_v2_send_window_update(h2c, stream->node->id,
  3452.                                        window - stream->recv_window)
  3453.         == NGX_ERROR)
  3454.     {
  3455.         stream->skip_data = 1;
  3456.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3457.     }

  3458.     if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3459.         stream->skip_data = 1;
  3460.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3461.     }

  3462.     stream->recv_window = window;

  3463.     return NGX_AGAIN;
  3464. }


  3465. static ngx_int_t
  3466. ‌ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
  3467.     ngx_http_v2_stream_t *stream, ngx_uint_t status)
  3468. {
  3469.     ngx_event_t       *rev;
  3470.     ngx_connection_t  *fc;

  3471.     if (stream->rst_sent) {
  3472.         return NGX_OK;
  3473.     }

  3474.     if (ngx_http_v2_send_rst_stream(h2c, stream->node->id, status)
  3475.         == NGX_ERROR)
  3476.     {
  3477.         return NGX_ERROR;
  3478.     }

  3479.     stream->rst_sent = 1;
  3480.     stream->skip_data = 1;

  3481.     fc = stream->request->connection;
  3482.     fc->error = 1;

  3483.     rev = fc->read;
  3484.     rev->handler(rev);

  3485.     return NGX_OK;
  3486. }


  3487. void
  3488. ‌ngx_http_v2_close_stream(ngx_http_v2_stream_t *stream, ngx_int_t rc)
  3489. {
  3490.     ngx_pool_t                *pool;
  3491.     ngx_event_t               *ev;
  3492.     ngx_connection_t          *fc;
  3493.     ngx_http_v2_node_t        *node;
  3494.     ngx_http_v2_connection_t  *h2c;

  3495.     h2c = stream->connection;
  3496.     node = stream->node;

  3497.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  3498.                    "http2 close stream %ui, queued %ui, processing %ui",
  3499.                    node->id, stream->queued, h2c->processing);

  3500.     fc = stream->request->connection;

  3501.     if (stream->queued) {
  3502.         fc->error = 1;
  3503.         fc->write->handler = ngx_http_v2_retry_close_stream_handler;
  3504.         fc->read->handler = ngx_http_v2_retry_close_stream_handler;
  3505.         return;
  3506.     }

  3507.     if (!stream->rst_sent && !h2c->connection->error) {

  3508.         if (!stream->out_closed) {
  3509.             if (ngx_http_v2_send_rst_stream(h2c, node->id,
  3510.                                       fc->timedout ? NGX_HTTP_V2_PROTOCOL_ERROR
  3511.                                                    : NGX_HTTP_V2_INTERNAL_ERROR)
  3512.                 != NGX_OK)
  3513.             {
  3514.                 h2c->connection->error = 1;
  3515.             }

  3516.         } else if (!stream->in_closed) {
  3517.             if (ngx_http_v2_send_rst_stream(h2c, node->id, NGX_HTTP_V2_NO_ERROR)
  3518.                 != NGX_OK)
  3519.             {
  3520.                 h2c->connection->error = 1;
  3521.             }
  3522.         }
  3523.     }

  3524.     if (h2c->state.stream == stream) {
  3525.         h2c->state.stream = NULL;
  3526.     }

  3527.     node->stream = NULL;

  3528.     ngx_queue_insert_tail(&h2c->closed, &node->reuse);
  3529.     h2c->closed_nodes++;

  3530.     /*
  3531.      * This pool keeps decoded request headers which can be used by log phase
  3532.      * handlers in ngx_http_free_request().
  3533.      *
  3534.      * The pointer is stored into local variable because the stream object
  3535.      * will be destroyed after a call to ngx_http_free_request().
  3536.      */
  3537.     pool = stream->pool;

  3538.     h2c->frames -= stream->frames;

  3539.     ngx_http_free_request(stream->request, rc);

  3540.     if (pool != h2c->state.pool) {
  3541.         ngx_destroy_pool(pool);

  3542.     } else {
  3543.         /* pool will be destroyed when the complete header is parsed */
  3544.         h2c->state.keep_pool = 0;
  3545.     }

  3546.     ev = fc->read;

  3547.     if (ev->timer_set) {
  3548.         ngx_del_timer(ev);
  3549.     }

  3550.     if (ev->posted) {
  3551.         ngx_delete_posted_event(ev);
  3552.     }

  3553.     ev = fc->write;

  3554.     if (ev->timer_set) {
  3555.         ngx_del_timer(ev);
  3556.     }

  3557.     if (ev->posted) {
  3558.         ngx_delete_posted_event(ev);
  3559.     }

  3560.     fc->data = h2c->free_fake_connections;
  3561.     h2c->free_fake_connections = fc;

  3562.     h2c->processing--;

  3563.     if (h2c->processing || h2c->blocked) {
  3564.         return;
  3565.     }

  3566.     ev = h2c->connection->read;

  3567.     ev->handler = ngx_http_v2_handle_connection_handler;
  3568.     ngx_post_event(ev, &ngx_posted_events);
  3569. }


  3570. static void
  3571. ‌ngx_http_v2_close_stream_handler(ngx_event_t *ev)
  3572. {
  3573.     ngx_connection_t    *fc;
  3574.     ngx_http_request_t  *r;

  3575.     fc = ev->data;
  3576.     r = fc->data;

  3577.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3578.                    "http2 close stream handler");

  3579.     if (ev->timedout) {
  3580.         ngx_log_error(NGX_LOG_INFO, fc->log, NGX_ETIMEDOUT, "client timed out");

  3581.         fc->timedout = 1;

  3582.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_REQUEST_TIME_OUT);
  3583.         return;
  3584.     }

  3585.     ngx_http_v2_close_stream(r->stream, 0);
  3586. }


  3587. static void
  3588. ‌ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev)
  3589. {
  3590.     ngx_connection_t    *fc;
  3591.     ngx_http_request_t  *r;

  3592.     fc = ev->data;
  3593.     r = fc->data;

  3594.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3595.                    "http2 retry close stream handler");

  3596.     ngx_http_v2_close_stream(r->stream, 0);
  3597. }


  3598. static void
  3599. ‌ngx_http_v2_handle_connection_handler(ngx_event_t *rev)
  3600. {
  3601.     ngx_connection_t          *c;
  3602.     ngx_http_v2_connection_t  *h2c;

  3603.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  3604.                    "http2 handle connection handler");

  3605.     c = rev->data;
  3606.     h2c = c->data;

  3607.     if (c->error) {
  3608.         ngx_http_v2_finalize_connection(h2c, 0);
  3609.         return;
  3610.     }

  3611.     rev->handler = ngx_http_v2_read_handler;

  3612.     if (rev->ready) {
  3613.         ngx_http_v2_read_handler(rev);
  3614.         return;
  3615.     }

  3616.     if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3617.         ngx_http_v2_finalize_connection(h2c, 0);
  3618.         return;
  3619.     }

  3620.     ngx_http_v2_handle_connection(c->data);
  3621. }


  3622. static void
  3623. ‌ngx_http_v2_idle_handler(ngx_event_t *rev)
  3624. {
  3625.     ngx_connection_t          *c;
  3626.     ngx_http_v2_srv_conf_t    *h2scf;
  3627.     ngx_http_v2_connection_t  *h2c;
  3628.     ngx_http_core_loc_conf_t  *clcf;

  3629.     c = rev->data;
  3630.     h2c = c->data;

  3631.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 idle handler");

  3632.     if (rev->timedout || c->close) {
  3633.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  3634.         return;
  3635.     }

  3636. #if (NGX_HAVE_KQUEUE)

  3637.     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
  3638.         if (rev->pending_eof) {
  3639.             c->log->handler = NULL;
  3640.             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
  3641.                           "kevent() reported that client %V closed "
  3642.                           "idle connection", &c->addr_text);
  3643. #if (NGX_HTTP_SSL)
  3644.             if (c->ssl) {
  3645.                 c->ssl->no_send_shutdown = 1;
  3646.             }
  3647. #endif
  3648.             ngx_http_close_connection(c);
  3649.             return;
  3650.         }
  3651.     }

  3652. #endif

  3653.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  3654.                                         ngx_http_core_module);

  3655.     if (h2c->idle++ > 10 * clcf->keepalive_requests) {
  3656.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  3657.                       "http2 flood detected");
  3658.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  3659.         return;
  3660.     }

  3661.     c->destroyed = 0;
  3662.     ngx_reusable_connection(c, 0);

  3663.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3664.                                          ngx_http_v2_module);

  3665.     h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
  3666.     if (h2c->pool == NULL) {
  3667.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  3668.         return;
  3669.     }

  3670.     c->write->handler = ngx_http_v2_write_handler;

  3671.     rev->handler = ngx_http_v2_read_handler;
  3672.     ngx_http_v2_read_handler(rev);
  3673. }


  3674. static void
  3675. ‌ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
  3676.     ngx_uint_t status)
  3677. {
  3678.     ngx_uint_t               i, size;
  3679.     ngx_event_t             *ev;
  3680.     ngx_connection_t        *c, *fc;
  3681.     ngx_http_request_t      *r;
  3682.     ngx_http_v2_node_t      *node;
  3683.     ngx_http_v2_stream_t    *stream;
  3684.     ngx_http_v2_srv_conf_t  *h2scf;

  3685.     c = h2c->connection;

  3686.     h2c->blocked = 1;

  3687.     if (!c->error && !h2c->goaway) {
  3688.         h2c->goaway = 1;

  3689.         if (ngx_http_v2_send_goaway(h2c, status) != NGX_ERROR) {
  3690.             (void) ngx_http_v2_send_output_queue(h2c);
  3691.         }
  3692.     }

  3693.     if (!h2c->processing) {
  3694.         goto done;
  3695.     }

  3696.     c->read->handler = ngx_http_empty_handler;
  3697.     c->write->handler = ngx_http_empty_handler;

  3698.     h2c->last_out = NULL;

  3699.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3700.                                          ngx_http_v2_module);

  3701.     size = ngx_http_v2_index_size(h2scf);

  3702.     for (i = 0; i < size; i++) {

  3703.         for (node = h2c->streams_index[i]; node; node = node->index) {
  3704.             stream = node->stream;

  3705.             if (stream == NULL) {
  3706.                 continue;
  3707.             }

  3708.             stream->waiting = 0;

  3709.             r = stream->request;
  3710.             fc = r->connection;

  3711.             fc->error = 1;

  3712.             if (stream->queued) {
  3713.                 stream->queued = 0;

  3714.                 ev = fc->write;
  3715.                 ev->active = 0;
  3716.                 ev->ready = 1;

  3717.             } else {
  3718.                 ev = fc->read;
  3719.             }

  3720.             ev->eof = 1;
  3721.             ev->handler(ev);
  3722.         }
  3723.     }

  3724.     h2c->blocked = 0;

  3725.     if (h2c->processing) {
  3726.         c->error = 1;
  3727.         return;
  3728.     }

  3729. done:

  3730.     if (c->error) {
  3731.         ngx_http_close_connection(c);
  3732.         return;
  3733.     }

  3734.     ngx_http_v2_lingering_close(c);
  3735. }


  3736. static ngx_int_t
  3737. ‌ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c, ssize_t delta)
  3738. {
  3739.     ngx_uint_t               i, size;
  3740.     ngx_event_t             *wev;
  3741.     ngx_http_v2_node_t      *node;
  3742.     ngx_http_v2_stream_t    *stream;
  3743.     ngx_http_v2_srv_conf_t  *h2scf;

  3744.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3745.                                          ngx_http_v2_module);

  3746.     size = ngx_http_v2_index_size(h2scf);

  3747.     for (i = 0; i < size; i++) {

  3748.         for (node = h2c->streams_index[i]; node; node = node->index) {
  3749.             stream = node->stream;

  3750.             if (stream == NULL) {
  3751.                 continue;
  3752.             }

  3753.             if (delta > 0
  3754.                 && stream->send_window
  3755.                       > (ssize_t) (NGX_HTTP_V2_MAX_WINDOW - delta))
  3756.             {
  3757.                 if (ngx_http_v2_terminate_stream(h2c, stream,
  3758.                                                  NGX_HTTP_V2_FLOW_CTRL_ERROR)
  3759.                     == NGX_ERROR)
  3760.                 {
  3761.                     return NGX_ERROR;
  3762.                 }

  3763.                 continue;
  3764.             }

  3765.             stream->send_window += delta;

  3766.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  3767.                            "http2:%ui adjusted window: %z",
  3768.                            node->id, stream->send_window);

  3769.             if (stream->send_window > 0 && stream->exhausted) {
  3770.                 stream->exhausted = 0;

  3771.                 wev = stream->request->connection->write;

  3772.                 wev->active = 0;
  3773.                 wev->ready = 1;

  3774.                 if (!wev->delayed) {
  3775.                     wev->handler(wev);
  3776.                 }
  3777.             }
  3778.         }
  3779.     }

  3780.     return NGX_OK;
  3781. }


  3782. static void
  3783. ‌ngx_http_v2_pool_cleanup(void *data)
  3784. {
  3785.     ngx_http_v2_connection_t  *h2c = data;

  3786.     if (h2c->state.pool) {
  3787.         ngx_destroy_pool(h2c->state.pool);
  3788.     }

  3789.     if (h2c->pool) {
  3790.         ngx_destroy_pool(h2c->pool);
  3791.     }
  3792. }