src/http/v2/ngx_http_v2.c - nginx

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

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

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

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

  1377.     } else {
  1378.         cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

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

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

  1390.         h->key.len = header->name.len;
  1391.         h->key.data = header->name.data;

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

  1397.         h->value.len = header->value.len;
  1398.         h->value.data = header->value.data;

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

  1400.         cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  1403.         if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
  1404.             goto error;
  1405.         }
  1406.     }

  1407.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1408.                    "http2 header: \"%V: %V\"",
  1409.                    &header->name, &header->value);

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

  1411. error:

  1412.     h2c->state.stream = NULL;

  1413.     ngx_http_run_posted_requests(fc);

  1414.     return ngx_http_v2_state_header_complete(h2c, pos, end);
  1415. }


  1416. static u_char *
  1417. ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
  1418.     u_char *end)
  1419. {
  1420.     ngx_http_v2_stream_t  *stream;

  1421.     if (h2c->state.length) {
  1422.         if (end - pos > 0) {
  1423.             h2c->state.handler = ngx_http_v2_state_header_block;
  1424.             return pos;
  1425.         }

  1426.         return ngx_http_v2_state_headers_save(h2c, pos, end,
  1427.                                               ngx_http_v2_state_header_block);
  1428.     }

  1429.     if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)) {
  1430.         return ngx_http_v2_handle_continuation(h2c, pos, end,
  1431.                                              ngx_http_v2_state_header_complete);
  1432.     }

  1433.     stream = h2c->state.stream;

  1434.     if (stream) {
  1435.         ngx_http_v2_run_request(stream->request);
  1436.     }

  1437.     if (!h2c->state.keep_pool) {
  1438.         ngx_destroy_pool(h2c->state.pool);
  1439.     }

  1440.     h2c->state.pool = NULL;
  1441.     h2c->state.keep_pool = 0;

  1442.     if (h2c->state.padding) {
  1443.         return ngx_http_v2_state_skip_padded(h2c, pos, end);
  1444.     }

  1445.     return ngx_http_v2_state_complete(h2c, pos, end);
  1446. }


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

  1454.     len = h2c->state.length;

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

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

  1458.         p = pos;
  1459.         pos += skip;
  1460.         ngx_memmove(pos, p, len);
  1461.     }

  1462.     if ((size_t) (end - pos) < len + NGX_HTTP_V2_FRAME_HEADER_SIZE) {
  1463.         return ngx_http_v2_state_headers_save(h2c, pos, end, handler);
  1464.     }

  1465.     p = pos + len;

  1466.     head = ngx_http_v2_parse_uint32(p);

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

  1470.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1471.     }

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

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

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

  1478.     p = pos;
  1479.     pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;

  1480.     ngx_memcpy(pos, p, len);

  1481.     len = ngx_http_v2_parse_length(head);

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

  1483.     if (h2c->state.stream) {
  1484.         h2c->state.stream->request->request_length += len;
  1485.     }

  1486.     h2c->state.handler = handler;
  1487.     return pos;
  1488. }


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

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

  1499.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1500.     }

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

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

  1506.     if (end - pos < NGX_HTTP_V2_PRIORITY_SIZE) {
  1507.         return ngx_http_v2_state_save(h2c, pos, end,
  1508.                                       ngx_http_v2_state_priority);
  1509.     }

  1510.     dependency = ngx_http_v2_parse_uint32(pos);

  1511.     depend = dependency & 0x7fffffff;
  1512.     excl = dependency >> 31;
  1513.     weight = pos[4] + 1;

  1514.     pos += NGX_HTTP_V2_PRIORITY_SIZE;

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

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

  1522.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1523.     }

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

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

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

  1531.     if (node == NULL) {
  1532.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1533.     }

  1534.     node->weight = weight;

  1535.     if (node->stream == NULL) {
  1536.         if (node->parent == NULL) {
  1537.             h2c->closed_nodes++;

  1538.         } else {
  1539.             ngx_queue_remove(&node->reuse);
  1540.         }

  1541.         ngx_queue_insert_tail(&h2c->closed, &node->reuse);
  1542.     }

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

  1544.     return ngx_http_v2_state_complete(h2c, pos, end);
  1545. }


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

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

  1559.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1560.     }

  1561.     if (end - pos < NGX_HTTP_V2_RST_STREAM_SIZE) {
  1562.         return ngx_http_v2_state_save(h2c, pos, end,
  1563.                                       ngx_http_v2_state_rst_stream);
  1564.     }

  1565.     status = ngx_http_v2_parse_uint32(pos);

  1566.     pos += NGX_HTTP_V2_RST_STREAM_SIZE;

  1567.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1568.                    "http2 RST_STREAM frame, sid:%ui status:%ui",
  1569.                    h2c->state.sid, status);

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

  1573.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1574.     }

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

  1576.     if (node == NULL || node->stream == NULL) {
  1577.         ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1578.                        "unknown http2 stream");

  1579.         return ngx_http_v2_state_complete(h2c, pos, end);
  1580.     }

  1581.     stream = node->stream;

  1582.     stream->in_closed = 1;
  1583.     stream->out_closed = 1;

  1584.     fc = stream->request->connection;
  1585.     fc->error = 1;

  1586.     switch (status) {

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

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

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

  1602.     ev = fc->read;
  1603.     ev->handler(ev);

  1604.     return ngx_http_v2_state_complete(h2c, pos, end);
  1605. }


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

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

  1615.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1616.     }

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

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

  1622.             return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1623.         }

  1624.         h2c->settings_ack = 1;

  1625.         return ngx_http_v2_state_complete(h2c, pos, end);
  1626.     }

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

  1631.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1632.     }

  1633.     return ngx_http_v2_state_settings_params(h2c, pos, end);
  1634. }


  1635. static u_char *
  1636. ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c, u_char *pos,
  1637.     u_char *end)
  1638. {
  1639.     ssize_t                   window_delta;
  1640.     ngx_uint_t                id, value;
  1641.     ngx_http_v2_out_frame_t  *frame;

  1642.     window_delta = 0;

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

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

  1649.         id = ngx_http_v2_parse_uint16(pos);
  1650.         value = ngx_http_v2_parse_uint32(&pos[2]);

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

  1653.         switch (id) {

  1654.         case NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING:

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

  1659.                 return ngx_http_v2_connection_error(h2c,
  1660.                                                   NGX_HTTP_V2_FLOW_CTRL_ERROR);
  1661.             }

  1662.             window_delta = value - h2c->init_window;
  1663.             break;

  1664.         case NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING:

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

  1671.                 return ngx_http_v2_connection_error(h2c,
  1672.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1673.             }

  1674.             h2c->frame_size = value;
  1675.             break;

  1676.         case NGX_HTTP_V2_ENABLE_PUSH_SETTING:

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

  1681.                 return ngx_http_v2_connection_error(h2c,
  1682.                                                     NGX_HTTP_V2_PROTOCOL_ERROR);
  1683.             }

  1684.             break;

  1685.         case NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING:

  1686.             h2c->table_update = 1;
  1687.             break;

  1688.         default:
  1689.             break;
  1690.         }

  1691.         pos += NGX_HTTP_V2_SETTINGS_PARAM_SIZE;
  1692.     }

  1693.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_SETTINGS_ACK_SIZE,
  1694.                                   NGX_HTTP_V2_SETTINGS_FRAME,
  1695.                                   NGX_HTTP_V2_ACK_FLAG, 0);
  1696.     if (frame == NULL) {
  1697.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1698.     }

  1699.     ngx_http_v2_queue_ordered_frame(h2c, frame);

  1700.     if (window_delta) {
  1701.         h2c->init_window += window_delta;

  1702.         if (ngx_http_v2_adjust_windows(h2c, window_delta) != NGX_OK) {
  1703.             return ngx_http_v2_connection_error(h2c,
  1704.                                                 NGX_HTTP_V2_INTERNAL_ERROR);
  1705.         }
  1706.     }

  1707.     return ngx_http_v2_state_complete(h2c, pos, end);
  1708. }


  1709. static u_char *
  1710. ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c, u_char *pos,
  1711.     u_char *end)
  1712. {
  1713.     ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1714.                   "client sent PUSH_PROMISE frame");

  1715.     return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1716. }


  1717. static u_char *
  1718. ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  1719. {
  1720.     ngx_buf_t                *buf;
  1721.     ngx_http_v2_out_frame_t  *frame;

  1722.     if (h2c->state.length != NGX_HTTP_V2_PING_SIZE) {
  1723.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1724.                       "client sent PING frame with incorrect length %uz",
  1725.                       h2c->state.length);

  1726.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1727.     }

  1728.     if (end - pos < NGX_HTTP_V2_PING_SIZE) {
  1729.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_ping);
  1730.     }

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

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

  1736.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1737.     }

  1738.     if (h2c->state.flags & NGX_HTTP_V2_ACK_FLAG) {
  1739.         return ngx_http_v2_state_skip(h2c, pos, end);
  1740.     }

  1741.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_PING_SIZE,
  1742.                                   NGX_HTTP_V2_PING_FRAME,
  1743.                                   NGX_HTTP_V2_ACK_FLAG, 0);
  1744.     if (frame == NULL) {
  1745.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1746.     }

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

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

  1749.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  1750.     return ngx_http_v2_state_complete(h2c, pos + NGX_HTTP_V2_PING_SIZE, end);
  1751. }


  1752. static u_char *
  1753. ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c, u_char *pos,
  1754.     u_char *end)
  1755. {
  1756. #if (NGX_DEBUG)
  1757.     ngx_uint_t  last_sid, error;
  1758. #endif

  1759.     if (h2c->state.length < NGX_HTTP_V2_GOAWAY_SIZE) {
  1760.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1761.                       "client sent GOAWAY frame "
  1762.                       "with incorrect length %uz", h2c->state.length);

  1763.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1764.     }

  1765.     if (end - pos < NGX_HTTP_V2_GOAWAY_SIZE) {
  1766.         return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_goaway);
  1767.     }

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

  1771.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1772.     }

  1773. #if (NGX_DEBUG)
  1774.     h2c->state.length -= NGX_HTTP_V2_GOAWAY_SIZE;

  1775.     last_sid = ngx_http_v2_parse_sid(pos);
  1776.     error = ngx_http_v2_parse_uint32(&pos[4]);

  1777.     pos += NGX_HTTP_V2_GOAWAY_SIZE;

  1778.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1779.                    "http2 GOAWAY frame: last sid %ui, error %ui",
  1780.                    last_sid, error);
  1781. #endif

  1782.     return ngx_http_v2_state_skip(h2c, pos, end);
  1783. }


  1784. static u_char *
  1785. ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos,
  1786.     u_char *end)
  1787. {
  1788.     size_t                 window;
  1789.     ngx_event_t           *wev;
  1790.     ngx_queue_t           *q;
  1791.     ngx_http_v2_node_t    *node;
  1792.     ngx_http_v2_stream_t  *stream;

  1793.     if (h2c->state.length != NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
  1794.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1795.                       "client sent WINDOW_UPDATE frame "
  1796.                       "with incorrect length %uz", h2c->state.length);

  1797.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
  1798.     }

  1799.     if (end - pos < NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
  1800.         return ngx_http_v2_state_save(h2c, pos, end,
  1801.                                       ngx_http_v2_state_window_update);
  1802.     }

  1803.     window = ngx_http_v2_parse_window(pos);

  1804.     pos += NGX_HTTP_V2_WINDOW_UPDATE_SIZE;

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

  1808.     if (window == 0) {
  1809.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1810.                       "client sent WINDOW_UPDATE frame "
  1811.                       "with incorrect window increment 0");

  1812.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1813.     }

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

  1816.         if (node == NULL || node->stream == NULL) {
  1817.             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1818.                            "unknown http2 stream");

  1819.             return ngx_http_v2_state_complete(h2c, pos, end);
  1820.         }

  1821.         stream = node->stream;

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

  1823.             ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1824.                           "client violated flow control for stream %ui: "
  1825.                           "received WINDOW_UPDATE frame "
  1826.                           "with window increment %uz "
  1827.                           "not allowed for window %z",
  1828.                           h2c->state.sid, window, stream->send_window);

  1829.             if (ngx_http_v2_terminate_stream(h2c, stream,
  1830.                                              NGX_HTTP_V2_FLOW_CTRL_ERROR)
  1831.                 == NGX_ERROR)
  1832.             {
  1833.                 return ngx_http_v2_connection_error(h2c,
  1834.                                                     NGX_HTTP_V2_INTERNAL_ERROR);
  1835.             }

  1836.             return ngx_http_v2_state_complete(h2c, pos, end);
  1837.         }

  1838.         stream->send_window += window;

  1839.         if (stream->exhausted) {
  1840.             stream->exhausted = 0;

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

  1842.             wev->active = 0;
  1843.             wev->ready = 1;

  1844.             if (!wev->delayed) {
  1845.                 wev->handler(wev);
  1846.             }
  1847.         }

  1848.         return ngx_http_v2_state_complete(h2c, pos, end);
  1849.     }

  1850.     if (window > NGX_HTTP_V2_MAX_WINDOW - h2c->send_window) {
  1851.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1852.                       "client violated connection flow control: "
  1853.                       "received WINDOW_UPDATE frame "
  1854.                       "with window increment %uz "
  1855.                       "not allowed for window %uz",
  1856.                       window, h2c->send_window);

  1857.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
  1858.     }

  1859.     h2c->send_window += window;

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

  1862.         ngx_queue_remove(q);

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

  1864.         stream->waiting = 0;

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

  1866.         wev->active = 0;
  1867.         wev->ready = 1;

  1868.         if (!wev->delayed) {
  1869.             wev->handler(wev);

  1870.             if (h2c->send_window == 0) {
  1871.                 break;
  1872.             }
  1873.         }
  1874.     }

  1875.     return ngx_http_v2_state_complete(h2c, pos, end);
  1876. }


  1877. static u_char *
  1878. ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
  1879.     u_char *end)
  1880. {
  1881.     ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  1882.                   "client sent unexpected CONTINUATION frame");

  1883.     return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
  1884. }


  1885. static u_char *
  1886. ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
  1887.     u_char *end)
  1888. {
  1889.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1890.                    "http2 frame complete pos:%p end:%p", pos, end);

  1891.     if (pos > end) {
  1892.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  1893.                       "receive buffer overrun");

  1894.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1895.     }

  1896.     h2c->state.stream = NULL;
  1897.     h2c->state.handler = ngx_http_v2_state_head;

  1898.     return pos;
  1899. }


  1900. static u_char *
  1901. ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c, u_char *pos,
  1902.     u_char *end)
  1903. {
  1904.     h2c->state.length += h2c->state.padding;
  1905.     h2c->state.padding = 0;

  1906.     return ngx_http_v2_state_skip(h2c, pos, end);
  1907. }


  1908. static u_char *
  1909. ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
  1910. {
  1911.     size_t  size;

  1912.     size = end - pos;

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

  1916.         h2c->state.length -= size;
  1917.         return ngx_http_v2_state_save(h2c, end, end, ngx_http_v2_state_skip);
  1918.     }

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

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


  1923. static u_char *
  1924. ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end,
  1925.     ngx_http_v2_handler_pt handler)
  1926. {
  1927.     size_t  size;

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

  1931.     size = end - pos;

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

  1935.         return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  1936.     }

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

  1938.     h2c->state.buffer_used = size;
  1939.     h2c->state.handler = handler;
  1940.     h2c->state.incomplete = 1;

  1941.     return end;
  1942. }


  1943. static u_char *
  1944. ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c, u_char *pos,
  1945.     u_char *end, ngx_http_v2_handler_pt handler)
  1946. {
  1947.     ngx_event_t               *rev;
  1948.     ngx_http_request_t        *r;
  1949.     ngx_http_core_srv_conf_t  *cscf;

  1950.     if (h2c->state.stream) {
  1951.         r = h2c->state.stream->request;
  1952.         rev = r->connection->read;

  1953.         if (!rev->timer_set) {
  1954.             cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  1955.             ngx_add_timer(rev, cscf->client_header_timeout);
  1956.         }
  1957.     }

  1958.     return ngx_http_v2_state_save(h2c, pos, end, handler);
  1959. }


  1960. static u_char *
  1961. ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
  1962.     ngx_uint_t err)
  1963. {
  1964.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  1965.                    "http2 state connection error");

  1966.     ngx_http_v2_finalize_connection(h2c, err);

  1967.     return NULL;
  1968. }


  1969. static ngx_int_t
  1970. ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c, u_char **pos, u_char *end,
  1971.     ngx_uint_t prefix)
  1972. {
  1973.     u_char      *start, *p;
  1974.     ngx_uint_t   value, octet, shift;

  1975.     start = *pos;
  1976.     p = start;

  1977.     value = *p++ & prefix;

  1978.     if (value != prefix) {
  1979.         if (h2c->state.length == 0) {
  1980.             return NGX_ERROR;
  1981.         }

  1982.         h2c->state.length--;

  1983.         *pos = p;
  1984.         return value;
  1985.     }

  1986.     if (end - start > NGX_HTTP_V2_INT_OCTETS) {
  1987.         end = start + NGX_HTTP_V2_INT_OCTETS;
  1988.     }

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

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

  1992.         if (octet < 128) {
  1993.             if ((size_t) (p - start) > h2c->state.length) {
  1994.                 return NGX_ERROR;
  1995.             }

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

  1997.             *pos = p;
  1998.             return value;
  1999.         }
  2000.     }

  2001.     if ((size_t) (end - start) >= h2c->state.length) {
  2002.         return NGX_ERROR;
  2003.     }

  2004.     if (end == start + NGX_HTTP_V2_INT_OCTETS) {
  2005.         return NGX_DECLINED;
  2006.     }

  2007.     return NGX_AGAIN;
  2008. }


  2009. static ngx_int_t
  2010. ngx_http_v2_send_settings(ngx_http_v2_connection_t *h2c)
  2011. {
  2012.     size_t                    len;
  2013.     ngx_buf_t                *buf;
  2014.     ngx_chain_t              *cl;
  2015.     ngx_http_v2_srv_conf_t   *h2scf;
  2016.     ngx_http_v2_out_frame_t  *frame;

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

  2019.     frame = ngx_palloc(h2c->pool, sizeof(ngx_http_v2_out_frame_t));
  2020.     if (frame == NULL) {
  2021.         return NGX_ERROR;
  2022.     }

  2023.     cl = ngx_alloc_chain_link(h2c->pool);
  2024.     if (cl == NULL) {
  2025.         return NGX_ERROR;
  2026.     }

  2027.     len = NGX_HTTP_V2_SETTINGS_PARAM_SIZE * 3;

  2028.     buf = ngx_create_temp_buf(h2c->pool, NGX_HTTP_V2_FRAME_HEADER_SIZE + len);
  2029.     if (buf == NULL) {
  2030.         return NGX_ERROR;
  2031.     }

  2032.     buf->last_buf = 1;

  2033.     cl->buf = buf;
  2034.     cl->next = NULL;

  2035.     frame->first = cl;
  2036.     frame->last = cl;
  2037.     frame->handler = ngx_http_v2_settings_frame_handler;
  2038.     frame->stream = NULL;
  2039. #if (NGX_DEBUG)
  2040.     frame->length = len;
  2041. #endif
  2042.     frame->blocked = 0;

  2043.     buf->last = ngx_http_v2_write_len_and_type(buf->last, len,
  2044.                                                NGX_HTTP_V2_SETTINGS_FRAME);

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

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

  2047.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2048.                                          ngx_http_v2_module);

  2049.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2050.                                          NGX_HTTP_V2_MAX_STREAMS_SETTING);
  2051.     buf->last = ngx_http_v2_write_uint32(buf->last,
  2052.                                          h2scf->concurrent_streams);

  2053.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2054.                                          NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING);
  2055.     buf->last = ngx_http_v2_write_uint32(buf->last, h2scf->preread_size);

  2056.     buf->last = ngx_http_v2_write_uint16(buf->last,
  2057.                                          NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING);
  2058.     buf->last = ngx_http_v2_write_uint32(buf->last,
  2059.                                          NGX_HTTP_V2_MAX_FRAME_SIZE);

  2060.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2061.     return NGX_OK;
  2062. }


  2063. static ngx_int_t
  2064. ngx_http_v2_settings_frame_handler(ngx_http_v2_connection_t *h2c,
  2065.     ngx_http_v2_out_frame_t *frame)
  2066. {
  2067.     ngx_buf_t  *buf;

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

  2069.     if (buf->pos != buf->last) {
  2070.         return NGX_AGAIN;
  2071.     }

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

  2073.     return NGX_OK;
  2074. }


  2075. static ngx_int_t
  2076. ngx_http_v2_send_window_update(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2077.     size_t window)
  2078. {
  2079.     ngx_buf_t                *buf;
  2080.     ngx_http_v2_out_frame_t  *frame;

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

  2084.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_WINDOW_UPDATE_SIZE,
  2085.                                   NGX_HTTP_V2_WINDOW_UPDATE_FRAME,
  2086.                                   NGX_HTTP_V2_NO_FLAG, sid);
  2087.     if (frame == NULL) {
  2088.         return NGX_ERROR;
  2089.     }

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

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

  2092.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2093.     return NGX_OK;
  2094. }


  2095. static ngx_int_t
  2096. ngx_http_v2_send_rst_stream(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2097.     ngx_uint_t status)
  2098. {
  2099.     ngx_buf_t                *buf;
  2100.     ngx_http_v2_out_frame_t  *frame;

  2101.     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  2102.                    "http2 send RST_STREAM frame sid:%ui, status:%ui",
  2103.                    sid, status);

  2104.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_RST_STREAM_SIZE,
  2105.                                   NGX_HTTP_V2_RST_STREAM_FRAME,
  2106.                                   NGX_HTTP_V2_NO_FLAG, sid);
  2107.     if (frame == NULL) {
  2108.         return NGX_ERROR;
  2109.     }

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

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

  2112.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2113.     return NGX_OK;
  2114. }


  2115. static ngx_int_t
  2116. ngx_http_v2_send_goaway(ngx_http_v2_connection_t *h2c, ngx_uint_t status)
  2117. {
  2118.     ngx_buf_t                *buf;
  2119.     ngx_http_v2_out_frame_t  *frame;

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

  2123.     frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_GOAWAY_SIZE,
  2124.                                   NGX_HTTP_V2_GOAWAY_FRAME,
  2125.                                   NGX_HTTP_V2_NO_FLAG, 0);
  2126.     if (frame == NULL) {
  2127.         return NGX_ERROR;
  2128.     }

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

  2130.     buf->last = ngx_http_v2_write_sid(buf->last, h2c->last_sid);
  2131.     buf->last = ngx_http_v2_write_uint32(buf->last, status);

  2132.     ngx_http_v2_queue_blocked_frame(h2c, frame);

  2133.     return NGX_OK;
  2134. }


  2135. static ngx_http_v2_out_frame_t *
  2136. ngx_http_v2_get_frame(ngx_http_v2_connection_t *h2c, size_t length,
  2137.     ngx_uint_t type, u_char flags, ngx_uint_t sid)
  2138. {
  2139.     ngx_buf_t                *buf;
  2140.     ngx_pool_t               *pool;
  2141.     ngx_http_v2_out_frame_t  *frame;

  2142.     frame = h2c->free_frames;

  2143.     if (frame) {
  2144.         h2c->free_frames = frame->next;

  2145.         buf = frame->first->buf;
  2146.         buf->pos = buf->start;

  2147.         frame->blocked = 0;

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

  2150.         frame = ngx_pcalloc(pool, sizeof(ngx_http_v2_out_frame_t));
  2151.         if (frame == NULL) {
  2152.             return NULL;
  2153.         }

  2154.         frame->first = ngx_alloc_chain_link(pool);
  2155.         if (frame->first == NULL) {
  2156.             return NULL;
  2157.         }

  2158.         buf = ngx_create_temp_buf(pool, NGX_HTTP_V2_FRAME_BUFFER_SIZE);
  2159.         if (buf == NULL) {
  2160.             return NULL;
  2161.         }

  2162.         buf->last_buf = 1;

  2163.         frame->first->buf = buf;
  2164.         frame->last = frame->first;

  2165.         frame->handler = ngx_http_v2_frame_handler;

  2166.         h2c->frames++;

  2167.     } else {
  2168.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  2169.                       "http2 flood detected");

  2170.         h2c->connection->error = 1;
  2171.         return NULL;
  2172.     }

  2173. #if (NGX_DEBUG)
  2174.     if (length > NGX_HTTP_V2_FRAME_BUFFER_SIZE - NGX_HTTP_V2_FRAME_HEADER_SIZE)
  2175.     {
  2176.         ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
  2177.                       "requested control frame is too large: %uz", length);
  2178.         return NULL;
  2179.     }
  2180. #endif

  2181.     frame->length = length;

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

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

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

  2185.     return frame;
  2186. }


  2187. static ngx_int_t
  2188. ngx_http_v2_frame_handler(ngx_http_v2_connection_t *h2c,
  2189.     ngx_http_v2_out_frame_t *frame)
  2190. {
  2191.     ngx_buf_t  *buf;

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

  2193.     if (buf->pos != buf->last) {
  2194.         return NGX_AGAIN;
  2195.     }

  2196.     frame->next = h2c->free_frames;
  2197.     h2c->free_frames = frame;

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

  2199.     return NGX_OK;
  2200. }


  2201. static ngx_http_v2_stream_t *
  2202. ngx_http_v2_create_stream(ngx_http_v2_connection_t *h2c)
  2203. {
  2204.     ngx_log_t                 *log;
  2205.     ngx_event_t               *rev, *wev;
  2206.     ngx_connection_t          *fc;
  2207.     ngx_http_log_ctx_t        *ctx;
  2208.     ngx_http_request_t        *r;
  2209.     ngx_http_v2_stream_t      *stream;
  2210.     ngx_http_v2_srv_conf_t    *h2scf;
  2211.     ngx_http_core_srv_conf_t  *cscf;

  2212.     fc = h2c->free_fake_connections;

  2213.     if (fc) {
  2214.         h2c->free_fake_connections = fc->data;

  2215.         rev = fc->read;
  2216.         wev = fc->write;
  2217.         log = fc->log;
  2218.         ctx = log->data;

  2219.     } else {
  2220.         fc = ngx_palloc(h2c->pool, sizeof(ngx_connection_t));
  2221.         if (fc == NULL) {
  2222.             return NULL;
  2223.         }

  2224.         rev = ngx_palloc(h2c->pool, sizeof(ngx_event_t));
  2225.         if (rev == NULL) {
  2226.             return NULL;
  2227.         }

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

  2232.         log = ngx_palloc(h2c->pool, sizeof(ngx_log_t));
  2233.         if (log == NULL) {
  2234.             return NULL;
  2235.         }

  2236.         ctx = ngx_palloc(h2c->pool, sizeof(ngx_http_log_ctx_t));
  2237.         if (ctx == NULL) {
  2238.             return NULL;
  2239.         }

  2240.         ctx->connection = fc;
  2241.         ctx->request = NULL;
  2242.         ctx->current_request = NULL;
  2243.     }

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

  2245.     log->data = ctx;
  2246.     log->action = "reading client request headers";

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

  2248.     rev->data = fc;
  2249.     rev->ready = 1;
  2250.     rev->handler = ngx_http_v2_close_stream_handler;
  2251.     rev->log = log;

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

  2253.     wev->write = 1;

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

  2255.     fc->data = h2c->http_connection;
  2256.     fc->read = rev;
  2257.     fc->write = wev;
  2258.     fc->sent = 0;
  2259.     fc->log = log;
  2260.     fc->buffered = 0;
  2261.     fc->sndlowat = 1;
  2262.     fc->tcp_nodelay = NGX_TCP_NODELAY_DISABLED;

  2263.     r = ngx_http_create_request(fc);
  2264.     if (r == NULL) {
  2265.         return NULL;
  2266.     }

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

  2268.     r->http_version = NGX_HTTP_VERSION_20;
  2269.     r->valid_location = 1;

  2270.     fc->data = r;
  2271.     h2c->connection->requests++;

  2272.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

  2273.     r->header_in = ngx_create_temp_buf(r->pool,
  2274.                                        cscf->client_header_buffer_size);
  2275.     if (r->header_in == NULL) {
  2276.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2277.         return NULL;
  2278.     }

  2279.     if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
  2280.                       sizeof(ngx_table_elt_t))
  2281.         != NGX_OK)
  2282.     {
  2283.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2284.         return NULL;
  2285.     }

  2286.     r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;

  2287.     stream = ngx_pcalloc(r->pool, sizeof(ngx_http_v2_stream_t));
  2288.     if (stream == NULL) {
  2289.         ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2290.         return NULL;
  2291.     }

  2292.     r->stream = stream;

  2293.     stream->request = r;
  2294.     stream->connection = h2c;

  2295.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

  2296.     stream->send_window = h2c->init_window;
  2297.     stream->recv_window = h2scf->preread_size;

  2298.     h2c->processing++;

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

  2300.     if (h2c->connection->read->timer_set) {
  2301.         ngx_del_timer(h2c->connection->read);
  2302.     }

  2303.     return stream;
  2304. }


  2305. static ngx_http_v2_node_t *
  2306. ngx_http_v2_get_node_by_id(ngx_http_v2_connection_t *h2c, ngx_uint_t sid,
  2307.     ngx_uint_t alloc)
  2308. {
  2309.     ngx_uint_t               index;
  2310.     ngx_http_v2_node_t      *node;
  2311.     ngx_http_v2_srv_conf_t  *h2scf;

  2312.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2313.                                          ngx_http_v2_module);

  2314.     index = ngx_http_v2_index(h2scf, sid);

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

  2316.         if (node->id == sid) {
  2317.             return node;
  2318.         }
  2319.     }

  2320.     if (!alloc) {
  2321.         return NULL;
  2322.     }

  2323.     if (h2c->closed_nodes < 32) {
  2324.         node = ngx_pcalloc(h2c->connection->pool, sizeof(ngx_http_v2_node_t));
  2325.         if (node == NULL) {
  2326.             return NULL;
  2327.         }

  2328.     } else {
  2329.         node = ngx_http_v2_get_closed_node(h2c);
  2330.     }

  2331.     node->id = sid;

  2332.     ngx_queue_init(&node->children);

  2333.     node->index = h2c->streams_index[index];
  2334.     h2c->streams_index[index] = node;

  2335.     return node;
  2336. }


  2337. static ngx_http_v2_node_t *
  2338. ngx_http_v2_get_closed_node(ngx_http_v2_connection_t *h2c)
  2339. {
  2340.     ngx_uint_t               weight;
  2341.     ngx_queue_t             *q, *children;
  2342.     ngx_http_v2_node_t      *node, **next, *n, *parent, *child;
  2343.     ngx_http_v2_srv_conf_t  *h2scf;

  2344.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  2345.                                          ngx_http_v2_module);

  2346.     h2c->closed_nodes--;

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

  2348.     ngx_queue_remove(q);

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

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

  2351.     for ( ;; ) {
  2352.         n = *next;

  2353.         if (n == node) {
  2354.             *next = n->index;
  2355.             break;
  2356.         }

  2357.         next = &n->index;
  2358.     }

  2359.     ngx_queue_remove(&node->queue);

  2360.     weight = 0;

  2361.     for (q = ngx_queue_head(&node->children);
  2362.          q != ngx_queue_sentinel(&node->children);
  2363.          q = ngx_queue_next(q))
  2364.     {
  2365.         child = ngx_queue_data(q, ngx_http_v2_node_t, queue);
  2366.         weight += child->weight;
  2367.     }

  2368.     parent = node->parent;

  2369.     for (q = ngx_queue_head(&node->children);
  2370.          q != ngx_queue_sentinel(&node->children);
  2371.          q = ngx_queue_next(q))
  2372.     {
  2373.         child = ngx_queue_data(q, ngx_http_v2_node_t, queue);
  2374.         child->parent = parent;
  2375.         child->weight = node->weight * child->weight / weight;

  2376.         if (child->weight == 0) {
  2377.             child->weight = 1;
  2378.         }
  2379.     }

  2380.     if (parent == NGX_HTTP_V2_ROOT) {
  2381.         node->rank = 0;
  2382.         node->rel_weight = 1.0;

  2383.         children = &h2c->dependencies;

  2384.     } else {
  2385.         node->rank = parent->rank;
  2386.         node->rel_weight = parent->rel_weight;

  2387.         children = &parent->children;
  2388.     }

  2389.     ngx_http_v2_node_children_update(node);
  2390.     ngx_queue_add(children, &node->children);

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

  2392.     return node;
  2393. }


  2394. static ngx_int_t
  2395. ngx_http_v2_validate_header(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2396. {
  2397.     u_char                     ch;
  2398.     ngx_uint_t                 i;
  2399.     ngx_http_core_srv_conf_t  *cscf;

  2400.     r->invalid_header = 0;

  2401.     cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

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

  2404.         if ((ch >= 'a' && ch <= 'z')
  2405.             || (ch == '-')
  2406.             || (ch >= '0' && ch <= '9')
  2407.             || (ch == '_' && cscf->underscores_in_headers))
  2408.         {
  2409.             continue;
  2410.         }

  2411.         if (ch <= 0x20 || ch == 0x7f || ch == ':'
  2412.             || (ch >= 'A' && ch <= 'Z'))
  2413.         {
  2414.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2415.                           "client sent invalid header name: \"%V\"",
  2416.                           &header->name);

  2417.             return NGX_ERROR;
  2418.         }

  2419.         r->invalid_header = 1;
  2420.     }

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

  2423.         if (ch == '\0' || ch == LF || ch == CR) {
  2424.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2425.                           "client sent header \"%V\" with "
  2426.                           "invalid value: \"%V\"",
  2427.                           &header->name, &header->value);

  2428.             return NGX_ERROR;
  2429.         }
  2430.     }

  2431.     return NGX_OK;
  2432. }


  2433. static ngx_int_t
  2434. ngx_http_v2_pseudo_header(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2435. {
  2436.     header->name.len--;
  2437.     header->name.data++;

  2438.     switch (header->name.len) {
  2439.     case 4:
  2440.         if (ngx_memcmp(header->name.data, "path", sizeof("path") - 1)
  2441.             == 0)
  2442.         {
  2443.             return ngx_http_v2_parse_path(r, &header->value);
  2444.         }

  2445.         break;

  2446.     case 6:
  2447.         if (ngx_memcmp(header->name.data, "method", sizeof("method") - 1)
  2448.             == 0)
  2449.         {
  2450.             return ngx_http_v2_parse_method(r, &header->value);
  2451.         }

  2452.         if (ngx_memcmp(header->name.data, "scheme", sizeof("scheme") - 1)
  2453.             == 0)
  2454.         {
  2455.             return ngx_http_v2_parse_scheme(r, &header->value);
  2456.         }

  2457.         break;

  2458.     case 9:
  2459.         if (ngx_memcmp(header->name.data, "authority", sizeof("authority") - 1)
  2460.             == 0)
  2461.         {
  2462.             return ngx_http_v2_parse_authority(r, &header->value);
  2463.         }

  2464.         break;
  2465.     }

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

  2469.     return NGX_DECLINED;
  2470. }


  2471. static ngx_int_t
  2472. ngx_http_v2_parse_path(ngx_http_request_t *r, ngx_str_t *value)
  2473. {
  2474.     if (r->unparsed_uri.len) {
  2475.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2476.                       "client sent duplicate :path header");

  2477.         return NGX_DECLINED;
  2478.     }

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

  2482.         return NGX_DECLINED;
  2483.     }

  2484.     r->uri_start = value->data;
  2485.     r->uri_end = value->data + value->len;

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

  2489.         return NGX_DECLINED;
  2490.     }

  2491.     if (ngx_http_process_request_uri(r) != NGX_OK) {
  2492.         /*
  2493.          * request has been finalized already
  2494.          * in ngx_http_process_request_uri()
  2495.          */
  2496.         return NGX_ABORT;
  2497.     }

  2498.     return NGX_OK;
  2499. }


  2500. static ngx_int_t
  2501. ngx_http_v2_parse_method(ngx_http_request_t *r, ngx_str_t *value)
  2502. {
  2503.     size_t         k, len;
  2504.     ngx_uint_t     n;
  2505.     const u_char  *p, *m;

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

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

  2536.         return NGX_DECLINED;
  2537.     }

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

  2541.         return NGX_DECLINED;
  2542.     }

  2543.     r->method_name.len = value->len;
  2544.     r->method_name.data = value->data;

  2545.     len = r->method_name.len;
  2546.     n = sizeof(tests) / sizeof(tests[0]);
  2547.     test = tests;

  2548.     do {
  2549.         if (len == test->len) {
  2550.             p = r->method_name.data;
  2551.             m = test->method;
  2552.             k = len;

  2553.             do {
  2554.                 if (*p++ != *m++) {
  2555.                     goto next;
  2556.                 }
  2557.             } while (--k);

  2558.             r->method = test->value;
  2559.             return NGX_OK;
  2560.         }

  2561.     next:
  2562.         test++;

  2563.     } while (--n);

  2564.     p = r->method_name.data;

  2565.     do {
  2566.         if ((*p < 'A' || *p > 'Z') && *p != '_' && *p != '-') {
  2567.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2568.                           "client sent invalid method: \"%V\"",
  2569.                           &r->method_name);

  2570.             return NGX_DECLINED;
  2571.         }

  2572.         p++;

  2573.     } while (--len);

  2574.     return NGX_OK;
  2575. }


  2576. static ngx_int_t
  2577. ngx_http_v2_parse_scheme(ngx_http_request_t *r, ngx_str_t *value)
  2578. {
  2579.     u_char      c, ch;
  2580.     ngx_uint_t  i;

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

  2584.         return NGX_DECLINED;
  2585.     }

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

  2589.         return NGX_DECLINED;
  2590.     }

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

  2593.         c = (u_char) (ch | 0x20);
  2594.         if (c >= 'a' && c <= 'z') {
  2595.             continue;
  2596.         }

  2597.         if (((ch >= '0' && ch <= '9') || ch == '+' || ch == '-' || ch == '.')
  2598.             && i > 0)
  2599.         {
  2600.             continue;
  2601.         }

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

  2604.         return NGX_DECLINED;
  2605.     }

  2606.     r->schema = *value;

  2607.     return NGX_OK;
  2608. }


  2609. static ngx_int_t
  2610. ngx_http_v2_parse_authority(ngx_http_request_t *r, ngx_str_t *value)
  2611. {
  2612.     ngx_int_t  rc;
  2613.     in_port_t  port;

  2614.     if (r->host_start) {
  2615.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2616.                       "client sent duplicate \":authority\" header");
  2617.         return NGX_DECLINED;
  2618.     }

  2619.     r->host_start = value->data;
  2620.     r->host_end = value->data + value->len;

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

  2622.     if (rc == NGX_DECLINED) {
  2623.         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2624.                       "client sent invalid \":authority\" header");
  2625.         return NGX_DECLINED;
  2626.     }

  2627.     if (rc == NGX_ERROR) {
  2628.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2629.         return NGX_ABORT;
  2630.     }

  2631.     if (ngx_http_set_virtual_server(r, value) == NGX_ERROR) {
  2632.         /*
  2633.          * request has been finalized already
  2634.          * in ngx_http_set_virtual_server()
  2635.          */
  2636.         return NGX_ABORT;
  2637.     }

  2638.     r->headers_in.server = *value;
  2639.     r->port = port;

  2640.     return NGX_OK;
  2641. }


  2642. static ngx_int_t
  2643. ngx_http_v2_construct_request_line(ngx_http_request_t *r)
  2644. {
  2645.     u_char  *p;

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

  2647.     if (r->method_name.len == 0
  2648.         || r->schema.len == 0
  2649.         || r->unparsed_uri.len == 0)
  2650.     {
  2651.         if (r->method_name.len == 0) {
  2652.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2653.                           "client sent no :method header");

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

  2657.         } else {
  2658.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  2659.                           "client sent no :path header");
  2660.         }

  2661.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2662.         return NGX_ERROR;
  2663.     }

  2664.     r->request_line.len = r->method_name.len + 1
  2665.                           + r->unparsed_uri.len
  2666.                           + sizeof(ending) - 1;

  2667.     p = ngx_pnalloc(r->pool, r->request_line.len + 1);
  2668.     if (p == NULL) {
  2669.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2670.         return NGX_ERROR;
  2671.     }

  2672.     r->request_line.data = p;

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

  2674.     *p++ = ' ';

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

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

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

  2679.     return NGX_OK;
  2680. }


  2681. static ngx_int_t
  2682. ngx_http_v2_cookie(ngx_http_request_t *r, ngx_http_v2_header_t *header)
  2683. {
  2684.     ngx_str_t    *val;
  2685.     ngx_array_t  *cookies;

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

  2687.     if (cookies == NULL) {
  2688.         cookies = ngx_array_create(r->pool, 2, sizeof(ngx_str_t));
  2689.         if (cookies == NULL) {
  2690.             return NGX_ERROR;
  2691.         }

  2692.         r->stream->cookies = cookies;
  2693.     }

  2694.     val = ngx_array_push(cookies);
  2695.     if (val == NULL) {
  2696.         return NGX_ERROR;
  2697.     }

  2698.     val->len = header->value.len;
  2699.     val->data = header->value.data;

  2700.     return NGX_OK;
  2701. }


  2702. static ngx_int_t
  2703. ngx_http_v2_construct_cookie_header(ngx_http_request_t *r)
  2704. {
  2705.     u_char                     *buf, *p, *end;
  2706.     size_t                      len;
  2707.     ngx_str_t                  *vals;
  2708.     ngx_uint_t                  i;
  2709.     ngx_array_t                *cookies;
  2710.     ngx_table_elt_t            *h;
  2711.     ngx_http_header_t          *hh;
  2712.     ngx_http_core_main_conf_t  *cmcf;

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

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

  2715.     if (cookies == NULL) {
  2716.         return NGX_OK;
  2717.     }

  2718.     vals = cookies->elts;

  2719.     i = 0;
  2720.     len = 0;

  2721.     do {
  2722.         len += vals[i].len + 2;
  2723.     } while (++i != cookies->nelts);

  2724.     len -= 2;

  2725.     buf = ngx_pnalloc(r->pool, len + 1);
  2726.     if (buf == NULL) {
  2727.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2728.         return NGX_ERROR;
  2729.     }

  2730.     p = buf;
  2731.     end = buf + len;

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

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

  2734.         if (p == end) {
  2735.             *p = '\0';
  2736.             break;
  2737.         }

  2738.         *p++ = ';'; *p++ = ' ';
  2739.     }

  2740.     h = ngx_list_push(&r->headers_in.headers);
  2741.     if (h == NULL) {
  2742.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2743.         return NGX_ERROR;
  2744.     }

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

  2747.     h->key.len = cookie.len;
  2748.     h->key.data = cookie.data;

  2749.     h->value.len = len;
  2750.     h->value.data = buf;

  2751.     h->lowcase_key = cookie.data;

  2752.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  2755.     if (hh == NULL) {
  2756.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2757.         return NGX_ERROR;
  2758.     }

  2759.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  2760.         /*
  2761.          * request has been finalized already
  2762.          * in ngx_http_process_header_line()
  2763.          */
  2764.         return NGX_ERROR;
  2765.     }

  2766.     return NGX_OK;
  2767. }


  2768. static ngx_int_t
  2769. ngx_http_v2_construct_host_header(ngx_http_request_t *r)
  2770. {
  2771.     ngx_table_elt_t            *h;
  2772.     ngx_http_header_t          *hh;
  2773.     ngx_http_core_main_conf_t  *cmcf;

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

  2775.     h = ngx_list_push(&r->headers_in.headers);
  2776.     if (h == NULL) {
  2777.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2778.         return NGX_ERROR;
  2779.     }

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

  2781.     h->key.len = host.len;
  2782.     h->key.data = host.data;

  2783.     h->value.len = r->host_end - r->host_start;
  2784.     h->value.data = r->host_start;

  2785.     h->lowcase_key = host.data;

  2786.     cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);

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

  2789.     if (hh == NULL) {
  2790.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_INTERNAL_SERVER_ERROR);
  2791.         return NGX_ERROR;
  2792.     }

  2793.     if (hh->handler(r, h, hh->offset) != NGX_OK) {
  2794.         /*
  2795.          * request has been finalized already
  2796.          * in ngx_http_process_host()
  2797.          */
  2798.         return NGX_ERROR;
  2799.     }

  2800.     return NGX_OK;
  2801. }


  2802. static void
  2803. ngx_http_v2_run_request(ngx_http_request_t *r)
  2804. {
  2805.     ngx_str_t                  host;
  2806.     ngx_connection_t          *fc;
  2807.     ngx_http_v2_srv_conf_t    *h2scf;
  2808.     ngx_http_v2_connection_t  *h2c;

  2809.     fc = r->connection;

  2810.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);

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

  2815.         ngx_http_finalize_request(r, NGX_HTTP_MISDIRECTED_REQUEST);
  2816.         goto failed;
  2817.     }

  2818.     if (ngx_http_v2_construct_request_line(r) != NGX_OK) {
  2819.         goto failed;
  2820.     }

  2821.     if (ngx_http_v2_construct_cookie_header(r) != NGX_OK) {
  2822.         goto failed;
  2823.     }

  2824.     r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;

  2825.     if (r->headers_in.connection) {
  2826.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2827.                       "client sent \"Connection\" header");
  2828.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2829.         goto failed;
  2830.     }

  2831.     if (r->headers_in.keep_alive) {
  2832.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2833.                       "client sent \"Keep-Alive\" header");
  2834.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2835.         goto failed;
  2836.     }

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

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

  2849.     if (r->headers_in.te
  2850.         && (r->headers_in.te->next
  2851.             || r->headers_in.te->value.len != 8
  2852.             || ngx_strncasecmp(r->headers_in.te->value.data,
  2853.                                (u_char *) "trailers", 8) != 0))
  2854.     {
  2855.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2856.                       "client sent invalid \"TE\" header");
  2857.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2858.         goto failed;
  2859.     }

  2860.     if (r->headers_in.server.len == 0) {
  2861.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2862.                       "client sent neither \":authority\" nor \"Host\" header");
  2863.         ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2864.         goto failed;
  2865.     }

  2866.     if (r->host_end) {

  2867.         host.len = r->host_end - r->host_start;
  2868.         host.data = r->host_start;

  2869.         if (r->headers_in.host) {
  2870.             if (r->headers_in.host->value.len != host.len
  2871.                 || ngx_memcmp(r->headers_in.host->value.data, host.data,
  2872.                               host.len)
  2873.                    != 0)
  2874.             {
  2875.                 ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2876.                               "client sent \":authority\" and \"Host\" headers "
  2877.                               "with different values");
  2878.                 ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2879.                 goto failed;
  2880.             }

  2881.         } else {
  2882.             /* compatibility for $http_host */

  2883.             if (ngx_http_v2_construct_host_header(r) != NGX_OK) {
  2884.                 goto failed;
  2885.             }
  2886.         }
  2887.     }

  2888.     if (r->headers_in.content_length) {
  2889.         r->headers_in.content_length_n =
  2890.                             ngx_atoof(r->headers_in.content_length->value.data,
  2891.                                       r->headers_in.content_length->value.len);

  2892.         if (r->headers_in.content_length_n == NGX_ERROR) {
  2893.             ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  2894.                           "client sent invalid \"Content-Length\" header");
  2895.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2896.             goto failed;
  2897.         }

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

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

  2902.             ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  2903.             goto failed;
  2904.         }

  2905.     } else if (!r->stream->in_closed) {
  2906.         r->headers_in.chunked = 1;
  2907.     }

  2908.     if (r->method == NGX_HTTP_CONNECT) {
  2909.         ngx_log_error(NGX_LOG_INFO, fc->log, 0, "client sent CONNECT method");
  2910.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  2911.         goto failed;
  2912.     }

  2913.     if (r->method == NGX_HTTP_TRACE) {
  2914.         ngx_log_error(NGX_LOG_INFO, fc->log, 0, "client sent TRACE method");
  2915.         ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  2916.         goto failed;
  2917.     }

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

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

  2920.     ngx_http_process_request(r);

  2921. failed:

  2922.     ngx_http_run_posted_requests(fc);
  2923. }


  2924. ngx_int_t
  2925. ngx_http_v2_read_request_body(ngx_http_request_t *r)
  2926. {
  2927.     off_t                      len;
  2928.     size_t                     size;
  2929.     ngx_buf_t                 *buf;
  2930.     ngx_int_t                  rc;
  2931.     ngx_http_v2_stream_t      *stream;
  2932.     ngx_http_v2_srv_conf_t    *h2scf;
  2933.     ngx_http_request_body_t   *rb;
  2934.     ngx_http_core_loc_conf_t  *clcf;
  2935.     ngx_http_v2_connection_t  *h2c;

  2936.     stream = r->stream;
  2937.     rb = r->request_body;

  2938.     if (stream->skip_data) {
  2939.         r->request_body_no_buffering = 0;
  2940.         rb->post_handler(r);
  2941.         return NGX_OK;
  2942.     }

  2943.     rb->rest = 1;

  2944.     /* set rb->filter_need_buffering */

  2945.     rc = ngx_http_top_request_body_filter(r, NULL);

  2946.     if (rc != NGX_OK) {
  2947.         stream->skip_data = 1;
  2948.         return rc;
  2949.     }

  2950.     h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);
  2951.     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  2952.     len = r->headers_in.content_length_n;

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

  2955.     } else {
  2956.         len++;
  2957.     }

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

  2959.         /*
  2960.          * We need a room to store data up to the stream's initial window size,
  2961.          * at least until this window will be exhausted.
  2962.          */

  2963.         if (len < (off_t) h2scf->preread_size) {
  2964.             len = h2scf->preread_size;
  2965.         }

  2966.         if (len > NGX_HTTP_V2_MAX_WINDOW) {
  2967.             len = NGX_HTTP_V2_MAX_WINDOW;
  2968.         }
  2969.     }

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

  2971.     if (rb->buf == NULL) {
  2972.         stream->skip_data = 1;
  2973.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  2974.     }

  2975.     buf = stream->preread;

  2976.     if (stream->in_closed) {
  2977.         if (!rb->filter_need_buffering) {
  2978.             r->request_body_no_buffering = 0;
  2979.         }

  2980.         if (buf) {
  2981.             rc = ngx_http_v2_process_request_body(r, buf->pos,
  2982.                                                   buf->last - buf->pos, 1, 0);
  2983.             ngx_pfree(r->pool, buf->start);

  2984.         } else {
  2985.             rc = ngx_http_v2_process_request_body(r, NULL, 0, 1, 0);
  2986.         }

  2987.         if (rc != NGX_AGAIN) {
  2988.             return rc;
  2989.         }

  2990.         r->read_event_handler = ngx_http_v2_read_client_request_body_handler;
  2991.         r->write_event_handler = ngx_http_request_empty_handler;

  2992.         return NGX_AGAIN;
  2993.     }

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

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

  2998.         if (rc != NGX_OK && rc != NGX_AGAIN) {
  2999.             stream->skip_data = 1;
  3000.             return rc;
  3001.         }
  3002.     }

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

  3005.     } else {
  3006.         stream->no_flow_control = 1;
  3007.         size = NGX_HTTP_V2_MAX_WINDOW - stream->recv_window;
  3008.     }

  3009.     if (size) {
  3010.         if (ngx_http_v2_send_window_update(stream->connection,
  3011.                                            stream->node->id, size)
  3012.             == NGX_ERROR)
  3013.         {
  3014.             stream->skip_data = 1;
  3015.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3016.         }

  3017.         h2c = stream->connection;

  3018.         if (!h2c->blocked) {
  3019.             if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3020.                 stream->skip_data = 1;
  3021.                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3022.             }
  3023.         }

  3024.         stream->recv_window += size;
  3025.     }

  3026.     if (!buf) {
  3027.         ngx_add_timer(r->connection->read, clcf->client_body_timeout);
  3028.     }

  3029.     r->read_event_handler = ngx_http_v2_read_client_request_body_handler;
  3030.     r->write_event_handler = ngx_http_request_empty_handler;

  3031.     return NGX_AGAIN;
  3032. }


  3033. static ngx_int_t
  3034. ngx_http_v2_process_request_body(ngx_http_request_t *r, u_char *pos,
  3035.     size_t size, ngx_uint_t last, ngx_uint_t flush)
  3036. {
  3037.     size_t                     n;
  3038.     ngx_int_t                  rc;
  3039.     ngx_connection_t          *fc;
  3040.     ngx_http_request_body_t   *rb;
  3041.     ngx_http_core_loc_conf_t  *clcf;

  3042.     fc = r->connection;
  3043.     rb = r->request_body;

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

  3046.     if (size == 0 && !last && !flush) {
  3047.         return NGX_AGAIN;
  3048.     }

  3049.     for ( ;; ) {
  3050.         for ( ;; ) {
  3051.             if (rb->buf->last == rb->buf->end && size) {

  3052.                 if (r->request_body_no_buffering) {

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

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

  3056.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3057.                 }

  3058.                 /* update chains */

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

  3061.                 rc = ngx_http_v2_filter_request_body(r);

  3062.                 if (rc != NGX_OK) {
  3063.                     return rc;
  3064.                 }

  3065.                 if (rb->busy != NULL) {
  3066.                     ngx_log_error(NGX_LOG_ALERT, fc->log, 0,
  3067.                                   "busy buffers after request body flush");
  3068.                     return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3069.                 }

  3070.                 rb->buf->pos = rb->buf->start;
  3071.                 rb->buf->last = rb->buf->start;
  3072.             }

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

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

  3075.             if (n > size) {
  3076.                 n = size;
  3077.             }

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

  3080.             if (n > 0) {
  3081.                 rb->buf->last = ngx_cpymem(rb->buf->last, pos, n);
  3082.                 pos += n;
  3083.                 size -= n;
  3084.             }

  3085.             if (size == 0 && last) {
  3086.                 rb->rest = 0;
  3087.             }

  3088.             if (size == 0) {
  3089.                 break;
  3090.             }
  3091.         }

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

  3094.         if (flush) {
  3095.             rc = ngx_http_v2_filter_request_body(r);

  3096.             if (rc != NGX_OK) {
  3097.                 return rc;
  3098.             }
  3099.         }

  3100.         if (rb->rest == 0 && rb->last_saved) {
  3101.             break;
  3102.         }

  3103.         if (size == 0) {
  3104.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  3105.             ngx_add_timer(fc->read, clcf->client_body_timeout);

  3106.             if (!flush) {
  3107.                 ngx_post_event(fc->read, &ngx_posted_events);
  3108.             }

  3109.             return NGX_AGAIN;
  3110.         }
  3111.     }

  3112.     if (fc->read->timer_set) {
  3113.         ngx_del_timer(fc->read);
  3114.     }

  3115.     if (r->request_body_no_buffering) {
  3116.         if (!flush) {
  3117.             ngx_post_event(fc->read, &ngx_posted_events);
  3118.         }

  3119.         return NGX_OK;
  3120.     }

  3121.     if (r->headers_in.chunked) {
  3122.         r->headers_in.content_length_n = rb->received;
  3123.     }

  3124.     r->read_event_handler = ngx_http_block_reading;
  3125.     rb->post_handler(r);

  3126.     return NGX_OK;
  3127. }


  3128. static ngx_int_t
  3129. ngx_http_v2_filter_request_body(ngx_http_request_t *r)
  3130. {
  3131.     ngx_buf_t                 *b, *buf;
  3132.     ngx_int_t                  rc;
  3133.     ngx_chain_t               *cl;
  3134.     ngx_http_request_body_t   *rb;
  3135.     ngx_http_core_loc_conf_t  *clcf;

  3136.     rb = r->request_body;
  3137.     buf = rb->buf;

  3138.     if (buf->pos == buf->last && (rb->rest || rb->last_sent)) {
  3139.         cl = NULL;
  3140.         goto update;
  3141.     }

  3142.     cl = ngx_chain_get_free_buf(r->pool, &rb->free);
  3143.     if (cl == NULL) {
  3144.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3145.     }

  3146.     b = cl->buf;

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

  3148.     if (buf->pos != buf->last) {
  3149.         r->request_length += buf->last - buf->pos;
  3150.         rb->received += buf->last - buf->pos;

  3151.         if (r->headers_in.content_length_n != -1) {
  3152.             if (rb->received > r->headers_in.content_length_n) {
  3153.                 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  3154.                               "client intended to send body data "
  3155.                               "larger than declared");

  3156.                 return NGX_HTTP_BAD_REQUEST;
  3157.             }

  3158.         } else {
  3159.             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);

  3160.             if (clcf->client_max_body_size
  3161.                 && rb->received > clcf->client_max_body_size)
  3162.             {
  3163.                 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  3164.                               "client intended to send too large chunked body: "
  3165.                               "%O bytes", rb->received);

  3166.                 return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
  3167.             }
  3168.         }

  3169.         b->temporary = 1;
  3170.         b->pos = buf->pos;
  3171.         b->last = buf->last;
  3172.         b->start = b->pos;
  3173.         b->end = b->last;

  3174.         buf->pos = buf->last;
  3175.     }

  3176.     if (!rb->rest) {
  3177.         if (r->headers_in.content_length_n != -1
  3178.             && r->headers_in.content_length_n != rb->received)
  3179.         {
  3180.             ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  3181.                           "client prematurely closed stream: "
  3182.                           "only %O out of %O bytes of request body received",
  3183.                           rb->received, r->headers_in.content_length_n);

  3184.             return NGX_HTTP_BAD_REQUEST;
  3185.         }

  3186.         b->last_buf = 1;
  3187.         rb->last_sent = 1;
  3188.     }

  3189.     b->tag = (ngx_buf_tag_t) &ngx_http_v2_filter_request_body;
  3190.     b->flush = r->request_body_no_buffering;

  3191. update:

  3192.     rc = ngx_http_top_request_body_filter(r, cl);

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

  3195.     return rc;
  3196. }


  3197. static void
  3198. ngx_http_v2_read_client_request_body_handler(ngx_http_request_t *r)
  3199. {
  3200.     size_t                     window;
  3201.     ngx_buf_t                 *buf;
  3202.     ngx_int_t                  rc;
  3203.     ngx_connection_t          *fc;
  3204.     ngx_http_v2_stream_t      *stream;
  3205.     ngx_http_v2_connection_t  *h2c;

  3206.     fc = r->connection;

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

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

  3211.         fc->timedout = 1;
  3212.         r->stream->skip_data = 1;

  3213.         ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  3214.         return;
  3215.     }

  3216.     if (fc->error) {
  3217.         ngx_log_error(NGX_LOG_INFO, fc->log, 0,
  3218.                       "client prematurely closed stream");

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

  3220.         ngx_http_finalize_request(r, NGX_HTTP_CLIENT_CLOSED_REQUEST);
  3221.         return;
  3222.     }

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

  3224.     if (rc != NGX_OK && rc != NGX_AGAIN) {
  3225.         r->stream->skip_data = 1;
  3226.         ngx_http_finalize_request(r, rc);
  3227.         return;
  3228.     }

  3229.     if (rc == NGX_OK) {
  3230.         return;
  3231.     }

  3232.     if (r->stream->no_flow_control) {
  3233.         return;
  3234.     }

  3235.     if (r->request_body->rest == 0) {
  3236.         return;
  3237.     }

  3238.     if (r->request_body->busy != NULL) {
  3239.         return;
  3240.     }

  3241.     stream = r->stream;
  3242.     h2c = stream->connection;

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

  3244.     buf->pos = buf->start;
  3245.     buf->last = buf->start;

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

  3247.     if (h2c->state.stream == stream) {
  3248.         window -= h2c->state.length;
  3249.     }

  3250.     if (window <= stream->recv_window) {
  3251.         if (window < stream->recv_window) {
  3252.             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  3253.                           "http2 negative window update");

  3254.             stream->skip_data = 1;

  3255.             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3256.             return;
  3257.         }

  3258.         return;
  3259.     }

  3260.     if (ngx_http_v2_send_window_update(h2c, stream->node->id,
  3261.                                        window - stream->recv_window)
  3262.         == NGX_ERROR)
  3263.     {
  3264.         stream->skip_data = 1;
  3265.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3266.         return;
  3267.     }

  3268.     stream->recv_window = window;

  3269.     if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3270.         stream->skip_data = 1;
  3271.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  3272.         return;
  3273.     }
  3274. }


  3275. ngx_int_t
  3276. ngx_http_v2_read_unbuffered_request_body(ngx_http_request_t *r)
  3277. {
  3278.     size_t                     window;
  3279.     ngx_buf_t                 *buf;
  3280.     ngx_int_t                  rc;
  3281.     ngx_connection_t          *fc;
  3282.     ngx_http_v2_stream_t      *stream;
  3283.     ngx_http_v2_connection_t  *h2c;

  3284.     stream = r->stream;
  3285.     fc = r->connection;

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

  3288.     if (fc->read->timedout) {
  3289.         if (stream->recv_window) {
  3290.             stream->skip_data = 1;
  3291.             fc->timedout = 1;

  3292.             return NGX_HTTP_REQUEST_TIME_OUT;
  3293.         }

  3294.         fc->read->timedout = 0;
  3295.     }

  3296.     if (fc->error) {
  3297.         stream->skip_data = 1;
  3298.         return NGX_HTTP_BAD_REQUEST;
  3299.     }

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

  3301.     if (rc != NGX_OK && rc != NGX_AGAIN) {
  3302.         stream->skip_data = 1;
  3303.         return rc;
  3304.     }

  3305.     if (rc == NGX_OK) {
  3306.         return NGX_OK;
  3307.     }

  3308.     if (r->request_body->rest == 0) {
  3309.         return NGX_AGAIN;
  3310.     }

  3311.     if (r->request_body->busy != NULL) {
  3312.         return NGX_AGAIN;
  3313.     }

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

  3315.     buf->pos = buf->start;
  3316.     buf->last = buf->start;

  3317.     window = buf->end - buf->start;
  3318.     h2c = stream->connection;

  3319.     if (h2c->state.stream == stream) {
  3320.         window -= h2c->state.length;
  3321.     }

  3322.     if (window <= stream->recv_window) {
  3323.         if (window < stream->recv_window) {
  3324.             ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
  3325.                           "http2 negative window update");
  3326.             stream->skip_data = 1;
  3327.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3328.         }

  3329.         return NGX_AGAIN;
  3330.     }

  3331.     if (ngx_http_v2_send_window_update(h2c, stream->node->id,
  3332.                                        window - stream->recv_window)
  3333.         == NGX_ERROR)
  3334.     {
  3335.         stream->skip_data = 1;
  3336.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3337.     }

  3338.     if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3339.         stream->skip_data = 1;
  3340.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  3341.     }

  3342.     stream->recv_window = window;

  3343.     return NGX_AGAIN;
  3344. }


  3345. static ngx_int_t
  3346. ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
  3347.     ngx_http_v2_stream_t *stream, ngx_uint_t status)
  3348. {
  3349.     ngx_event_t       *rev;
  3350.     ngx_connection_t  *fc;

  3351.     if (stream->rst_sent) {
  3352.         return NGX_OK;
  3353.     }

  3354.     if (ngx_http_v2_send_rst_stream(h2c, stream->node->id, status)
  3355.         == NGX_ERROR)
  3356.     {
  3357.         return NGX_ERROR;
  3358.     }

  3359.     stream->rst_sent = 1;
  3360.     stream->skip_data = 1;

  3361.     fc = stream->request->connection;
  3362.     fc->error = 1;

  3363.     rev = fc->read;
  3364.     rev->handler(rev);

  3365.     return NGX_OK;
  3366. }


  3367. void
  3368. ngx_http_v2_close_stream(ngx_http_v2_stream_t *stream, ngx_int_t rc)
  3369. {
  3370.     ngx_pool_t                *pool;
  3371.     ngx_event_t               *ev;
  3372.     ngx_connection_t          *fc;
  3373.     ngx_http_v2_node_t        *node;
  3374.     ngx_http_v2_connection_t  *h2c;

  3375.     h2c = stream->connection;
  3376.     node = stream->node;

  3377.     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  3378.                    "http2 close stream %ui, queued %ui, processing %ui",
  3379.                    node->id, stream->queued, h2c->processing);

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

  3381.     if (stream->queued) {
  3382.         fc->error = 1;
  3383.         fc->write->handler = ngx_http_v2_retry_close_stream_handler;
  3384.         fc->read->handler = ngx_http_v2_retry_close_stream_handler;
  3385.         return;
  3386.     }

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

  3388.         if (!stream->out_closed) {
  3389.             if (ngx_http_v2_send_rst_stream(h2c, node->id,
  3390.                                       fc->timedout ? NGX_HTTP_V2_PROTOCOL_ERROR
  3391.                                                    : NGX_HTTP_V2_INTERNAL_ERROR)
  3392.                 != NGX_OK)
  3393.             {
  3394.                 h2c->connection->error = 1;
  3395.             }

  3396.         } else if (!stream->in_closed) {
  3397.             if (ngx_http_v2_send_rst_stream(h2c, node->id, NGX_HTTP_V2_NO_ERROR)
  3398.                 != NGX_OK)
  3399.             {
  3400.                 h2c->connection->error = 1;
  3401.             }
  3402.         }
  3403.     }

  3404.     if (h2c->state.stream == stream) {
  3405.         h2c->state.stream = NULL;
  3406.     }

  3407.     node->stream = NULL;

  3408.     ngx_queue_insert_tail(&h2c->closed, &node->reuse);
  3409.     h2c->closed_nodes++;

  3410.     /*
  3411.      * This pool keeps decoded request headers which can be used by log phase
  3412.      * handlers in ngx_http_free_request().
  3413.      *
  3414.      * The pointer is stored into local variable because the stream object
  3415.      * will be destroyed after a call to ngx_http_free_request().
  3416.      */
  3417.     pool = stream->pool;

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

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

  3420.     if (pool != h2c->state.pool) {
  3421.         ngx_destroy_pool(pool);

  3422.     } else {
  3423.         /* pool will be destroyed when the complete header is parsed */
  3424.         h2c->state.keep_pool = 0;
  3425.     }

  3426.     ev = fc->read;

  3427.     if (ev->timer_set) {
  3428.         ngx_del_timer(ev);
  3429.     }

  3430.     if (ev->posted) {
  3431.         ngx_delete_posted_event(ev);
  3432.     }

  3433.     ev = fc->write;

  3434.     if (ev->timer_set) {
  3435.         ngx_del_timer(ev);
  3436.     }

  3437.     if (ev->posted) {
  3438.         ngx_delete_posted_event(ev);
  3439.     }

  3440.     fc->data = h2c->free_fake_connections;
  3441.     h2c->free_fake_connections = fc;

  3442.     h2c->processing--;

  3443.     if (h2c->processing || h2c->blocked) {
  3444.         return;
  3445.     }

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

  3447.     ev->handler = ngx_http_v2_handle_connection_handler;
  3448.     ngx_post_event(ev, &ngx_posted_events);
  3449. }


  3450. static void
  3451. ngx_http_v2_close_stream_handler(ngx_event_t *ev)
  3452. {
  3453.     ngx_connection_t    *fc;
  3454.     ngx_http_request_t  *r;

  3455.     fc = ev->data;
  3456.     r = fc->data;

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

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

  3461.         fc->timedout = 1;

  3462.         ngx_http_v2_close_stream(r->stream, NGX_HTTP_REQUEST_TIME_OUT);
  3463.         return;
  3464.     }

  3465.     ngx_http_v2_close_stream(r->stream, 0);
  3466. }


  3467. static void
  3468. ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev)
  3469. {
  3470.     ngx_connection_t    *fc;
  3471.     ngx_http_request_t  *r;

  3472.     fc = ev->data;
  3473.     r = fc->data;

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

  3476.     ngx_http_v2_close_stream(r->stream, 0);
  3477. }


  3478. static void
  3479. ngx_http_v2_handle_connection_handler(ngx_event_t *rev)
  3480. {
  3481.     ngx_connection_t          *c;
  3482.     ngx_http_v2_connection_t  *h2c;

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

  3485.     c = rev->data;
  3486.     h2c = c->data;

  3487.     if (c->error) {
  3488.         ngx_http_v2_finalize_connection(h2c, 0);
  3489.         return;
  3490.     }

  3491.     rev->handler = ngx_http_v2_read_handler;

  3492.     if (rev->ready) {
  3493.         ngx_http_v2_read_handler(rev);
  3494.         return;
  3495.     }

  3496.     if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
  3497.         ngx_http_v2_finalize_connection(h2c, 0);
  3498.         return;
  3499.     }

  3500.     ngx_http_v2_handle_connection(c->data);
  3501. }


  3502. static void
  3503. ngx_http_v2_idle_handler(ngx_event_t *rev)
  3504. {
  3505.     ngx_connection_t          *c;
  3506.     ngx_http_v2_srv_conf_t    *h2scf;
  3507.     ngx_http_v2_connection_t  *h2c;
  3508.     ngx_http_core_loc_conf_t  *clcf;

  3509.     c = rev->data;
  3510.     h2c = c->data;

  3511.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 idle handler");

  3512.     if (rev->timedout || c->close) {
  3513.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  3514.         return;
  3515.     }

  3516. #if (NGX_HAVE_KQUEUE)

  3517.     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
  3518.         if (rev->pending_eof) {
  3519.             c->log->handler = NULL;
  3520.             ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
  3521.                           "kevent() reported that client %V closed "
  3522.                           "idle connection", &c->addr_text);
  3523. #if (NGX_HTTP_SSL)
  3524.             if (c->ssl) {
  3525.                 c->ssl->no_send_shutdown = 1;
  3526.             }
  3527. #endif
  3528.             ngx_http_close_connection(c);
  3529.             return;
  3530.         }
  3531.     }

  3532. #endif

  3533.     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
  3534.                                         ngx_http_core_module);

  3535.     if (h2c->idle++ > 10 * clcf->keepalive_requests) {
  3536.         ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
  3537.                       "http2 flood detected");
  3538.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
  3539.         return;
  3540.     }

  3541.     c->destroyed = 0;
  3542.     ngx_reusable_connection(c, 0);

  3543.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3544.                                          ngx_http_v2_module);

  3545.     h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
  3546.     if (h2c->pool == NULL) {
  3547.         ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
  3548.         return;
  3549.     }

  3550.     c->write->handler = ngx_http_v2_write_handler;

  3551.     rev->handler = ngx_http_v2_read_handler;
  3552.     ngx_http_v2_read_handler(rev);
  3553. }


  3554. static void
  3555. ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
  3556.     ngx_uint_t status)
  3557. {
  3558.     ngx_uint_t               i, size;
  3559.     ngx_event_t             *ev;
  3560.     ngx_connection_t        *c, *fc;
  3561.     ngx_http_request_t      *r;
  3562.     ngx_http_v2_node_t      *node;
  3563.     ngx_http_v2_stream_t    *stream;
  3564.     ngx_http_v2_srv_conf_t  *h2scf;

  3565.     c = h2c->connection;

  3566.     h2c->blocked = 1;

  3567.     if (!c->error && !h2c->goaway) {
  3568.         h2c->goaway = 1;

  3569.         if (ngx_http_v2_send_goaway(h2c, status) != NGX_ERROR) {
  3570.             (void) ngx_http_v2_send_output_queue(h2c);
  3571.         }
  3572.     }

  3573.     if (!h2c->processing) {
  3574.         goto done;
  3575.     }

  3576.     c->read->handler = ngx_http_empty_handler;
  3577.     c->write->handler = ngx_http_empty_handler;

  3578.     h2c->last_out = NULL;

  3579.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3580.                                          ngx_http_v2_module);

  3581.     size = ngx_http_v2_index_size(h2scf);

  3582.     for (i = 0; i < size; i++) {

  3583.         for (node = h2c->streams_index[i]; node; node = node->index) {
  3584.             stream = node->stream;

  3585.             if (stream == NULL) {
  3586.                 continue;
  3587.             }

  3588.             stream->waiting = 0;

  3589.             r = stream->request;
  3590.             fc = r->connection;

  3591.             fc->error = 1;

  3592.             if (stream->queued) {
  3593.                 stream->queued = 0;

  3594.                 ev = fc->write;
  3595.                 ev->active = 0;
  3596.                 ev->ready = 1;

  3597.             } else {
  3598.                 ev = fc->read;
  3599.             }

  3600.             ev->eof = 1;
  3601.             ev->handler(ev);
  3602.         }
  3603.     }

  3604.     h2c->blocked = 0;

  3605.     if (h2c->processing) {
  3606.         c->error = 1;
  3607.         return;
  3608.     }

  3609. done:

  3610.     if (c->error) {
  3611.         ngx_http_close_connection(c);
  3612.         return;
  3613.     }

  3614.     ngx_http_v2_lingering_close(c);
  3615. }


  3616. static ngx_int_t
  3617. ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c, ssize_t delta)
  3618. {
  3619.     ngx_uint_t               i, size;
  3620.     ngx_event_t             *wev;
  3621.     ngx_http_v2_node_t      *node;
  3622.     ngx_http_v2_stream_t    *stream;
  3623.     ngx_http_v2_srv_conf_t  *h2scf;

  3624.     h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
  3625.                                          ngx_http_v2_module);

  3626.     size = ngx_http_v2_index_size(h2scf);

  3627.     for (i = 0; i < size; i++) {

  3628.         for (node = h2c->streams_index[i]; node; node = node->index) {
  3629.             stream = node->stream;

  3630.             if (stream == NULL) {
  3631.                 continue;
  3632.             }

  3633.             if (delta > 0
  3634.                 && stream->send_window
  3635.                       > (ssize_t) (NGX_HTTP_V2_MAX_WINDOW - delta))
  3636.             {
  3637.                 if (ngx_http_v2_terminate_stream(h2c, stream,
  3638.                                                  NGX_HTTP_V2_FLOW_CTRL_ERROR)
  3639.                     == NGX_ERROR)
  3640.                 {
  3641.                     return NGX_ERROR;
  3642.                 }

  3643.                 continue;
  3644.             }

  3645.             stream->send_window += delta;

  3646.             ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
  3647.                            "http2:%ui adjusted window: %z",
  3648.                            node->id, stream->send_window);

  3649.             if (stream->send_window > 0 && stream->exhausted) {
  3650.                 stream->exhausted = 0;

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

  3652.                 wev->active = 0;
  3653.                 wev->ready = 1;

  3654.                 if (!wev->delayed) {
  3655.                     wev->handler(wev);
  3656.                 }
  3657.             }
  3658.         }
  3659.     }

  3660.     return NGX_OK;
  3661. }


  3662. static void
  3663. ngx_http_v2_set_dependency(ngx_http_v2_connection_t *h2c,
  3664.     ngx_http_v2_node_t *node, ngx_uint_t depend, ngx_uint_t exclusive)
  3665. {
  3666.     ngx_queue_t         *children, *q;
  3667.     ngx_http_v2_node_t  *parent, *child, *next;

  3668.     parent = depend ? ngx_http_v2_get_node_by_id(h2c, depend, 0) : NULL;

  3669.     if (parent == NULL) {
  3670.         parent = NGX_HTTP_V2_ROOT;

  3671.         if (depend != 0) {
  3672.             exclusive = 0;
  3673.         }

  3674.         node->rank = 1;
  3675.         node->rel_weight = (1.0 / 256) * node->weight;

  3676.         children = &h2c->dependencies;

  3677.     } else {
  3678.         if (node->parent != NULL) {

  3679.             for (next = parent->parent;
  3680.                  next != NGX_HTTP_V2_ROOT && next->rank >= node->rank;
  3681.                  next = next->parent)
  3682.             {
  3683.                 if (next != node) {
  3684.                     continue;
  3685.                 }

  3686.                 ngx_queue_remove(&parent->queue);
  3687.                 ngx_queue_insert_after(&node->queue, &parent->queue);

  3688.                 parent->parent = node->parent;

  3689.                 if (node->parent == NGX_HTTP_V2_ROOT) {
  3690.                     parent->rank = 1;
  3691.                     parent->rel_weight = (1.0 / 256) * parent->weight;

  3692.                 } else {
  3693.                     parent->rank = node->parent->rank + 1;
  3694.                     parent->rel_weight = (node->parent->rel_weight / 256)
  3695.                                          * parent->weight;
  3696.                 }

  3697.                 if (!exclusive) {
  3698.                     ngx_http_v2_node_children_update(parent);
  3699.                 }

  3700.                 break;
  3701.             }
  3702.         }

  3703.         node->rank = parent->rank + 1;
  3704.         node->rel_weight = (parent->rel_weight / 256) * node->weight;

  3705.         if (parent->stream == NULL) {
  3706.             ngx_queue_remove(&parent->reuse);
  3707.             ngx_queue_insert_tail(&h2c->closed, &parent->reuse);
  3708.         }

  3709.         children = &parent->children;
  3710.     }

  3711.     if (exclusive) {
  3712.         for (q = ngx_queue_head(children);
  3713.              q != ngx_queue_sentinel(children);
  3714.              q = ngx_queue_next(q))
  3715.         {
  3716.             child = ngx_queue_data(q, ngx_http_v2_node_t, queue);
  3717.             child->parent = node;
  3718.         }

  3719.         ngx_queue_add(&node->children, children);
  3720.         ngx_queue_init(children);
  3721.     }

  3722.     if (node->parent != NULL) {
  3723.         ngx_queue_remove(&node->queue);
  3724.     }

  3725.     ngx_queue_insert_tail(children, &node->queue);

  3726.     node->parent = parent;

  3727.     ngx_http_v2_node_children_update(node);
  3728. }


  3729. static void
  3730. ngx_http_v2_node_children_update(ngx_http_v2_node_t *node)
  3731. {
  3732.     ngx_queue_t         *q;
  3733.     ngx_http_v2_node_t  *child;

  3734.     for (q = ngx_queue_head(&node->children);
  3735.          q != ngx_queue_sentinel(&node->children);
  3736.          q = ngx_queue_next(q))
  3737.     {
  3738.         child = ngx_queue_data(q, ngx_http_v2_node_t, queue);

  3739.         child->rank = node->rank + 1;
  3740.         child->rel_weight = (node->rel_weight / 256) * child->weight;

  3741.         ngx_http_v2_node_children_update(child);
  3742.     }
  3743. }


  3744. static void
  3745. ngx_http_v2_pool_cleanup(void *data)
  3746. {
  3747.     ngx_http_v2_connection_t  *h2c = data;

  3748.     if (h2c->state.pool) {
  3749.         ngx_destroy_pool(h2c->state.pool);
  3750.     }

  3751.     if (h2c->pool) {
  3752.         ngx_destroy_pool(h2c->pool);
  3753.     }
  3754. }