src/http/v2/ngx_http_v2.c - nginx-1.31.4 nginx/ @ 8d9666701

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_FRAME_BUFFER_SIZE            24

  39. #define NGX_HTTP_V2_ROOT                         (void *) -1


  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_rst_stream(ngx_http_v2_connection_t *h2c,
  76.     u_char *pos, u_char *end);
  77. static u_char *ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c,
  78.     u_char *pos, u_char *end);
  79. static u_char *ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c,
  80.     u_char *pos, u_char *end);
  81. static u_char *ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c,
  82.     u_char *pos, u_char *end);
  83. static u_char *ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c,
  84.     u_char *pos, u_char *end);
  85. static u_char *ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c,
  86.     u_char *pos, u_char *end);
  87. static u_char *ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c,
  88.     u_char *pos, u_char *end);
  89. static u_char *ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c,
  90.     u_char *pos, u_char *end);
  91. static u_char *ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c,
  92.     u_char *pos, u_char *end);
  93. static u_char *ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c,
  94.     u_char *pos, u_char *end);
  95. static u_char *ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c,
  96.     u_char *pos, u_char *end);
  97. static u_char *ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c,
  98.     u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
  99. static u_char *ngx_http_v2_state_headers_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_connection_error(ngx_http_v2_connection_t *h2c,
  102.     ngx_uint_t err);

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

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

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

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

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

  149. static ngx_int_t ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
  150.     ngx_http_v2_stream_t *stream, ngx_uint_t status);
  151. static void ngx_http_v2_close_stream_handler(ngx_event_t *ev);
  152. static void ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev);
  153. static void ngx_http_v2_handle_connection_handler(ngx_event_t *rev);
  154. static void ngx_http_v2_idle_handler(ngx_event_t *rev);
  155. static void ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
  156.     ngx_uint_t status);

  157. static ngx_int_t ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c,
  158.     ssize_t delta);
  159. static void ngx_http_v2_set_dependency(ngx_http_v2_connection_t *h2c,
  160.     ngx_http_v2_node_t *node, ngx_uint_t depend, ngx_uint_t exclusive);
  161. static void ngx_http_v2_node_children_update(ngx_http_v2_node_t *node);

  162. static void ngx_http_v2_pool_cleanup(void *data);


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

  175. #define NGX_HTTP_V2_FRAME_STATES                                              \
  176.     (sizeof(ngx_http_v2_frame_states) / sizeof(ngx_http_v2_handler_pt))


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

  188.     c = rev->data;
  189.     hc = c->data;

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

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

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

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

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

  206.     h2c->connection = c;
  207.     h2c->http_connection = hc;

  208.     h2c->send_window = NGX_HTTP_V2_DEFAULT_WINDOW;
  209.     h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;

  210.     h2c->init_window = NGX_HTTP_V2_DEFAULT_WINDOW;

  211.     h2c->frame_size = NGX_HTTP_V2_DEFAULT_FRAME_SIZE;

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

  213.     h2c->priority_limit = ngx_max(h2scf->concurrent_streams, 100);

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

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

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

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

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

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

  243.     h2c->state.handler = ngx_http_v2_state_preface;

  244.     ngx_queue_init(&h2c->waiting);
  245.     ngx_queue_init(&h2c->dependencies);
  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.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  699.                       "client sent frame with unknown type %ui", type);
  700.         return ngx_http_v2_state_skip(h2c, pos, end);
  701.     }

  702.     return ngx_http_v2_frame_states[type](h2c, pos, end);
  703. }


  704. static u_char *
  705. ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  706. {
  707.     size_t                 size;
  708.     ngx_http_v2_node_t    *node;
  709.     ngx_http_v2_stream_t  *stream;

  710.     size = h2c->state.length;

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

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

  716.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  717.         }

  718.         if (end - pos == 0) {
  719.             return ngx_http_v2_state_save(h2c, pos, end,
  720.                                           ngx_http_v2_state_data);
  721.         }

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

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

  728.             return ngx_http_v2_connection_error(h2c,
  729.                                                 NGX_HTTP_V2_PROTOCOL_ERROR);
  730.         }

  731.         h2c->state.length -= 1 + h2c->state.padding;
  732.     }

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

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

  738.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  739.     }

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

  745.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
  746.     }

  747.     h2c->recv_window -= size;

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

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

  756.         h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;
  757.     }

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

  759.     if (node == NULL || node->stream == NULL) {
  760.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  761.                        "unknown http2 stream");

  762.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  763.     }

  764.     stream = node->stream;

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

  770.         if (ngx_http_v2_terminate_stream(h2c, stream,
  771.                                          NGX_HTTP_V2_FLOW_CTRL_ERROR)
  772.             == NGX_ERROR)
  773.         {
  774.             return ngx_http_v2_connection_error(h2c,
  775.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  776.         }

  777.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  778.     }

  779.     stream->recv_window -= size;

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

  791.         stream->recv_window = NGX_HTTP_V2_MAX_WINDOW;
  792.     }

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

  797.         if (ngx_http_v2_terminate_stream(h2c, stream,
  798.                                          NGX_HTTP_V2_STREAM_CLOSED)
  799.             == NGX_ERROR)
  800.         {
  801.             return ngx_http_v2_connection_error(h2c,
  802.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  803.         }

  804.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  805.     }

  806.     h2c->state.stream = stream;

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


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

  820.     stream = h2c->state.stream;

  821.     if (stream == NULL) {
  822.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  823.     }

  824.     if (stream->skip_data) {
  825.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  826.                        "skipping http2 DATA frame");

  827.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  828.     }

  829.     r = stream->request;
  830.     fc = r->connection;

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

  834.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  835.     }

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

  839.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  840.     }

  841.     size = end - pos;

  842.     if (size >= h2c->state.length) {
  843.         size = h2c->state.length;
  844.         stream->in_closed = h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG;
  845.     }

  846.     h2c->payload_bytes += size;

  847.     if (r->request_body) {
  848.         rc = ngx_http_v2_process_request_body(r, pos, size,
  849.                                               stream->in_closed, 0);

  850.         if (rc != NGX_OK && rc != NGX_AGAIN) {
  851.             stream->skip_data = 1;
  852.             ngx_http_finalize_request(r, rc);
  853.         }

  854.         ngx_http_run_posted_requests(fc);

  855.     } else if (size) {
  856.         buf = stream->preread;

  857.         if (buf == NULL) {
  858.             h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

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

  864.             stream->preread = buf;
  865.         }

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

  872.         buf->last = ngx_cpymem(buf->last, pos, size);
  873.     }

  874.     pos += size;
  875.     h2c->state.length -= size;

  876.     if (h2c->state.length) {
  877.         return ngx_http_v2_state_save(h2c, pos, end,
  878.                                       ngx_http_v2_state_read_data);
  879.     }

  880.     if (h2c->state.padding) {
  881.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  882.     }

  883.     return ngx_http_v2_state_complete(h2c, pos, end);
  884. }


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

  898.     padded = h2c->state.flags & NGX_HTTP_V2_PADDED_FLAG;
  899.     priority = h2c->state.flags & NGX_HTTP_V2_PRIORITY_FLAG;

  900.     size = 0;

  901.     if (padded) {
  902.         size++;
  903.     }

  904.     if (priority) {
  905.         size += sizeof(uint32_t) + 1;
  906.     }

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

  911.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  912.     }

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

  916.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  917.     }

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

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

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

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

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

  935.             return ngx_http_v2_connection_error(h2c,
  936.                                                 NGX_HTTP_V2_PROTOCOL_ERROR);
  937.         }

  938.         h2c->state.length -= h2c->state.padding;
  939.     }

  940.     depend = 0;
  941.     excl = 0;
  942.     weight = NGX_HTTP_V2_DEFAULT_WEIGHT;

  943.     if (priority) {
  944.         dependency = ngx_http_v2_parse_uint32(pos);

  945.         depend = dependency & 0x7fffffff;
  946.         excl = dependency >> 31;
  947.         weight = pos[4] + 1;

  948.         pos += sizeof(uint32_t) + 1;
  949.     }

  950.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  951.                    "http2 HEADERS frame sid:%ui "
  952.                    "depends on %ui excl:%ui weight:%ui",
  953.                    h2c->state.sid, depend, excl, weight);

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

  958.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  959.     }

  960.     if (depend == h2c->state.sid) {
  961.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  962.                       "client sent HEADERS frame for stream %ui "
  963.                       "with incorrect dependency", h2c->state.sid);

  964.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  965.     }

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

  967.     h2c->state.pool = ngx_create_pool(1024, h2c->connection->log);
  968.     if (h2c->state.pool == NULL) {
  969.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  970.     }

  971.     cscf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  972.                                         ngx_http_core_module);

  973.     h2c->state.header_limit = cscf->large_client_header_buffers.size
  974.                               * cscf->large_client_header_buffers.num;

  975.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  976.                                          ngx_http_v2_module);

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

  980.         status = NGX_HTTP_V2_REFUSED_STREAM;
  981.         goto rst_stream;
  982.     }

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

  986.         status = NGX_HTTP_V2_REFUSED_STREAM;
  987.         goto rst_stream;
  988.     }

  989.     if (!h2c->settings_ack
  990.         && !(h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG)
  991.         && h2scf->preread_size < NGX_HTTP_V2_DEFAULT_WINDOW)
  992.     {
  993.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  994.                       "client sent stream with data "
  995.                       "before settings were acknowledged");

  996.         status = NGX_HTTP_V2_REFUSED_STREAM;
  997.         goto rst_stream;
  998.     }

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

  1000.     if (node == NULL) {
  1001.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1002.     }

  1003.     if (node->parent) {
  1004.         ngx_queue_remove(&node->reuse);
  1005.         h2c->closed_nodes--;
  1006.     }

  1007.     stream = ngx_http_v2_create_stream(h2c);
  1008.     if (stream == NULL) {
  1009.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1010.     }

  1011.     h2c->state.stream = stream;

  1012.     stream->pool = h2c->state.pool;
  1013.     h2c->state.keep_pool = 1;

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

  1015.     stream->in_closed = h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG;
  1016.     stream->node = node;

  1017.     node->stream = stream;

  1018.     if (priority || node->parent == NULL) {
  1019.         node->weight = weight;
  1020.         ngx_http_v2_set_dependency(h2c, node, depend, excl);
  1021.     }

  1022.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  1023.                                         ngx_http_core_module);

  1024.     if (clcf->keepalive_timeout == 0
  1025.         || h2c->connection->requests >= clcf->keepalive_requests
  1026.         || ngx_current_msec - h2c->connection->start_time
  1027.            > clcf->keepalive_time)
  1028.     {
  1029.         h2c->goaway = 1;

  1030.         if (ngx_http_v2_send_goaway(h2c, NGX_HTTP_V2_NO_ERROR) == NGX_ERROR) {
  1031.             return ngx_http_v2_connection_error(h2c,
  1032.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1033.         }
  1034.     }

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

  1036. rst_stream:

  1037.     if (h2c->refused_streams++ > ngx_max(h2scf->concurrent_streams, 100)) {
  1038.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1039.                       "client sent too many refused streams");
  1040.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_NO_ERROR);
  1041.     }

  1042.     if (ngx_http_v2_send_rst_stream(h2c, h2c->state.sid, status) != NGX_OK) {
  1043.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1044.     }

  1045.     return ngx_http_v2_state_header_block(h2c, pos, end);
  1046. }


  1047. static u_char *
  1048. ngx_http_v2_state_header_block(ngx_http_v2_connection_t *h2c, u_char *pos,
  1049.     u_char *end)
  1050. {
  1051.     u_char      ch;
  1052.     ngx_int_t   value;
  1053.     ngx_uint_t  indexed, size_update, prefix;

  1054.     if (end - pos < 1) {
  1055.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1056.                                               ngx_http_v2_state_header_block);
  1057.     }

  1058.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)
  1059.         && h2c->state.length < NGX_HTTP_V2_INT_OCTETS)
  1060.     {
  1061.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1062.                                                ngx_http_v2_state_header_block);
  1063.     }

  1064.     size_update = 0;
  1065.     indexed = 0;

  1066.     ch = *pos;

  1067.     if (ch >= (1 << 7)) {
  1068.         /* indexed header field */
  1069.         indexed = 1;
  1070.         prefix = ngx_http_v2_prefix(7);

  1071.     } else if (ch >= (1 << 6)) {
  1072.         /* literal header field with incremental indexing */
  1073.         h2c->state.index = 1;
  1074.         prefix = ngx_http_v2_prefix(6);

  1075.     } else if (ch >= (1 << 5)) {
  1076.         /* dynamic table size update */
  1077.         size_update = 1;
  1078.         prefix = ngx_http_v2_prefix(5);

  1079.     } else if (ch >= (1 << 4)) {
  1080.         /* literal header field never indexed */
  1081.         prefix = ngx_http_v2_prefix(4);

  1082.     } else {
  1083.         /* literal header field without indexing */
  1084.         prefix = ngx_http_v2_prefix(4);
  1085.     }

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

  1087.     if (value < 0) {
  1088.         if (value == NGX_AGAIN) {
  1089.             return ngx_http_v2_state_headers_save(h2c, pos, end,
  1090.                                                ngx_http_v2_state_header_block);
  1091.         }

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

  1096.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1097.         }

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

  1100.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1101.     }

  1102.     if (indexed) {
  1103.         if (ngx_http_v2_get_indexed_header(h2c, value, 0) != NGX_OK) {
  1104.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1105.         }

  1106.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1107.     }

  1108.     if (size_update) {
  1109.         if (ngx_http_v2_table_size(h2c, value) != NGX_OK) {
  1110.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1111.         }

  1112.         return ngx_http_v2_state_header_complete(h2c, pos, end);
  1113.     }

  1114.     if (value == 0) {
  1115.         h2c->state.parse_name = 1;

  1116.     } else if (ngx_http_v2_get_indexed_header(h2c, value, 1) != NGX_OK) {
  1117.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1118.     }

  1119.     h2c->state.parse_value = 1;

  1120.     return ngx_http_v2_state_field_len(h2c, pos, end);
  1121. }


  1122. static u_char *
  1123. ngx_http_v2_state_field_len(ngx_http_v2_connection_t *h2c, u_char *pos,
  1124.     u_char *end)
  1125. {
  1126.     size_t                     alloc;
  1127.     ngx_int_t                  len;
  1128.     ngx_uint_t                 huff;
  1129.     ngx_http_core_srv_conf_t  *cscf;

  1130.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)
  1131.         && h2c->state.length < NGX_HTTP_V2_INT_OCTETS)
  1132.     {
  1133.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1134.                                                ngx_http_v2_state_field_len);
  1135.     }

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

  1139.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1140.     }

  1141.     if (end - pos < 1) {
  1142.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1143.                                               ngx_http_v2_state_field_len);
  1144.     }

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

  1147.     if (len < 0) {
  1148.         if (len == NGX_AGAIN) {
  1149.             return ngx_http_v2_state_headers_save(h2c, pos, end,
  1150.                                                   ngx_http_v2_state_field_len);
  1151.         }

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

  1155.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1156.         }

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

  1159.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1160.     }

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

  1164.     cscf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  1165.                                         ngx_http_core_module);

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

  1169.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
  1170.     }

  1171.     h2c->state.field_rest = len;

  1172.     if (h2c->state.stream == NULL && !h2c->state.index) {
  1173.         return ngx_http_v2_state_field_skip(h2c, pos, end);
  1174.     }

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

  1176.     h2c->state.field_start = ngx_pnalloc(h2c->state.pool, alloc);
  1177.     if (h2c->state.field_start == NULL) {
  1178.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1179.     }

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

  1181.     if (huff) {
  1182.         return ngx_http_v2_state_field_huff(h2c, pos, end);
  1183.     }

  1184.     return ngx_http_v2_state_field_raw(h2c, pos, end);
  1185. }


  1186. static u_char *
  1187. ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c, u_char *pos,
  1188.     u_char *end)
  1189. {
  1190.     size_t  size;

  1191.     size = end - pos;

  1192.     if (size > h2c->state.field_rest) {
  1193.         size = h2c->state.field_rest;
  1194.     }

  1195.     if (size > h2c->state.length) {
  1196.         size = h2c->state.length;
  1197.     }

  1198.     h2c->state.length -= size;
  1199.     h2c->state.field_rest -= size;

  1200.     if (ngx_http_huff_decode(&h2c->state.field_state, pos, size,
  1201.                              &h2c->state.field_end,
  1202.                              h2c->state.field_rest == 0,
  1203.                              h2c->connection->log)
  1204.         != NGX_OK)
  1205.     {
  1206.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1207.                       "client sent invalid encoded header field");

  1208.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
  1209.     }

  1210.     pos += size;

  1211.     if (h2c->state.field_rest == 0) {
  1212.         *h2c->state.field_end = '\0';
  1213.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1214.     }

  1215.     if (h2c->state.length) {
  1216.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1217.                                               ngx_http_v2_state_field_huff);
  1218.     }

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

  1222.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1223.     }

  1224.     return ngx_http_v2_handle_continuation(h2c, pos, end,
  1225.                                            ngx_http_v2_state_field_huff);
  1226. }


  1227. static u_char *
  1228. ngx_http_v2_state_field_raw(ngx_http_v2_connection_t *h2c, u_char *pos,
  1229.     u_char *end)
  1230. {
  1231.     size_t  size;

  1232.     size = end - pos;

  1233.     if (size > h2c->state.field_rest) {
  1234.         size = h2c->state.field_rest;
  1235.     }

  1236.     if (size > h2c->state.length) {
  1237.         size = h2c->state.length;
  1238.     }

  1239.     h2c->state.length -= size;
  1240.     h2c->state.field_rest -= size;

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

  1242.     pos += size;

  1243.     if (h2c->state.field_rest == 0) {
  1244.         *h2c->state.field_end = '\0';
  1245.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1246.     }

  1247.     if (h2c->state.length) {
  1248.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1249.                                               ngx_http_v2_state_field_raw);
  1250.     }

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

  1254.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1255.     }

  1256.     return ngx_http_v2_handle_continuation(h2c, pos, end,
  1257.                                            ngx_http_v2_state_field_raw);
  1258. }


  1259. static u_char *
  1260. ngx_http_v2_state_field_skip(ngx_http_v2_connection_t *h2c, u_char *pos,
  1261.     u_char *end)
  1262. {
  1263.     size_t  size;

  1264.     size = end - pos;

  1265.     if (size > h2c->state.field_rest) {
  1266.         size = h2c->state.field_rest;
  1267.     }

  1268.     if (size > h2c->state.length) {
  1269.         size = h2c->state.length;
  1270.     }

  1271.     h2c->state.length -= size;
  1272.     h2c->state.field_rest -= size;

  1273.     pos += size;

  1274.     if (h2c->state.field_rest == 0) {
  1275.         return ngx_http_v2_state_process_header(h2c, pos, end);
  1276.     }

  1277.     if (h2c->state.length) {
  1278.         return ngx_http_v2_state_save(h2c, pos, end,
  1279.                                       ngx_http_v2_state_field_skip);
  1280.     }

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

  1284.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1285.     }

  1286.     return ngx_http_v2_handle_continuation(h2c, pos, end,
  1287.                                            ngx_http_v2_state_field_skip);
  1288. }


  1289. static u_char *
  1290. ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c, u_char *pos,
  1291.     u_char *end)
  1292. {
  1293.     size_t                      len;
  1294.     ngx_int_t                   rc;
  1295.     ngx_table_elt_t            *h;
  1296.     ngx_connection_t           *fc;
  1297.     ngx_http_header_t          *hh;
  1298.     ngx_http_request_t         *r;
  1299.     ngx_http_v2_header_t       *header;
  1300.     ngx_http_core_srv_conf_t   *cscf;
  1301.     ngx_http_core_main_conf_t  *cmcf;

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

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

  1304.     if (h2c->state.parse_name) {
  1305.         h2c->state.parse_name = 0;

  1306.         header->name.len = h2c->state.field_end - h2c->state.field_start;
  1307.         header->name.data = h2c->state.field_start;

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

  1311.             return ngx_http_v2_connection_error(h2c,
  1312.                                                 NGX_HTTP_V2_PROTOCOL_ERROR);
  1313.         }

  1314.         return ngx_http_v2_state_field_len(h2c, pos, end);
  1315.     }

  1316.     if (h2c->state.parse_value) {
  1317.         h2c->state.parse_value = 0;

  1318.         header->value.len = h2c->state.field_end - h2c->state.field_start;
  1319.         header->value.data = h2c->state.field_start;
  1320.     }

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

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

  1325.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
  1326.     }

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

  1328.     if (h2c->state.index) {
  1329.         if (ngx_http_v2_add_header(h2c, header) != NGX_OK) {
  1330.             return ngx_http_v2_connection_error(h2c,
  1331.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1332.         }

  1333.         h2c->state.index = 0;
  1334.     }

  1335.     if (h2c->state.stream == NULL) {
  1336.         return ngx_http_v2_state_header_complete(h2c, pos, end);
  1337.     }

  1338.     r = h2c->state.stream->request;
  1339.     fc = r->connection;

  1340.     /* TODO Optimization: validate headers while parsing. */
  1341.     if (ngx_http_v2_validate_header(r, header) != NGX_OK) {
  1342.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1343.         goto error;
  1344.     }

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

  1347.         if (rc == NGX_OK) {
  1348.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1349.                            "http2 header: \":%V: %V\"",
  1350.                            &header->name, &header->value);

  1351.             return ngx_http_v2_state_header_complete(h2c, pos, end);
  1352.         }

  1353.         if (rc == NGX_ABORT) {
  1354.             goto error;
  1355.         }

  1356.         if (rc == NGX_DECLINED) {
  1357.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1358.             goto error;
  1359.         }

  1360.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1361.     }

  1362.     if (ngx_http_v2_construct_request_line(r) != NGX_OK) {
  1363.         goto error;
  1364.     }

  1365.     if (r->invalid_header) {
  1366.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

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

  1370.             return ngx_http_v2_state_header_complete(h2c, pos, end);
  1371.         }
  1372.     }

  1373.     if (header->name.len == cookie.len
  1374.         && ngx_memcmp(header->name.data, cookie.data, cookie.len) == 0)
  1375.     {
  1376.         if (ngx_http_v2_cookie(r, header) != NGX_OK) {
  1377.             return ngx_http_v2_connection_error(h2c,
  1378.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1379.         }

  1380.     } else {
  1381.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

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

  1388.         h = ngx_list_push(&r->headers_in.headers);
  1389.         if (h == NULL) {
  1390.             return ngx_http_v2_connection_error(h2c,
  1391.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1392.         }

  1393.         h->key.len = header->name.len;
  1394.         h->key.data = header->name.data;

  1395.         /*
  1396.          * TODO Optimization: precalculate hash
  1397.          * and handler for indexed headers.
  1398.          */
  1399.         h->hash = ngx_hash_key(h->key.data, h->key.len);

  1400.         h->value.len = header->value.len;
  1401.         h->value.data = header->value.data;

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

  1403.         cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  1406.         if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
  1407.             goto error;
  1408.         }
  1409.     }

  1410.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1411.                    "http2 header: \"%V: %V\"",
  1412.                    &header->name, &header->value);

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

  1414. error:

  1415.     h2c->state.stream = NULL;

  1416.     ngx_http_run_posted_requests(fc);

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


  1419. static u_char *
  1420. ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
  1421.     u_char *end)
  1422. {
  1423.     ngx_http_v2_stream_t  *stream;

  1424.     if (h2c->state.length) {
  1425.         if (end - pos > 0) {
  1426.             h2c->state.handler = ngx_http_v2_state_header_block;
  1427.             return pos;
  1428.         }

  1429.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1430.                                               ngx_http_v2_state_header_block);
  1431.     }

  1432.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)) {
  1433.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1434.                                              ngx_http_v2_state_header_complete);
  1435.     }

  1436.     stream = h2c->state.stream;

  1437.     if (stream) {
  1438.         ngx_http_v2_run_request(stream->request);
  1439.     }

  1440.     if (!h2c->state.keep_pool) {
  1441.         ngx_destroy_pool(h2c->state.pool);
  1442.     }

  1443.     h2c->state.pool = NULL;
  1444.     h2c->state.keep_pool = 0;

  1445.     if (h2c->state.padding) {
  1446.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  1447.     }

  1448.     return ngx_http_v2_state_complete(h2c, pos, end);
  1449. }


  1450. static u_char *
  1451. ngx_http_v2_handle_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
  1452.     u_char *end, ngx_http_v2_handler_pt handler)
  1453. {
  1454.     u_char    *p;
  1455.     size_t     len, skip;
  1456.     uint32_t   head;

  1457.     len = h2c->state.length;

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

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

  1461.         p = pos;
  1462.         pos += skip;
  1463.         ngx_memmove(pos, p, len);
  1464.     }

  1465.     if ((size_t) (end - pos) < len + NGX_HTTP_V2_FRAME_HEADER_SIZE) {
  1466.         return ngx_http_v2_state_headers_save(h2c, pos, end, handler);
  1467.     }

  1468.     p = pos + len;

  1469.     head = ngx_http_v2_parse_uint32(p);

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

  1473.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1474.     }

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

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

  1479.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1480.     }

  1481.     p = pos;
  1482.     pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;

  1483.     ngx_memcpy(pos, p, len);

  1484.     len = ngx_http_v2_parse_length(head);

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

  1486.     if (h2c->state.stream) {
  1487.         h2c->state.stream->request->request_length += len;
  1488.     }

  1489.     h2c->state.handler = handler;
  1490.     return pos;
  1491. }


  1492. static u_char *
  1493. ngx_http_v2_state_priority(ngx_http_v2_connection_t *h2c, u_char *pos,
  1494.     u_char *end)
  1495. {
  1496.     ngx_uint_t           depend, dependency, excl, weight;
  1497.     ngx_http_v2_node_t  *node;

  1498.     if (h2c->state.length != NGX_HTTP_V2_PRIORITY_SIZE) {
  1499.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1500.                       "client sent PRIORITY frame with incorrect length %uz",
  1501.                       h2c->state.length);

  1502.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1503.     }

  1504.     if (--h2c->priority_limit == 0) {
  1505.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1506.                       "client sent too many PRIORITY frames");

  1507.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
  1508.     }

  1509.     if (end - pos < NGX_HTTP_V2_PRIORITY_SIZE) {
  1510.         return ngx_http_v2_state_save(h2c, pos, end,
  1511.                                       ngx_http_v2_state_priority);
  1512.     }

  1513.     dependency = ngx_http_v2_parse_uint32(pos);

  1514.     depend = dependency & 0x7fffffff;
  1515.     excl = dependency >> 31;
  1516.     weight = pos[4] + 1;

  1517.     pos += NGX_HTTP_V2_PRIORITY_SIZE;

  1518.     ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1519.                    "http2 PRIORITY frame sid:%ui "
  1520.                    "depends on %ui excl:%ui weight:%ui",
  1521.                    h2c->state.sid, depend, excl, weight);

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

  1525.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1526.     }

  1527.     if (depend == h2c->state.sid) {
  1528.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1529.                       "client sent PRIORITY frame for stream %ui "
  1530.                       "with incorrect dependency", h2c->state.sid);

  1531.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1532.     }

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

  1534.     if (node == NULL) {
  1535.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1536.     }

  1537.     node->weight = weight;

  1538.     if (node->stream == NULL) {
  1539.         if (node->parent == NULL) {
  1540.             h2c->closed_nodes++;

  1541.         } else {
  1542.             ngx_queue_remove(&node->reuse);
  1543.         }

  1544.         ngx_queue_insert_tail(&h2c->closed, &node->reuse);
  1545.     }

  1546.     ngx_http_v2_set_dependency(h2c, node, depend, excl);

  1547.     return ngx_http_v2_state_complete(h2c, pos, end);
  1548. }


  1549. static u_char *
  1550. ngx_http_v2_state_rst_stream(ngx_http_v2_connection_t *h2c, u_char *pos,
  1551.     u_char *end)
  1552. {
  1553.     ngx_uint_t             status;
  1554.     ngx_event_t           *ev;
  1555.     ngx_connection_t      *fc;
  1556.     ngx_http_v2_node_t    *node;
  1557.     ngx_http_v2_stream_t  *stream;

  1558.     if (h2c->state.length != NGX_HTTP_V2_RST_STREAM_SIZE) {
  1559.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1560.                       "client sent RST_STREAM frame with incorrect length %uz",
  1561.                       h2c->state.length);

  1562.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1563.     }

  1564.     if (end - pos < NGX_HTTP_V2_RST_STREAM_SIZE) {
  1565.         return ngx_http_v2_state_save(h2c, pos, end,
  1566.                                       ngx_http_v2_state_rst_stream);
  1567.     }

  1568.     status = ngx_http_v2_parse_uint32(pos);

  1569.     pos += NGX_HTTP_V2_RST_STREAM_SIZE;

  1570.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1571.                    "http2 RST_STREAM frame, sid:%ui status:%ui",
  1572.                    h2c->state.sid, status);

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

  1576.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1577.     }

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

  1579.     if (node == NULL || node->stream == NULL) {
  1580.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1581.                        "unknown http2 stream");

  1582.         return ngx_http_v2_state_complete(h2c, pos, end);
  1583.     }

  1584.     stream = node->stream;

  1585.     stream->in_closed = 1;
  1586.     stream->out_closed = 1;

  1587.     fc = stream->request->connection;
  1588.     fc->error = 1;

  1589.     switch (status) {

  1590.     case NGX_HTTP_V2_CANCEL:
  1591.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  1592.                       "client canceled stream %ui", h2c->state.sid);
  1593.         break;

  1594.     case NGX_HTTP_V2_INTERNAL_ERROR:
  1595.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  1596.                       "client terminated stream %ui due to internal error",
  1597.                       h2c->state.sid);
  1598.         break;

  1599.     default:
  1600.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  1601.                       "client terminated stream %ui with status %ui",
  1602.                       h2c->state.sid, status);
  1603.         break;
  1604.     }

  1605.     ev = fc->read;
  1606.     ev->handler(ev);

  1607.     return ngx_http_v2_state_complete(h2c, pos, end);
  1608. }


  1609. static u_char *
  1610. ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c, u_char *pos,
  1611.     u_char *end)
  1612. {
  1613.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1614.                    "http2 SETTINGS frame");

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

  1618.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1619.     }

  1620.     if (h2c->state.flags == NGX_HTTP_V2_ACK_FLAG) {

  1621.         if (h2c->state.length != 0) {
  1622.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1623.                           "client sent SETTINGS frame with the ACK flag "
  1624.                           "and nonzero length");

  1625.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1626.         }

  1627.         h2c->settings_ack = 1;

  1628.         return ngx_http_v2_state_complete(h2c, pos, end);
  1629.     }

  1630.     if (h2c->state.length % NGX_HTTP_V2_SETTINGS_PARAM_SIZE) {
  1631.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1632.                       "client sent SETTINGS frame with incorrect length %uz",
  1633.                       h2c->state.length);

  1634.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1635.     }

  1636.     h2c->state.window_delta = 0;

  1637.     return ngx_http_v2_state_settings_params(h2c, pos, end);
  1638. }


  1639. static u_char *
  1640. ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c, u_char *pos,
  1641.     u_char *end)
  1642. {
  1643.     ngx_uint_t                id, value;
  1644.     ngx_http_v2_out_frame_t  *frame;

  1645.     while (h2c->state.length) {
  1646.         if (end - pos < NGX_HTTP_V2_SETTINGS_PARAM_SIZE) {
  1647.             return ngx_http_v2_state_save(h2c, pos, end,
  1648.                                           ngx_http_v2_state_settings_params);
  1649.         }

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

  1651.         id = ngx_http_v2_parse_uint16(pos);
  1652.         value = ngx_http_v2_parse_uint32(&pos[2]);

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

  1655.         switch (id) {

  1656.         case NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING:

  1657.             if (value > NGX_HTTP_V2_MAX_WINDOW) {
  1658.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1659.                               "client sent SETTINGS frame with incorrect "
  1660.                               "INITIAL_WINDOW_SIZE value %ui", value);

  1661.                 return ngx_http_v2_connection_error(h2c,
  1662.                                                   NGX_HTTP_V2_FLOW_CTRL_ERROR);
  1663.             }

  1664.             h2c->state.window_delta = (ssize_t) value
  1665.                                       - (ssize_t) h2c->init_window;
  1666.             break;

  1667.         case NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING:

  1668.             if (value > NGX_HTTP_V2_MAX_FRAME_SIZE
  1669.                 || value < NGX_HTTP_V2_DEFAULT_FRAME_SIZE)
  1670.             {
  1671.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1672.                               "client sent SETTINGS frame with incorrect "
  1673.                               "MAX_FRAME_SIZE value %ui", value);

  1674.                 return ngx_http_v2_connection_error(h2c,
  1675.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1676.             }

  1677.             h2c->frame_size = value;
  1678.             break;

  1679.         case NGX_HTTP_V2_ENABLE_PUSH_SETTING:

  1680.             if (value > 1) {
  1681.                 ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1682.                               "client sent SETTINGS frame with incorrect "
  1683.                               "ENABLE_PUSH value %ui", value);

  1684.                 return ngx_http_v2_connection_error(h2c,
  1685.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1686.             }

  1687.             break;

  1688.         case NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING:

  1689.             h2c->table_update = 1;
  1690.             break;

  1691.         default:
  1692.             break;
  1693.         }

  1694.         pos += NGX_HTTP_V2_SETTINGS_PARAM_SIZE;
  1695.     }

  1696.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_SETTINGS_ACK_SIZE,
  1697.                                   NGX_HTTP_V2_SETTINGS_FRAME,
  1698.                                   NGX_HTTP_V2_ACK_FLAG, 0);
  1699.     if (frame == NULL) {
  1700.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1701.     }

  1702.     ngx_http_v2_queue_ordered_frame(h2c, frame);

  1703.     if (h2c->state.window_delta) {
  1704.         h2c->init_window += h2c->state.window_delta;

  1705.         if (ngx_http_v2_adjust_windows(h2c, h2c->state.window_delta) != NGX_OK)
  1706.         {
  1707.             return ngx_http_v2_connection_error(h2c,
  1708.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1709.         }

  1710.         h2c->state.window_delta = 0;
  1711.     }

  1712.     return ngx_http_v2_state_complete(h2c, pos, end);
  1713. }


  1714. static u_char *
  1715. ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c, u_char *pos,
  1716.     u_char *end)
  1717. {
  1718.     ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1719.                   "client sent PUSH_PROMISE frame");

  1720.     return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1721. }


  1722. static u_char *
  1723. ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  1724. {
  1725.     ngx_buf_t                *buf;
  1726.     ngx_http_v2_out_frame_t  *frame;

  1727.     if (h2c->state.length != NGX_HTTP_V2_PING_SIZE) {
  1728.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1729.                       "client sent PING frame with incorrect length %uz",
  1730.                       h2c->state.length);

  1731.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1732.     }

  1733.     if (end - pos < NGX_HTTP_V2_PING_SIZE) {
  1734.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_ping);
  1735.     }

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

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

  1741.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1742.     }

  1743.     if (h2c->state.flags & NGX_HTTP_V2_ACK_FLAG) {
  1744.         return ngx_http_v2_state_skip(h2c, pos, end);
  1745.     }

  1746.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_PING_SIZE,
  1747.                                   NGX_HTTP_V2_PING_FRAME,
  1748.                                   NGX_HTTP_V2_ACK_FLAG, 0);
  1749.     if (frame == NULL) {
  1750.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1751.     }

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

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

  1754.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  1755.     return ngx_http_v2_state_complete(h2c, pos + NGX_HTTP_V2_PING_SIZE, end);
  1756. }


  1757. static u_char *
  1758. ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c, u_char *pos,
  1759.     u_char *end)
  1760. {
  1761. #if (NGX_DEBUG)
  1762.     ngx_uint_t  last_sid, error;
  1763. #endif

  1764.     if (h2c->state.length < NGX_HTTP_V2_GOAWAY_SIZE) {
  1765.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1766.                       "client sent GOAWAY frame "
  1767.                       "with incorrect length %uz", h2c->state.length);

  1768.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1769.     }

  1770.     if (end - pos < NGX_HTTP_V2_GOAWAY_SIZE) {
  1771.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_goaway);
  1772.     }

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

  1776.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1777.     }

  1778. #if (NGX_DEBUG)
  1779.     h2c->state.length -= NGX_HTTP_V2_GOAWAY_SIZE;

  1780.     last_sid = ngx_http_v2_parse_sid(pos);
  1781.     error = ngx_http_v2_parse_uint32(&pos[4]);

  1782.     pos += NGX_HTTP_V2_GOAWAY_SIZE;

  1783.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1784.                    "http2 GOAWAY frame: last sid %ui, error %ui",
  1785.                    last_sid, error);
  1786. #endif

  1787.     return ngx_http_v2_state_skip(h2c, pos, end);
  1788. }


  1789. static u_char *
  1790. ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos,
  1791.     u_char *end)
  1792. {
  1793.     size_t                 window;
  1794.     ngx_event_t           *wev;
  1795.     ngx_queue_t           *q;
  1796.     ngx_http_v2_node_t    *node;
  1797.     ngx_http_v2_stream_t  *stream;

  1798.     if (h2c->state.length != NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
  1799.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1800.                       "client sent WINDOW_UPDATE frame "
  1801.                       "with incorrect length %uz", h2c->state.length);

  1802.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1803.     }

  1804.     if (end - pos < NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
  1805.         return ngx_http_v2_state_save(h2c, pos, end,
  1806.                                       ngx_http_v2_state_window_update);
  1807.     }

  1808.     window = ngx_http_v2_parse_window(pos);

  1809.     pos += NGX_HTTP_V2_WINDOW_UPDATE_SIZE;

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

  1813.     if (window == 0) {
  1814.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1815.                       "client sent WINDOW_UPDATE frame "
  1816.                       "with incorrect window increment 0");

  1817.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1818.     }

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

  1821.         if (node == NULL || node->stream == NULL) {
  1822.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1823.                            "unknown http2 stream");

  1824.             return ngx_http_v2_state_complete(h2c, pos, end);
  1825.         }

  1826.         stream = node->stream;

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

  1828.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1829.                           "client violated flow control for stream %ui: "
  1830.                           "received WINDOW_UPDATE frame "
  1831.                           "with window increment %uz "
  1832.                           "not allowed for window %z",
  1833.                           h2c->state.sid, window, stream->send_window);

  1834.             if (ngx_http_v2_terminate_stream(h2c, stream,
  1835.                                              NGX_HTTP_V2_FLOW_CTRL_ERROR)
  1836.                 == NGX_ERROR)
  1837.             {
  1838.                 return ngx_http_v2_connection_error(h2c,
  1839.                                                     NGX_HTTP_V2_INTERNAL_ERROR);
  1840.             }

  1841.             return ngx_http_v2_state_complete(h2c, pos, end);
  1842.         }

  1843.         stream->send_window += window;

  1844.         if (stream->exhausted) {
  1845.             stream->exhausted = 0;

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

  1847.             wev->active = 0;
  1848.             wev->ready = 1;

  1849.             if (!wev->delayed) {
  1850.                 wev->handler(wev);
  1851.             }
  1852.         }

  1853.         return ngx_http_v2_state_complete(h2c, pos, end);
  1854.     }

  1855.     if (window > NGX_HTTP_V2_MAX_WINDOW - h2c->send_window) {
  1856.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1857.                       "client violated connection flow control: "
  1858.                       "received WINDOW_UPDATE frame "
  1859.                       "with window increment %uz "
  1860.                       "not allowed for window %uz",
  1861.                       window, h2c->send_window);

  1862.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
  1863.     }

  1864.     h2c->send_window += window;

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

  1867.         ngx_queue_remove(q);

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

  1869.         stream->waiting = 0;

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

  1871.         wev->active = 0;
  1872.         wev->ready = 1;

  1873.         if (!wev->delayed) {
  1874.             wev->handler(wev);

  1875.             if (h2c->send_window == 0) {
  1876.                 break;
  1877.             }
  1878.         }
  1879.     }

  1880.     return ngx_http_v2_state_complete(h2c, pos, end);
  1881. }


  1882. static u_char *
  1883. ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
  1884.     u_char *end)
  1885. {
  1886.     ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1887.                   "client sent unexpected CONTINUATION frame");

  1888.     return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1889. }


  1890. static u_char *
  1891. ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
  1892.     u_char *end)
  1893. {
  1894.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1895.                    "http2 frame complete pos:%p end:%p", pos, end);

  1896.     if (pos > end) {
  1897.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  1898.                       "receive buffer overrun");

  1899.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1900.     }

  1901.     h2c->state.stream = NULL;
  1902.     h2c->state.handler = ngx_http_v2_state_head;

  1903.     return pos;
  1904. }


  1905. static u_char *
  1906. ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c, u_char *pos,
  1907.     u_char *end)
  1908. {
  1909.     h2c->state.length += h2c->state.padding;
  1910.     h2c->state.padding = 0;

  1911.     return ngx_http_v2_state_skip(h2c, pos, end);
  1912. }


  1913. static u_char *
  1914. ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  1915. {
  1916.     size_t  size;

  1917.     size = end - pos;

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

  1921.         h2c->state.length -= size;
  1922.         return ngx_http_v2_state_save(h2c, end, end, ngx_http_v2_state_skip);
  1923.     }

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

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


  1928. static u_char *
  1929. ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end,
  1930.     ngx_http_v2_handler_pt handler)
  1931. {
  1932.     size_t  size;

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

  1936.     size = end - pos;

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

  1940.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1941.     }

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

  1943.     h2c->state.buffer_used = size;
  1944.     h2c->state.handler = handler;
  1945.     h2c->state.incomplete = 1;

  1946.     return end;
  1947. }


  1948. static u_char *
  1949. ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c, u_char *pos,
  1950.     u_char *end, ngx_http_v2_handler_pt handler)
  1951. {
  1952.     ngx_event_t               *rev;
  1953.     ngx_http_request_t        *r;
  1954.     ngx_http_core_srv_conf_t  *cscf;

  1955.     if (h2c->state.stream) {
  1956.         r = h2c->state.stream->request;
  1957.         rev = r->connection->read;

  1958.         if (!rev->timer_set) {
  1959.             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  1960.             ngx_add_timer(rev, cscf->client_header_timeout);
  1961.         }
  1962.     }

  1963.     return ngx_http_v2_state_save(h2c, pos, end, handler);
  1964. }


  1965. static u_char *
  1966. ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
  1967.     ngx_uint_t err)
  1968. {
  1969.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1970.                    "http2 state connection error");

  1971.     ngx_http_v2_finalize_connection(h2c, err);

  1972.     return NULL;
  1973. }


  1974. static ngx_int_t
  1975. ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c, u_char **pos, u_char *end,
  1976.     ngx_uint_t prefix)
  1977. {
  1978.     u_char      *start, *p;
  1979.     ngx_uint_t   value, octet, shift;

  1980.     start = *pos;
  1981.     p = start;

  1982.     value = *p++ & prefix;

  1983.     if (value != prefix) {
  1984.         if (h2c->state.length == 0) {
  1985.             return NGX_ERROR;
  1986.         }

  1987.         h2c->state.length--;

  1988.         *pos = p;
  1989.         return value;
  1990.     }

  1991.     if (end - start > NGX_HTTP_V2_INT_OCTETS) {
  1992.         end = start + NGX_HTTP_V2_INT_OCTETS;
  1993.     }

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

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

  1997.         if (octet < 128) {
  1998.             if ((size_t) (p - start) > h2c->state.length) {
  1999.                 return NGX_ERROR;
  2000.             }

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

  2002.             *pos = p;
  2003.             return value;
  2004.         }
  2005.     }

  2006.     if ((size_t) (end - start) >= h2c->state.length) {
  2007.         return NGX_ERROR;
  2008.     }

  2009.     if (end == start + NGX_HTTP_V2_INT_OCTETS) {
  2010.         return NGX_DECLINED;
  2011.     }

  2012.     return NGX_AGAIN;
  2013. }


  2014. static ngx_int_t
  2015. ngx_http_v2_send_settings(ngx_http_v2_connection_t *h2c)
  2016. {
  2017.     size_t                    len;
  2018.     ngx_buf_t                *buf;
  2019.     ngx_chain_t              *cl;
  2020.     ngx_http_v2_srv_conf_t   *h2scf;
  2021.     ngx_http_v2_out_frame_t  *frame;

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

  2024.     frame = ngx_palloc(h2c->pool, sizeof(ngx_http_v2_out_frame_t));
  2025.     if (frame == NULL) {
  2026.         return NGX_ERROR;
  2027.     }

  2028.     cl = ngx_alloc_chain_link(h2c->pool);
  2029.     if (cl == NULL) {
  2030.         return NGX_ERROR;
  2031.     }

  2032.     len = NGX_HTTP_V2_SETTINGS_PARAM_SIZE * 3;

  2033.     buf = ngx_create_temp_buf(h2c->pool, NGX_HTTP_V2_FRAME_HEADER_SIZE + len);
  2034.     if (buf == NULL) {
  2035.         return NGX_ERROR;
  2036.     }

  2037.     buf->last_buf = 1;

  2038.     cl->buf = buf;
  2039.     cl->next = NULL;

  2040.     frame->first = cl;
  2041.     frame->last = cl;
  2042.     frame->handler = ngx_http_v2_settings_frame_handler;
  2043.     frame->stream = NULL;
  2044. #if (NGX_DEBUG)
  2045.     frame->length = len;
  2046. #endif
  2047.     frame->blocked = 0;

  2048.     buf->last = ngx_http_v2_write_len_and_type(buf->last, len,
  2049.                                                NGX_HTTP_V2_SETTINGS_FRAME);

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

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

  2052.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2053.                                          ngx_http_v2_module);

  2054.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2055.                                          NGX_HTTP_V2_MAX_STREAMS_SETTING);
  2056.     buf->last = ngx_http_v2_write_uint32(buf->last,
  2057.                                          h2scf->concurrent_streams);

  2058.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2059.                                          NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING);
  2060.     buf->last = ngx_http_v2_write_uint32(buf->last, h2scf->preread_size);

  2061.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2062.                                          NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING);
  2063.     buf->last = ngx_http_v2_write_uint32(buf->last,
  2064.                                          NGX_HTTP_V2_MAX_FRAME_SIZE);

  2065.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2066.     return NGX_OK;
  2067. }


  2068. static ngx_int_t
  2069. ngx_http_v2_settings_frame_handler(ngx_http_v2_connection_t *h2c,
  2070.     ngx_http_v2_out_frame_t *frame)
  2071. {
  2072.     ngx_buf_t  *buf;

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

  2074.     if (buf->pos != buf->last) {
  2075.         return NGX_AGAIN;
  2076.     }

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

  2078.     return NGX_OK;
  2079. }


  2080. static ngx_int_t
  2081. ngx_http_v2_send_window_update(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2082.     size_t window)
  2083. {
  2084.     ngx_buf_t                *buf;
  2085.     ngx_http_v2_out_frame_t  *frame;

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

  2089.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_WINDOW_UPDATE_SIZE,
  2090.                                   NGX_HTTP_V2_WINDOW_UPDATE_FRAME,
  2091.                                   NGX_HTTP_V2_NO_FLAG, sid);
  2092.     if (frame == NULL) {
  2093.         return NGX_ERROR;
  2094.     }

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

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

  2097.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2098.     return NGX_OK;
  2099. }


  2100. static ngx_int_t
  2101. ngx_http_v2_send_rst_stream(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2102.     ngx_uint_t status)
  2103. {
  2104.     ngx_buf_t                *buf;
  2105.     ngx_http_v2_out_frame_t  *frame;

  2106.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2107.                    "http2 send RST_STREAM frame sid:%ui, status:%ui",
  2108.                    sid, status);

  2109.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_RST_STREAM_SIZE,
  2110.                                   NGX_HTTP_V2_RST_STREAM_FRAME,
  2111.                                   NGX_HTTP_V2_NO_FLAG, sid);
  2112.     if (frame == NULL) {
  2113.         return NGX_ERROR;
  2114.     }

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

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

  2117.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2118.     return NGX_OK;
  2119. }


  2120. static ngx_int_t
  2121. ngx_http_v2_send_goaway(ngx_http_v2_connection_t *h2c, ngx_uint_t status)
  2122. {
  2123.     ngx_buf_t                *buf;
  2124.     ngx_http_v2_out_frame_t  *frame;

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

  2128.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_GOAWAY_SIZE,
  2129.                                   NGX_HTTP_V2_GOAWAY_FRAME,
  2130.                                   NGX_HTTP_V2_NO_FLAG, 0);
  2131.     if (frame == NULL) {
  2132.         return NGX_ERROR;
  2133.     }

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

  2135.     buf->last = ngx_http_v2_write_sid(buf->last, h2c->last_sid);
  2136.     buf->last = ngx_http_v2_write_uint32(buf->last, status);

  2137.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2138.     return NGX_OK;
  2139. }


  2140. static ngx_http_v2_out_frame_t *
  2141. ngx_http_v2_get_frame(ngx_http_v2_connection_t *h2c, size_t length,
  2142.     ngx_uint_t type, u_char flags, ngx_uint_t sid)
  2143. {
  2144.     ngx_buf_t                *buf;
  2145.     ngx_pool_t               *pool;
  2146.     ngx_http_v2_out_frame_t  *frame;

  2147.     frame = h2c->free_frames;

  2148.     if (frame) {
  2149.         h2c->free_frames = frame->next;

  2150.         buf = frame->first->buf;
  2151.         buf->pos = buf->start;

  2152.         frame->blocked = 0;

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

  2155.         frame = ngx_pcalloc(pool, sizeof(ngx_http_v2_out_frame_t));
  2156.         if (frame == NULL) {
  2157.             return NULL;
  2158.         }

  2159.         frame->first = ngx_alloc_chain_link(pool);
  2160.         if (frame->first == NULL) {
  2161.             return NULL;
  2162.         }

  2163.         buf = ngx_create_temp_buf(pool, NGX_HTTP_V2_FRAME_BUFFER_SIZE);
  2164.         if (buf == NULL) {
  2165.             return NULL;
  2166.         }

  2167.         buf->last_buf = 1;

  2168.         frame->first->buf = buf;
  2169.         frame->last = frame->first;

  2170.         frame->handler = ngx_http_v2_frame_handler;

  2171.         h2c->frames++;

  2172.     } else {
  2173.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  2174.                       "http2 flood detected");

  2175.         h2c->connection->error = 1;
  2176.         return NULL;
  2177.     }

  2178. #if (NGX_DEBUG)
  2179.     if (length > NGX_HTTP_V2_FRAME_BUFFER_SIZE - NGX_HTTP_V2_FRAME_HEADER_SIZE)
  2180.     {
  2181.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  2182.                       "requested control frame is too large: %uz", length);
  2183.         return NULL;
  2184.     }
  2185. #endif

  2186.     frame->length = length;

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

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

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

  2190.     return frame;
  2191. }


  2192. static ngx_int_t
  2193. ngx_http_v2_frame_handler(ngx_http_v2_connection_t *h2c,
  2194.     ngx_http_v2_out_frame_t *frame)
  2195. {
  2196.     ngx_buf_t  *buf;

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

  2198.     if (buf->pos != buf->last) {
  2199.         return NGX_AGAIN;
  2200.     }

  2201.     frame->next = h2c->free_frames;
  2202.     h2c->free_frames = frame;

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

  2204.     return NGX_OK;
  2205. }


  2206. static ngx_http_v2_stream_t *
  2207. ngx_http_v2_create_stream(ngx_http_v2_connection_t *h2c)
  2208. {
  2209.     ngx_log_t                 *log;
  2210.     ngx_event_t               *rev, *wev;
  2211.     ngx_connection_t          *fc;
  2212.     ngx_http_log_ctx_t        *ctx;
  2213.     ngx_http_request_t        *r;
  2214.     ngx_http_v2_stream_t      *stream;
  2215.     ngx_http_v2_srv_conf_t    *h2scf;
  2216.     ngx_http_core_srv_conf_t  *cscf;

  2217.     fc = h2c->free_fake_connections;

  2218.     if (fc) {
  2219.         h2c->free_fake_connections = fc->data;

  2220.         rev = fc->read;
  2221.         wev = fc->write;
  2222.         log = fc->log;
  2223.         ctx = log->data;

  2224.     } else {
  2225.         fc = ngx_palloc(h2c->pool, sizeof(ngx_connection_t));
  2226.         if (fc == NULL) {
  2227.             return NULL;
  2228.         }

  2229.         rev = ngx_palloc(h2c->pool, sizeof(ngx_event_t));
  2230.         if (rev == NULL) {
  2231.             return NULL;
  2232.         }

  2233.         wev = ngx_palloc(h2c->pool, sizeof(ngx_event_t));
  2234.         if (wev == NULL) {
  2235.             return NULL;
  2236.         }

  2237.         log = ngx_palloc(h2c->pool, sizeof(ngx_log_t));
  2238.         if (log == NULL) {
  2239.             return NULL;
  2240.         }

  2241.         ctx = ngx_palloc(h2c->pool, sizeof(ngx_http_log_ctx_t));
  2242.         if (ctx == NULL) {
  2243.             return NULL;
  2244.         }

  2245.         ctx->connection = fc;
  2246.         ctx->request = NULL;
  2247.         ctx->current_request = NULL;
  2248.     }

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

  2250.     log->data = ctx;
  2251.     log->action = "reading client request headers";

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

  2253.     rev->data = fc;
  2254.     rev->ready = 1;
  2255.     rev->handler = ngx_http_v2_close_stream_handler;
  2256.     rev->log = log;

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

  2258.     wev->write = 1;

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

  2260.     fc->data = h2c->http_connection;
  2261.     fc->read = rev;
  2262.     fc->write = wev;
  2263.     fc->sent = 0;
  2264.     fc->log = log;
  2265.     fc->buffered = 0;
  2266.     fc->sndlowat = 1;
  2267.     fc->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;

  2268.     r = ngx_http_create_request(fc);
  2269.     if (r == NULL) {
  2270.         return NULL;
  2271.     }

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

  2273.     r->http_version = NGX_HTTP_VERSION_20;
  2274.     r->valid_location = 1;

  2275.     fc->data = r;
  2276.     h2c->connection->requests++;

  2277.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  2278.     r->header_in = ngx_create_temp_buf(r->pool,
  2279.                                        cscf->client_header_buffer_size);
  2280.     if (r->header_in == NULL) {
  2281.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2282.         return NULL;
  2283.     }

  2284.     if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
  2285.                       sizeof(ngx_table_elt_t))
  2286.         != NGX_OK)
  2287.     {
  2288.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2289.         return NULL;
  2290.     }

  2291.     r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;

  2292.     stream = ngx_pcalloc(r->pool, sizeof(ngx_http_v2_stream_t));
  2293.     if (stream == NULL) {
  2294.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2295.         return NULL;
  2296.     }

  2297.     r->stream = stream;

  2298.     stream->request = r;
  2299.     stream->connection = h2c;

  2300.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

  2301.     stream->send_window = h2c->init_window;
  2302.     stream->recv_window = h2scf->preread_size;

  2303.     h2c->processing++;

  2304.     h2c->priority_limit += h2scf->concurrent_streams;

  2305.     if (h2c->connection->read->timer_set) {
  2306.         ngx_del_timer(h2c->connection->read);
  2307.     }

  2308.     return stream;
  2309. }


  2310. static ngx_http_v2_node_t *
  2311. ngx_http_v2_get_node_by_id(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2312.     ngx_uint_t alloc)
  2313. {
  2314.     ngx_uint_t               index;
  2315.     ngx_http_v2_node_t      *node;
  2316.     ngx_http_v2_srv_conf_t  *h2scf;

  2317.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2318.                                          ngx_http_v2_module);

  2319.     index = ngx_http_v2_index(h2scf, sid);

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

  2321.         if (node->id == sid) {
  2322.             return node;
  2323.         }
  2324.     }

  2325.     if (!alloc) {
  2326.         return NULL;
  2327.     }

  2328.     if (h2c->closed_nodes < 32) {
  2329.         node = ngx_pcalloc(h2c->connection->pool, sizeof(ngx_http_v2_node_t));
  2330.         if (node == NULL) {
  2331.             return NULL;
  2332.         }

  2333.     } else {
  2334.         node = ngx_http_v2_get_closed_node(h2c);
  2335.     }

  2336.     node->id = sid;

  2337.     ngx_queue_init(&node->children);

  2338.     node->index = h2c->streams_index[index];
  2339.     h2c->streams_index[index] = node;

  2340.     return node;
  2341. }


  2342. static ngx_http_v2_node_t *
  2343. ngx_http_v2_get_closed_node(ngx_http_v2_connection_t *h2c)
  2344. {
  2345.     ngx_uint_t               weight;
  2346.     ngx_queue_t             *q, *children;
  2347.     ngx_http_v2_node_t      *node, **next, *n, *parent, *child;
  2348.     ngx_http_v2_srv_conf_t  *h2scf;

  2349.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2350.                                          ngx_http_v2_module);

  2351.     h2c->closed_nodes--;

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

  2353.     ngx_queue_remove(q);

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

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

  2356.     for ( ;; ) {
  2357.         n = *next;

  2358.         if (n == node) {
  2359.             *next = n->index;
  2360.             break;
  2361.         }

  2362.         next = &n->index;
  2363.     }

  2364.     ngx_queue_remove(&node->queue);

  2365.     weight = 0;

  2366.     for (q = ngx_queue_head(&node->children);
  2367.          q != ngx_queue_sentinel(&node->children);
  2368.          q = ngx_queue_next(q))
  2369.     {
  2370.         child = ngx_queue_data(q, ngx_http_v2_node_t, queue);
  2371.         weight += child->weight;
  2372.     }

  2373.     parent = node->parent;

  2374.     for (q = ngx_queue_head(&node->children);
  2375.          q != ngx_queue_sentinel(&node->children);
  2376.          q = ngx_queue_next(q))
  2377.     {
  2378.         child = ngx_queue_data(q, ngx_http_v2_node_t, queue);
  2379.         child->parent = parent;
  2380.         child->weight = node->weight * child->weight / weight;

  2381.         if (child->weight == 0) {
  2382.             child->weight = 1;
  2383.         }
  2384.     }

  2385.     if (parent == NGX_HTTP_V2_ROOT) {
  2386.         node->rank = 0;
  2387.         node->rel_weight = 1.0;

  2388.         children = &h2c->dependencies;

  2389.     } else {
  2390.         node->rank = parent->rank;
  2391.         node->rel_weight = parent->rel_weight;

  2392.         children = &parent->children;
  2393.     }

  2394.     ngx_http_v2_node_children_update(node);
  2395.     ngx_queue_add(children, &node->children);

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

  2397.     return node;
  2398. }


  2399. static ngx_int_t
  2400. ngx_http_v2_validate_header(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2401. {
  2402.     u_char                     ch;
  2403.     ngx_uint_t                 i;
  2404.     ngx_http_core_srv_conf_t  *cscf;

  2405.     r->invalid_header = 0;

  2406.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

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

  2409.         if ((ch >= 'a' && ch <= 'z')
  2410.             || (ch == '-')
  2411.             || (ch >= '0' && ch <= '9')
  2412.             || (ch == '_' && cscf->underscores_in_headers))
  2413.         {
  2414.             continue;
  2415.         }

  2416.         if (ch <= 0x20 || ch == 0x7f || ch == ':'
  2417.             || (ch >= 'A' && ch <= 'Z'))
  2418.         {
  2419.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2420.                           "client sent invalid header name: \"%V\"",
  2421.                           &header->name);

  2422.             return NGX_ERROR;
  2423.         }

  2424.         r->invalid_header = 1;
  2425.     }

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

  2428.         if (ch == '\0' || ch == LF || ch == CR) {
  2429.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2430.                           "client sent header \"%V\" with "
  2431.                           "invalid value: \"%V\"",
  2432.                           &header->name, &header->value);

  2433.             return NGX_ERROR;
  2434.         }
  2435.     }

  2436.     return NGX_OK;
  2437. }


  2438. static ngx_int_t
  2439. ngx_http_v2_pseudo_header(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2440. {
  2441.     header->name.len--;
  2442.     header->name.data++;

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

  2446.         return NGX_DECLINED;
  2447.     }

  2448.     switch (header->name.len) {
  2449.     case 4:
  2450.         if (ngx_memcmp(header->name.data, "path", sizeof("path") - 1)
  2451.             == 0)
  2452.         {
  2453.             return ngx_http_v2_parse_path(r, &header->value);
  2454.         }

  2455.         break;

  2456.     case 6:
  2457.         if (ngx_memcmp(header->name.data, "method", sizeof("method") - 1)
  2458.             == 0)
  2459.         {
  2460.             return ngx_http_v2_parse_method(r, &header->value);
  2461.         }

  2462.         if (ngx_memcmp(header->name.data, "scheme", sizeof("scheme") - 1)
  2463.             == 0)
  2464.         {
  2465.             return ngx_http_v2_parse_scheme(r, &header->value);
  2466.         }

  2467.         break;

  2468.     case 9:
  2469.         if (ngx_memcmp(header->name.data, "authority", sizeof("authority") - 1)
  2470.             == 0)
  2471.         {
  2472.             return ngx_http_v2_parse_authority(r, &header->value);
  2473.         }

  2474.         break;
  2475.     }

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

  2479.     return NGX_DECLINED;
  2480. }


  2481. static ngx_int_t
  2482. ngx_http_v2_parse_path(ngx_http_request_t *r, ngx_str_t *value)
  2483. {
  2484.     if (r->unparsed_uri.len) {
  2485.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2486.                       "client sent duplicate :path header");

  2487.         return NGX_DECLINED;
  2488.     }

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

  2492.         return NGX_DECLINED;
  2493.     }

  2494.     r->uri_start = value->data;
  2495.     r->uri_end = value->data + value->len;

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

  2499.         return NGX_DECLINED;
  2500.     }

  2501.     if (ngx_http_process_request_uri(r) != NGX_OK) {
  2502.         /*
  2503.          * request has been finalized already
  2504.          * in ngx_http_process_request_uri()
  2505.          */
  2506.         return NGX_ABORT;
  2507.     }

  2508.     return NGX_OK;
  2509. }


  2510. static ngx_int_t
  2511. ngx_http_v2_parse_method(ngx_http_request_t *r, ngx_str_t *value)
  2512. {
  2513.     size_t         k, len;
  2514.     ngx_uint_t     n;
  2515.     const u_char  *p, *m;

  2516.     /*
  2517.      * This array takes less than 256 sequential bytes,
  2518.      * and if typical CPU cache line size is 64 bytes,
  2519.      * it is prefetched for 4 load operations.
  2520.      */
  2521.     static const struct {
  2522.         u_char            len;
  2523.         const u_char      method[11];
  2524.         uint32_t          value;
  2525.     } tests[] = {
  2526.         { 3, "GET",       NGX_HTTP_GET },
  2527.         { 4, "POST",      NGX_HTTP_POST },
  2528.         { 4, "HEAD",      NGX_HTTP_HEAD },
  2529.         { 7, "OPTIONS",   NGX_HTTP_OPTIONS },
  2530.         { 8, "PROPFIND"NGX_HTTP_PROPFIND },
  2531.         { 3, "PUT",       NGX_HTTP_PUT },
  2532.         { 5, "MKCOL",     NGX_HTTP_MKCOL },
  2533.         { 6, "DELETE",    NGX_HTTP_DELETE },
  2534.         { 4, "COPY",      NGX_HTTP_COPY },
  2535.         { 4, "MOVE",      NGX_HTTP_MOVE },
  2536.         { 9, "PROPPATCH", NGX_HTTP_PROPPATCH },
  2537.         { 4, "LOCK",      NGX_HTTP_LOCK },
  2538.         { 6, "UNLOCK",    NGX_HTTP_UNLOCK },
  2539.         { 5, "PATCH",     NGX_HTTP_PATCH },
  2540.         { 5, "TRACE",     NGX_HTTP_TRACE },
  2541.         { 7, "CONNECT",   NGX_HTTP_CONNECT }
  2542.     }, *test;

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

  2546.         return NGX_DECLINED;
  2547.     }

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

  2551.         return NGX_DECLINED;
  2552.     }

  2553.     r->method_name.len = value->len;
  2554.     r->method_name.data = value->data;

  2555.     len = r->method_name.len;
  2556.     n = sizeof(tests) / sizeof(tests[0]);
  2557.     test = tests;

  2558.     do {
  2559.         if (len == test->len) {
  2560.             p = r->method_name.data;
  2561.             m = test->method;
  2562.             k = len;

  2563.             do {
  2564.                 if (*p++ != *m++) {
  2565.                     goto next;
  2566.                 }
  2567.             } while (--k);

  2568.             r->method = test->value;
  2569.             return NGX_OK;
  2570.         }

  2571.     next:
  2572.         test++;

  2573.     } while (--n);

  2574.     p = r->method_name.data;

  2575.     do {
  2576.         if ((*p < 'A' || *p > 'Z') && *p != '_' && *p != '-') {
  2577.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2578.                           "client sent invalid method: \"%V\"",
  2579.                           &r->method_name);

  2580.             return NGX_DECLINED;
  2581.         }

  2582.         p++;

  2583.     } while (--len);

  2584.     return NGX_OK;
  2585. }


  2586. static ngx_int_t
  2587. ngx_http_v2_parse_scheme(ngx_http_request_t *r, ngx_str_t *value)
  2588. {
  2589.     u_char      c, ch;
  2590.     ngx_uint_t  i;

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

  2594.         return NGX_DECLINED;
  2595.     }

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

  2599.         return NGX_DECLINED;
  2600.     }

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

  2603.         c = (u_char) (ch | 0x20);
  2604.         if (c >= 'a' && c <= 'z') {
  2605.             continue;
  2606.         }

  2607.         if (((ch >= '0' && ch <= '9') || ch == '+' || ch == '-' || ch == '.')
  2608.             && i > 0)
  2609.         {
  2610.             continue;
  2611.         }

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

  2614.         return NGX_DECLINED;
  2615.     }

  2616.     r->schema = *value;

  2617.     return NGX_OK;
  2618. }


  2619. static ngx_int_t
  2620. ngx_http_v2_parse_authority(ngx_http_request_t *r, ngx_str_t *value)
  2621. {
  2622.     ngx_int_t  rc;
  2623.     in_port_t  port;

  2624.     if (r->host_start) {
  2625.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2626.                       "client sent duplicate \":authority\" header");
  2627.         return NGX_DECLINED;
  2628.     }

  2629.     r->host_start = value->data;
  2630.     r->host_end = value->data + value->len;

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

  2632.     if (rc == NGX_DECLINED) {
  2633.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2634.                       "client sent invalid \":authority\" header");
  2635.         return NGX_DECLINED;
  2636.     }

  2637.     if (rc == NGX_ERROR) {
  2638.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2639.         return NGX_ABORT;
  2640.     }

  2641.     if (ngx_http_set_virtual_server(r, value) == NGX_ERROR) {
  2642.         /*
  2643.          * request has been finalized already
  2644.          * in ngx_http_set_virtual_server()
  2645.          */
  2646.         return NGX_ABORT;
  2647.     }

  2648.     r->headers_in.server = *value;
  2649.     r->port = port;

  2650.     return NGX_OK;
  2651. }


  2652. static ngx_int_t
  2653. ngx_http_v2_construct_request_line(ngx_http_request_t *r)
  2654. {
  2655.     u_char  *p;

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

  2657.     if (r->request_line.len) {
  2658.         return NGX_OK;
  2659.     }

  2660.     if (r->method_name.len == 0
  2661.         || r->schema.len == 0
  2662.         || r->unparsed_uri.len == 0)
  2663.     {
  2664.         if (r->method_name.len == 0) {
  2665.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2666.                           "client sent no :method header");

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

  2670.         } else {
  2671.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2672.                           "client sent no :path header");
  2673.         }

  2674.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2675.         return NGX_ERROR;
  2676.     }

  2677.     r->request_line.len = r->method_name.len + 1
  2678.                           + r->unparsed_uri.len
  2679.                           + sizeof(ending) - 1;

  2680.     p = ngx_pnalloc(r->pool, r->request_line.len + 1);
  2681.     if (p == NULL) {
  2682.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2683.         return NGX_ERROR;
  2684.     }

  2685.     r->request_line.data = p;

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

  2687.     *p++ = ' ';

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

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

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

  2692.     return NGX_OK;
  2693. }


  2694. static ngx_int_t
  2695. ngx_http_v2_cookie(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2696. {
  2697.     ngx_str_t    *val;
  2698.     ngx_array_t  *cookies;

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

  2700.     if (cookies == NULL) {
  2701.         cookies = ngx_array_create(r->pool, 2, sizeof(ngx_str_t));
  2702.         if (cookies == NULL) {
  2703.             return NGX_ERROR;
  2704.         }

  2705.         r->stream->cookies = cookies;
  2706.     }

  2707.     val = ngx_array_push(cookies);
  2708.     if (val == NULL) {
  2709.         return NGX_ERROR;
  2710.     }

  2711.     val->len = header->value.len;
  2712.     val->data = header->value.data;

  2713.     return NGX_OK;
  2714. }


  2715. static ngx_int_t
  2716. ngx_http_v2_construct_cookie_header(ngx_http_request_t *r)
  2717. {
  2718.     u_char                     *buf, *p, *end;
  2719.     size_t                      len;
  2720.     ngx_str_t                  *vals;
  2721.     ngx_uint_t                  i;
  2722.     ngx_array_t                *cookies;
  2723.     ngx_table_elt_t            *h;
  2724.     ngx_http_header_t          *hh;
  2725.     ngx_http_core_main_conf_t  *cmcf;

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

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

  2728.     if (cookies == NULL) {
  2729.         return NGX_OK;
  2730.     }

  2731.     vals = cookies->elts;

  2732.     i = 0;
  2733.     len = 0;

  2734.     do {
  2735.         len += vals[i].len + 2;
  2736.     } while (++i != cookies->nelts);

  2737.     len -= 2;

  2738.     buf = ngx_pnalloc(r->pool, len + 1);
  2739.     if (buf == NULL) {
  2740.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2741.         return NGX_ERROR;
  2742.     }

  2743.     p = buf;
  2744.     end = buf + len;

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

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

  2747.         if (p == end) {
  2748.             *p = '\0';
  2749.             break;
  2750.         }

  2751.         *p++ = ';'; *p++ = ' ';
  2752.     }

  2753.     h = ngx_list_push(&r->headers_in.headers);
  2754.     if (h == NULL) {
  2755.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2756.         return NGX_ERROR;
  2757.     }

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

  2760.     h->key.len = cookie.len;
  2761.     h->key.data = cookie.data;

  2762.     h->value.len = len;
  2763.     h->value.data = buf;

  2764.     h->lowcase_key = cookie.data;

  2765.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  2768.     if (hh == NULL) {
  2769.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2770.         return NGX_ERROR;
  2771.     }

  2772.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  2773.         /*
  2774.          * request has been finalized already
  2775.          * in ngx_http_process_header_line()
  2776.          */
  2777.         return NGX_ERROR;
  2778.     }

  2779.     return NGX_OK;
  2780. }


  2781. static ngx_int_t
  2782. ngx_http_v2_construct_host_header(ngx_http_request_t *r)
  2783. {
  2784.     ngx_table_elt_t            *h;
  2785.     ngx_http_header_t          *hh;
  2786.     ngx_http_core_main_conf_t  *cmcf;

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

  2788.     h = ngx_list_push(&r->headers_in.headers);
  2789.     if (h == NULL) {
  2790.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2791.         return NGX_ERROR;
  2792.     }

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

  2794.     h->key.len = host.len;
  2795.     h->key.data = host.data;

  2796.     h->value.len = r->host_end - r->host_start;
  2797.     h->value.data = r->host_start;

  2798.     h->lowcase_key = host.data;

  2799.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  2802.     if (hh == NULL) {
  2803.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2804.         return NGX_ERROR;
  2805.     }

  2806.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  2807.         /*
  2808.          * request has been finalized already
  2809.          * in ngx_http_process_host()
  2810.          */
  2811.         return NGX_ERROR;
  2812.     }

  2813.     return NGX_OK;
  2814. }


  2815. static void
  2816. ngx_http_v2_run_request(ngx_http_request_t *r)
  2817. {
  2818.     ngx_str_t                  host;
  2819.     ngx_connection_t          *fc;
  2820.     ngx_http_v2_srv_conf_t    *h2scf;
  2821.     ngx_http_v2_connection_t  *h2c;

  2822.     fc = r->connection;

  2823.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

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

  2828.         ngx_http_finalize_request(r, NGX_HTTP_MISDIRECTED_REQUEST);
  2829.         goto failed;
  2830.     }

  2831.     if (ngx_http_v2_construct_request_line(r) != NGX_OK) {
  2832.         goto failed;
  2833.     }

  2834.     if (ngx_http_v2_construct_cookie_header(r) != NGX_OK) {
  2835.         goto failed;
  2836.     }

  2837.     r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;

  2838.     if (r->headers_in.connection) {
  2839.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2840.                       "client sent \"Connection\" header");
  2841.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2842.         goto failed;
  2843.     }

  2844.     if (r->headers_in.keep_alive) {
  2845.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2846.                       "client sent \"Keep-Alive\" header");
  2847.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2848.         goto failed;
  2849.     }

  2850.     if (r->headers_in.transfer_encoding) {
  2851.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2852.                       "client sent \"Transfer-Encoding\" header");
  2853.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2854.         goto failed;
  2855.     }

  2856.     if (r->headers_in.upgrade) {
  2857.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2858.                       "client sent \"Upgrade\" header");
  2859.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2860.         goto failed;
  2861.     }

  2862.     if (r->headers_in.te
  2863.         && (r->headers_in.te->next
  2864.             || r->headers_in.te->value.len != 8
  2865.             || ngx_strncasecmp(r->headers_in.te->value.data,
  2866.                                (u_char *) "trailers", 8) != 0))
  2867.     {
  2868.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2869.                       "client sent invalid \"TE\" header");
  2870.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2871.         goto failed;
  2872.     }

  2873.     if (r->headers_in.server.len == 0) {
  2874.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2875.                       "client sent neither \":authority\" nor \"Host\" header");
  2876.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2877.         goto failed;
  2878.     }

  2879.     if (r->host_end) {

  2880.         host.len = r->host_end - r->host_start;
  2881.         host.data = r->host_start;

  2882.         if (r->headers_in.host) {
  2883.             if (r->headers_in.host->value.len != host.len
  2884.                 || ngx_memcmp(r->headers_in.host->value.data, host.data,
  2885.                               host.len)
  2886.                    != 0)
  2887.             {
  2888.                 ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2889.                               "client sent \":authority\" and \"Host\" headers "
  2890.                               "with different values");
  2891.                 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2892.                 goto failed;
  2893.             }

  2894.         } else {
  2895.             /* compatibility for $http_host */

  2896.             if (ngx_http_v2_construct_host_header(r) != NGX_OK) {
  2897.                 goto failed;
  2898.             }
  2899.         }
  2900.     }

  2901.     if (r->headers_in.content_length) {
  2902.         r->headers_in.content_length_n =
  2903.                             ngx_atoof(r->headers_in.content_length->value.data,
  2904.                                       r->headers_in.content_length->value.len);

  2905.         if (r->headers_in.content_length_n == NGX_ERROR) {
  2906.             ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2907.                           "client sent invalid \"Content-Length\" header");
  2908.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2909.             goto failed;
  2910.         }

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

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

  2915.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2916.             goto failed;
  2917.         }

  2918.     } else if (!r->stream->in_closed) {
  2919.         r->headers_in.chunked = 1;
  2920.     }

  2921.     if (r->method == NGX_HTTP_CONNECT) {
  2922.         ngx_log_error(NGX_LOG_INFO, fc->log, 0, "client sent CONNECT method");
  2923.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  2924.         goto failed;
  2925.     }

  2926.     if (r->method == NGX_HTTP_TRACE) {
  2927.         ngx_log_error(NGX_LOG_INFO, fc->log, 0, "client sent TRACE method");
  2928.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  2929.         goto failed;
  2930.     }

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

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

  2933.     ngx_http_process_request(r);

  2934. failed:

  2935.     ngx_http_run_posted_requests(fc);
  2936. }


  2937. ngx_int_t
  2938. ngx_http_v2_read_request_body(ngx_http_request_t *r)
  2939. {
  2940.     off_t                      len;
  2941.     size_t                     size;
  2942.     ngx_buf_t                 *buf;
  2943.     ngx_int_t                  rc;
  2944.     ngx_http_v2_stream_t      *stream;
  2945.     ngx_http_v2_srv_conf_t    *h2scf;
  2946.     ngx_http_request_body_t   *rb;
  2947.     ngx_http_core_loc_conf_t  *clcf;
  2948.     ngx_http_v2_connection_t  *h2c;

  2949.     stream = r->stream;
  2950.     rb = r->request_body;

  2951.     if (stream->skip_data) {
  2952.         r->request_body_no_buffering = 0;
  2953.         rb->post_handler(r);
  2954.         return NGX_OK;
  2955.     }

  2956.     rb->rest = 1;

  2957.     /* set rb->filter_need_buffering */

  2958.     rc = ngx_http_top_request_body_filter(r, NULL);

  2959.     if (rc != NGX_OK) {
  2960.         stream->skip_data = 1;
  2961.         return rc;
  2962.     }

  2963.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);
  2964.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  2965.     len = r->headers_in.content_length_n;

  2966.     if (len < 0 || len > (off_t) clcf->client_body_buffer_size) {
  2967.         len = clcf->client_body_buffer_size;

  2968.     } else {
  2969.         len++;
  2970.     }

  2971.     if (r->request_body_no_buffering || rb->filter_need_buffering) {

  2972.         /*
  2973.          * We need a room to store data up to the stream's initial window size,
  2974.          * at least until this window will be exhausted.
  2975.          */

  2976.         if (len < (off_t) h2scf->preread_size) {
  2977.             len = h2scf->preread_size;
  2978.         }

  2979.         if (len > NGX_HTTP_V2_MAX_WINDOW) {
  2980.             len = NGX_HTTP_V2_MAX_WINDOW;
  2981.         }
  2982.     }

  2983.     rb->buf = ngx_create_temp_buf(r->pool, (size_t) len);

  2984.     if (rb->buf == NULL) {
  2985.         stream->skip_data = 1;
  2986.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  2987.     }

  2988.     buf = stream->preread;

  2989.     if (stream->in_closed) {
  2990.         if (!rb->filter_need_buffering) {
  2991.             r->request_body_no_buffering = 0;
  2992.         }

  2993.         if (buf) {
  2994.             rc = ngx_http_v2_process_request_body(r, buf->pos,
  2995.                                                   buf->last - buf->pos, 1, 0);
  2996.             ngx_pfree(r->pool, buf->start);

  2997.         } else {
  2998.             rc = ngx_http_v2_process_request_body(r, NULL, 0, 1, 0);
  2999.         }

  3000.         if (rc != NGX_AGAIN) {
  3001.             return rc;
  3002.         }

  3003.         r->read_event_handler = ngx_http_v2_read_client_request_body_handler;
  3004.         r->write_event_handler = ngx_http_request_empty_handler;

  3005.         return NGX_AGAIN;
  3006.     }

  3007.     if (buf) {
  3008.         rc = ngx_http_v2_process_request_body(r, buf->pos,
  3009.                                               buf->last - buf->pos, 0, 0);

  3010.         ngx_pfree(r->pool, buf->start);

  3011.         if (rc != NGX_OK && rc != NGX_AGAIN) {
  3012.             stream->skip_data = 1;
  3013.             return rc;
  3014.         }
  3015.     }

  3016.     if (r->request_body_no_buffering || rb->filter_need_buffering) {
  3017.         size = (size_t) len - h2scf->preread_size;

  3018.     } else {
  3019.         stream->no_flow_control = 1;
  3020.         size = NGX_HTTP_V2_MAX_WINDOW - stream->recv_window;
  3021.     }

  3022.     if (size) {
  3023.         if (ngx_http_v2_send_window_update(stream->connection,
  3024.                                            stream->node->id, size)
  3025.             == NGX_ERROR)
  3026.         {
  3027.             stream->skip_data = 1;
  3028.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3029.         }

  3030.         h2c = stream->connection;

  3031.         if (!h2c->blocked) {
  3032.             if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3033.                 stream->skip_data = 1;
  3034.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3035.             }
  3036.         }

  3037.         stream->recv_window += size;
  3038.     }

  3039.     if (!buf) {
  3040.         ngx_add_timer(r->connection->read, clcf->client_body_timeout);
  3041.     }

  3042.     r->read_event_handler = ngx_http_v2_read_client_request_body_handler;
  3043.     r->write_event_handler = ngx_http_request_empty_handler;

  3044.     return NGX_AGAIN;
  3045. }


  3046. static ngx_int_t
  3047. ngx_http_v2_process_request_body(ngx_http_request_t *r, u_char *pos,
  3048.     size_t size, ngx_uint_t last, ngx_uint_t flush)
  3049. {
  3050.     size_t                     n;
  3051.     ngx_int_t                  rc;
  3052.     ngx_connection_t          *fc;
  3053.     ngx_http_request_body_t   *rb;
  3054.     ngx_http_core_loc_conf_t  *clcf;

  3055.     fc = r->connection;
  3056.     rb = r->request_body;

  3057.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3058.                    "http2 process request body");

  3059.     if (size == 0 && !last && !flush) {
  3060.         return NGX_AGAIN;
  3061.     }

  3062.     for ( ;; ) {
  3063.         for ( ;; ) {
  3064.             if (rb->buf->last == rb->buf->end && size) {

  3065.                 if (r->request_body_no_buffering) {

  3066.                     /* should never happen due to flow control */

  3067.                     ngx_log_error(NGX_LOG_ALERT, fc->log, 0,
  3068.                                   "no space in http2 body buffer");

  3069.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3070.                 }

  3071.                 /* update chains */

  3072.                 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3073.                                "http2 body update chains");

  3074.                 rc = ngx_http_v2_filter_request_body(r);

  3075.                 if (rc != NGX_OK) {
  3076.                     return rc;
  3077.                 }

  3078.                 if (rb->busy != NULL) {
  3079.                     ngx_log_error(NGX_LOG_ALERT, fc->log, 0,
  3080.                                   "busy buffers after request body flush");
  3081.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3082.                 }

  3083.                 rb->buf->pos = rb->buf->start;
  3084.                 rb->buf->last = rb->buf->start;
  3085.             }

  3086.             /* copy body data to the buffer */

  3087.             n = rb->buf->end - rb->buf->last;

  3088.             if (n > size) {
  3089.                 n = size;
  3090.             }

  3091.             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3092.                            "http2 request body recv %uz", n);

  3093.             if (n > 0) {
  3094.                 rb->buf->last = ngx_cpymem(rb->buf->last, pos, n);
  3095.                 pos += n;
  3096.                 size -= n;
  3097.             }

  3098.             if (size == 0 && last) {
  3099.                 rb->rest = 0;
  3100.             }

  3101.             if (size == 0) {
  3102.                 break;
  3103.             }
  3104.         }

  3105.         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3106.                        "http2 request body rest %O", rb->rest);

  3107.         if (flush) {
  3108.             rc = ngx_http_v2_filter_request_body(r);

  3109.             if (rc != NGX_OK) {
  3110.                 return rc;
  3111.             }
  3112.         }

  3113.         if (rb->rest == 0 && rb->last_saved) {
  3114.             break;
  3115.         }

  3116.         if (size == 0) {
  3117.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  3118.             ngx_add_timer(fc->read, clcf->client_body_timeout);

  3119.             if (!flush) {
  3120.                 ngx_post_event(fc->read, &ngx_posted_events);
  3121.             }

  3122.             return NGX_AGAIN;
  3123.         }
  3124.     }

  3125.     if (fc->read->timer_set) {
  3126.         ngx_del_timer(fc->read);
  3127.     }

  3128.     if (r->request_body_no_buffering) {
  3129.         if (!flush) {
  3130.             ngx_post_event(fc->read, &ngx_posted_events);
  3131.         }

  3132.         return NGX_OK;
  3133.     }

  3134.     if (r->headers_in.chunked) {
  3135.         r->headers_in.content_length_n = rb->received;
  3136.     }

  3137.     r->read_event_handler = ngx_http_block_reading;
  3138.     rb->post_handler(r);

  3139.     return NGX_OK;
  3140. }


  3141. static ngx_int_t
  3142. ngx_http_v2_filter_request_body(ngx_http_request_t *r)
  3143. {
  3144.     ngx_buf_t                 *b, *buf;
  3145.     ngx_int_t                  rc;
  3146.     ngx_chain_t               *cl;
  3147.     ngx_http_request_body_t   *rb;
  3148.     ngx_http_core_loc_conf_t  *clcf;

  3149.     rb = r->request_body;
  3150.     buf = rb->buf;

  3151.     if (buf->pos == buf->last && (rb->rest || rb->last_sent)) {
  3152.         cl = NULL;
  3153.         goto update;
  3154.     }

  3155.     cl = ngx_chain_get_free_buf(r->pool, &rb->free);
  3156.     if (cl == NULL) {
  3157.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3158.     }

  3159.     b = cl->buf;

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

  3161.     if (buf->pos != buf->last) {
  3162.         r->request_length += buf->last - buf->pos;
  3163.         rb->received += buf->last - buf->pos;

  3164.         if (r->headers_in.content_length_n != -1) {
  3165.             if (rb->received > r->headers_in.content_length_n) {
  3166.                 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  3167.                               "client intended to send body data "
  3168.                               "larger than declared");

  3169.                 return NGX_HTTP_BAD_REQUEST;
  3170.             }

  3171.         } else {
  3172.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  3173.             if (clcf->client_max_body_size
  3174.                 && rb->received > clcf->client_max_body_size)
  3175.             {
  3176.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  3177.                               "client intended to send too large chunked body: "
  3178.                               "%O bytes", rb->received);

  3179.                 return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
  3180.             }
  3181.         }

  3182.         b->temporary = 1;
  3183.         b->pos = buf->pos;
  3184.         b->last = buf->last;
  3185.         b->start = b->pos;
  3186.         b->end = b->last;

  3187.         buf->pos = buf->last;
  3188.     }

  3189.     if (!rb->rest) {
  3190.         if (r->headers_in.content_length_n != -1
  3191.             && r->headers_in.content_length_n != rb->received)
  3192.         {
  3193.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  3194.                           "client prematurely closed stream: "
  3195.                           "only %O out of %O bytes of request body received",
  3196.                           rb->received, r->headers_in.content_length_n);

  3197.             return NGX_HTTP_BAD_REQUEST;
  3198.         }

  3199.         b->last_buf = 1;
  3200.         rb->last_sent = 1;
  3201.     }

  3202.     b->tag = (ngx_buf_tag_t) &ngx_http_v2_filter_request_body;
  3203.     b->flush = r->request_body_no_buffering;

  3204. update:

  3205.     rc = ngx_http_top_request_body_filter(r, cl);

  3206.     ngx_chain_update_chains(r->pool, &rb->free, &rb->busy, &cl,
  3207.                             (ngx_buf_tag_t) &ngx_http_v2_filter_request_body);

  3208.     return rc;
  3209. }


  3210. static void
  3211. ngx_http_v2_read_client_request_body_handler(ngx_http_request_t *r)
  3212. {
  3213.     size_t                     window;
  3214.     ngx_buf_t                 *buf;
  3215.     ngx_int_t                  rc;
  3216.     ngx_connection_t          *fc;
  3217.     ngx_http_v2_stream_t      *stream;
  3218.     ngx_http_v2_connection_t  *h2c;

  3219.     fc = r->connection;

  3220.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3221.                    "http2 read client request body handler");

  3222.     if (fc->read->timedout) {
  3223.         ngx_log_error(NGX_LOG_INFO, fc->log, NGX_ETIMEDOUT, "client timed out");

  3224.         fc->timedout = 1;
  3225.         r->stream->skip_data = 1;

  3226.         ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  3227.         return;
  3228.     }

  3229.     if (fc->error) {
  3230.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  3231.                       "client prematurely closed stream");

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

  3233.         ngx_http_finalize_request(r, NGX_HTTP_CLIENT_CLOSED_REQUEST);
  3234.         return;
  3235.     }

  3236.     rc = ngx_http_v2_process_request_body(r, NULL, 0, r->stream->in_closed, 1);

  3237.     if (rc != NGX_OK && rc != NGX_AGAIN) {
  3238.         r->stream->skip_data = 1;
  3239.         ngx_http_finalize_request(r, rc);
  3240.         return;
  3241.     }

  3242.     if (rc == NGX_OK) {
  3243.         return;
  3244.     }

  3245.     if (r->stream->no_flow_control) {
  3246.         return;
  3247.     }

  3248.     if (r->request_body->rest == 0) {
  3249.         return;
  3250.     }

  3251.     if (r->request_body->busy != NULL) {
  3252.         return;
  3253.     }

  3254.     stream = r->stream;
  3255.     h2c = stream->connection;

  3256.     buf = r->request_body->buf;

  3257.     buf->pos = buf->start;
  3258.     buf->last = buf->start;

  3259.     window = buf->end - buf->start;

  3260.     if (h2c->state.stream == stream) {
  3261.         window -= h2c->state.length;
  3262.     }

  3263.     if (window <= stream->recv_window) {
  3264.         if (window < stream->recv_window) {
  3265.             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  3266.                           "http2 negative window update");

  3267.             stream->skip_data = 1;

  3268.             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3269.             return;
  3270.         }

  3271.         return;
  3272.     }

  3273.     if (ngx_http_v2_send_window_update(h2c, stream->node->id,
  3274.                                        window - stream->recv_window)
  3275.         == NGX_ERROR)
  3276.     {
  3277.         stream->skip_data = 1;
  3278.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3279.         return;
  3280.     }

  3281.     stream->recv_window = window;

  3282.     if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3283.         stream->skip_data = 1;
  3284.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3285.         return;
  3286.     }
  3287. }


  3288. ngx_int_t
  3289. ngx_http_v2_read_unbuffered_request_body(ngx_http_request_t *r)
  3290. {
  3291.     size_t                     window;
  3292.     ngx_buf_t                 *buf;
  3293.     ngx_int_t                  rc;
  3294.     ngx_connection_t          *fc;
  3295.     ngx_http_v2_stream_t      *stream;
  3296.     ngx_http_v2_connection_t  *h2c;

  3297.     stream = r->stream;
  3298.     fc = r->connection;

  3299.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3300.                    "http2 read unbuffered request body");

  3301.     if (fc->read->timedout) {
  3302.         if (stream->recv_window) {
  3303.             stream->skip_data = 1;
  3304.             fc->timedout = 1;

  3305.             return NGX_HTTP_REQUEST_TIME_OUT;
  3306.         }

  3307.         fc->read->timedout = 0;
  3308.     }

  3309.     if (fc->error) {
  3310.         stream->skip_data = 1;
  3311.         return NGX_HTTP_BAD_REQUEST;
  3312.     }

  3313.     rc = ngx_http_v2_process_request_body(r, NULL, 0, r->stream->in_closed, 1);

  3314.     if (rc != NGX_OK && rc != NGX_AGAIN) {
  3315.         stream->skip_data = 1;
  3316.         return rc;
  3317.     }

  3318.     if (rc == NGX_OK) {
  3319.         return NGX_OK;
  3320.     }

  3321.     if (r->request_body->rest == 0) {
  3322.         return NGX_AGAIN;
  3323.     }

  3324.     if (r->request_body->busy != NULL) {
  3325.         return NGX_AGAIN;
  3326.     }

  3327.     buf = r->request_body->buf;

  3328.     buf->pos = buf->start;
  3329.     buf->last = buf->start;

  3330.     window = buf->end - buf->start;
  3331.     h2c = stream->connection;

  3332.     if (h2c->state.stream == stream) {
  3333.         window -= h2c->state.length;
  3334.     }

  3335.     if (window <= stream->recv_window) {
  3336.         if (window < stream->recv_window) {
  3337.             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  3338.                           "http2 negative window update");
  3339.             stream->skip_data = 1;
  3340.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3341.         }

  3342.         return NGX_AGAIN;
  3343.     }

  3344.     if (ngx_http_v2_send_window_update(h2c, stream->node->id,
  3345.                                        window - stream->recv_window)
  3346.         == NGX_ERROR)
  3347.     {
  3348.         stream->skip_data = 1;
  3349.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3350.     }

  3351.     if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3352.         stream->skip_data = 1;
  3353.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3354.     }

  3355.     stream->recv_window = window;

  3356.     return NGX_AGAIN;
  3357. }


  3358. static ngx_int_t
  3359. ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
  3360.     ngx_http_v2_stream_t *stream, ngx_uint_t status)
  3361. {
  3362.     ngx_event_t       *rev;
  3363.     ngx_connection_t  *fc;

  3364.     if (stream->rst_sent) {
  3365.         return NGX_OK;
  3366.     }

  3367.     if (ngx_http_v2_send_rst_stream(h2c, stream->node->id, status)
  3368.         == NGX_ERROR)
  3369.     {
  3370.         return NGX_ERROR;
  3371.     }

  3372.     stream->rst_sent = 1;
  3373.     stream->skip_data = 1;

  3374.     fc = stream->request->connection;
  3375.     fc->error = 1;

  3376.     rev = fc->read;
  3377.     rev->handler(rev);

  3378.     return NGX_OK;
  3379. }


  3380. void
  3381. ngx_http_v2_close_stream(ngx_http_v2_stream_t *stream, ngx_int_t rc)
  3382. {
  3383.     ngx_pool_t                *pool;
  3384.     ngx_event_t               *ev;
  3385.     ngx_connection_t          *fc;
  3386.     ngx_http_v2_node_t        *node;
  3387.     ngx_http_v2_connection_t  *h2c;

  3388.     h2c = stream->connection;
  3389.     node = stream->node;

  3390.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  3391.                    "http2 close stream %ui, queued %ui, processing %ui",
  3392.                    node->id, stream->queued, h2c->processing);

  3393.     fc = stream->request->connection;

  3394.     if (stream->queued) {
  3395.         fc->error = 1;
  3396.         fc->write->handler = ngx_http_v2_retry_close_stream_handler;
  3397.         fc->read->handler = ngx_http_v2_retry_close_stream_handler;
  3398.         return;
  3399.     }

  3400.     if (!stream->rst_sent && !h2c->connection->error) {

  3401.         if (!stream->out_closed) {
  3402.             if (ngx_http_v2_send_rst_stream(h2c, node->id,
  3403.                                       fc->timedout ? NGX_HTTP_V2_PROTOCOL_ERROR
  3404.                                                    : NGX_HTTP_V2_INTERNAL_ERROR)
  3405.                 != NGX_OK)
  3406.             {
  3407.                 h2c->connection->error = 1;
  3408.             }

  3409.         } else if (!stream->in_closed) {
  3410.             if (ngx_http_v2_send_rst_stream(h2c, node->id, NGX_HTTP_V2_NO_ERROR)
  3411.                 != NGX_OK)
  3412.             {
  3413.                 h2c->connection->error = 1;
  3414.             }
  3415.         }
  3416.     }

  3417.     if (h2c->state.stream == stream) {
  3418.         h2c->state.stream = NULL;
  3419.     }

  3420.     node->stream = NULL;

  3421.     ngx_queue_insert_tail(&h2c->closed, &node->reuse);
  3422.     h2c->closed_nodes++;

  3423.     /*
  3424.      * This pool keeps decoded request headers which can be used by log phase
  3425.      * handlers in ngx_http_free_request().
  3426.      *
  3427.      * The pointer is stored into local variable because the stream object
  3428.      * will be destroyed after a call to ngx_http_free_request().
  3429.      */
  3430.     pool = stream->pool;

  3431.     h2c->frames -= stream->frames;

  3432.     ngx_http_free_request(stream->request, rc);

  3433.     if (pool != h2c->state.pool) {
  3434.         ngx_destroy_pool(pool);

  3435.     } else {
  3436.         /* pool will be destroyed when the complete header is parsed */
  3437.         h2c->state.keep_pool = 0;
  3438.     }

  3439.     ev = fc->read;

  3440.     if (ev->timer_set) {
  3441.         ngx_del_timer(ev);
  3442.     }

  3443.     if (ev->posted) {
  3444.         ngx_delete_posted_event(ev);
  3445.     }

  3446.     ev = fc->write;

  3447.     if (ev->timer_set) {
  3448.         ngx_del_timer(ev);
  3449.     }

  3450.     if (ev->posted) {
  3451.         ngx_delete_posted_event(ev);
  3452.     }

  3453.     fc->data = h2c->free_fake_connections;
  3454.     h2c->free_fake_connections = fc;

  3455.     h2c->processing--;

  3456.     if (h2c->processing || h2c->blocked) {
  3457.         return;
  3458.     }

  3459.     ev = h2c->connection->read;

  3460.     ev->handler = ngx_http_v2_handle_connection_handler;
  3461.     ngx_post_event(ev, &ngx_posted_events);
  3462. }


  3463. static void
  3464. ngx_http_v2_close_stream_handler(ngx_event_t *ev)
  3465. {
  3466.     ngx_connection_t    *fc;
  3467.     ngx_http_request_t  *r;

  3468.     fc = ev->data;
  3469.     r = fc->data;

  3470.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3471.                    "http2 close stream handler");

  3472.     if (ev->timedout) {
  3473.         ngx_log_error(NGX_LOG_INFO, fc->log, NGX_ETIMEDOUT, "client timed out");

  3474.         fc->timedout = 1;

  3475.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_REQUEST_TIME_OUT);
  3476.         return;
  3477.     }

  3478.     ngx_http_v2_close_stream(r->stream, 0);
  3479. }


  3480. static void
  3481. ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev)
  3482. {
  3483.     ngx_connection_t    *fc;
  3484.     ngx_http_request_t  *r;

  3485.     fc = ev->data;
  3486.     r = fc->data;

  3487.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
  3488.                    "http2 retry close stream handler");

  3489.     ngx_http_v2_close_stream(r->stream, 0);
  3490. }


  3491. static void
  3492. ngx_http_v2_handle_connection_handler(ngx_event_t *rev)
  3493. {
  3494.     ngx_connection_t          *c;
  3495.     ngx_http_v2_connection_t  *h2c;

  3496.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  3497.                    "http2 handle connection handler");

  3498.     c = rev->data;
  3499.     h2c = c->data;

  3500.     if (c->error) {
  3501.         ngx_http_v2_finalize_connection(h2c, 0);
  3502.         return;
  3503.     }

  3504.     rev->handler = ngx_http_v2_read_handler;

  3505.     if (rev->ready) {
  3506.         ngx_http_v2_read_handler(rev);
  3507.         return;
  3508.     }

  3509.     if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3510.         ngx_http_v2_finalize_connection(h2c, 0);
  3511.         return;
  3512.     }

  3513.     ngx_http_v2_handle_connection(c->data);
  3514. }


  3515. static void
  3516. ngx_http_v2_idle_handler(ngx_event_t *rev)
  3517. {
  3518.     ngx_connection_t          *c;
  3519.     ngx_http_v2_srv_conf_t    *h2scf;
  3520.     ngx_http_v2_connection_t  *h2c;
  3521.     ngx_http_core_loc_conf_t  *clcf;

  3522.     c = rev->data;
  3523.     h2c = c->data;

  3524.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 idle handler");

  3525.     if (rev->timedout || c->close) {
  3526.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  3527.         return;
  3528.     }

  3529. #if (NGX_HAVE_KQUEUE)

  3530.     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
  3531.         if (rev->pending_eof) {
  3532.             c->log->handler = NULL;
  3533.             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
  3534.                           "kevent() reported that client %V closed "
  3535.                           "idle connection", &c->addr_text);
  3536. #if (NGX_HTTP_SSL)
  3537.             if (c->ssl) {
  3538.                 c->ssl->no_send_shutdown = 1;
  3539.             }
  3540. #endif
  3541.             ngx_http_close_connection(c);
  3542.             return;
  3543.         }
  3544.     }

  3545. #endif

  3546.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  3547.                                         ngx_http_core_module);

  3548.     if (h2c->idle++ > 10 * clcf->keepalive_requests) {
  3549.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  3550.                       "http2 flood detected");
  3551.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  3552.         return;
  3553.     }

  3554.     c->destroyed = 0;
  3555.     ngx_reusable_connection(c, 0);

  3556.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3557.                                          ngx_http_v2_module);

  3558.     h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
  3559.     if (h2c->pool == NULL) {
  3560.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  3561.         return;
  3562.     }

  3563.     c->write->handler = ngx_http_v2_write_handler;

  3564.     rev->handler = ngx_http_v2_read_handler;
  3565.     ngx_http_v2_read_handler(rev);
  3566. }


  3567. static void
  3568. ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
  3569.     ngx_uint_t status)
  3570. {
  3571.     ngx_uint_t               i, size;
  3572.     ngx_event_t             *ev;
  3573.     ngx_connection_t        *c, *fc;
  3574.     ngx_http_request_t      *r;
  3575.     ngx_http_v2_node_t      *node;
  3576.     ngx_http_v2_stream_t    *stream;
  3577.     ngx_http_v2_srv_conf_t  *h2scf;

  3578.     c = h2c->connection;

  3579.     h2c->blocked = 1;

  3580.     if (!c->error && !h2c->goaway) {
  3581.         h2c->goaway = 1;

  3582.         if (ngx_http_v2_send_goaway(h2c, status) != NGX_ERROR) {
  3583.             (void) ngx_http_v2_send_output_queue(h2c);
  3584.         }
  3585.     }

  3586.     if (!h2c->processing) {
  3587.         goto done;
  3588.     }

  3589.     c->read->handler = ngx_http_empty_handler;
  3590.     c->write->handler = ngx_http_empty_handler;

  3591.     h2c->last_out = NULL;

  3592.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3593.                                          ngx_http_v2_module);

  3594.     size = ngx_http_v2_index_size(h2scf);

  3595.     for (i = 0; i < size; i++) {

  3596.         for (node = h2c->streams_index[i]; node; node = node->index) {
  3597.             stream = node->stream;

  3598.             if (stream == NULL) {
  3599.                 continue;
  3600.             }

  3601.             stream->waiting = 0;

  3602.             r = stream->request;
  3603.             fc = r->connection;

  3604.             fc->error = 1;

  3605.             if (stream->queued) {
  3606.                 stream->queued = 0;

  3607.                 ev = fc->write;
  3608.                 ev->active = 0;
  3609.                 ev->ready = 1;

  3610.             } else {
  3611.                 ev = fc->read;
  3612.             }

  3613.             ev->eof = 1;
  3614.             ev->handler(ev);
  3615.         }
  3616.     }

  3617.     h2c->blocked = 0;

  3618.     if (h2c->processing) {
  3619.         c->error = 1;
  3620.         return;
  3621.     }

  3622. done:

  3623.     if (c->error) {
  3624.         ngx_http_close_connection(c);
  3625.         return;
  3626.     }

  3627.     ngx_http_v2_lingering_close(c);
  3628. }


  3629. static ngx_int_t
  3630. ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c, ssize_t delta)
  3631. {
  3632.     ngx_uint_t               i, size;
  3633.     ngx_event_t             *wev;
  3634.     ngx_http_v2_node_t      *node;
  3635.     ngx_http_v2_stream_t    *stream;
  3636.     ngx_http_v2_srv_conf_t  *h2scf;

  3637.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3638.                                          ngx_http_v2_module);

  3639.     size = ngx_http_v2_index_size(h2scf);

  3640.     for (i = 0; i < size; i++) {

  3641.         for (node = h2c->streams_index[i]; node; node = node->index) {
  3642.             stream = node->stream;

  3643.             if (stream == NULL) {
  3644.                 continue;
  3645.             }

  3646.             if (delta > 0
  3647.                 && stream->send_window
  3648.                       > (ssize_t) (NGX_HTTP_V2_MAX_WINDOW - delta))
  3649.             {
  3650.                 if (ngx_http_v2_terminate_stream(h2c, stream,
  3651.                                                  NGX_HTTP_V2_FLOW_CTRL_ERROR)
  3652.                     == NGX_ERROR)
  3653.                 {
  3654.                     return NGX_ERROR;
  3655.                 }

  3656.                 continue;
  3657.             }

  3658.             stream->send_window += delta;

  3659.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  3660.                            "http2:%ui adjusted window: %z",
  3661.                            node->id, stream->send_window);

  3662.             if (stream->send_window > 0 && stream->exhausted) {
  3663.                 stream->exhausted = 0;

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

  3665.                 wev->active = 0;
  3666.                 wev->ready = 1;

  3667.                 if (!wev->delayed) {
  3668.                     wev->handler(wev);
  3669.                 }
  3670.             }
  3671.         }
  3672.     }

  3673.     return NGX_OK;
  3674. }


  3675. static void
  3676. ngx_http_v2_set_dependency(ngx_http_v2_connection_t *h2c,
  3677.     ngx_http_v2_node_t *node, ngx_uint_t depend, ngx_uint_t exclusive)
  3678. {
  3679.     ngx_queue_t         *children, *q;
  3680.     ngx_http_v2_node_t  *parent, *child, *next;

  3681.     parent = depend ? ngx_http_v2_get_node_by_id(h2c, depend, 0) : NULL;

  3682.     if (parent == NULL) {
  3683.         parent = NGX_HTTP_V2_ROOT;

  3684.         if (depend != 0) {
  3685.             exclusive = 0;
  3686.         }

  3687.         node->rank = 1;
  3688.         node->rel_weight = (1.0 / 256) * node->weight;

  3689.         children = &h2c->dependencies;

  3690.     } else {
  3691.         if (node->parent != NULL) {

  3692.             for (next = parent->parent;
  3693.                  next != NGX_HTTP_V2_ROOT && next->rank >= node->rank;
  3694.                  next = next->parent)
  3695.             {
  3696.                 if (next != node) {
  3697.                     continue;
  3698.                 }

  3699.                 ngx_queue_remove(&parent->queue);
  3700.                 ngx_queue_insert_after(&node->queue, &parent->queue);

  3701.                 parent->parent = node->parent;

  3702.                 if (node->parent == NGX_HTTP_V2_ROOT) {
  3703.                     parent->rank = 1;
  3704.                     parent->rel_weight = (1.0 / 256) * parent->weight;

  3705.                 } else {
  3706.                     parent->rank = node->parent->rank + 1;
  3707.                     parent->rel_weight = (node->parent->rel_weight / 256)
  3708.                                          * parent->weight;
  3709.                 }

  3710.                 if (!exclusive) {
  3711.                     ngx_http_v2_node_children_update(parent);
  3712.                 }

  3713.                 break;
  3714.             }
  3715.         }

  3716.         node->rank = parent->rank + 1;
  3717.         node->rel_weight = (parent->rel_weight / 256) * node->weight;

  3718.         if (parent->stream == NULL) {
  3719.             ngx_queue_remove(&parent->reuse);
  3720.             ngx_queue_insert_tail(&h2c->closed, &parent->reuse);
  3721.         }

  3722.         children = &parent->children;
  3723.     }

  3724.     if (exclusive) {
  3725.         for (q = ngx_queue_head(children);
  3726.              q != ngx_queue_sentinel(children);
  3727.              q = ngx_queue_next(q))
  3728.         {
  3729.             child = ngx_queue_data(q, ngx_http_v2_node_t, queue);
  3730.             child->parent = node;
  3731.         }

  3732.         ngx_queue_add(&node->children, children);
  3733.         ngx_queue_init(children);
  3734.     }

  3735.     if (node->parent != NULL) {
  3736.         ngx_queue_remove(&node->queue);
  3737.     }

  3738.     ngx_queue_insert_tail(children, &node->queue);

  3739.     node->parent = parent;

  3740.     ngx_http_v2_node_children_update(node);
  3741. }


  3742. static void
  3743. ngx_http_v2_node_children_update(ngx_http_v2_node_t *node)
  3744. {
  3745.     ngx_queue_t         *q;
  3746.     ngx_http_v2_node_t  *child;

  3747.     for (q = ngx_queue_head(&node->children);
  3748.          q != ngx_queue_sentinel(&node->children);
  3749.          q = ngx_queue_next(q))
  3750.     {
  3751.         child = ngx_queue_data(q, ngx_http_v2_node_t, queue);

  3752.         child->rank = node->rank + 1;
  3753.         child->rel_weight = (node->rel_weight / 256) * child->weight;

  3754.         ngx_http_v2_node_children_update(child);
  3755.     }
  3756. }


  3757. static void
  3758. ngx_http_v2_pool_cleanup(void *data)
  3759. {
  3760.     ngx_http_v2_connection_t  *h2c = data;

  3761.     if (h2c->state.pool) {
  3762.         ngx_destroy_pool(h2c->state.pool);
  3763.     }

  3764.     if (h2c->pool) {
  3765.         ngx_destroy_pool(h2c->pool);
  3766.     }
  3767. }