# HG changeset patch # User Roman Arutyunyan # Date 1584528395 -10800 # Node ID 268f4389130de67bdbc7ae53a3a40fceed3081ce # Parent 714a19dba6af855f7752708034ad88a87011dc66 Refactored HTTP/3 parser. diff --git a/auto/modules b/auto/modules --- a/auto/modules +++ b/auto/modules @@ -412,8 +412,10 @@ if [ $HTTP = YES ]; then ngx_module_name=ngx_http_v3_module ngx_module_incs=src/http/v3 - ngx_module_deps=src/http/v3/ngx_http_v3.h + ngx_module_deps="src/http/v3/ngx_http_v3.h \ + src/http/v3/ngx_http_v3_parse.h" ngx_module_srcs="src/http/v3/ngx_http_v3.c \ + src/http/v3/ngx_http_v3_parse.c \ src/http/v3/ngx_http_v3_tables.c \ src/http/v3/ngx_http_v3_streams.c \ src/http/v3/ngx_http_v3_request.c \ diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c --- a/src/http/ngx_http_request.c +++ b/src/http/ngx_http_request.c @@ -1163,7 +1163,7 @@ ngx_http_process_request_line(ngx_event_ switch (r->http_version) { #if (NGX_HTTP_V3) case NGX_HTTP_VERSION_30: - rc = ngx_http_v3_parse_header(r, r->header_in, 1); + rc = ngx_http_v3_parse_header(r, r->header_in); break; #endif @@ -1510,7 +1510,7 @@ ngx_http_process_request_headers(ngx_eve switch (r->http_version) { #if (NGX_HTTP_V3) case NGX_HTTP_VERSION_30: - rc = ngx_http_v3_parse_header(r, r->header_in, 0); + rc = ngx_http_v3_parse_header(r, r->header_in); break; #endif @@ -1547,11 +1547,17 @@ ngx_http_process_request_headers(ngx_eve h->key.len = r->header_name_end - r->header_name_start; h->key.data = r->header_name_start; - h->key.data[h->key.len] = '\0'; + + if (h->key.data[h->key.len]) { + h->key.data[h->key.len] = '\0'; + } h->value.len = r->header_end - r->header_start; h->value.data = r->header_start; - h->value.data[h->value.len] = '\0'; + + if (h->value.data[h->value.len]) { + h->value.data[h->value.len] = '\0'; + } h->lowcase_key = ngx_pnalloc(r->pool, h->key.len); if (h->lowcase_key == NULL) { diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h --- a/src/http/ngx_http_request.h +++ b/src/http/ngx_http_request.h @@ -595,14 +595,7 @@ struct ngx_http_request_s { u_char *port_end; #if (NGX_HTTP_V3) - ngx_uint_t h3_length; - ngx_uint_t h3_index; - ngx_uint_t h3_insert_count; - ngx_uint_t h3_sign; - ngx_uint_t h3_delta_base; - ngx_uint_t h3_huffman; - ngx_uint_t h3_dynamic; - ngx_uint_t h3_offset; + void *h3_parse; #endif unsigned http_minor:16; diff --git a/src/http/v3/ngx_http_v3.c b/src/http/v3/ngx_http_v3.c --- a/src/http/v3/ngx_http_v3.c +++ b/src/http/v3/ngx_http_v3.c @@ -94,83 +94,3 @@ ngx_http_v3_encode_prefix_int(u_char *p, return (uintptr_t) p; } - - -uint64_t -ngx_http_v3_decode_varlen_int(u_char *p) -{ - uint64_t value; - ngx_uint_t len; - - len = *p >> 6; - value = *p & 0x3f; - - while (len--) { - value = (value << 8) + *p++; - } - - return value; -} - - -int64_t -ngx_http_v3_decode_prefix_int(u_char **src, size_t len, ngx_uint_t prefix) -{ - u_char *p; - int64_t value, thresh; - - if (len == 0) { - return NGX_ERROR; - } - - p = *src; - - thresh = (1 << prefix) - 1; - value = *p++ & thresh; - - if (value != thresh) { - *src = p; - return value; - } - - value = 0; - - /* XXX handle overflows */ - - while (--len) { - value = (value << 7) + (*p & 0x7f); - if ((*p++ & 0x80) == 0) { - *src = p; - return value + thresh; - } - } - - return NGX_ERROR; -} - - -ngx_int_t -ngx_http_v3_decode_huffman(ngx_connection_t *c, ngx_str_t *s) -{ - u_char state, *p, *data; - - state = 0; - - p = ngx_pnalloc(c->pool, s->len * 8 / 5); - if (p == NULL) { - return NGX_ERROR; - } - - data = p; - - if (ngx_http_v2_huff_decode(&state, s->data, s->len, &p, 1, c->log) - != NGX_OK) - { - return NGX_ERROR; - } - - s->len = p - data; - s->data = data; - - return NGX_OK; -} diff --git a/src/http/v3/ngx_http_v3.h b/src/http/v3/ngx_http_v3.h --- a/src/http/v3/ngx_http_v3.h +++ b/src/http/v3/ngx_http_v3.h @@ -12,13 +12,31 @@ #include #include #include +#include #define NGX_HTTP_V3_STREAM 0x48335354 /* "H3ST" */ -#define NGX_HTTP_V3_VARLEN_INT_LEN 4 -#define NGX_HTTP_V3_PREFIX_INT_LEN 11 +#define NGX_HTTP_V3_VARLEN_INT_LEN 4 +#define NGX_HTTP_V3_PREFIX_INT_LEN 11 + +#define NGX_HTTP_V3_STREAM_CONTROL 0x00 +#define NGX_HTTP_V3_STREAM_PUSH 0x01 +#define NGX_HTTP_V3_STREAM_ENCODER 0x02 +#define NGX_HTTP_V3_STREAM_DECODER 0x03 + +#define NGX_HTTP_V3_FRAME_DATA 0x00 +#define NGX_HTTP_V3_FRAME_HEADERS 0x01 +#define NGX_HTTP_V3_FRAME_CANCEL_PUSH 0x03 +#define NGX_HTTP_V3_FRAME_SETTINGS 0x04 +#define NGX_HTTP_V3_FRAME_PUSH_PROMISE 0x05 +#define NGX_HTTP_V3_FRAME_GOAWAY 0x07 +#define NGX_HTTP_V3_FRAME_MAX_PUSH_ID 0x0d + +#define NGX_HTTP_V3_PARAM_MAX_TABLE_CAPACITY 0x01 +#define NGX_HTTP_V3_PARAM_MAX_HEADER_LIST_SIZE 0x06 +#define NGX_HTTP_V3_PARAM_BLOCKED_STREAMS 0x07 typedef struct { @@ -28,8 +46,11 @@ typedef struct { ngx_connection_t *client_encoder; ngx_connection_t *client_decoder; + ngx_connection_t *client_control; + ngx_connection_t *server_encoder; ngx_connection_t *server_decoder; + ngx_connection_t *server_control; } ngx_http_v3_connection_t; @@ -39,18 +60,12 @@ typedef struct { } ngx_http_v3_header_t; -ngx_int_t ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b, - ngx_uint_t pseudo); +ngx_int_t ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b); ngx_chain_t *ngx_http_v3_create_header(ngx_http_request_t *r); - uintptr_t ngx_http_v3_encode_varlen_int(u_char *p, uint64_t value); uintptr_t ngx_http_v3_encode_prefix_int(u_char *p, uint64_t value, ngx_uint_t prefix); -uint64_t ngx_http_v3_decode_varlen_int(u_char *p); -int64_t ngx_http_v3_decode_prefix_int(u_char **src, size_t len, - ngx_uint_t prefix); -ngx_int_t ngx_http_v3_decode_huffman(ngx_connection_t *c, ngx_str_t *s); void ngx_http_v3_handle_client_uni_stream(ngx_connection_t *c); @@ -67,6 +82,8 @@ ngx_http_v3_header_t *ngx_http_v3_lookup ngx_uint_t dynamic, ngx_uint_t index); ngx_int_t ngx_http_v3_check_insert_count(ngx_connection_t *c, ngx_uint_t insert_count); +ngx_int_t ngx_http_v3_set_param(ngx_connection_t *c, uint64_t id, + uint64_t value); ngx_int_t ngx_http_v3_client_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic, ngx_uint_t index, ngx_str_t *value); diff --git a/src/http/v3/ngx_http_v3_parse.c b/src/http/v3/ngx_http_v3_parse.c new file mode 100644 --- /dev/null +++ b/src/http/v3/ngx_http_v3_parse.c @@ -0,0 +1,1424 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#include +#include +#include + + +ngx_int_t +ngx_http_v3_parse_varlen_int(ngx_connection_t *c, + ngx_http_v3_parse_varlen_int_t *st, u_char ch) +{ + enum { + sw_start = 0, + sw_length_2, + sw_length_3, + sw_length_4, + sw_length_5, + sw_length_6, + sw_length_7, + sw_length_8 + }; + + switch (st->state) { + + case sw_start: + + st->value = ch; + if (st->value & 0xc0) { + st->state = sw_length_2; + break; + } + + goto done; + + case sw_length_2: + + st->value = (st->value << 8) + ch; + if ((st->value & 0xc000) == 0x4000) { + st->value &= 0x3fff; + goto done; + } + + st->state = sw_length_3; + break; + + case sw_length_4: + + st->value = (st->value << 8) + ch; + if ((st->value & 0xc0000000) == 0x80000000) { + st->value &= 0x3fffffff; + goto done; + } + + st->state = sw_length_5; + break; + + case sw_length_3: + case sw_length_5: + case sw_length_6: + case sw_length_7: + + st->value = (st->value << 8) + ch; + st->state++; + break; + + case sw_length_8: + + st->value = (st->value << 8) + ch; + st->value &= 0x3fffffffffffffff; + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse varlen int %uL", st->value); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_prefix_int(ngx_connection_t *c, + ngx_http_v3_parse_prefix_int_t *st, ngx_uint_t prefix, u_char ch) +{ + enum { + sw_start = 0, + sw_value + }; + + switch (st->state) { + + case sw_start: + + st->mask = (1 << prefix) - 1; + st->value = (ch & st->mask); + + if (st->value != st->mask) { + goto done; + } + + st->value = 0; + st->state = sw_value; + break; + + case sw_value: + + st->value = (st->value << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + st->value += st->mask; + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse prefix int %uL", st->value); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_headers(ngx_connection_t *c, ngx_http_v3_parse_headers_t *st, + u_char ch) +{ + ngx_int_t rc; + enum { + sw_start = 0, + sw_length, + sw_prefix, + sw_header_rep, + sw_done + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse headers"); + + if (ch != NGX_HTTP_V3_FRAME_HEADERS) { + return NGX_ERROR; + } + + st->state = sw_length; + break; + + case sw_length: + + if (ngx_http_v3_parse_varlen_int(c, &st->vlint, ch) != NGX_DONE) { + break; + } + + st->length = st->vlint.value; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse headers len:%ui", st->length); + + st->state = sw_prefix; + break; + + case sw_prefix: + + if (st->length-- == 0) { + return NGX_ERROR; + } + + rc = ngx_http_v3_parse_header_block_prefix(c, &st->prefix, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc != NGX_DONE) { + break; + } + + if (st->length == 0) { + return NGX_ERROR; + } + + st->state = sw_header_rep; + break; + + case sw_header_rep: + + rc = ngx_http_v3_parse_header_rep(c, &st->header_rep, st->prefix.base, + ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (--st->length == 0) { + if (rc != NGX_DONE) { +ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "XXX len"); + return NGX_ERROR; + } + + goto done; + } + + if (rc == NGX_DONE) { + return NGX_OK; + } + + break; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse headers done"); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_block_prefix(ngx_connection_t *c, + ngx_http_v3_parse_header_block_prefix_t *st, u_char ch) +{ + enum { + sw_start = 0, + sw_req_insert_count, + sw_delta_base, + sw_read_delta_base + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header block prefix"); + + st->state = sw_req_insert_count; + + /* fall through */ + + case sw_req_insert_count: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 8, ch) != NGX_DONE) { + break; + } + + st->insert_count = st->pint.value; + st->state = sw_delta_base; + break; + + case sw_delta_base: + + st->sign = (ch & 0x80) ? 1 : 0; + st->state = sw_read_delta_base; + + /* fall through */ + + case sw_read_delta_base: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 7, ch) != NGX_DONE) { + break; + } + + st->delta_base = st->pint.value; + goto done; + } + + return NGX_AGAIN; + +done: + + if (st->sign) { + st->base = st->insert_count - st->delta_base - 1; + } else { + st->base = st->insert_count + st->delta_base; + } + + ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header block prefix done " + "i:%ui, s:%ui, d:%ui, base:%uL", + st->insert_count, st->sign, st->delta_base, st->base); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_rep(ngx_connection_t *c, + ngx_http_v3_parse_header_rep_t *st, ngx_uint_t base, u_char ch) +{ + ngx_int_t rc; + enum { + sw_start = 0, + sw_header_ri, + sw_header_lri, + sw_header_l, + sw_header_pbi, + sw_header_lpbi + }; + + if (st->state == sw_start) { + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header representation"); + + ngx_memzero(&st->header, sizeof(ngx_http_v3_parse_header_t)); + + st->header.base = base; + + if (ch & 0x80) { + /* Indexed Header Field */ + + st->state = sw_header_ri; + + } else if (ch & 0x40) { + /* Literal Header Field With Name Reference */ + + st->state = sw_header_lri; + + } else if (ch & 0x20) { + /* Literal Header Field Without Name Reference */ + + st->state = sw_header_l; + + } else if (ch & 0x10) { + /* Indexed Header Field With Post-Base Index */ + + st->state = sw_header_pbi; + + } else { + /* Literal Header Field With Post-Base Name Reference */ + + st->state = sw_header_lpbi; + } + } + + switch (st->state) { + + case sw_header_ri: + rc = ngx_http_v3_parse_header_ri(c, &st->header, ch); + break; + + case sw_header_lri: + rc = ngx_http_v3_parse_header_lri(c, &st->header, ch); + break; + + case sw_header_l: + rc = ngx_http_v3_parse_header_l(c, &st->header, ch); + break; + + case sw_header_pbi: + rc = ngx_http_v3_parse_header_pbi(c, &st->header, ch); + break; + + case sw_header_lpbi: + rc = ngx_http_v3_parse_header_lpbi(c, &st->header, ch); + break; + + default: + rc = NGX_OK; + } + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_AGAIN) { + return NGX_AGAIN; + } + + /* rc == NGX_DONE */ + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header representation done"); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_literal(ngx_connection_t *c, ngx_http_v3_parse_literal_t *st, + u_char ch) +{ + ngx_uint_t n; + enum { + sw_start = 0, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse literal huff:%ui, len:%ui", + st->huffman, st->length); + + n = st->length; + + if (st->huffman) { + n = n * 8 / 5; + st->huffstate = 0; + } + + st->last = ngx_pnalloc(c->pool, n + 1); + if (st->last == NULL) { + return NGX_ERROR; + } + + st->value.data = st->last; + st->state = sw_value; + + /* fall through */ + + case sw_value: + + if (st->huffman) { + if (ngx_http_v2_huff_decode(&st->huffstate, &ch, 1, &st->last, + st->length == 1, c->log) + != NGX_OK) + { + return NGX_ERROR; + } + + } else { + *st->last++ = ch; + } + + if (--st->length) { + break; + } + + st->value.len = st->last - st->value.data; + *st->last = '\0'; + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse literal done \"%V\"", &st->value); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_ri(ngx_connection_t *c, ngx_http_v3_parse_header_t *st, + u_char ch) +{ + ngx_http_v3_header_t *h; + enum { + sw_start = 0, + sw_index + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header ri"); + + st->dynamic = (ch & 0x40) ? 0 : 1; + st->state = sw_index; + + /* fall through */ + + case sw_index: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 6, ch) != NGX_DONE) { + break; + } + + st->index = st->pint.value; + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header ri done %s%ui]", + st->dynamic ? "dynamic[-" : "static[", st->index); + + if (st->dynamic) { + st->index = st->base - st->index - 1; + } + + h = ngx_http_v3_lookup_table(c, st->dynamic, st->index); + if (h == NULL) { + return NGX_ERROR; + } + + st->name = h->name; + st->value = h->value; + st->state = sw_start; + + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_lri(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch) +{ + ngx_int_t rc; + ngx_http_v3_header_t *h; + enum { + sw_start = 0, + sw_index, + sw_value_len, + sw_read_value_len, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header lri"); + + st->dynamic = (ch & 0x10) ? 0 : 1; + st->state = sw_index; + + /* fall through */ + + case sw_index: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 4, ch) != NGX_DONE) { + break; + } + + st->index = st->pint.value; + st->state = sw_value_len; + break; + + case sw_value_len: + + st->literal.huffman = (ch & 0x80) ? 1 : 0; + st->state = sw_read_value_len; + + /* fall through */ + + case sw_read_value_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 7, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + goto done; + } + + st->state = sw_value; + break; + + case sw_value: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->value = st->literal.value; + goto done; + } + + break; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header lri done %s%ui] \"%V\"", + st->dynamic ? "dynamic[-" : "static[", + st->index, &st->value); + + if (st->dynamic) { + st->index = st->base - st->index - 1; + } + + h = ngx_http_v3_lookup_table(c, st->dynamic, st->index); + if (h == NULL) { + return NGX_ERROR; + } + + st->name = h->name; + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_l(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch) +{ + ngx_int_t rc; + enum { + sw_start = 0, + sw_name_len, + sw_name, + sw_value_len, + sw_read_value_len, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header l"); + + st->literal.huffman = (ch & 0x08) ? 1 : 0; + st->state = sw_name_len; + + /* fall through */ + + case sw_name_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 3, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + return NGX_ERROR; + } + + st->state = sw_name; + break; + + case sw_name: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->name = st->literal.value; + st->state = sw_value_len; + } + + break; + + case sw_value_len: + + st->literal.huffman = (ch & 0x80) ? 1 : 0; + st->state = sw_read_value_len; + + /* fall through */ + + case sw_read_value_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 7, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + goto done; + } + + st->state = sw_value; + break; + + case sw_value: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->value = st->literal.value; + goto done; + } + + break; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header l done \"%V\" \"%V\"", + &st->name, &st->value); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_pbi(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch) +{ + ngx_http_v3_header_t *h; + enum { + sw_start = 0, + sw_index + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header pbi"); + + st->state = sw_index; + + /* fall through */ + + case sw_index: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 4, ch) != NGX_DONE) { + break; + } + + st->index = st->pint.value; + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header pbi done dynamic[+%ui]", st->index); + + h = ngx_http_v3_lookup_table(c, 1, st->base + st->index); + if (h == NULL) { + return NGX_ERROR; + } + + st->name = h->name; + st->value = h->value; + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_lpbi(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch) +{ + ngx_int_t rc; + ngx_http_v3_header_t *h; + enum { + sw_start = 0, + sw_index, + sw_value_len, + sw_read_value_len, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header lpbi"); + + st->state = sw_index; + + /* fall through */ + + case sw_index: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 3, ch) != NGX_DONE) { + break; + } + + st->index = st->pint.value; + st->state = sw_value_len; + break; + + case sw_value_len: + + st->literal.huffman = (ch & 0x80) ? 1 : 0; + st->state = sw_read_value_len; + + /* fall through */ + + case sw_read_value_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 7, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + goto done; + } + + st->state = sw_value; + break; + + case sw_value: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->value = st->literal.value; + goto done; + } + + break; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header lpbi done dynamic[+%ui] \"%V\"", + st->index, &st->value); + + h = ngx_http_v3_lookup_table(c, 1, st->base + st->index); + if (h == NULL) { + return NGX_ERROR; + } + + st->name = h->name; + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch) +{ + ngx_http_v3_parse_control_t *st = data; + + ngx_int_t rc; + enum { + sw_start = 0, + sw_type, + sw_length, + sw_settings, + sw_max_push_id, + sw_skip + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse control"); + + st->state = sw_type; + + /* fall through */ + + case sw_type: + + if (ngx_http_v3_parse_varlen_int(c, &st->vlint, ch) != NGX_DONE) { + break; + } + + st->type = st->vlint.value; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse frame type:%ui", st->type); + + st->state = sw_length; + break; + + case sw_length: + + if (ngx_http_v3_parse_varlen_int(c, &st->vlint, ch) != NGX_DONE) { + break; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse frame len:%uL", st->vlint.value); + + st->length = st->vlint.value; + if (st->length == 0) { + st->state = sw_type; + break; + } + + switch (st->type) { + + case NGX_HTTP_V3_FRAME_SETTINGS: + st->state = sw_settings; + break; + + case NGX_HTTP_V3_FRAME_MAX_PUSH_ID: + st->state = sw_max_push_id; + break; + + default: + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse skip unknown frame"); + st->state = sw_skip; + } + + break; + + case sw_settings: + + rc = ngx_http_v3_parse_settings(c, &st->settings, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (--st->length > 0) { + break; + } + + if (rc != NGX_DONE) { + return NGX_ERROR; + } + + st->state = sw_type; + break; + + case sw_max_push_id: + + if (ngx_http_v3_parse_varlen_int(c, &st->vlint, ch) != NGX_DONE) { + break; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse MAX_PUSH_ID:%uL", st->vlint.value); + + st->state = sw_type; + break; + + case sw_skip: + + if (--st->length == 0) { + st->state = sw_type; + } + + break; + } + + return NGX_AGAIN; +} + + +ngx_int_t +ngx_http_v3_parse_settings(ngx_connection_t *c, + ngx_http_v3_parse_settings_t *st, u_char ch) +{ + enum { + sw_start = 0, + sw_id, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse settings"); + + st->state = sw_id; + + /* fall through */ + + case sw_id: + + if (ngx_http_v3_parse_varlen_int(c, &st->vlint, ch) != NGX_DONE) { + break; + } + + st->id = st->vlint.value; + st->state = sw_value; + break; + + case sw_value: + + if (ngx_http_v3_parse_varlen_int(c, &st->vlint, ch) != NGX_DONE) { + break; + } + + if (ngx_http_v3_set_param(c, st->id, st->vlint.value) != NGX_OK) { + return NGX_ERROR; + } + + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse settings done"); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_encoder(ngx_connection_t *c, void *data, u_char ch) +{ + ngx_http_v3_parse_encoder_t *st = data; + + ngx_int_t rc; + enum { + sw_start = 0, + sw_inr, + sw_iwnr, + sw_capacity, + sw_duplicate + }; + + if (st->state == sw_start) { + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse encoder instruction"); + + if (ch & 0x80) { + /* Insert With Name Reference */ + + st->state = sw_inr; + + } else if (ch & 0x40) { + /* Insert Without Name Reference */ + + st->state = sw_iwnr; + + } else if (ch & 0x20) { + /* Set Dynamic Table Capacity */ + + st->state = sw_capacity; + + } else { + /* Duplicate */ + + st->state = sw_duplicate; + } + } + + switch (st->state) { + + case sw_inr: + + rc = ngx_http_v3_parse_header_inr(c, &st->header, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc != NGX_DONE) { + break; + } + + goto done; + + case sw_iwnr: + + rc = ngx_http_v3_parse_header_iwnr(c, &st->header, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc != NGX_DONE) { + break; + } + + goto done; + + case sw_capacity: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 5, ch) != NGX_DONE) { + break; + } + + if (ngx_http_v3_set_capacity(c, st->pint.value) != NGX_OK) { + return NGX_ERROR; + } + + goto done; + + case sw_duplicate: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 5, ch) != NGX_DONE) { + break; + } + + if (ngx_http_v3_duplicate(c, st->pint.value) != NGX_OK) { + return NGX_ERROR; + } + + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse encoder instruction done"); + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_inr(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch) +{ + ngx_int_t rc; + enum { + sw_start = 0, + sw_name_index, + sw_value_len, + sw_read_value_len, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header inr"); + + st->dynamic = (ch & 0x40) ? 0 : 1; + st->state = sw_name_index; + + /* fall through */ + + case sw_name_index: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 6, ch) != NGX_DONE) { + break; + } + + st->index = st->pint.value; + st->state = sw_value_len; + break; + + case sw_value_len: + + st->literal.huffman = (ch & 0x80) ? 1 : 0; + st->state = sw_read_value_len; + + /* fall through */ + + case sw_read_value_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 7, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + goto done; + } + + st->state = sw_value; + break; + + case sw_value: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->value = st->literal.value; + goto done; + } + + break; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header inr done %s[%ui] \"%V\"", + st->dynamic ? "dynamic" : "static", + st->index, &st->value); + + if (ngx_http_v3_ref_insert(c, st->dynamic, st->index, &st->value) != NGX_OK) + { + return NGX_ERROR; + } + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_header_iwnr(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch) +{ + ngx_int_t rc; + enum { + sw_start = 0, + sw_name_len, + sw_name, + sw_value_len, + sw_read_value_len, + sw_value + }; + + switch (st->state) { + + case sw_start: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header iwnr"); + + st->literal.huffman = (ch & 0x20) ? 1 : 0; + st->state = sw_name_len; + + /* fall through */ + + case sw_name_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 5, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + return NGX_ERROR; + } + + st->state = sw_name; + break; + + case sw_name: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->name = st->literal.value; + st->state = sw_value_len; + } + + break; + + case sw_value_len: + + st->literal.huffman = (ch & 0x80) ? 1 : 0; + st->state = sw_read_value_len; + + /* fall through */ + + case sw_read_value_len: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 7, ch) != NGX_DONE) { + break; + } + + st->literal.length = st->pint.value; + if (st->literal.length == 0) { + goto done; + } + + st->state = sw_value; + break; + + case sw_value: + + rc = ngx_http_v3_parse_literal(c, &st->literal, ch); + + if (rc == NGX_ERROR) { + return NGX_ERROR; + } + + if (rc == NGX_DONE) { + st->value = st->literal.value; + goto done; + } + + break; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header iwnr done \"%V\":\"%V\"", + &st->name, &st->value); + + if (ngx_http_v3_insert(c, &st->name, &st->value) != NGX_OK) { + return NGX_ERROR; + } + + st->state = sw_start; + return NGX_DONE; +} + + +ngx_int_t +ngx_http_v3_parse_decoder(ngx_connection_t *c, void *data, u_char ch) +{ + ngx_http_v3_parse_decoder_t *st = data; + + enum { + sw_start = 0, + sw_ack_header, + sw_cancel_stream, + sw_inc_insert_count + }; + + if (st->state == sw_start) { + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse decoder instruction"); + + if (ch & 0x80) { + /* Header Acknowledgement */ + + st->state = sw_ack_header; + + } else if (ch & 0x40) { + /* Stream Cancellation */ + + st->state = sw_cancel_stream; + + } else { + /* Insert Count Increment */ + + st->state = sw_inc_insert_count; + } + } + + switch (st->state) { + + case sw_ack_header: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 6, ch) != NGX_DONE) { + break; + } + + if (ngx_http_v3_ack_header(c, st->pint.value) != NGX_OK) { + return NGX_ERROR; + } + + goto done; + + case sw_cancel_stream: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 6, ch) != NGX_DONE) { + break; + } + + if (ngx_http_v3_cancel_stream(c, st->pint.value) != NGX_OK) { + return NGX_ERROR; + } + + goto done; + + case sw_inc_insert_count: + + if (ngx_http_v3_parse_prefix_int(c, &st->pint, 6, ch) != NGX_DONE) { + break; + } + + if (ngx_http_v3_inc_insert_count(c, st->pint.value) != NGX_OK) { + return NGX_ERROR; + } + + goto done; + } + + return NGX_AGAIN; + +done: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse decoder instruction done"); + + st->state = sw_start; + return NGX_DONE; +} diff --git a/src/http/v3/ngx_http_v3_parse.h b/src/http/v3/ngx_http_v3_parse.h new file mode 100644 --- /dev/null +++ b/src/http/v3/ngx_http_v3_parse.h @@ -0,0 +1,145 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#ifndef _NGX_HTTP_V3_PARSE_H_INCLUDED_ +#define _NGX_HTTP_V3_PARSE_H_INCLUDED_ + + +#include +#include +#include + + +typedef struct { + ngx_uint_t state; + uint64_t value; +} ngx_http_v3_parse_varlen_int_t; + + +typedef struct { + ngx_uint_t state; + ngx_uint_t mask; + uint64_t value; +} ngx_http_v3_parse_prefix_int_t; + + +typedef struct { + ngx_uint_t state; + uint64_t id; + ngx_http_v3_parse_varlen_int_t vlint; +} ngx_http_v3_parse_settings_t; + + +typedef struct { + ngx_uint_t state; + ngx_uint_t insert_count; + ngx_uint_t delta_base; + ngx_uint_t sign; + ngx_uint_t base; + ngx_http_v3_parse_prefix_int_t pint; +} ngx_http_v3_parse_header_block_prefix_t; + + +typedef struct { + ngx_uint_t state; + ngx_uint_t length; + ngx_uint_t huffman; + ngx_str_t value; + u_char *last; + u_char huffstate; +} ngx_http_v3_parse_literal_t; + + +typedef struct { + ngx_uint_t state; + ngx_uint_t index; + ngx_uint_t base; + ngx_uint_t dynamic; + + ngx_str_t name; + ngx_str_t value; + + ngx_http_v3_parse_prefix_int_t pint; + ngx_http_v3_parse_literal_t literal; +} ngx_http_v3_parse_header_t; + + +typedef struct { + ngx_uint_t state; + ngx_http_v3_parse_header_t header; +} ngx_http_v3_parse_header_rep_t; + + +typedef struct { + ngx_uint_t state; + ngx_uint_t length; + ngx_http_v3_parse_varlen_int_t vlint; + ngx_http_v3_parse_header_block_prefix_t prefix; + ngx_http_v3_parse_header_rep_t header_rep; +} ngx_http_v3_parse_headers_t; + + +typedef struct { + ngx_uint_t state; + ngx_http_v3_parse_header_t header; + ngx_http_v3_parse_prefix_int_t pint; +} ngx_http_v3_parse_encoder_t; + + +typedef struct { + ngx_uint_t state; + ngx_http_v3_parse_prefix_int_t pint; +} ngx_http_v3_parse_decoder_t; + + +typedef struct { + ngx_uint_t state; + ngx_uint_t type; + ngx_uint_t length; + ngx_http_v3_parse_varlen_int_t vlint; + ngx_http_v3_parse_settings_t settings; +} ngx_http_v3_parse_control_t; + + +ngx_int_t ngx_http_v3_parse_varlen_int(ngx_connection_t *c, + ngx_http_v3_parse_varlen_int_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_prefix_int(ngx_connection_t *c, + ngx_http_v3_parse_prefix_int_t *st, ngx_uint_t prefix, u_char ch); + +ngx_int_t ngx_http_v3_parse_headers(ngx_connection_t *c, + ngx_http_v3_parse_headers_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_block_prefix(ngx_connection_t *c, + ngx_http_v3_parse_header_block_prefix_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_rep(ngx_connection_t *c, + ngx_http_v3_parse_header_rep_t *st, ngx_uint_t base, u_char ch); +ngx_int_t ngx_http_v3_parse_literal(ngx_connection_t *c, + ngx_http_v3_parse_literal_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_ri(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_lri(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_l(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_pbi(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_lpbi(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); + +ngx_int_t ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch); +ngx_int_t ngx_http_v3_parse_settings(ngx_connection_t *c, + ngx_http_v3_parse_settings_t *st, u_char ch); + +ngx_int_t ngx_http_v3_parse_encoder(ngx_connection_t *c, void *data, u_char ch); +ngx_int_t ngx_http_v3_parse_header_inr(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); +ngx_int_t ngx_http_v3_parse_header_iwnr(ngx_connection_t *c, + ngx_http_v3_parse_header_t *st, u_char ch); + +ngx_int_t ngx_http_v3_parse_decoder(ngx_connection_t *c, void *data, u_char ch); + + +#endif /* _NGX_HTTP_V3_PARSE_H_INCLUDED_ */ diff --git a/src/http/v3/ngx_http_v3_request.c b/src/http/v3/ngx_http_v3_request.c --- a/src/http/v3/ngx_http_v3_request.c +++ b/src/http/v3/ngx_http_v3_request.c @@ -10,15 +10,6 @@ #include -#define NGX_HTTP_V3_FRAME_DATA 0x00 -#define NGX_HTTP_V3_FRAME_HEADERS 0x01 -#define NGX_HTTP_V3_FRAME_CANCEL_PUSH 0x03 -#define NGX_HTTP_V3_FRAME_SETTINGS 0x04 -#define NGX_HTTP_V3_FRAME_PUSH_PROMISE 0x05 -#define NGX_HTTP_V3_FRAME_GOAWAY 0x07 -#define NGX_HTTP_V3_FRAME_MAX_PUSH_ID 0x0d - - static ngx_int_t ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name, ngx_str_t *value); @@ -47,6 +38,110 @@ struct { ngx_int_t +ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b) +{ + ngx_int_t rc; + ngx_str_t *name, *value; + ngx_connection_t *c; + ngx_http_v3_parse_headers_t *st; + enum { + sw_start = 0, + sw_prev, + sw_headers, + sw_last, + sw_done + }; + + c = r->connection; + st = r->h3_parse; + + if (st == NULL) { + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header"); + + st = ngx_pcalloc(c->pool, sizeof(ngx_http_v3_parse_headers_t)); + if (st == NULL) { + goto failed; + } + + r->h3_parse = st; + } + + switch (r->state) { + + case sw_prev: + r->state = sw_headers; + return NGX_OK; + + case sw_done: + goto done; + + case sw_last: + r->state = sw_done; + return NGX_OK; + + default: + break; + } + + for ( /* void */ ; b->pos < b->last; b->pos++) { + + rc = ngx_http_v3_parse_headers(c, st, *b->pos); + + if (rc == NGX_ERROR) { + goto failed; + } + + if (rc == NGX_AGAIN) { + continue; + } + + name = &st->header_rep.header.name; + value = &st->header_rep.header.value; + + if (r->state == sw_start + && ngx_http_v3_process_pseudo_header(r, name, value) != NGX_OK) + { + if (rc == NGX_DONE) { + r->state = sw_last; + } else { + r->state = sw_prev; + } + + } else if (rc == NGX_DONE) { + r->state = sw_done; + } + + if (r->state == sw_start) { + continue; + } + + r->header_name_start = name->data; + r->header_name_end = name->data + name->len; + r->header_start = value->data; + r->header_end = value->data + value->len; + r->header_hash = ngx_hash_key(name->data, name->len); + + /* XXX r->lowcase_index = i; */ + + return NGX_OK; + } + + return NGX_AGAIN; + +failed: + + return r->state == sw_start ? NGX_HTTP_PARSE_INVALID_REQUEST + : NGX_HTTP_PARSE_INVALID_HEADER; + +done: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header done"); + + return NGX_HTTP_PARSE_HEADER_DONE; +} + +#if 0 +ngx_int_t ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b, ngx_uint_t pseudo) { u_char *p, ch; @@ -167,11 +262,14 @@ again: break; } - /* fall through */ + length &= 0x3fffff; + state = sw_header_block; + break; case sw_length_3: - length &= 0x3fffff; + length = (length << 8) + ch; + length &= 0x3fffffff; state = sw_header_block; break; @@ -567,6 +665,7 @@ failed: return NGX_HTTP_PARSE_INVALID_REQUEST; } +#endif static ngx_int_t @@ -576,6 +675,10 @@ ngx_http_v3_process_pseudo_header(ngx_ht ngx_uint_t i; ngx_connection_t *c; + if (name->len == 0 || name->data[0] != ':') { + return NGX_DECLINED; + } + c = r->connection; if (name->len == 7 && ngx_strncmp(name->data, ":method", 7) == 0) { @@ -635,14 +738,10 @@ ngx_http_v3_process_pseudo_header(ngx_ht return NGX_OK; } - if (name->len && name->data[0] == ':') { - ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, - "http3 unknown pseudo header \"%V\" \"%V\"", - name, value); - return NGX_OK; - } + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 unknown pseudo header \"%V\" \"%V\"", name, value); - return NGX_DONE; + return NGX_OK; } @@ -789,7 +888,7 @@ ngx_http_v3_create_header(ngx_http_reque b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 25, 4); *b->last = 0; b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 3, 7); - b->last = ngx_sprintf(b->last, "%03ui ", r->headers_out.status); + b->last = ngx_sprintf(b->last, "%03ui", r->headers_out.status); } if (r->headers_out.server == NULL) { diff --git a/src/http/v3/ngx_http_v3_streams.c b/src/http/v3/ngx_http_v3_streams.c --- a/src/http/v3/ngx_http_v3_streams.c +++ b/src/http/v3/ngx_http_v3_streams.c @@ -10,42 +10,30 @@ #include -#define NGX_HTTP_V3_CONTROL_STREAM 0x00 -#define NGX_HTTP_V3_PUSH_STREAM 0x01 -#define NGX_HTTP_V3_ENCODER_STREAM 0x02 -#define NGX_HTTP_V3_DECODER_STREAM 0x03 +typedef ngx_int_t (*ngx_http_v3_handler_pt)(ngx_connection_t *c, void *data, + u_char ch); typedef struct { - uint32_t signature; /* QSTR */ - u_char buf[4]; + uint32_t signature; /* QSTR */ - ngx_uint_t len; - ngx_uint_t type; - ngx_uint_t state; - ngx_uint_t index; - ngx_uint_t offset; + ngx_http_v3_handler_pt handler; + void *data; - ngx_str_t name; - ngx_str_t value; - - unsigned client:1; - unsigned dynamic:1; - unsigned huffman:1; + ngx_uint_t type; + ngx_uint_t client; /* unsigned client:1; */ } ngx_http_v3_uni_stream_t; static void ngx_http_v3_close_uni_stream(ngx_connection_t *c); static void ngx_http_v3_uni_stream_cleanup(void *data); static void ngx_http_v3_read_uni_stream_type(ngx_event_t *rev); -static void ngx_http_v3_dummy_stream_handler(ngx_event_t *rev); -static void ngx_http_v3_client_encoder_handler(ngx_event_t *rev); -static void ngx_http_v3_client_decoder_handler(ngx_event_t *rev); - +static void ngx_http_v3_uni_read_handler(ngx_event_t *rev); static ngx_connection_t *ngx_http_v3_create_uni_stream(ngx_connection_t *c, ngx_uint_t type); -static ngx_connection_t *ngx_http_v3_get_server_encoder(ngx_connection_t *c); -static ngx_connection_t *ngx_http_v3_get_server_decoder(ngx_connection_t *c); +static ngx_connection_t *ngx_http_v3_get_control(ngx_connection_t *c); +static ngx_connection_t *ngx_http_v3_get_encoder(ngx_connection_t *c); +static ngx_connection_t *ngx_http_v3_get_decoder(ngx_connection_t *c); void @@ -56,8 +44,13 @@ ngx_http_v3_handle_client_uni_stream(ngx c->log->connection = c->number; + /* XXX */ + (void) ngx_http_v3_get_control(c); + (void) ngx_http_v3_get_encoder(c); + (void) ngx_http_v3_get_decoder(c); + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, - "http3 new uni stream id:0x%uXL", c->qs->id); + "http3 new uni stream id:0x%uxL", c->qs->id); us = ngx_pcalloc(c->pool, sizeof(ngx_http_v3_uni_stream_t)); if (us == NULL) { @@ -81,7 +74,7 @@ ngx_http_v3_handle_client_uni_stream(ngx cln->data = c; c->read->handler = ngx_http_v3_read_uni_stream_type; - c->read->handler(c->read); + ngx_http_v3_read_uni_stream_type(c->read); } @@ -115,7 +108,7 @@ ngx_http_v3_uni_stream_cleanup(void *dat switch (us->type) { - case NGX_HTTP_V3_ENCODER_STREAM: + case NGX_HTTP_V3_STREAM_ENCODER: if (us->client) { h3c->client_encoder = NULL; @@ -125,7 +118,7 @@ ngx_http_v3_uni_stream_cleanup(void *dat break; - case NGX_HTTP_V3_DECODER_STREAM: + case NGX_HTTP_V3_STREAM_DECODER: if (us->client) { h3c->client_decoder = NULL; @@ -134,6 +127,16 @@ ngx_http_v3_uni_stream_cleanup(void *dat } break; + + case NGX_HTTP_V3_STREAM_CONTROL: + + if (us->client) { + h3c->client_control = NULL; + } else { + h3c->server_control = NULL; + } + + break; } } @@ -141,112 +144,96 @@ ngx_http_v3_uni_stream_cleanup(void *dat static void ngx_http_v3_read_uni_stream_type(ngx_event_t *rev) { - u_char *p; - ssize_t n, len; + u_char ch; + ssize_t n; ngx_connection_t *c; ngx_http_v3_connection_t *h3c; - ngx_http_v3_uni_stream_t *us; + ngx_http_v3_uni_stream_t *st; c = rev->data; - us = c->data; + st = c->data; h3c = c->qs->parent->data; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 read stream type"); while (rev->ready) { - p = &us->buf[us->len]; - - if (us->len == 0) { - len = 1; - } else { - len = (us->buf[0] >> 6) + 1 - us->len; - } - - n = c->recv(c, p, len); + n = c->recv(c, &ch, 1); if (n == NGX_ERROR) { goto failed; } - if (n == NGX_AGAIN) { - break; - } - - us->len += n; - - if (n != len) { + if (n == NGX_AGAIN || n != 1) { break; } - if ((us->buf[0] >> 6) + 1 == us->len) { - us->type = ngx_http_v3_decode_varlen_int(us->buf); + st->type = ch; + + switch (st->type) { + + case NGX_HTTP_V3_STREAM_ENCODER: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 encoder stream"); + + if (h3c->client_encoder) { + goto failed; + } + + h3c->client_encoder = c; + st->handler = ngx_http_v3_parse_encoder; + n = sizeof(ngx_http_v3_parse_encoder_t); + + break; + + case NGX_HTTP_V3_STREAM_DECODER: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 decoder stream"); + + if (h3c->client_decoder) { + goto failed; + } + + h3c->client_decoder = c; + st->handler = ngx_http_v3_parse_decoder; + n = sizeof(ngx_http_v3_parse_decoder_t); + + break; + + case NGX_HTTP_V3_STREAM_CONTROL: + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 control stream"); + + if (h3c->client_control) { + goto failed; + } + + h3c->client_control = c; + st->handler = ngx_http_v3_parse_control; + n = sizeof(ngx_http_v3_parse_control_t); + + break; + + default: ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, - "http3 stream type:%ui", us->type); - - switch (us->type) { - - case NGX_HTTP_V3_ENCODER_STREAM: - if (h3c->client_encoder) { - goto failed; - } - - h3c->client_encoder = c; - rev->handler = ngx_http_v3_client_encoder_handler; - break; - - case NGX_HTTP_V3_DECODER_STREAM: - if (h3c->client_decoder) { - goto failed; - } - - h3c->client_decoder = c; - rev->handler = ngx_http_v3_client_decoder_handler; - break; - - case NGX_HTTP_V3_CONTROL_STREAM: - case NGX_HTTP_V3_PUSH_STREAM: - - /* ignore these */ - - default: - rev->handler = ngx_http_v3_dummy_stream_handler; - } + "http3 stream 0x%02xi", st->type); + n = 0; + } - rev->handler(rev); - return; + if (n) { + st->data = ngx_pcalloc(c->pool, n); + if (st->data == NULL) { + goto failed; + } } - } - - if (ngx_handle_read_event(rev, 0) != NGX_OK) { - goto failed; - } - - return; - -failed: - - ngx_http_v3_close_uni_stream(c); -} - -static void -ngx_http_v3_dummy_stream_handler(ngx_event_t *rev) -{ - u_char buf[128]; - ngx_connection_t *c; - - /* read out and ignore */ - - c = rev->data; - - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 dummy stream reader"); - - while (rev->ready) { - if (c->recv(c, buf, sizeof(buf)) == NGX_ERROR) { - goto failed; - } + rev->handler = ngx_http_v3_uni_read_handler; + ngx_http_v3_uni_read_handler(rev); + return; } if (ngx_handle_read_event(rev, 0) != NGX_OK) { @@ -262,519 +249,60 @@ failed: static void -ngx_http_v3_client_encoder_handler(ngx_event_t *rev) +ngx_http_v3_uni_read_handler(ngx_event_t *rev) { - u_char v; + u_char buf[128]; ssize_t n; - ngx_str_t name, value; - ngx_uint_t dynamic, huffman, index, offset; - ngx_connection_t *c, *pc; + ngx_int_t rc, i; + ngx_connection_t *c; ngx_http_v3_uni_stream_t *st; - enum { - sw_start = 0, - sw_inr_name_index, - sw_inr_value_length, - sw_inr_read_value_length, - sw_inr_value, - sw_iwnr_name_length, - sw_iwnr_name, - sw_iwnr_value_length, - sw_iwnr_read_value_length, - sw_iwnr_value, - sw_capacity, - sw_duplicate - } state; c = rev->data; st = c->data; - pc = c->qs->parent; - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client encoder"); - - state = st->state; - dynamic = st->dynamic; - huffman = st->huffman; - index = st->index; - offset = st->offset; - name = st->name; - value = st->value; + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 read handler"); while (rev->ready) { - /* XXX limit checks */ - /* XXX buffer input */ - - n = c->recv(c, &v, 1); + n = c->recv(c, buf, sizeof(buf)); if (n == NGX_ERROR || n == 0) { goto failed; } - if (n != 1) { + if (n == NGX_AGAIN) { break; } - /* XXX v -> ch */ - - switch (state) { - - case sw_start: - - if (v & 0x80) { - /* Insert With Name Reference */ - - dynamic = (v & 0x40) ? 0 : 1; - index = v & 0x3f; - - if (index != 0x3f) { - state = sw_inr_value_length; - break; - } - - index = 0; - state = sw_inr_name_index; - break; - } - - if (v & 0x40) { - /* Insert Without Name Reference */ - - huffman = (v & 0x20) ? 1 : 0; - name.len = v & 0x1f; - - if (name.len != 0x1f) { - offset = 0; - state = sw_iwnr_name; - break; - } - - name.len = 0; - state = sw_iwnr_name_length; - break; - } - - if (v & 0x20) { - /* Set Dynamic Table Capacity */ - - index = v & 0x1f; - - if (index != 0x1f) { - if (ngx_http_v3_set_capacity(c, index) != NGX_OK) { - goto failed; - } - - break; - } - - index = 0; - state = sw_capacity; - break; - } - - /* Duplicate */ - - index = v & 0x1f; + if (st->handler == NULL) { + continue; + } - if (index != 0x1f) { - if (ngx_http_v3_duplicate(c, index) != NGX_OK) { - goto failed; - } - - break; - } - - index = 0; - state = sw_duplicate; - break; - - case sw_inr_name_index: - - index = (index << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - index += 0x3f; - state = sw_inr_value_length; - break; - - case sw_inr_value_length: - - huffman = (v & 0x80) ? 1 : 0; - value.len = v & 0x7f; - - if (value.len == 0) { - value.data = NULL; + for (i = 0; i < n; i++) { - if (ngx_http_v3_ref_insert(c, dynamic, index, &value) != NGX_OK) - { - goto failed; - } - - state = sw_start; - break; - } - - if (value.len != 0x7f) { - value.data = ngx_pnalloc(pc->pool, value.len); - if (value.data == NULL) { - goto failed; - } + rc = st->handler(c, st->data, buf[i]); - state = sw_inr_value; - offset = 0; - break; - } - - value.len = 0; - state = sw_inr_read_value_length; - break; - - case sw_inr_read_value_length: - - value.len = (value.len << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - value.len += 0x7f; - - value.data = ngx_pnalloc(pc->pool, value.len); - if (value.data == NULL) { + if (rc == NGX_ERROR) { goto failed; } - state = sw_inr_value; - offset = 0; - break; - - case sw_inr_value: - - value.data[offset++] = v; - if (offset != value.len) { - break; - } - - if (huffman) { - if (ngx_http_v3_decode_huffman(pc, &value) != NGX_OK) { - goto failed; - } - } - - if (ngx_http_v3_ref_insert(c, dynamic, index, &value) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; - - case sw_iwnr_name_length: - - name.len = (name.len << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - name.len += 0x1f; - - name.data = ngx_pnalloc(pc->pool, name.len); - if (name.data == NULL) { - goto failed; - } - - offset = 0; - state = sw_iwnr_name; - break; - - case sw_iwnr_name: - - name.data[offset++] = v; - if (offset != name.len) { - break; - } - - if (huffman) { - if (ngx_http_v3_decode_huffman(pc, &name) != NGX_OK) { - goto failed; - } - } - - state = sw_iwnr_value_length; - break; - - case sw_iwnr_value_length: - - huffman = (v & 0x80) ? 1 : 0; - value.len = v & 0x7f; - - if (value.len == 0) { - value.data = NULL; - - if (ngx_http_v3_insert(c, &name, &value) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; + if (rc == NGX_DONE) { + goto done; } - if (value.len != 0x7f) { - value.data = ngx_pnalloc(pc->pool, value.len); - if (value.data == NULL) { - goto failed; - } - - offset = 0; - state = sw_iwnr_value; - break; - } - - state = sw_iwnr_read_value_length; - break; - - case sw_iwnr_read_value_length: - - value.len = (value.len << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - value.data = ngx_pnalloc(pc->pool, value.len); - if (value.data == NULL) { - goto failed; - } - - offset = 0; - state = sw_iwnr_value; - break; - - case sw_iwnr_value: - - value.data[offset++] = v; - if (offset != value.len) { - break; - } - - if (huffman) { - if (ngx_http_v3_decode_huffman(pc, &value) != NGX_OK) { - goto failed; - } - } - - if (ngx_http_v3_insert(c, &name, &value) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; - - - case sw_capacity: - - index = (index << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - index += 0x1f; - - if (ngx_http_v3_set_capacity(c, index) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; - - case sw_duplicate: - - index = (index << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - index += 0x1f; - - if (ngx_http_v3_duplicate(c, index) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; + /* rc == NGX_AGAIN */ } } - st->state = state; - st->dynamic = dynamic; - st->huffman = huffman; - st->index = index; - st->offset = offset; - st->name = name; - st->value = value; - if (ngx_handle_read_event(rev, 0) != NGX_OK) { goto failed; } return; -failed: - - ngx_http_v3_close_uni_stream(c); -} - - -static void -ngx_http_v3_client_decoder_handler(ngx_event_t *rev) -{ - u_char v; - ssize_t n; - ngx_uint_t index; - ngx_connection_t *c; - ngx_http_v3_uni_stream_t *st; - enum { - sw_start = 0, - sw_ack_header, - sw_cancel_stream, - sw_inc_insert_count - } state; - - c = rev->data; - st = c->data; - - ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client decoder"); - - state = st->state; - index = st->index; - - while (rev->ready) { - - /* XXX limit checks */ - /* XXX buffer input */ - - n = c->recv(c, &v, 1); - - if (n == NGX_ERROR || n == 0) { - goto failed; - } - - if (n != 1) { - break; - } - - switch (state) { - - case sw_start: - - if (v & 0x80) { - /* Header Acknowledgement */ - - index = v & 0x7f; - - if (index != 0x7f) { - if (ngx_http_v3_ack_header(c, index) != NGX_OK) { - goto failed; - } - - break; - } - - index = 0; - state = sw_ack_header; - break; - } - - if (v & 0x40) { - /* Stream Cancellation */ - - index = v & 0x3f; - - if (index != 0x3f) { - if (ngx_http_v3_cancel_stream(c, index) != NGX_OK) { - goto failed; - } - - break; - } +done: - index = 0; - state = sw_cancel_stream; - break; - } - - /* Insert Count Increment */ - - index = v & 0x3f; - - if (index != 0x3f) { - if (ngx_http_v3_inc_insert_count(c, index) != NGX_OK) { - goto failed; - } - - break; - } - - index = 0; - state = sw_inc_insert_count; - break; - - case sw_ack_header: - - index = (index << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - index += 0x7f; - - if (ngx_http_v3_ack_header(c, index) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; - - case sw_cancel_stream: - - index = (index << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - index += 0x3f; - - if (ngx_http_v3_cancel_stream(c, index) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; - - case sw_inc_insert_count: - - index = (index << 7) + (v & 0x7f); - if (v & 0x80) { - break; - } - - index += 0x3f; - - if (ngx_http_v3_inc_insert_count(c, index) != NGX_OK) { - goto failed; - } - - state = sw_start; - break; - } - } - - st->state = state; - st->index = index; - - if (ngx_handle_read_event(rev, 0) != NGX_OK) { - goto failed; - } - - return; + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 read done"); failed: @@ -835,7 +363,7 @@ failed: static ngx_connection_t * -ngx_http_v3_get_server_encoder(ngx_connection_t *c) +ngx_http_v3_get_control(ngx_connection_t *c) { ngx_http_v3_connection_t *h3c; @@ -843,7 +371,7 @@ ngx_http_v3_get_server_encoder(ngx_conne if (h3c->server_encoder == NULL) { h3c->server_encoder = ngx_http_v3_create_uni_stream(c, - NGX_HTTP_V3_ENCODER_STREAM); + NGX_HTTP_V3_STREAM_CONTROL); } return h3c->server_encoder; @@ -851,18 +379,34 @@ ngx_http_v3_get_server_encoder(ngx_conne static ngx_connection_t * -ngx_http_v3_get_server_decoder(ngx_connection_t *c) +ngx_http_v3_get_encoder(ngx_connection_t *c) { ngx_http_v3_connection_t *h3c; h3c = c->qs->parent->data; - if (h3c->server_decoder == NULL) { - h3c->server_decoder = ngx_http_v3_create_uni_stream(c, - NGX_HTTP_V3_DECODER_STREAM); + if (h3c->server_encoder == NULL) { + h3c->server_encoder = ngx_http_v3_create_uni_stream(c, + NGX_HTTP_V3_STREAM_ENCODER); } - return h3c->server_decoder; + return h3c->server_encoder; +} + + +static ngx_connection_t * +ngx_http_v3_get_decoder(ngx_connection_t *c) +{ + ngx_http_v3_connection_t *h3c; + + h3c = c->qs->parent->data; + + if (h3c->server_encoder == NULL) { + h3c->server_encoder = ngx_http_v3_create_uni_stream(c, + NGX_HTTP_V3_STREAM_DECODER); + } + + return h3c->server_encoder; } @@ -878,7 +422,7 @@ ngx_http_v3_client_ref_insert(ngx_connec "http3 client ref insert, %s[%ui] \"%V\"", dynamic ? "dynamic" : "static", index, value); - ec = ngx_http_v3_get_server_encoder(c); + ec = ngx_http_v3_get_encoder(c); if (ec == NULL) { return NGX_ERROR; } @@ -923,7 +467,7 @@ ngx_http_v3_client_insert(ngx_connection ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client insert \"%V\":\"%V\"", name, value); - ec = ngx_http_v3_get_server_encoder(c); + ec = ngx_http_v3_get_encoder(c); if (ec == NULL) { return NGX_ERROR; } @@ -972,7 +516,7 @@ ngx_http_v3_client_set_capacity(ngx_conn ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client set capacity %ui", capacity); - ec = ngx_http_v3_get_server_encoder(c); + ec = ngx_http_v3_get_encoder(c); if (ec == NULL) { return NGX_ERROR; } @@ -999,7 +543,7 @@ ngx_http_v3_client_duplicate(ngx_connect ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client duplicate %ui", index); - ec = ngx_http_v3_get_server_encoder(c); + ec = ngx_http_v3_get_encoder(c); if (ec == NULL) { return NGX_ERROR; } @@ -1026,7 +570,7 @@ ngx_http_v3_client_ack_header(ngx_connec ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client ack header %ui", stream_id); - dc = ngx_http_v3_get_server_decoder(c); + dc = ngx_http_v3_get_decoder(c); if (dc == NULL) { return NGX_ERROR; } @@ -1053,7 +597,7 @@ ngx_http_v3_client_cancel_stream(ngx_con ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client cancel stream %ui", stream_id); - dc = ngx_http_v3_get_server_decoder(c); + dc = ngx_http_v3_get_decoder(c); if (dc == NULL) { return NGX_ERROR; } @@ -1080,7 +624,7 @@ ngx_http_v3_client_inc_insert_count(ngx_ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 client increment insert count %ui", inc); - dc = ngx_http_v3_get_server_decoder(c); + dc = ngx_http_v3_get_decoder(c); if (dc == NULL) { return NGX_ERROR; } diff --git a/src/http/v3/ngx_http_v3_tables.c b/src/http/v3/ngx_http_v3_tables.c --- a/src/http/v3/ngx_http_v3_tables.c +++ b/src/http/v3/ngx_http_v3_tables.c @@ -383,3 +383,33 @@ ngx_http_v3_new_header(ngx_connection_t return NGX_OK; } + + +ngx_int_t +ngx_http_v3_set_param(ngx_connection_t *c, uint64_t id, uint64_t value) +{ + switch (id) { + + case NGX_HTTP_V3_PARAM_MAX_TABLE_CAPACITY: + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 param QPACK_MAX_TABLE_CAPACITY:%uL", value); + break; + + case NGX_HTTP_V3_PARAM_MAX_HEADER_LIST_SIZE: + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 param SETTINGS_MAX_HEADER_LIST_SIZE:%uL", value); + break; + + case NGX_HTTP_V3_PARAM_BLOCKED_STREAMS: + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 param QPACK_BLOCKED_STREAMS:%uL", value); + break; + + default: + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 param #%uL:%uL", id, value); + } + + return NGX_OK; +}