blob: a5952849468180da28355e084004fb565d7000de [file] [log] [blame]
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_http_v2_module.h>
typedef struct {
ngx_str_t name;
ngx_uint_t offset;
ngx_uint_t hash;
ngx_http_header_t *hh;
} ngx_http_v2_parse_header_t;
/* errors */
#define NGX_HTTP_V2_NO_ERROR 0x0
#define NGX_HTTP_V2_PROTOCOL_ERROR 0x1
#define NGX_HTTP_V2_INTERNAL_ERROR 0x2
#define NGX_HTTP_V2_FLOW_CTRL_ERROR 0x3
#define NGX_HTTP_V2_SETTINGS_TIMEOUT 0x4
#define NGX_HTTP_V2_STREAM_CLOSED 0x5
#define NGX_HTTP_V2_SIZE_ERROR 0x6
#define NGX_HTTP_V2_REFUSED_STREAM 0x7
#define NGX_HTTP_V2_CANCEL 0x8
#define NGX_HTTP_V2_COMP_ERROR 0x9
#define NGX_HTTP_V2_CONNECT_ERROR 0xa
#define NGX_HTTP_V2_ENHANCE_YOUR_CALM 0xb
#define NGX_HTTP_V2_INADEQUATE_SECURITY 0xc
#define NGX_HTTP_V2_HTTP_1_1_REQUIRED 0xd
/* frame sizes */
#define NGX_HTTP_V2_SETTINGS_ACK_SIZE 0
#define NGX_HTTP_V2_RST_STREAM_SIZE 4
#define NGX_HTTP_V2_PRIORITY_SIZE 5
#define NGX_HTTP_V2_PING_SIZE 8
#define NGX_HTTP_V2_GOAWAY_SIZE 8
#define NGX_HTTP_V2_WINDOW_UPDATE_SIZE 4
#define NGX_HTTP_V2_SETTINGS_PARAM_SIZE 6
/* settings fields */
#define NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING 0x1
#define NGX_HTTP_V2_ENABLE_PUSH_SETTING 0x2
#define NGX_HTTP_V2_MAX_STREAMS_SETTING 0x3
#define NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING 0x4
#define NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING 0x5
#define NGX_HTTP_V2_FRAME_BUFFER_SIZE 24
#define NGX_HTTP_V2_ROOT (void *) -1
static void ngx_http_v2_read_handler(ngx_event_t *rev);
static void ngx_http_v2_write_handler(ngx_event_t *wev);
static void ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c);
static void ngx_http_v2_lingering_close(ngx_connection_t *c);
static void ngx_http_v2_lingering_close_handler(ngx_event_t *rev);
static u_char *ngx_http_v2_state_proxy_protocol(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_preface(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_preface_end(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_head(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_read_data(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_header_block(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_len(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_raw(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_field_skip(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_handle_continuation(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
static u_char *ngx_http_v2_state_priority(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_rst_stream(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end);
static u_char *ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
static u_char *ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c,
u_char *pos, u_char *end, ngx_http_v2_handler_pt handler);
static u_char *ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
ngx_uint_t err);
static ngx_int_t ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c,
u_char **pos, u_char *end, ngx_uint_t prefix);
static ngx_http_v2_stream_t *ngx_http_v2_create_stream(
ngx_http_v2_connection_t *h2c, ngx_uint_t push);
static ngx_http_v2_node_t *ngx_http_v2_get_node_by_id(
ngx_http_v2_connection_t *h2c, ngx_uint_t sid, ngx_uint_t alloc);
static ngx_http_v2_node_t *ngx_http_v2_get_closed_node(
ngx_http_v2_connection_t *h2c);
#define ngx_http_v2_index_size(h2scf) (h2scf->streams_index_mask + 1)
#define ngx_http_v2_index(h2scf, sid) ((sid >> 1) & h2scf->streams_index_mask)
static ngx_int_t ngx_http_v2_send_settings(ngx_http_v2_connection_t *h2c);
static ngx_int_t ngx_http_v2_settings_frame_handler(
ngx_http_v2_connection_t *h2c, ngx_http_v2_out_frame_t *frame);
static ngx_int_t ngx_http_v2_send_window_update(ngx_http_v2_connection_t *h2c,
ngx_uint_t sid, size_t window);
static ngx_int_t ngx_http_v2_send_rst_stream(ngx_http_v2_connection_t *h2c,
ngx_uint_t sid, ngx_uint_t status);
static ngx_int_t ngx_http_v2_send_goaway(ngx_http_v2_connection_t *h2c,
ngx_uint_t status);
static ngx_http_v2_out_frame_t *ngx_http_v2_get_frame(
ngx_http_v2_connection_t *h2c, size_t length, ngx_uint_t type,
u_char flags, ngx_uint_t sid);
static ngx_int_t ngx_http_v2_frame_handler(ngx_http_v2_connection_t *h2c,
ngx_http_v2_out_frame_t *frame);
static ngx_int_t ngx_http_v2_validate_header(ngx_http_request_t *r,
ngx_http_v2_header_t *header);
static ngx_int_t ngx_http_v2_pseudo_header(ngx_http_request_t *r,
ngx_http_v2_header_t *header);
static ngx_int_t ngx_http_v2_parse_path(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_method(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_scheme(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_authority(ngx_http_request_t *r,
ngx_str_t *value);
static ngx_int_t ngx_http_v2_parse_header(ngx_http_request_t *r,
ngx_http_v2_parse_header_t *header, ngx_str_t *value);
static ngx_int_t ngx_http_v2_construct_request_line(ngx_http_request_t *r);
static ngx_int_t ngx_http_v2_cookie(ngx_http_request_t *r,
ngx_http_v2_header_t *header);
static ngx_int_t ngx_http_v2_construct_cookie_header(ngx_http_request_t *r);
static void ngx_http_v2_run_request(ngx_http_request_t *r);
static void ngx_http_v2_run_request_handler(ngx_event_t *ev);
static ngx_int_t ngx_http_v2_process_request_body(ngx_http_request_t *r,
u_char *pos, size_t size, ngx_uint_t last);
static ngx_int_t ngx_http_v2_filter_request_body(ngx_http_request_t *r);
static void ngx_http_v2_read_client_request_body_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
ngx_http_v2_stream_t *stream, ngx_uint_t status);
static void ngx_http_v2_close_stream_handler(ngx_event_t *ev);
static void ngx_http_v2_retry_close_stream_handler(ngx_event_t *ev);
static void ngx_http_v2_handle_connection_handler(ngx_event_t *rev);
static void ngx_http_v2_idle_handler(ngx_event_t *rev);
static void ngx_http_v2_finalize_connection(ngx_http_v2_connection_t *h2c,
ngx_uint_t status);
static ngx_int_t ngx_http_v2_adjust_windows(ngx_http_v2_connection_t *h2c,
ssize_t delta);
static void ngx_http_v2_set_dependency(ngx_http_v2_connection_t *h2c,
ngx_http_v2_node_t *node, ngx_uint_t depend, ngx_uint_t exclusive);
static void ngx_http_v2_node_children_update(ngx_http_v2_node_t *node);
static void ngx_http_v2_pool_cleanup(void *data);
static ngx_http_v2_handler_pt ngx_http_v2_frame_states[] = {
ngx_http_v2_state_data, /* NGX_HTTP_V2_DATA_FRAME */
ngx_http_v2_state_headers, /* NGX_HTTP_V2_HEADERS_FRAME */
ngx_http_v2_state_priority, /* NGX_HTTP_V2_PRIORITY_FRAME */
ngx_http_v2_state_rst_stream, /* NGX_HTTP_V2_RST_STREAM_FRAME */
ngx_http_v2_state_settings, /* NGX_HTTP_V2_SETTINGS_FRAME */
ngx_http_v2_state_push_promise, /* NGX_HTTP_V2_PUSH_PROMISE_FRAME */
ngx_http_v2_state_ping, /* NGX_HTTP_V2_PING_FRAME */
ngx_http_v2_state_goaway, /* NGX_HTTP_V2_GOAWAY_FRAME */
ngx_http_v2_state_window_update, /* NGX_HTTP_V2_WINDOW_UPDATE_FRAME */
ngx_http_v2_state_continuation /* NGX_HTTP_V2_CONTINUATION_FRAME */
};
#define NGX_HTTP_V2_FRAME_STATES \
(sizeof(ngx_http_v2_frame_states) / sizeof(ngx_http_v2_handler_pt))
static ngx_http_v2_parse_header_t ngx_http_v2_parse_headers[] = {
{ ngx_string("host"),
offsetof(ngx_http_headers_in_t, host), 0, NULL },
{ ngx_string("accept-encoding"),
offsetof(ngx_http_headers_in_t, accept_encoding), 0, NULL },
{ ngx_string("accept-language"),
offsetof(ngx_http_headers_in_t, accept_language), 0, NULL },
{ ngx_string("user-agent"),
offsetof(ngx_http_headers_in_t, user_agent), 0, NULL },
{ ngx_null_string, 0, 0, NULL }
};
void
ngx_http_v2_init(ngx_event_t *rev)
{
ngx_connection_t *c;
ngx_pool_cleanup_t *cln;
ngx_http_connection_t *hc;
ngx_http_v2_srv_conf_t *h2scf;
ngx_http_v2_main_conf_t *h2mcf;
ngx_http_v2_connection_t *h2c;
c = rev->data;
hc = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "init http2 connection");
c->log->action = "processing HTTP/2 connection";
h2mcf = ngx_http_get_module_main_conf(hc->conf_ctx, ngx_http_v2_module);
if (h2mcf->recv_buffer == NULL) {
h2mcf->recv_buffer = ngx_palloc(ngx_cycle->pool,
h2mcf->recv_buffer_size);
if (h2mcf->recv_buffer == NULL) {
ngx_http_close_connection(c);
return;
}
}
h2c = ngx_pcalloc(c->pool, sizeof(ngx_http_v2_connection_t));
if (h2c == NULL) {
ngx_http_close_connection(c);
return;
}
h2c->connection = c;
h2c->http_connection = hc;
h2c->send_window = NGX_HTTP_V2_DEFAULT_WINDOW;
h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;
h2c->init_window = NGX_HTTP_V2_DEFAULT_WINDOW;
h2c->frame_size = NGX_HTTP_V2_DEFAULT_FRAME_SIZE;
h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);
h2c->concurrent_pushes = h2scf->concurrent_pushes;
h2c->priority_limit = h2scf->concurrent_streams;
h2c->pool = ngx_create_pool(h2scf->pool_size, h2c->connection->log);
if (h2c->pool == NULL) {
ngx_http_close_connection(c);
return;
}
cln = ngx_pool_cleanup_add(c->pool, 0);
if (cln == NULL) {
ngx_http_close_connection(c);
return;
}
cln->handler = ngx_http_v2_pool_cleanup;
cln->data = h2c;
h2c->streams_index = ngx_pcalloc(c->pool, ngx_http_v2_index_size(h2scf)
* sizeof(ngx_http_v2_node_t *));
if (h2c->streams_index == NULL) {
ngx_http_close_connection(c);
return;
}
if (ngx_http_v2_send_settings(h2c) == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
if (ngx_http_v2_send_window_update(h2c, 0, NGX_HTTP_V2_MAX_WINDOW
- NGX_HTTP_V2_DEFAULT_WINDOW)
== NGX_ERROR)
{
ngx_http_close_connection(c);
return;
}
h2c->state.handler = hc->proxy_protocol ? ngx_http_v2_state_proxy_protocol
: ngx_http_v2_state_preface;
ngx_queue_init(&h2c->waiting);
ngx_queue_init(&h2c->dependencies);
ngx_queue_init(&h2c->closed);
c->data = h2c;
rev->handler = ngx_http_v2_read_handler;
c->write->handler = ngx_http_v2_write_handler;
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
c->idle = 1;
ngx_reusable_connection(c, 0);
ngx_http_v2_read_handler(rev);
}
static void
ngx_http_v2_read_handler(ngx_event_t *rev)
{
u_char *p, *end;
size_t available;
ssize_t n;
ngx_connection_t *c;
ngx_http_v2_main_conf_t *h2mcf;
ngx_http_v2_connection_t *h2c;
c = rev->data;
h2c = c->data;
if (rev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 read handler");
h2c->blocked = 1;
if (c->close) {
c->close = 0;
if (c->error) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
if (!h2c->processing && !h2c->pushing) {
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
return;
}
if (!h2c->goaway) {
h2c->goaway = 1;
if (ngx_http_v2_send_goaway(h2c, NGX_HTTP_V2_NO_ERROR)
== NGX_ERROR)
{
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
}
h2c->blocked = 0;
return;
}
h2mcf = ngx_http_get_module_main_conf(h2c->http_connection->conf_ctx,
ngx_http_v2_module);
available = h2mcf->recv_buffer_size - 2 * NGX_HTTP_V2_STATE_BUFFER_SIZE;
do {
p = h2mcf->recv_buffer;
ngx_memcpy(p, h2c->state.buffer, NGX_HTTP_V2_STATE_BUFFER_SIZE);
end = p + h2c->state.buffer_used;
n = c->recv(c, end, available);
if (n == NGX_AGAIN) {
break;
}
if (n == 0
&& (h2c->state.incomplete || h2c->processing || h2c->pushing))
{
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"client prematurely closed connection");
}
if (n == 0 || n == NGX_ERROR) {
c->error = 1;
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
end += n;
h2c->state.buffer_used = 0;
h2c->state.incomplete = 0;
do {
p = h2c->state.handler(h2c, p, end);
if (p == NULL) {
return;
}
} while (p != end);
h2c->total_bytes += n;
if (h2c->total_bytes / 8 > h2c->payload_bytes + 1048576) {
ngx_log_error(NGX_LOG_INFO, c->log, 0, "http2 flood detected");
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_NO_ERROR);
return;
}
} while (rev->ready);
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_v2_finalize_connection(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
return;
}
if (h2c->last_out && ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
h2c->blocked = 0;
ngx_http_v2_handle_connection(h2c);
}
static void
ngx_http_v2_write_handler(ngx_event_t *wev)
{
ngx_int_t rc;
ngx_connection_t *c;
ngx_http_v2_connection_t *h2c;
c = wev->data;
h2c = c->data;
if (wev->timedout) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 write event timed out");
c->error = 1;
c->timedout = 1;
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http2 write handler");
if (h2c->last_out == NULL && !c->buffered) {
if (wev->timer_set) {
ngx_del_timer(wev);
}
ngx_http_v2_handle_connection(h2c);
return;
}
h2c->blocked = 1;
rc = ngx_http_v2_send_output_queue(h2c);
if (rc == NGX_ERROR) {
ngx_http_v2_finalize_connection(h2c, 0);
return;
}
h2c->blocked = 0;
if (rc == NGX_AGAIN) {
return;
}
ngx_http_v2_handle_connection(h2c);
}
ngx_int_t
ngx_http_v2_send_output_queue(ngx_http_v2_connection_t *h2c)
{
int tcp_nodelay;
ngx_chain_t *cl;
ngx_event_t *wev;
ngx_connection_t *c;
ngx_http_v2_out_frame_t *out, *frame, *fn;
ngx_http_core_loc_conf_t *clcf;
c = h2c->connection;
wev = c->write;
if (c->error) {
goto error;
}
if (!wev->ready) {
return NGX_AGAIN;
}
cl = NULL;
out = NULL;
for (frame = h2c->last_out; frame; frame = fn) {
frame->last->next = cl;
cl = frame->first;
fn = frame->next;
frame->next = out;
out = frame;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 frame out: %p sid:%ui bl:%d len:%uz",
out, out->stream ? out->stream->node->id : 0,
out->blocked, out->length);
}
cl = c->send_chain(c, cl, 0);
if (cl == NGX_CHAIN_ERROR) {
goto error;
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
goto error;
}
if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
if (ngx_tcp_push(c->fd) == -1) {
ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
goto error;
}
c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
} else {
tcp_nodelay = 1;
}
if (tcp_nodelay && clcf->tcp_nodelay && ngx_tcp_nodelay(c) != NGX_OK) {
goto error;
}
for ( /* void */ ; out; out = fn) {
fn = out->next;
if (out->handler(h2c, out) != NGX_OK) {
out->blocked = 1;
break;
}
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 frame sent: %p sid:%ui bl:%d len:%uz",
out, out->stream ? out->stream->node->id : 0,
out->blocked, out->length);
}
frame = NULL;
for ( /* void */ ; out; out = fn) {
fn = out->next;
out->next = frame;
frame = out;
}
h2c->last_out = frame;
if (!wev->ready) {
ngx_add_timer(wev, clcf->send_timeout);
return NGX_AGAIN;
}
if (wev->timer_set) {
ngx_del_timer(wev);
}
return NGX_OK;
error:
c->error = 1;
if (!h2c->blocked) {
ngx_post_event(wev, &ngx_posted_events);
}
return NGX_ERROR;
}
static void
ngx_http_v2_handle_connection(ngx_http_v2_connection_t *h2c)
{
ngx_int_t rc;
ngx_connection_t *c;
ngx_http_core_loc_conf_t *clcf;
if (h2c->last_out || h2c->processing || h2c->pushing) {
return;
}
c = h2c->connection;
if (c->error) {
ngx_http_close_connection(c);
return;
}
if (c->buffered) {
h2c->blocked = 1;
rc = ngx_http_v2_send_output_queue(h2c);
h2c->blocked = 0;
if (rc == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
if (rc == NGX_AGAIN) {
return;
}
/* rc == NGX_OK */
}
if (h2c->goaway) {
ngx_http_v2_lingering_close(c);
return;
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (!c->read->timer_set) {
ngx_add_timer(c->read, clcf->keepalive_timeout);
}
ngx_reusable_connection(c, 1);
if (h2c->state.incomplete) {
return;
}
ngx_destroy_pool(h2c->pool);
h2c->pool = NULL;
h2c->free_frames = NULL;
h2c->frames = 0;
h2c->free_fake_connections = NULL;
#if (NGX_HTTP_SSL)
if (c->ssl) {
ngx_ssl_free_buffer(c);
}
#endif
c->destroyed = 1;
c->write->handler = ngx_http_empty_handler;
c->read->handler = ngx_http_v2_idle_handler;
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
}
static void
ngx_http_v2_lingering_close(ngx_connection_t *c)
{
ngx_event_t *rev, *wev;
ngx_http_v2_connection_t *h2c;
ngx_http_core_loc_conf_t *clcf;
h2c = c->data;
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (clcf->lingering_close == NGX_HTTP_LINGERING_OFF) {
ngx_http_close_connection(c);
return;
}
if (h2c->lingering_time == 0) {
h2c->lingering_time = ngx_time()
+ (time_t) (clcf->lingering_time / 1000);
}
#if (NGX_HTTP_SSL)
if (c->ssl) {
ngx_int_t rc;
rc = ngx_ssl_shutdown(c);
if (rc == NGX_ERROR) {
ngx_http_close_connection(c);
return;
}
if (rc == NGX_AGAIN) {
c->ssl->handler = ngx_http_v2_lingering_close;
return;
}
}
#endif
rev = c->read;
rev->handler = ngx_http_v2_lingering_close_handler;
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
wev = c->write;
wev->handler = ngx_http_empty_handler;
if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
}
if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
ngx_connection_error(c, ngx_socket_errno,
ngx_shutdown_socket_n " failed");
ngx_http_close_connection(c);
return;
}
c->close = 0;
ngx_reusable_connection(c, 1);
ngx_add_timer(rev, clcf->lingering_timeout);
if (rev->ready) {
ngx_http_v2_lingering_close_handler(rev);
}
}
static void
ngx_http_v2_lingering_close_handler(ngx_event_t *rev)
{
ssize_t n;
ngx_msec_t timer;
ngx_connection_t *c;
ngx_http_core_loc_conf_t *clcf;
ngx_http_v2_connection_t *h2c;
u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
c = rev->data;
h2c = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http2 lingering close handler");
if (rev->timedout || c->close) {
ngx_http_close_connection(c);
return;
}
timer = (ngx_msec_t) h2c->lingering_time - (ngx_msec_t) ngx_time();
if ((ngx_msec_int_t) timer <= 0) {
ngx_http_close_connection(c);
return;
}
do {
n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %z", n);
if (n == NGX_AGAIN) {
break;
}
if (n == NGX_ERROR || n == 0) {
ngx_http_close_connection(c);
return;
}
} while (rev->ready);
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_close_connection(c);
return;
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
timer *= 1000;
if (timer > clcf->lingering_timeout) {
timer = clcf->lingering_timeout;
}
ngx_add_timer(rev, timer);
}
static u_char *
ngx_http_v2_state_proxy_protocol(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_log_t *log;
log = h2c->connection->log;
log->action = "reading PROXY protocol";
pos = ngx_proxy_protocol_read(h2c->connection, pos, end);
log->action = "processing HTTP/2 connection";
if (pos == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
return ngx_http_v2_state_preface(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_preface(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
static const u_char preface[] = "PRI * HTTP/2.0\r\n";
if ((size_t) (end - pos) < sizeof(preface) - 1) {
return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_preface);
}
if (ngx_memcmp(pos, preface, sizeof(preface) - 1) != 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"invalid connection preface");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
return ngx_http_v2_state_preface_end(h2c, pos + sizeof(preface) - 1, end);
}
static u_char *
ngx_http_v2_state_preface_end(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
static const u_char preface[] = "\r\nSM\r\n\r\n";
if ((size_t) (end - pos) < sizeof(preface) - 1) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_preface_end);
}
if (ngx_memcmp(pos, preface, sizeof(preface) - 1) != 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"invalid connection preface");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 preface verified");
return ngx_http_v2_state_head(h2c, pos + sizeof(preface) - 1, end);
}
static u_char *
ngx_http_v2_state_head(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
{
uint32_t head;
ngx_uint_t type;
if (end - pos < NGX_HTTP_V2_FRAME_HEADER_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_head);
}
head = ngx_http_v2_parse_uint32(pos);
h2c->state.length = ngx_http_v2_parse_length(head);
h2c->state.flags = pos[4];
h2c->state.sid = ngx_http_v2_parse_sid(&pos[5]);
pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;
type = ngx_http_v2_parse_type(head);
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 frame type:%ui f:%Xd l:%uz sid:%ui",
type, h2c->state.flags, h2c->state.length, h2c->state.sid);
if (type >= NGX_HTTP_V2_FRAME_STATES) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent frame with unknown type %ui", type);
return ngx_http_v2_state_skip(h2c, pos, end);
}
return ngx_http_v2_frame_states[type](h2c, pos, end);
}
static u_char *
ngx_http_v2_state_data(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
{
size_t size;
ngx_http_v2_node_t *node;
ngx_http_v2_stream_t *stream;
size = h2c->state.length;
if (h2c->state.flags & NGX_HTTP_V2_PADDED_FLAG) {
if (h2c->state.length == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent padded DATA frame "
"with incorrect length: 0");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos == 0) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_data);
}
h2c->state.padding = *pos++;
if (h2c->state.padding >= size) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent padded DATA frame "
"with incorrect length: %uz, padding: %uz",
size, h2c->state.padding);
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->state.length -= 1 + h2c->state.padding;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 DATA frame");
if (h2c->state.sid == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent DATA frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (size > h2c->recv_window) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated connection flow control: "
"received DATA frame length %uz, available window %uz",
size, h2c->recv_window);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
}
h2c->recv_window -= size;
if (h2c->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4) {
if (ngx_http_v2_send_window_update(h2c, 0, NGX_HTTP_V2_MAX_WINDOW
- h2c->recv_window)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
h2c->recv_window = NGX_HTTP_V2_MAX_WINDOW;
}
node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);
if (node == NULL || node->stream == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"unknown http2 stream");
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
stream = node->stream;
if (size > stream->recv_window) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated flow control for stream %ui: "
"received DATA frame length %uz, available window %uz",
node->id, size, stream->recv_window);
if (ngx_http_v2_terminate_stream(h2c, stream,
NGX_HTTP_V2_FLOW_CTRL_ERROR)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
stream->recv_window -= size;
if (stream->no_flow_control
&& stream->recv_window < NGX_HTTP_V2_MAX_WINDOW / 4)
{
if (ngx_http_v2_send_window_update(h2c, node->id,
NGX_HTTP_V2_MAX_WINDOW
- stream->recv_window)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
stream->recv_window = NGX_HTTP_V2_MAX_WINDOW;
}
if (stream->in_closed) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent DATA frame for half-closed stream %ui",
node->id);
if (ngx_http_v2_terminate_stream(h2c, stream,
NGX_HTTP_V2_STREAM_CLOSED)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
h2c->state.stream = stream;
return ngx_http_v2_state_read_data(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_read_data(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t size;
ngx_buf_t *buf;
ngx_int_t rc;
ngx_connection_t *fc;
ngx_http_request_t *r;
ngx_http_v2_stream_t *stream;
ngx_http_v2_srv_conf_t *h2scf;
stream = h2c->state.stream;
if (stream == NULL) {
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
if (stream->skip_data) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"skipping http2 DATA frame");
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
r = stream->request;
fc = r->connection;
if (r->reading_body && !r->request_body_no_buffering) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"skipping http2 DATA frame");
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
if (r->headers_in.content_length_n < 0 && !r->headers_in.chunked) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"skipping http2 DATA frame");
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
size = end - pos;
if (size >= h2c->state.length) {
size = h2c->state.length;
stream->in_closed = h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG;
}
h2c->payload_bytes += size;
if (r->request_body) {
rc = ngx_http_v2_process_request_body(r, pos, size, stream->in_closed);
if (rc != NGX_OK) {
stream->skip_data = 1;
ngx_http_finalize_request(r, rc);
}
ngx_http_run_posted_requests(fc);
} else if (size) {
buf = stream->preread;
if (buf == NULL) {
h2scf = ngx_http_get_module_srv_conf(r, ngx_http_v2_module);
buf = ngx_create_temp_buf(r->pool, h2scf->preread_size);
if (buf == NULL) {
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
stream->preread = buf;
}
if (size > (size_t) (buf->end - buf->last)) {
ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
"http2 preread buffer overflow");
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
buf->last = ngx_cpymem(buf->last, pos, size);
}
pos += size;
h2c->state.length -= size;
if (h2c->state.length) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_read_data);
}
if (h2c->state.padding) {
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
return ngx_http_v2_state_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_headers(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t size;
ngx_uint_t padded, priority, depend, dependency, excl,
weight;
ngx_uint_t status;
ngx_http_v2_node_t *node;
ngx_http_v2_stream_t *stream;
ngx_http_v2_srv_conf_t *h2scf;
ngx_http_core_srv_conf_t *cscf;
ngx_http_core_loc_conf_t *clcf;
padded = h2c->state.flags & NGX_HTTP_V2_PADDED_FLAG;
priority = h2c->state.flags & NGX_HTTP_V2_PRIORITY_FLAG;
size = 0;
if (padded) {
size++;
}
if (priority) {
size += sizeof(uint32_t) + 1;
}
if (h2c->state.length < size) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent HEADERS frame with incorrect length %uz",
h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (h2c->state.length == size) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent HEADERS frame with empty header block");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (h2c->goaway) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"skipping http2 HEADERS frame");
return ngx_http_v2_state_skip(h2c, pos, end);
}
if ((size_t) (end - pos) < size) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_headers);
}
h2c->state.length -= size;
if (padded) {
h2c->state.padding = *pos++;
if (h2c->state.padding > h2c->state.length) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent padded HEADERS frame "
"with incorrect length: %uz, padding: %uz",
h2c->state.length, h2c->state.padding);
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->state.length -= h2c->state.padding;
}
depend = 0;
excl = 0;
weight = NGX_HTTP_V2_DEFAULT_WEIGHT;
if (priority) {
dependency = ngx_http_v2_parse_uint32(pos);
depend = dependency & 0x7fffffff;
excl = dependency >> 31;
weight = pos[4] + 1;
pos += sizeof(uint32_t) + 1;
}
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 HEADERS frame sid:%ui "
"depends on %ui excl:%ui weight:%ui",
h2c->state.sid, depend, excl, weight);
if (h2c->state.sid % 2 == 0 || h2c->state.sid <= h2c->last_sid) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent HEADERS frame with incorrect identifier "
"%ui, the last was %ui", h2c->state.sid, h2c->last_sid);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (depend == h2c->state.sid) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent HEADERS frame for stream %ui "
"with incorrect dependency", h2c->state.sid);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->last_sid = h2c->state.sid;
h2c->state.pool = ngx_create_pool(1024, h2c->connection->log);
if (h2c->state.pool == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
cscf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
h2c->state.header_limit = cscf->large_client_header_buffers.size
* cscf->large_client_header_buffers.num;
h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
ngx_http_v2_module);
if (h2c->processing >= h2scf->concurrent_streams) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"concurrent streams exceeded %ui", h2c->processing);
status = NGX_HTTP_V2_REFUSED_STREAM;
goto rst_stream;
}
if (!h2c->settings_ack
&& !(h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG)
&& h2scf->preread_size < NGX_HTTP_V2_DEFAULT_WINDOW)
{
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent stream with data "
"before settings were acknowledged");
status = NGX_HTTP_V2_REFUSED_STREAM;
goto rst_stream;
}
node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 1);
if (node == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
if (node->parent) {
ngx_queue_remove(&node->reuse);
h2c->closed_nodes--;
}
stream = ngx_http_v2_create_stream(h2c, 0);
if (stream == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
h2c->state.stream = stream;
stream->pool = h2c->state.pool;
h2c->state.keep_pool = 1;
stream->request->request_length = h2c->state.length;
stream->in_closed = h2c->state.flags & NGX_HTTP_V2_END_STREAM_FLAG;
stream->node = node;
node->stream = stream;
if (priority || node->parent == NULL) {
node->weight = weight;
ngx_http_v2_set_dependency(h2c, node, depend, excl);
}
clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if (h2c->connection->requests >= clcf->keepalive_requests) {
h2c->goaway = 1;
if (ngx_http_v2_send_goaway(h2c, NGX_HTTP_V2_NO_ERROR) == NGX_ERROR) {
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
}
return ngx_http_v2_state_header_block(h2c, pos, end);
rst_stream:
if (ngx_http_v2_send_rst_stream(h2c, h2c->state.sid, status) != NGX_OK) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
return ngx_http_v2_state_header_block(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_header_block(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
u_char ch;
ngx_int_t value;
ngx_uint_t indexed, size_update, prefix;
if (end - pos < 1) {
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_header_block);
}
if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)
&& h2c->state.length < NGX_HTTP_V2_INT_OCTETS)
{
return ngx_http_v2_handle_continuation(h2c, pos, end,
ngx_http_v2_state_header_block);
}
size_update = 0;
indexed = 0;
ch = *pos;
if (ch >= (1 << 7)) {
/* indexed header field */
indexed = 1;
prefix = ngx_http_v2_prefix(7);
} else if (ch >= (1 << 6)) {
/* literal header field with incremental indexing */
h2c->state.index = 1;
prefix = ngx_http_v2_prefix(6);
} else if (ch >= (1 << 5)) {
/* dynamic table size update */
size_update = 1;
prefix = ngx_http_v2_prefix(5);
} else if (ch >= (1 << 4)) {
/* literal header field never indexed */
prefix = ngx_http_v2_prefix(4);
} else {
/* literal header field without indexing */
prefix = ngx_http_v2_prefix(4);
}
value = ngx_http_v2_parse_int(h2c, &pos, end, prefix);
if (value < 0) {
if (value == NGX_AGAIN) {
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_header_block);
}
if (value == NGX_DECLINED) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header block with too long %s value",
size_update ? "size update" : "header index");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
}
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header block with incorrect length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (indexed) {
if (ngx_http_v2_get_indexed_header(h2c, value, 0) != NGX_OK) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
}
return ngx_http_v2_state_process_header(h2c, pos, end);
}
if (size_update) {
if (ngx_http_v2_table_size(h2c, value) != NGX_OK) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
}
return ngx_http_v2_state_header_complete(h2c, pos, end);
}
if (value == 0) {
h2c->state.parse_name = 1;
} else if (ngx_http_v2_get_indexed_header(h2c, value, 1) != NGX_OK) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
}
h2c->state.parse_value = 1;
return ngx_http_v2_state_field_len(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_field_len(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t alloc;
ngx_int_t len;
ngx_uint_t huff;
ngx_http_core_srv_conf_t *cscf;
if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)
&& h2c->state.length < NGX_HTTP_V2_INT_OCTETS)
{
return ngx_http_v2_handle_continuation(h2c, pos, end,
ngx_http_v2_state_field_len);
}
if (h2c->state.length < 1) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header block with incorrect length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos < 1) {
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_field_len);
}
huff = *pos >> 7;
len = ngx_http_v2_parse_int(h2c, &pos, end, ngx_http_v2_prefix(7));
if (len < 0) {
if (len == NGX_AGAIN) {
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_field_len);
}
if (len == NGX_DECLINED) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header field with too long length value");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
}
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header block with incorrect length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 %s string, len:%i",
huff ? "encoded" : "raw", len);
cscf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
ngx_http_core_module);
if ((size_t) len > cscf->large_client_header_buffers.size) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent too large header field");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
}
h2c->state.field_rest = len;
if (h2c->state.stream == NULL && !h2c->state.index) {
return ngx_http_v2_state_field_skip(h2c, pos, end);
}
alloc = (huff ? len * 8 / 5 : len) + 1;
h2c->state.field_start = ngx_pnalloc(h2c->state.pool, alloc);
if (h2c->state.field_start == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
h2c->state.field_end = h2c->state.field_start;
if (huff) {
return ngx_http_v2_state_field_huff(h2c, pos, end);
}
return ngx_http_v2_state_field_raw(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_field_huff(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t size;
size = end - pos;
if (size > h2c->state.field_rest) {
size = h2c->state.field_rest;
}
if (size > h2c->state.length) {
size = h2c->state.length;
}
h2c->state.length -= size;
h2c->state.field_rest -= size;
if (ngx_http_v2_huff_decode(&h2c->state.field_state, pos, size,
&h2c->state.field_end,
h2c->state.field_rest == 0,
h2c->connection->log)
!= NGX_OK)
{
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent invalid encoded header field");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_COMP_ERROR);
}
pos += size;
if (h2c->state.field_rest == 0) {
*h2c->state.field_end = '\0';
return ngx_http_v2_state_process_header(h2c, pos, end);
}
if (h2c->state.length) {
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_field_huff);
}
if (h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header field with incorrect length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
return ngx_http_v2_handle_continuation(h2c, pos, end,
ngx_http_v2_state_field_huff);
}
static u_char *
ngx_http_v2_state_field_raw(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t size;
size = end - pos;
if (size > h2c->state.field_rest) {
size = h2c->state.field_rest;
}
if (size > h2c->state.length) {
size = h2c->state.length;
}
h2c->state.length -= size;
h2c->state.field_rest -= size;
h2c->state.field_end = ngx_cpymem(h2c->state.field_end, pos, size);
pos += size;
if (h2c->state.field_rest == 0) {
*h2c->state.field_end = '\0';
return ngx_http_v2_state_process_header(h2c, pos, end);
}
if (h2c->state.length) {
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_field_raw);
}
if (h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header field with incorrect length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
return ngx_http_v2_handle_continuation(h2c, pos, end,
ngx_http_v2_state_field_raw);
}
static u_char *
ngx_http_v2_state_field_skip(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t size;
size = end - pos;
if (size > h2c->state.field_rest) {
size = h2c->state.field_rest;
}
if (size > h2c->state.length) {
size = h2c->state.length;
}
h2c->state.length -= size;
h2c->state.field_rest -= size;
pos += size;
if (h2c->state.field_rest == 0) {
return ngx_http_v2_state_process_header(h2c, pos, end);
}
if (h2c->state.length) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_field_skip);
}
if (h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent header field with incorrect length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
return ngx_http_v2_handle_continuation(h2c, pos, end,
ngx_http_v2_state_field_skip);
}
static u_char *
ngx_http_v2_state_process_header(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t len;
ngx_int_t rc;
ngx_table_elt_t *h;
ngx_http_header_t *hh;
ngx_http_request_t *r;
ngx_http_v2_header_t *header;
ngx_http_core_srv_conf_t *cscf;
ngx_http_core_main_conf_t *cmcf;
static ngx_str_t cookie = ngx_string("cookie");
header = &h2c->state.header;
if (h2c->state.parse_name) {
h2c->state.parse_name = 0;
header->name.len = h2c->state.field_end - h2c->state.field_start;
header->name.data = h2c->state.field_start;
if (header->name.len == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent zero header name length");
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_PROTOCOL_ERROR);
}
return ngx_http_v2_state_field_len(h2c, pos, end);
}
if (h2c->state.parse_value) {
h2c->state.parse_value = 0;
header->value.len = h2c->state.field_end - h2c->state.field_start;
header->value.data = h2c->state.field_start;
}
len = header->name.len + header->value.len;
if (len > h2c->state.header_limit) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent too large header");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
}
h2c->state.header_limit -= len;
if (h2c->state.index) {
if (ngx_http_v2_add_header(h2c, header) != NGX_OK) {
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
h2c->state.index = 0;
}
if (h2c->state.stream == NULL) {
return ngx_http_v2_state_header_complete(h2c, pos, end);
}
r = h2c->state.stream->request;
/* TODO Optimization: validate headers while parsing. */
if (ngx_http_v2_validate_header(r, header) != NGX_OK) {
if (ngx_http_v2_terminate_stream(h2c, h2c->state.stream,
NGX_HTTP_V2_PROTOCOL_ERROR)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
goto error;
}
if (header->name.data[0] == ':') {
rc = ngx_http_v2_pseudo_header(r, header);
if (rc == NGX_OK) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http2 header: \":%V: %V\"",
&header->name, &header->value);
return ngx_http_v2_state_header_complete(h2c, pos, end);
}
if (rc == NGX_ABORT) {
goto error;
}
if (rc == NGX_DECLINED) {
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
goto error;
}
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
if (r->invalid_header) {
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
if (cscf->ignore_invalid_headers) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent invalid header: \"%V\"", &header->name);
return ngx_http_v2_state_header_complete(h2c, pos, end);
}
}
if (header->name.len == cookie.len
&& ngx_memcmp(header->name.data, cookie.data, cookie.len) == 0)
{
if (ngx_http_v2_cookie(r, header) != NGX_OK) {
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
} else {
h = ngx_list_push(&r->headers_in.headers);
if (h == NULL) {
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
h->key.len = header->name.len;
h->key.data = header->name.data;
/*
* TODO Optimization: precalculate hash
* and handler for indexed headers.
*/
h->hash = ngx_hash_key(h->key.data, h->key.len);
h->value.len = header->value.len;
h->value.data = header->value.data;
h->lowcase_key = h->key.data;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
h->lowcase_key, h->key.len);
if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
goto error;
}
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http2 header: \"%V: %V\"",
&header->name, &header->value);
return ngx_http_v2_state_header_complete(h2c, pos, end);
error:
h2c->state.stream = NULL;
return ngx_http_v2_state_header_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_header_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_http_v2_stream_t *stream;
if (h2c->state.length) {
if (end - pos > 0) {
h2c->state.handler = ngx_http_v2_state_header_block;
return pos;
}
return ngx_http_v2_state_headers_save(h2c, pos, end,
ngx_http_v2_state_header_block);
}
if (!(h2c->state.flags & NGX_HTTP_V2_END_HEADERS_FLAG)) {
return ngx_http_v2_handle_continuation(h2c, pos, end,
ngx_http_v2_state_header_complete);
}
stream = h2c->state.stream;
if (stream) {
ngx_http_v2_run_request(stream->request);
}
if (!h2c->state.keep_pool) {
ngx_destroy_pool(h2c->state.pool);
}
h2c->state.pool = NULL;
h2c->state.keep_pool = 0;
if (h2c->state.padding) {
return ngx_http_v2_state_skip_padded(h2c, pos, end);
}
return ngx_http_v2_state_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_handle_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end, ngx_http_v2_handler_pt handler)
{
u_char *p;
size_t len, skip;
uint32_t head;
len = h2c->state.length;
if (h2c->state.padding && (size_t) (end - pos) > len) {
skip = ngx_min(h2c->state.padding, (end - pos) - len);
h2c->state.padding -= skip;
p = pos;
pos += skip;
ngx_memmove(pos, p, len);
}
if ((size_t) (end - pos) < len + NGX_HTTP_V2_FRAME_HEADER_SIZE) {
return ngx_http_v2_state_headers_save(h2c, pos, end, handler);
}
p = pos + len;
head = ngx_http_v2_parse_uint32(p);
if (ngx_http_v2_parse_type(head) != NGX_HTTP_V2_CONTINUATION_FRAME) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent inappropriate frame while CONTINUATION was expected");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->state.flags |= p[4];
if (h2c->state.sid != ngx_http_v2_parse_sid(&p[5])) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent CONTINUATION frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
p = pos;
pos += NGX_HTTP_V2_FRAME_HEADER_SIZE;
ngx_memcpy(pos, p, len);
len = ngx_http_v2_parse_length(head);
h2c->state.length += len;
if (h2c->state.stream) {
h2c->state.stream->request->request_length += len;
}
h2c->state.handler = handler;
return pos;
}
static u_char *
ngx_http_v2_state_priority(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_uint_t depend, dependency, excl, weight;
ngx_http_v2_node_t *node;
if (h2c->state.length != NGX_HTTP_V2_PRIORITY_SIZE) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent PRIORITY frame with incorrect length %uz",
h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (--h2c->priority_limit == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent too many PRIORITY frames");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_ENHANCE_YOUR_CALM);
}
if (end - pos < NGX_HTTP_V2_PRIORITY_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_priority);
}
dependency = ngx_http_v2_parse_uint32(pos);
depend = dependency & 0x7fffffff;
excl = dependency >> 31;
weight = pos[4] + 1;
pos += NGX_HTTP_V2_PRIORITY_SIZE;
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 PRIORITY frame sid:%ui "
"depends on %ui excl:%ui weight:%ui",
h2c->state.sid, depend, excl, weight);
if (h2c->state.sid == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent PRIORITY frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (depend == h2c->state.sid) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent PRIORITY frame for stream %ui "
"with incorrect dependency", h2c->state.sid);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 1);
if (node == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
node->weight = weight;
if (node->stream == NULL) {
if (node->parent == NULL) {
h2c->closed_nodes++;
} else {
ngx_queue_remove(&node->reuse);
}
ngx_queue_insert_tail(&h2c->closed, &node->reuse);
}
ngx_http_v2_set_dependency(h2c, node, depend, excl);
return ngx_http_v2_state_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_rst_stream(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_uint_t status;
ngx_event_t *ev;
ngx_connection_t *fc;
ngx_http_v2_node_t *node;
ngx_http_v2_stream_t *stream;
if (h2c->state.length != NGX_HTTP_V2_RST_STREAM_SIZE) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent RST_STREAM frame with incorrect length %uz",
h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos < NGX_HTTP_V2_RST_STREAM_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_rst_stream);
}
status = ngx_http_v2_parse_uint32(pos);
pos += NGX_HTTP_V2_RST_STREAM_SIZE;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 RST_STREAM frame, sid:%ui status:%ui",
h2c->state.sid, status);
if (h2c->state.sid == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent RST_STREAM frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);
if (node == NULL || node->stream == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"unknown http2 stream");
return ngx_http_v2_state_complete(h2c, pos, end);
}
stream = node->stream;
stream->in_closed = 1;
stream->out_closed = 1;
fc = stream->request->connection;
fc->error = 1;
switch (status) {
case NGX_HTTP_V2_CANCEL:
ngx_log_error(NGX_LOG_INFO, fc->log, 0,
"client canceled stream %ui", h2c->state.sid);
break;
case NGX_HTTP_V2_REFUSED_STREAM:
ngx_log_error(NGX_LOG_INFO, fc->log, 0,
"client refused stream %ui", h2c->state.sid);
break;
case NGX_HTTP_V2_INTERNAL_ERROR:
ngx_log_error(NGX_LOG_INFO, fc->log, 0,
"client terminated stream %ui due to internal error",
h2c->state.sid);
break;
default:
ngx_log_error(NGX_LOG_INFO, fc->log, 0,
"client terminated stream %ui with status %ui",
h2c->state.sid, status);
break;
}
ev = fc->read;
ev->handler(ev);
return ngx_http_v2_state_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_settings(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 SETTINGS frame");
if (h2c->state.sid) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent SETTINGS frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (h2c->state.flags == NGX_HTTP_V2_ACK_FLAG) {
if (h2c->state.length != 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent SETTINGS frame with the ACK flag "
"and nonzero length");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
h2c->settings_ack = 1;
return ngx_http_v2_state_complete(h2c, pos, end);
}
if (h2c->state.length % NGX_HTTP_V2_SETTINGS_PARAM_SIZE) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent SETTINGS frame with incorrect length %uz",
h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
return ngx_http_v2_state_settings_params(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_settings_params(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ssize_t window_delta;
ngx_uint_t id, value;
ngx_http_v2_srv_conf_t *h2scf;
ngx_http_v2_out_frame_t *frame;
window_delta = 0;
while (h2c->state.length) {
if (end - pos < NGX_HTTP_V2_SETTINGS_PARAM_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_settings_params);
}
h2c->state.length -= NGX_HTTP_V2_SETTINGS_PARAM_SIZE;
id = ngx_http_v2_parse_uint16(pos);
value = ngx_http_v2_parse_uint32(&pos[2]);
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 setting %ui:%ui", id, value);
switch (id) {
case NGX_HTTP_V2_INIT_WINDOW_SIZE_SETTING:
if (value > NGX_HTTP_V2_MAX_WINDOW) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent SETTINGS frame with incorrect "
"INITIAL_WINDOW_SIZE value %ui", value);
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_FLOW_CTRL_ERROR);
}
window_delta = value - h2c->init_window;
break;
case NGX_HTTP_V2_MAX_FRAME_SIZE_SETTING:
if (value > NGX_HTTP_V2_MAX_FRAME_SIZE
|| value < NGX_HTTP_V2_DEFAULT_FRAME_SIZE)
{
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent SETTINGS frame with incorrect "
"MAX_FRAME_SIZE value %ui", value);
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->frame_size = value;
break;
case NGX_HTTP_V2_ENABLE_PUSH_SETTING:
if (value > 1) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent SETTINGS frame with incorrect "
"ENABLE_PUSH value %ui", value);
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_PROTOCOL_ERROR);
}
h2c->push_disabled = !value;
break;
case NGX_HTTP_V2_MAX_STREAMS_SETTING:
h2scf = ngx_http_get_module_srv_conf(h2c->http_connection->conf_ctx,
ngx_http_v2_module);
h2c->concurrent_pushes = ngx_min(value, h2scf->concurrent_pushes);
break;
case NGX_HTTP_V2_HEADER_TABLE_SIZE_SETTING:
h2c->table_update = 1;
break;
default:
break;
}
pos += NGX_HTTP_V2_SETTINGS_PARAM_SIZE;
}
frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_SETTINGS_ACK_SIZE,
NGX_HTTP_V2_SETTINGS_FRAME,
NGX_HTTP_V2_ACK_FLAG, 0);
if (frame == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
ngx_http_v2_queue_ordered_frame(h2c, frame);
if (window_delta) {
h2c->init_window += window_delta;
if (ngx_http_v2_adjust_windows(h2c, window_delta) != NGX_OK) {
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
}
return ngx_http_v2_state_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_push_promise(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent PUSH_PROMISE frame");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
static u_char *
ngx_http_v2_state_ping(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
{
ngx_buf_t *buf;
ngx_http_v2_out_frame_t *frame;
if (h2c->state.length != NGX_HTTP_V2_PING_SIZE) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent PING frame with incorrect length %uz",
h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos < NGX_HTTP_V2_PING_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_ping);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 PING frame");
if (h2c->state.sid) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent PING frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (h2c->state.flags & NGX_HTTP_V2_ACK_FLAG) {
return ngx_http_v2_state_skip(h2c, pos, end);
}
frame = ngx_http_v2_get_frame(h2c, NGX_HTTP_V2_PING_SIZE,
NGX_HTTP_V2_PING_FRAME,
NGX_HTTP_V2_ACK_FLAG, 0);
if (frame == NULL) {
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
buf = frame->first->buf;
buf->last = ngx_cpymem(buf->last, pos, NGX_HTTP_V2_PING_SIZE);
ngx_http_v2_queue_blocked_frame(h2c, frame);
return ngx_http_v2_state_complete(h2c, pos + NGX_HTTP_V2_PING_SIZE, end);
}
static u_char *
ngx_http_v2_state_goaway(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
#if (NGX_DEBUG)
ngx_uint_t last_sid, error;
#endif
if (h2c->state.length < NGX_HTTP_V2_GOAWAY_SIZE) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent GOAWAY frame "
"with incorrect length %uz", h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos < NGX_HTTP_V2_GOAWAY_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end, ngx_http_v2_state_goaway);
}
if (h2c->state.sid) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent GOAWAY frame with incorrect identifier");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
#if (NGX_DEBUG)
h2c->state.length -= NGX_HTTP_V2_GOAWAY_SIZE;
last_sid = ngx_http_v2_parse_sid(pos);
error = ngx_http_v2_parse_uint32(&pos[4]);
pos += NGX_HTTP_V2_GOAWAY_SIZE;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 GOAWAY frame: last sid %ui, error %ui",
last_sid, error);
#endif
return ngx_http_v2_state_skip(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_window_update(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
size_t window;
ngx_event_t *wev;
ngx_queue_t *q;
ngx_http_v2_node_t *node;
ngx_http_v2_stream_t *stream;
if (h2c->state.length != NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent WINDOW_UPDATE frame "
"with incorrect length %uz", h2c->state.length);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_SIZE_ERROR);
}
if (end - pos < NGX_HTTP_V2_WINDOW_UPDATE_SIZE) {
return ngx_http_v2_state_save(h2c, pos, end,
ngx_http_v2_state_window_update);
}
window = ngx_http_v2_parse_window(pos);
pos += NGX_HTTP_V2_WINDOW_UPDATE_SIZE;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 WINDOW_UPDATE frame sid:%ui window:%uz",
h2c->state.sid, window);
if (window == 0) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent WINDOW_UPDATE frame "
"with incorrect window increment 0");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
if (h2c->state.sid) {
node = ngx_http_v2_get_node_by_id(h2c, h2c->state.sid, 0);
if (node == NULL || node->stream == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"unknown http2 stream");
return ngx_http_v2_state_complete(h2c, pos, end);
}
stream = node->stream;
if (window > (size_t) (NGX_HTTP_V2_MAX_WINDOW - stream->send_window)) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated flow control for stream %ui: "
"received WINDOW_UPDATE frame "
"with window increment %uz "
"not allowed for window %z",
h2c->state.sid, window, stream->send_window);
if (ngx_http_v2_terminate_stream(h2c, stream,
NGX_HTTP_V2_FLOW_CTRL_ERROR)
== NGX_ERROR)
{
return ngx_http_v2_connection_error(h2c,
NGX_HTTP_V2_INTERNAL_ERROR);
}
return ngx_http_v2_state_complete(h2c, pos, end);
}
stream->send_window += window;
if (stream->exhausted) {
stream->exhausted = 0;
wev = stream->request->connection->write;
wev->active = 0;
wev->ready = 1;
if (!wev->delayed) {
wev->handler(wev);
}
}
return ngx_http_v2_state_complete(h2c, pos, end);
}
if (window > NGX_HTTP_V2_MAX_WINDOW - h2c->send_window) {
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client violated connection flow control: "
"received WINDOW_UPDATE frame "
"with window increment %uz "
"not allowed for window %uz",
window, h2c->send_window);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_FLOW_CTRL_ERROR);
}
h2c->send_window += window;
while (!ngx_queue_empty(&h2c->waiting)) {
q = ngx_queue_head(&h2c->waiting);
ngx_queue_remove(q);
stream = ngx_queue_data(q, ngx_http_v2_stream_t, queue);
stream->waiting = 0;
wev = stream->request->connection->write;
wev->active = 0;
wev->ready = 1;
if (!wev->delayed) {
wev->handler(wev);
if (h2c->send_window == 0) {
break;
}
}
}
return ngx_http_v2_state_complete(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_continuation(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_log_error(NGX_LOG_INFO, h2c->connection->log, 0,
"client sent unexpected CONTINUATION frame");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_PROTOCOL_ERROR);
}
static u_char *
ngx_http_v2_state_complete(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 frame complete pos:%p end:%p", pos, end);
if (pos > end) {
ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
"receive buffer overrun");
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
h2c->state.stream = NULL;
h2c->state.handler = ngx_http_v2_state_head;
return pos;
}
static u_char *
ngx_http_v2_state_skip_padded(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end)
{
h2c->state.length += h2c->state.padding;
h2c->state.padding = 0;
return ngx_http_v2_state_skip(h2c, pos, end);
}
static u_char *
ngx_http_v2_state_skip(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end)
{
size_t size;
size = end - pos;
if (size < h2c->state.length) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 frame skip %uz of %uz", size, h2c->state.length);
h2c->state.length -= size;
return ngx_http_v2_state_save(h2c, end, end, ngx_http_v2_state_skip);
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 frame skip %uz", h2c->state.length);
return ngx_http_v2_state_complete(h2c, pos + h2c->state.length, end);
}
static u_char *
ngx_http_v2_state_save(ngx_http_v2_connection_t *h2c, u_char *pos, u_char *end,
ngx_http_v2_handler_pt handler)
{
size_t size;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 frame state save pos:%p end:%p handler:%p",
pos, end, handler);
size = end - pos;
if (size > NGX_HTTP_V2_STATE_BUFFER_SIZE) {
ngx_log_error(NGX_LOG_ALERT, h2c->connection->log, 0,
"state buffer overflow: %uz bytes required", size);
return ngx_http_v2_connection_error(h2c, NGX_HTTP_V2_INTERNAL_ERROR);
}
ngx_memcpy(h2c->state.buffer, pos, NGX_HTTP_V2_STATE_BUFFER_SIZE);
h2c->state.buffer_used = size;
h2c->state.handler = handler;
h2c->state.incomplete = 1;
return end;
}
static u_char *
ngx_http_v2_state_headers_save(ngx_http_v2_connection_t *h2c, u_char *pos,
u_char *end, ngx_http_v2_handler_pt handler)
{
ngx_event_t *rev;
ngx_http_request_t *r;
ngx_http_core_srv_conf_t *cscf;
if (h2c->state.stream) {
r = h2c->state.stream->request;
rev = r->connection->read;
if (!rev->timer_set) {
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
ngx_add_timer(rev, cscf->client_header_timeout);
}
}
return ngx_http_v2_state_save(h2c, pos, end, handler);
}
static u_char *
ngx_http_v2_connection_error(ngx_http_v2_connection_t *h2c,
ngx_uint_t err)
{
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
"http2 state connection error");
ngx_http_v2_finalize_connection(h2c, err);
return NULL;
}
static ngx_int_t
ngx_http_v2_parse_int(ngx_http_v2_connection_t *h2c, u_char **pos, u_char *end,
ngx_uint_t prefix)
{
u_char *start, *p;
ngx_uint_t value, octet, shift;
start = *pos;
p = start;
value = *p++ & prefix;
if (value != prefix) {
if (h2c->state.length == 0) {
return NGX_ERROR;
}
h2c->state.length--;
*pos = p;
return value;
}
if (end - start > NGX_HTTP_V2_INT_OCTETS) {
end = start + NGX_HTTP_V2_INT_OCTETS;
}
for (shift = 0; p != end; shift += 7) {
octet = *p++;
value += (octet & 0x7f) << shift;
if (octet < 128) {
if ((size_t) (p - start) > h2c->state.length) {
return NGX_ERROR;
}
h2c->state.length -= p - start;
*pos = p;
return value;
}
}
if ((size_t) (end - start) >= h2c->state.length) {
return NGX_ERROR;