diff options
| author | Roman Arutyunyan <arut@nginx.com> | 2020-03-13 19:36:33 +0300 |
|---|---|---|
| committer | Roman Arutyunyan <arut@nginx.com> | 2020-03-13 19:36:33 +0300 |
| commit | 7739b6073b11086d9a3dc4b9744418070e182c33 (patch) | |
| tree | 7a16aeff28275722458173e83e2f6dd1889cc44a /src/http/v3 | |
| parent | 365b77b58732a708168c995c7956f50d110fee33 (diff) | |
| download | nginx-7739b6073b11086d9a3dc4b9744418070e182c33.tar.gz nginx-7739b6073b11086d9a3dc4b9744418070e182c33.tar.bz2 | |
HTTP/3.
Diffstat (limited to 'src/http/v3')
| -rw-r--r-- | src/http/v3/ngx_http_v3.c | 176 | ||||
| -rw-r--r-- | src/http/v3/ngx_http_v3.h | 89 | ||||
| -rw-r--r-- | src/http/v3/ngx_http_v3_module.c | 46 | ||||
| -rw-r--r-- | src/http/v3/ngx_http_v3_request.c | 971 | ||||
| -rw-r--r-- | src/http/v3/ngx_http_v3_streams.c | 1097 | ||||
| -rw-r--r-- | src/http/v3/ngx_http_v3_tables.c | 385 |
6 files changed, 2764 insertions, 0 deletions
diff --git a/src/http/v3/ngx_http_v3.c b/src/http/v3/ngx_http_v3.c new file mode 100644 index 000000000..e804cf6f5 --- /dev/null +++ b/src/http/v3/ngx_http_v3.c @@ -0,0 +1,176 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#include <ngx_config.h> +#include <ngx_core.h> +#include <ngx_http.h> + + +uintptr_t +ngx_http_v3_encode_varlen_int(u_char *p, uint64_t value) +{ + if (value <= 63) { + if (p == NULL) { + return 1; + } + + *p++ = value; + return (uintptr_t) p; + } + + if (value <= 16383) { + if (p == NULL) { + return 2; + } + + *p++ = 0x40 | (value >> 8); + *p++ = value; + return (uintptr_t) p; + } + + if (value <= 1073741823) { + if (p == NULL) { + return 3; + } + + *p++ = 0x80 | (value >> 16); + *p++ = (value >> 8); + *p++ = value; + return (uintptr_t) p; + + } + + if (p == NULL) { + return 4; + } + + *p++ = 0xc0 | (value >> 24); + *p++ = (value >> 16); + *p++ = (value >> 8); + *p++ = value; + return (uintptr_t) p; +} + + +uintptr_t +ngx_http_v3_encode_prefix_int(u_char *p, uint64_t value, ngx_uint_t prefix) +{ + ngx_uint_t thresh, n; + + thresh = (1 << prefix) - 1; + + if (value < thresh) { + if (p == NULL) { + return 1; + } + + *p++ |= value; + return (uintptr_t) p; + } + + value -= thresh; + + for (n = 10; n > 1; n--) { + if (value >> (7 * (n - 1))) { + break; + } + } + + if (p == NULL) { + return n + 1; + } + + *p++ |= thresh; + + for ( /* void */ ; n > 1; n--) { + *p++ = 0x80 | (value >> 7 * (n - 1)); + } + + *p++ = value & 0x7f; + + 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 new file mode 100644 index 000000000..8fa54d8e5 --- /dev/null +++ b/src/http/v3/ngx_http_v3.h @@ -0,0 +1,89 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#ifndef _NGX_HTTP_V3_H_INCLUDED_ +#define _NGX_HTTP_V3_H_INCLUDED_ + + +#include <ngx_config.h> +#include <ngx_core.h> +#include <ngx_http.h> + + +#define NGX_HTTP_V3_STREAM 0x48335354 /* "H3ST" */ + + +#define NGX_HTTP_V3_VARLEN_INT_LEN 4 +#define NGX_HTTP_V3_PREFIX_INT_LEN 11 + + +typedef struct { + ngx_http_connection_t hc; + + ngx_array_t *dynamic; + + ngx_connection_t *client_encoder; + ngx_connection_t *client_decoder; + ngx_connection_t *server_encoder; + ngx_connection_t *server_decoder; +} ngx_http_v3_connection_t; + + +typedef struct { + ngx_str_t name; + ngx_str_t value; +} 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_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); + +ngx_int_t ngx_http_v3_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic, + ngx_uint_t index, ngx_str_t *value); +ngx_int_t ngx_http_v3_insert(ngx_connection_t *c, ngx_str_t *name, + ngx_str_t *value); +ngx_int_t ngx_http_v3_set_capacity(ngx_connection_t *c, ngx_uint_t capacity); +ngx_int_t ngx_http_v3_duplicate(ngx_connection_t *c, ngx_uint_t index); +ngx_int_t ngx_http_v3_ack_header(ngx_connection_t *c, ngx_uint_t stream_id); +ngx_int_t ngx_http_v3_cancel_stream(ngx_connection_t *c, ngx_uint_t stream_id); +ngx_int_t ngx_http_v3_inc_insert_count(ngx_connection_t *c, ngx_uint_t inc); +ngx_http_v3_header_t *ngx_http_v3_lookup_table(ngx_connection_t *c, + 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_client_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic, + ngx_uint_t index, ngx_str_t *value); +ngx_int_t ngx_http_v3_client_insert(ngx_connection_t *c, ngx_str_t *name, + ngx_str_t *value); +ngx_int_t ngx_http_v3_client_set_capacity(ngx_connection_t *c, + ngx_uint_t capacity); +ngx_int_t ngx_http_v3_client_duplicate(ngx_connection_t *c, ngx_uint_t index); +ngx_int_t ngx_http_v3_client_ack_header(ngx_connection_t *c, + ngx_uint_t stream_id); +ngx_int_t ngx_http_v3_client_cancel_stream(ngx_connection_t *c, + ngx_uint_t stream_id); +ngx_int_t ngx_http_v3_client_inc_insert_count(ngx_connection_t *c, + ngx_uint_t inc); + + +extern ngx_module_t ngx_http_v3_module; + + +#endif /* _NGX_HTTP_V3_H_INCLUDED_ */ diff --git a/src/http/v3/ngx_http_v3_module.c b/src/http/v3/ngx_http_v3_module.c new file mode 100644 index 000000000..af310bd44 --- /dev/null +++ b/src/http/v3/ngx_http_v3_module.c @@ -0,0 +1,46 @@ + +/* + * Copyright (C) Nginx, Inc. + * Copyright (C) Roman Arutyunyan + */ + + +#include <ngx_config.h> +#include <ngx_core.h> +#include <ngx_http.h> + + +static ngx_command_t ngx_http_v3_commands[] = { + ngx_null_command +}; + + +static ngx_http_module_t ngx_http_v3_module_ctx = { + NULL, /* preconfiguration */ + NULL, /* postconfiguration */ + + NULL, /* create main configuration */ + NULL, /* init main configuration */ + + NULL, /* create server configuration */ + NULL, /* merge server configuration */ + + NULL, /* create location configuration */ + NULL /* merge location configuration */ +}; + + +ngx_module_t ngx_http_v3_module = { + NGX_MODULE_V1, + &ngx_http_v3_module_ctx, /* module context */ + ngx_http_v3_commands, /* module directives */ + NGX_HTTP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + NULL, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ + NGX_MODULE_V1_PADDING +}; diff --git a/src/http/v3/ngx_http_v3_request.c b/src/http/v3/ngx_http_v3_request.c new file mode 100644 index 000000000..b34eed98e --- /dev/null +++ b/src/http/v3/ngx_http_v3_request.c @@ -0,0 +1,971 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#include <ngx_config.h> +#include <ngx_core.h> +#include <ngx_http.h> + + +#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); + + +struct { + ngx_str_t name; + ngx_uint_t method; +} ngx_http_v3_methods[] = { + + { ngx_string("GET"), NGX_HTTP_GET }, + { ngx_string("POST"), NGX_HTTP_POST }, + { ngx_string("HEAD"), NGX_HTTP_HEAD }, + { ngx_string("OPTIONS"), NGX_HTTP_OPTIONS }, + { ngx_string("PROPFIND"), NGX_HTTP_PROPFIND }, + { ngx_string("PUT"), NGX_HTTP_PUT }, + { ngx_string("MKCOL"), NGX_HTTP_MKCOL }, + { ngx_string("DELETE"), NGX_HTTP_DELETE }, + { ngx_string("COPY"), NGX_HTTP_COPY }, + { ngx_string("MOVE"), NGX_HTTP_MOVE }, + { ngx_string("PROPPATCH"), NGX_HTTP_PROPPATCH }, + { ngx_string("LOCK"), NGX_HTTP_LOCK }, + { ngx_string("UNLOCK"), NGX_HTTP_UNLOCK }, + { ngx_string("PATCH"), NGX_HTTP_PATCH }, + { ngx_string("TRACE"), NGX_HTTP_TRACE } +}; + + +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; + ngx_str_t name, value; + ngx_int_t rc; + ngx_uint_t length, index, insert_count, sign, base, delta_base, + huffman, dynamic, offset; + ngx_connection_t *c; + ngx_http_v3_header_t *h; + enum { + sw_start = 0, + sw_length, + sw_length_1, + sw_length_2, + sw_length_3, + sw_header_block, + sw_req_insert_count, + sw_delta_base, + sw_read_delta_base, + sw_header, + sw_old_header, + sw_header_ri, + sw_header_pbi, + sw_header_lri, + sw_header_lpbi, + sw_header_l_name_len, + sw_header_l_name, + sw_header_value_len, + sw_header_read_value_len, + sw_header_value + } state; + + c = r->connection; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 parse header, pseudo:%ui", pseudo); + + if (r->state == sw_old_header) { + r->state = sw_header; + return NGX_OK; + } + + length = r->h3_length; + index = r->h3_index; + insert_count = r->h3_insert_count; + sign = r->h3_sign; + delta_base = r->h3_delta_base; + huffman = r->h3_huffman; + dynamic = r->h3_dynamic; + offset = r->h3_offset; + + name.data = r->header_name_start; + name.len = r->header_name_end - r->header_name_start; + value.data = r->header_start; + value.len = r->header_end - r->header_start; + + if (r->state == sw_start) { + length = 1; + } + +again: + + state = r->state; + + if (state == sw_header && length == 0) { + r->state = sw_start; + return NGX_HTTP_PARSE_HEADER_DONE; + } + + for (p = b->pos; p < b->last; p++) { + + if (state >= sw_header_block && length-- == 0) { + goto failed; + } + + ch = *p; + + switch (state) { + + case sw_start: + + if (ch != NGX_HTTP_V3_FRAME_HEADERS) { + goto failed; + } + + r->request_start = p; + state = sw_length; + break; + + case sw_length: + + length = ch; + if (length & 0xc0) { + state = sw_length_1; + break; + } + + state = sw_header_block; + break; + + case sw_length_1: + + length = (length << 8) + ch; + if ((length & 0xc000) != 0x4000) { + state = sw_length_2; + break; + } + + length &= 0x3fff; + state = sw_header_block; + break; + + case sw_length_2: + + length = (length << 8) + ch; + if ((length & 0xc00000) != 0x800000) { + state = sw_length_3; + break; + } + + /* fall through */ + + case sw_length_3: + + length &= 0x3fffff; + state = sw_header_block; + break; + + case sw_header_block: + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 header block length:%ui", length); + + if (ch != 0xff) { + insert_count = ch; + state = sw_delta_base; + break; + } + + insert_count = 0; + state = sw_req_insert_count; + break; + + case sw_req_insert_count: + + insert_count = (insert_count << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + insert_count += 0xff; + state = sw_delta_base; + break; + + case sw_delta_base: + + sign = (ch & 0x80) ? 1 : 0; + delta_base = ch & 0x7f; + + if (delta_base != 0x7f) { + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 header block " + "insert_count:%ui, sign:%ui, delta_base:%ui", + insert_count, sign, delta_base); + goto done; + } + + delta_base = 0; + state = sw_read_delta_base; + break; + + case sw_read_delta_base: + + delta_base = (delta_base << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + delta_base += 0x7f; + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 header block " + "insert_count:%ui, sign:%ui, delta_base:%ui", + insert_count, sign, delta_base); + goto done; + + case sw_header: + + index = 0; + huffman = 0; + ngx_str_null(&name); + ngx_str_null(&value); + + if (ch & 0x80) { + /* Indexed Header Field */ + + dynamic = (ch & 0x40) ? 0 : 1; + index = ch & 0x3f; + + if (index != 0x3f) { + goto done; + } + + index = 0; + state = sw_header_ri; + break; + } + + if (ch & 0x40) { + /* Literal Header Field With Name Reference */ + + dynamic = (ch & 0x10) ? 0 : 1; + index = ch & 0x0f; + + if (index != 0x0f) { + state = sw_header_value_len; + break; + } + + index = 0; + state = sw_header_lri; + break; + } + + if (ch & 0x20) { + /* Literal Header Field Without Name Reference */ + + huffman = (ch & 0x08) ? 1 : 0; + name.len = ch & 0x07; + + if (name.len == 0) { + goto failed; + } + + if (name.len != 0x07) { + offset = 0; + state = sw_header_l_name; + break; + } + + name.len = 0; + state = sw_header_l_name_len; + break; + } + + if (ch & 10) { + /* Indexed Header Field With Post-Base Index */ + + dynamic = 2; + index = ch & 0x0f; + + if (index != 0x0f) { + goto done; + } + + index = 0; + state = sw_header_pbi; + break; + } + + /* Literal Header Field With Post-Base Name Reference */ + + dynamic = 2; + index = ch & 0x07; + + if (index != 0x07) { + state = sw_header_value_len; + break; + } + + index = 0; + state = sw_header_lpbi; + break; + + case sw_header_ri: + + index = (index << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + index += 0x3f; + goto done; + + case sw_header_pbi: + + index = (index << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + index += 0x0f; + goto done; + + case sw_header_lri: + + index = (index << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + index += 0x0f; + state = sw_header_value_len; + break; + + case sw_header_lpbi: + + index = (index << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + index += 0x07; + state = sw_header_value_len; + break; + + + case sw_header_l_name_len: + + name.len = (name.len << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + name.len += 0x07; + offset = 0; + state = sw_header_l_name; + break; + + case sw_header_l_name: + if (offset++ == 0) { + name.data = p; + } + + if (offset != name.len) { + break; + } + + if (huffman) { + if (ngx_http_v3_decode_huffman(c, &name) != NGX_OK) { + goto failed; + } + } + + state = sw_header_value_len; + break; + + case sw_header_value_len: + + huffman = (ch & 0x80) ? 1 : 0; + value.len = ch & 0x7f; + + if (value.len == 0) { + value.data = p; + goto done; + } + + if (value.len != 0x7f) { + offset = 0; + state = sw_header_value; + break; + } + + value.len = 0; + state = sw_header_read_value_len; + break; + + case sw_header_read_value_len: + + value.len = (value.len << 7) + (ch & 0x7f); + if (ch & 0x80) { + break; + } + + value.len += 0x7f; + offset = 0; + state = sw_header_value; + break; + + case sw_header_value: + + if (offset++ == 0) { + value.data = p; + } + + if (offset != value.len) { + break; + } + + if (huffman) { + if (ngx_http_v3_decode_huffman(c, &value) != NGX_OK) { + goto failed; + } + } + + goto done; + + case sw_old_header: + + break; + } + } + + b->pos = p; + r->state = state; + r->h3_length = length; + r->h3_index = index; + r->h3_insert_count = insert_count; + r->h3_sign = sign; + r->h3_delta_base = delta_base; + r->h3_huffman = huffman; + r->h3_dynamic = dynamic; + r->h3_offset = offset; + + /* XXX fix large reallocations */ + 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; + + /* XXX r->lowcase_index = i; */ + + return NGX_AGAIN; + +done: + + b->pos = p + 1; + r->state = sw_header; + r->h3_length = length; + r->h3_insert_count = insert_count; + r->h3_sign = sign; + r->h3_delta_base = delta_base; + + if (state < sw_header) { + if (ngx_http_v3_check_insert_count(c, insert_count) != NGX_OK) { + return NGX_DONE; + } + + goto again; + } + + if (sign == 0) { + base = insert_count + delta_base; + } else { + base = insert_count - delta_base - 1; + } + + ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 header %s[%ui], base:%ui, \"%V\":\"%V\"", + dynamic ? "dynamic" : "static", index, base, &name, &value); + + if (name.data == NULL) { + + if (dynamic == 2) { + index = base - index - 1; + } else if (dynamic == 1) { + index += base; + } + + h = ngx_http_v3_lookup_table(c, dynamic, index); + if (h == NULL) { + goto failed; + } + + name = h->name; + + if (value.data == NULL) { + value = h->value; + } + } + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 header \"%V\":\"%V\"", &name, &value); + + if (pseudo) { + rc = ngx_http_v3_process_pseudo_header(r, &name, &value); + + if (rc == NGX_ERROR) { + goto failed; + } + + if (rc == NGX_OK) { + r->request_end = p + 1; + goto again; + } + + /* rc == NGX_DONE */ + + r->state = sw_old_header; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 header left:%ui", length); + + 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 */ + + /* XXX r->lowcase_index = i; */ + + return NGX_OK; + +failed: + + return NGX_HTTP_PARSE_INVALID_REQUEST; +} + + +static ngx_int_t +ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name, + ngx_str_t *value) +{ + ngx_uint_t i; + ngx_connection_t *c; + + c = r->connection; + + if (name->len == 7 && ngx_strncmp(name->data, ":method", 7) == 0) { + r->method_start = value->data; + r->method_end = value->data + value->len; + + for (i = 0; i < sizeof(ngx_http_v3_methods) + / sizeof(ngx_http_v3_methods[0]); i++) + { + if (value->len == ngx_http_v3_methods[i].name.len + && ngx_strncmp(value->data, ngx_http_v3_methods[i].name.data, + value->len) == 0) + { + r->method = ngx_http_v3_methods[i].method; + break; + } + } + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 method \"%V\" %ui", value, r->method); + return NGX_OK; + } + + if (name->len == 5 && ngx_strncmp(name->data, ":path", 5) == 0) { + r->uri_start = value->data; + r->uri_end = value->data + value->len; + + if (ngx_http_parse_uri(r) != NGX_OK) { + ngx_log_error(NGX_LOG_INFO, c->log, 0, + "client sent invalid :path header: \"%V\"", value); + return NGX_ERROR; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 path \"%V\"", value); + + return NGX_OK; + } + + if (name->len == 7 && ngx_strncmp(name->data, ":scheme", 7) == 0) { + r->schema_start = value->data; + r->schema_end = value->data + value->len; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 schema \"%V\"", value); + + return NGX_OK; + } + + if (name->len == 10 && ngx_strncmp(name->data, ":authority", 10) == 0) { + r->host_start = value->data; + r->host_end = value->data + value->len; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 authority \"%V\"", value); + + 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; + } + + return NGX_DONE; +} + + +ngx_chain_t * +ngx_http_v3_create_header(ngx_http_request_t *r) +{ + u_char *p; + size_t len, hlen, n; + ngx_buf_t *b; + ngx_uint_t i, j; + ngx_chain_t *hl, *cl, *bl; + ngx_list_part_t *part; + ngx_table_elt_t *header; + ngx_connection_t *c; + ngx_http_core_loc_conf_t *clcf; + + c = r->connection; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 create header"); + + /* XXX support chunked body in the chunked filter */ + if (r->headers_out.content_length_n == -1) { + return NULL; + } + + len = 0; + + if (r->headers_out.status == NGX_HTTP_OK) { + len += ngx_http_v3_encode_prefix_int(NULL, 25, 6); + + } else { + len += 3 + ngx_http_v3_encode_prefix_int(NULL, 25, 4) + + ngx_http_v3_encode_prefix_int(NULL, 3, 7); + } + + clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); + + if (r->headers_out.server == NULL) { + if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) { + n = sizeof(NGINX_VER) - 1; + + } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) { + n = sizeof(NGINX_VER_BUILD) - 1; + + } else { + n = sizeof("nginx") - 1; + } + + len += ngx_http_v3_encode_prefix_int(NULL, 92, 4) + + ngx_http_v3_encode_prefix_int(NULL, n, 7) + n; + } + + if (r->headers_out.date == NULL) { + len += ngx_http_v3_encode_prefix_int(NULL, 6, 4) + + ngx_http_v3_encode_prefix_int(NULL, ngx_cached_http_time.len, + 7) + + ngx_cached_http_time.len; + } + + if (r->headers_out.content_type.len) { + n = r->headers_out.content_type.len; + + if (r->headers_out.content_type_len == r->headers_out.content_type.len + && r->headers_out.charset.len) + { + n += sizeof("; charset=") - 1 + r->headers_out.charset.len; + } + + len += ngx_http_v3_encode_prefix_int(NULL, 53, 4) + + ngx_http_v3_encode_prefix_int(NULL, n, 7) + n; + } + + if (r->headers_out.content_length_n == 0) { + len += ngx_http_v3_encode_prefix_int(NULL, 4, 6); + + } else { + len += ngx_http_v3_encode_prefix_int(NULL, 4, 4) + 1 + NGX_OFF_T_LEN; + } + + if (r->headers_out.last_modified == NULL + && r->headers_out.last_modified_time != -1) + { + len += ngx_http_v3_encode_prefix_int(NULL, 10, 4) + 1 + + sizeof("Last-Modified: Mon, 28 Sep 1970 06:00:00 GMT"); + } + + /* XXX location */ + +#if (NGX_HTTP_GZIP) + if (r->gzip_vary) { + if (clcf->gzip_vary) { + /* Vary: Accept-Encoding */ + len += ngx_http_v3_encode_prefix_int(NULL, 59, 6); + + } else { + r->gzip_vary = 0; + } + } +#endif + + part = &r->headers_out.headers.part; + header = part->elts; + + for (i = 0; /* void */; i++) { + + if (i >= part->nelts) { + if (part->next == NULL) { + break; + } + + part = part->next; + header = part->elts; + i = 0; + } + + if (header[i].hash == 0) { + continue; + } + + len += ngx_http_v3_encode_prefix_int(NULL, header[i].key.len, 3) + + header[i].key.len + + ngx_http_v3_encode_prefix_int(NULL, header[i].value.len, 7 ) + + header[i].value.len; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 header len:%uz", len); + + b = ngx_create_temp_buf(r->pool, len); + if (b == NULL) { + return NULL; + } + + *b->last++ = 0; + *b->last++ = 0; + + if (r->headers_out.status == NGX_HTTP_OK) { + /* :status: 200 */ + *b->last = 0xc0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 25, 6); + + } else { + /* :status: 200 */ + *b->last = 0x70; + 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); + } + + if (r->headers_out.server == NULL) { + if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_ON) { + p = (u_char *) NGINX_VER; + n = sizeof(NGINX_VER) - 1; + + } else if (clcf->server_tokens == NGX_HTTP_SERVER_TOKENS_BUILD) { + p = (u_char *) NGINX_VER_BUILD; + n = sizeof(NGINX_VER_BUILD) - 1; + + } else { + p = (u_char *) "nginx"; + n = sizeof("nginx") - 1; + } + + /* server */ + *b->last = 0x70; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 92, 4); + *b->last = 0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, n, 7); + b->last = ngx_cpymem(b->last, p, n); + } + + if (r->headers_out.date == NULL) { + /* date */ + *b->last = 0x70; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 6, 4); + *b->last = 0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, + ngx_cached_http_time.len, 7); + b->last = ngx_cpymem(b->last, ngx_cached_http_time.data, + ngx_cached_http_time.len); + } + + if (r->headers_out.content_type.len) { + n = r->headers_out.content_type.len; + + if (r->headers_out.content_type_len == r->headers_out.content_type.len + && r->headers_out.charset.len) + { + n += sizeof("; charset=") - 1 + r->headers_out.charset.len; + } + + /* content-type: text/plain */ + *b->last = 0x70; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 53, 4); + *b->last = 0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, n, 7); + + p = b->last; + b->last = ngx_copy(b->last, r->headers_out.content_type.data, + r->headers_out.content_type.len); + + if (r->headers_out.content_type_len == r->headers_out.content_type.len + && r->headers_out.charset.len) + { + b->last = ngx_cpymem(b->last, "; charset=", + sizeof("; charset=") - 1); + b->last = ngx_copy(b->last, r->headers_out.charset.data, + r->headers_out.charset.len); + + /* update r->headers_out.content_type for possible logging */ + + r->headers_out.content_type.len = b->last - p; + r->headers_out.content_type.data = p; + } + } + + if (r->headers_out.content_length_n == 0) { + /* content-length: 0 */ + *b->last = 0xc0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 4, 6); + + } else if (r->headers_out.content_length_n > 0) { + /* content-length: 0 */ + *b->last = 0x70; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 4, 4); + p = b->last++; + b->last = ngx_sprintf(b->last, "%O", r->headers_out.content_length_n); + *p = b->last - p - 1; + } + + if (r->headers_out.last_modified == NULL + && r->headers_out.last_modified_time != -1) + { + /* last-modified */ + *b->last = 0x70; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 10, 4); + p = b->last++; + b->last = ngx_http_time(b->last, r->headers_out.last_modified_time); + *p = b->last - p - 1; + } + +#if (NGX_HTTP_GZIP) + if (r->gzip_vary) { + /* vary: accept-encoding */ + *b->last = 0xc0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 59, 6); + } +#endif + + part = &r->headers_out.headers.part; + header = part->elts; + + for (i = 0; /* void */; i++) { + + if (i >= part->nelts) { + if (part->next == NULL) { + break; + } + + part = part->next; + header = part->elts; + i = 0; + } + + if (header[i].hash == 0) { + continue; + } + + *b->last = 0x30; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, + header[i].key.len, + 3); + for (j = 0; j < header[i].key.len; j++) { + *b->last++ = ngx_tolower(header[i].key.data[j]); + } + + *b->last = 0; + b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, + header[i].value.len, + 7); + b->last = ngx_copy(b->last, header[i].value.data, header[i].value.len); + } + + cl = ngx_alloc_chain_link(c->pool); + if (cl == NULL) { + return NULL; + } + + cl->buf = b; + cl->next = NULL; + + n = b->last - b->pos; + + len = 1 + ngx_http_v3_encode_varlen_int(NULL, n); + + b = ngx_create_temp_buf(c->pool, len); + if (b == NULL) { + return NULL; + } + + *b->last++ = NGX_HTTP_V3_FRAME_HEADERS; + b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last, n); + + hl = ngx_alloc_chain_link(c->pool); + if (hl == NULL) { + return NULL; + } + + hl->buf = b; + hl->next = cl; + + hlen = 1 + ngx_http_v3_encode_varlen_int(NULL, len); + + if (r->headers_out.content_length_n >= 0) { + len = 1 + ngx_http_v3_encode_varlen_int(NULL, + r->headers_out.content_length_n); + + b = ngx_create_temp_buf(c->pool, len); + if (b == NULL) { + NULL; + } + + *b->last++ = NGX_HTTP_V3_FRAME_DATA; + b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last, + r->headers_out.content_length_n); + + bl = ngx_alloc_chain_link(c->pool); + if (bl == NULL) { + return NULL; + } + + bl->buf = b; + bl->next = NULL; + cl->next = bl; + } + + return hl; +} diff --git a/src/http/v3/ngx_http_v3_streams.c b/src/http/v3/ngx_http_v3_streams.c new file mode 100644 index 000000000..2b757d81f --- /dev/null +++ b/src/http/v3/ngx_http_v3_streams.c @@ -0,0 +1,1097 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#include <ngx_config.h> +#include <ngx_core.h> +#include <ngx_http.h> + + +#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 struct { + uint32_t signature; /* QSTR */ + u_char buf[4]; + + ngx_uint_t len; + ngx_uint_t type; + ngx_uint_t state; + ngx_uint_t index; + ngx_uint_t offset; + + ngx_str_t name; + ngx_str_t value; + + unsigned client:1; + unsigned dynamic:1; + unsigned huffman: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 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); + + +void +ngx_http_v3_handle_client_uni_stream(ngx_connection_t *c) +{ + ngx_pool_cleanup_t *cln; + ngx_http_v3_uni_stream_t *us; + + c->log->connection = c->number; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "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) { + ngx_http_v3_close_uni_stream(c); + return; + } + + us->signature = NGX_HTTP_V3_STREAM; + us->client = 1; + us->type = (ngx_uint_t) -1; + + c->data = us; + + cln = ngx_pool_cleanup_add(c->pool, 0); + if (cln == NULL) { + ngx_http_v3_close_uni_stream(c); + return; + } + + cln->handler = ngx_http_v3_uni_stream_cleanup; + cln->data = c; + + c->read->handler = ngx_http_v3_read_uni_stream_type; + c->read->handler(c->read); +} + + +static void +ngx_http_v3_close_uni_stream(ngx_connection_t *c) +{ + ngx_pool_t *pool; + + c->destroyed = 1; + + pool = c->pool; + + ngx_close_connection(c); + + ngx_destroy_pool(pool); +} + + +static void +ngx_http_v3_uni_stream_cleanup(void *data) +{ + ngx_connection_t *c = data; + + ngx_http_v3_connection_t *h3c; + ngx_http_v3_uni_stream_t *us; + + us = c->data; + h3c = c->qs->parent->data; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 close stream"); + + switch (us->type) { + + case NGX_HTTP_V3_ENCODER_STREAM: + + if (us->client) { + h3c->client_encoder = NULL; + } else { + h3c->server_encoder = NULL; + } + + break; + + case NGX_HTTP_V3_DECODER_STREAM: + + if (us->client) { + h3c->client_decoder = NULL; + } else { + h3c->server_decoder = NULL; + } + + break; + } +} + + +static void +ngx_http_v3_read_uni_stream_type(ngx_event_t *rev) +{ + u_char *p; + ssize_t n, len; + ngx_connection_t *c; + ngx_http_v3_connection_t *h3c; + ngx_http_v3_uni_stream_t *us; + + c = rev->data; + us = 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); + + if (n == NGX_ERROR) { + goto failed; + } + + if (n == NGX_AGAIN) { + break; + } + + us->len += n; + + if (n != len) { + break; + } + + if ((us->buf[0] >> 6) + 1 == us->len) { + us->type = ngx_http_v3_decode_varlen_int(us->buf); + + 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; + } + + rev->handler(rev); + return; + } + } + + 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; + } + } + + 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_encoder_handler(ngx_event_t *rev) +{ + u_char v; + ssize_t n; + ngx_str_t name, value; + ngx_uint_t dynamic, huffman, index, offset; + ngx_connection_t *c, *pc; + 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; + + 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; + } + + /* 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 (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; + + 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; + } + + 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) { + 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 (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; + } + } + + 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; + } + + 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; + +failed: + + ngx_http_v3_close_uni_stream(c); +} + + +/* XXX async & buffered stream writes */ + +static ngx_connection_t * +ngx_http_v3_create_uni_stream(ngx_connection_t *c, ngx_uint_t type) +{ + u_char buf[NGX_HTTP_V3_VARLEN_INT_LEN]; + size_t n; + ngx_connection_t *sc; + ngx_pool_cleanup_t *cln; + ngx_http_v3_uni_stream_t *us; + + sc = ngx_quic_create_uni_stream(c->qs->parent); + if (sc == NULL) { + return NULL; + } + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 create uni stream, type:%ui", type); + + us = ngx_pcalloc(sc->pool, sizeof(ngx_http_v3_uni_stream_t)); + if (us == NULL) { + goto failed; + } + + us->signature = NGX_HTTP_V3_STREAM; + us->type = type; + sc->data = us; + + cln = ngx_pool_cleanup_add(sc->pool, 0); + if (cln == NULL) { + goto failed; + } + + cln->handler = ngx_http_v3_uni_stream_cleanup; + cln->data = sc; + + n = (u_char *) ngx_http_v3_encode_varlen_int(buf, type) - buf; + + if (sc->send(sc, buf, n) != (ssize_t) n) { + goto failed; + } + + return sc; + +failed: + + ngx_http_v3_close_uni_stream(sc); + + return NULL; +} + + +static ngx_connection_t * +ngx_http_v3_get_server_encoder(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_ENCODER_STREAM); + } + + return h3c->server_encoder; +} + + +static ngx_connection_t * +ngx_http_v3_get_server_decoder(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); + } + + return h3c->server_decoder; +} + + +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) +{ + u_char *p, buf[NGX_HTTP_V3_PREFIX_INT_LEN * 2]; + size_t n; + ngx_connection_t *ec; + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 client ref insert, %s[%ui] \"%V\"", + dynamic ? "dynamic" : "static", index, value); + + ec = ngx_http_v3_get_server_encoder(c); + if (ec == NULL) { + return NGX_ERROR; + } + + p = buf; + + *p = (dynamic ? 0x80 : 0xc0); + p = (u_char *) ngx_http_v3_encode_prefix_int(p, index, 6); + + /* XXX option for huffman? */ + *p = 0; + p = (u_char *) ngx_http_v3_encode_prefix_int(p, value->len, 7); + + n = p - buf; + + if (ec->send(ec, buf, n) != (ssize_t) n) { + goto failed; + } + + if (ec->send(ec, value->data, value->len) != (ssize_t) value->len) { + goto failed; + } + + return NGX_OK; + +failed: + + ngx_http_v3_close_uni_stream(ec); + + return NGX_ERROR; +} + + +ngx_int_t +ngx_http_v3_client_insert(ngx_connection_t *c, ngx_str_t *name, + ngx_str_t *value) +{ + u_char buf[NGX_HTTP_V3_PREFIX_INT_LEN]; + size_t n; + ngx_connection_t *ec; + + 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); + if (ec == NULL) { + return NGX_ERROR; + } + + /* XXX option for huffman? */ + buf[0] = 0x40; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, name->len, 5) - buf; + + if (ec->send(ec, buf, n) != (ssize_t) n) { + goto failed; + } + + if (ec->send(ec, name->data, name->len) != (ssize_t) name->len) { + goto failed; + } + + /* XXX option for huffman? */ + buf[0] = 0; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, value->len, 7) - buf; + + if (ec->send(ec, buf, n) != (ssize_t) n) { + goto failed; + } + + if (ec->send(ec, value->data, value->len) != (ssize_t) value->len) { + goto failed; + } + + return NGX_OK; + +failed: + + ngx_http_v3_close_uni_stream(ec); + + return NGX_ERROR; +} + + +ngx_int_t +ngx_http_v3_client_set_capacity(ngx_connection_t *c, ngx_uint_t capacity) +{ + u_char buf[NGX_HTTP_V3_PREFIX_INT_LEN]; + size_t n; + ngx_connection_t *ec; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 client set capacity %ui", capacity); + + ec = ngx_http_v3_get_server_encoder(c); + if (ec == NULL) { + return NGX_ERROR; + } + + buf[0] = 0x20; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, capacity, 5) - buf; + + if (ec->send(ec, buf, n) != (ssize_t) n) { + ngx_http_v3_close_uni_stream(ec); + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_client_duplicate(ngx_connection_t *c, ngx_uint_t index) +{ + u_char buf[NGX_HTTP_V3_PREFIX_INT_LEN]; + size_t n; + ngx_connection_t *ec; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 client duplicate %ui", index); + + ec = ngx_http_v3_get_server_encoder(c); + if (ec == NULL) { + return NGX_ERROR; + } + + buf[0] = 0; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, index, 5) - buf; + + if (ec->send(ec, buf, n) != (ssize_t) n) { + ngx_http_v3_close_uni_stream(ec); + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_client_ack_header(ngx_connection_t *c, ngx_uint_t stream_id) +{ + u_char buf[NGX_HTTP_V3_PREFIX_INT_LEN]; + size_t n; + ngx_connection_t *dc; + + 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); + if (dc == NULL) { + return NGX_ERROR; + } + + buf[0] = 0x80; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, stream_id, 7) - buf; + + if (dc->send(dc, buf, n) != (ssize_t) n) { + ngx_http_v3_close_uni_stream(dc); + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_client_cancel_stream(ngx_connection_t *c, ngx_uint_t stream_id) +{ + u_char buf[NGX_HTTP_V3_PREFIX_INT_LEN]; + size_t n; + ngx_connection_t *dc; + + 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); + if (dc == NULL) { + return NGX_ERROR; + } + + buf[0] = 0x40; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, stream_id, 6) - buf; + + if (dc->send(dc, buf, n) != (ssize_t) n) { + ngx_http_v3_close_uni_stream(dc); + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_client_inc_insert_count(ngx_connection_t *c, ngx_uint_t inc) +{ + u_char buf[NGX_HTTP_V3_PREFIX_INT_LEN]; + size_t n; + ngx_connection_t *dc; + + 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); + if (dc == NULL) { + return NGX_ERROR; + } + + buf[0] = 0; + n = (u_char *) ngx_http_v3_encode_prefix_int(buf, inc, 6) - buf; + + if (dc->send(dc, buf, n) != (ssize_t) n) { + ngx_http_v3_close_uni_stream(dc); + return NGX_ERROR; + } + + return NGX_OK; +} diff --git a/src/http/v3/ngx_http_v3_tables.c b/src/http/v3/ngx_http_v3_tables.c new file mode 100644 index 000000000..1c1d8c051 --- /dev/null +++ b/src/http/v3/ngx_http_v3_tables.c @@ -0,0 +1,385 @@ + +/* + * Copyright (C) Roman Arutyunyan + * Copyright (C) Nginx, Inc. + */ + + +#include <ngx_config.h> +#include <ngx_core.h> +#include <ngx_http.h> + + +static ngx_array_t *ngx_http_v3_get_dynamic_table(ngx_connection_t *c); +static ngx_int_t ngx_http_v3_new_header(ngx_connection_t *c); + + +static ngx_http_v3_header_t ngx_http_v3_static_table[] = { + + { ngx_string(":authority"), ngx_string("") }, + { ngx_string(":path"), ngx_string("/") }, + { ngx_string("age"), ngx_string("0") }, + { ngx_string("content-disposition"), ngx_string("") }, + { ngx_string("content-length"), ngx_string("0") }, + { ngx_string("cookie"), ngx_string("") }, + { ngx_string("date"), ngx_string("") }, + { ngx_string("etag"), ngx_string("") }, + { ngx_string("if-modified-since"), ngx_string("") }, + { ngx_string("if-none-match"), ngx_string("") }, + { ngx_string("last-modified"), ngx_string("") }, + { ngx_string("link"), ngx_string("") }, + { ngx_string("location"), ngx_string("") }, + { ngx_string("referer"), ngx_string("") }, + { ngx_string("set-cookie"), ngx_string("") }, + { ngx_string(":method"), ngx_string("CONNECT") }, + { ngx_string(":method"), ngx_string("DELETE") }, + { ngx_string(":method"), ngx_string("GET") }, + { ngx_string(":method"), ngx_string("HEAD") }, + { ngx_string(":method"), ngx_string("OPTIONS") }, + { ngx_string(":method"), ngx_string("POST") }, + { ngx_string(":method"), ngx_string("PUT") }, + { ngx_string(":scheme"), ngx_string("http") }, + { ngx_string(":scheme"), ngx_string("https") }, + { ngx_string(":status"), ngx_string("103") }, + { ngx_string(":status"), ngx_string("200") }, + { ngx_string(":status"), ngx_string("304") }, + { ngx_string(":status"), ngx_string("404") }, + { ngx_string(":status"), ngx_string("503") }, + { ngx_string("accept"), ngx_string("*/*") }, + { ngx_string("accept"), + ngx_string("application/dns-message ") }, + { ngx_string("accept-encoding"), ngx_string("gzip,") }, + { ngx_string("accept-ranges"), ngx_string("bytes") }, + { ngx_string("access-control-allow-headers"), + ngx_string("cache-control") }, + { ngx_string("access-control-allow-headers"), + ngx_string("content-type") }, + { ngx_string("access-control-allow-origin"), + ngx_string("*") }, + { ngx_string("cache-control"), ngx_string("max-age=0") }, + { ngx_string("cache-control"), ngx_string("max-age=2592000") }, + { ngx_string("cache-control"), ngx_string("max-age=604800") }, + { ngx_string("cache-control"), ngx_string("no-cache") }, + { ngx_string("cache-control"), ngx_string("no-store") }, + { ngx_string("cache-control"), + ngx_string("public, max-age=31536000 ") }, + { ngx_string("content-encoding"), ngx_string("br") }, + { ngx_string("content-encoding"), ngx_string("gzip") }, + { ngx_string("content-type"), + ngx_string("application/dns-message") }, + { ngx_string("content-type"), + ngx_string("application/javascript") }, + { ngx_string("content-type"), ngx_string("application/json") }, + { ngx_string("content-type"), + ngx_string("application/x-www-form-urlencoded") }, + { ngx_string("content-type"), ngx_string("image/gif") }, + { ngx_string("content-type"), ngx_string("image/jpeg") }, + { ngx_string("content-type"), ngx_string("image/png") }, + { ngx_string("content-type"), ngx_string("text/css") }, + { ngx_string("content-type"), + ngx_string("text/html;charset=utf-8") }, + { ngx_string("content-type"), ngx_string("text/plain") }, + { ngx_string("content-type"), + ngx_string("text/plain;charset=utf-8") }, + { ngx_string("range"), ngx_string("bytes=0-") }, + { ngx_string("strict-transport-security"), + ngx_string("max-age=31536000") }, + { ngx_string("strict-transport-security"), + ngx_string("max-age=31536000;includesubdomains") }, + { ngx_string("strict-transport-security"), + ngx_string("max-age=31536000;includesubdomains;preload") }, + { ngx_string("vary"), ngx_string("accept-encoding") }, + { ngx_string("vary"), ngx_string("origin") }, + { ngx_string("x-content-type-options"),ngx_string("nosniff") }, + { ngx_string("x-xss-protection"), ngx_string("1;mode=block") }, + { ngx_string(":status"), ngx_string("100") }, + { ngx_string(":status"), ngx_string("204") }, + { ngx_string(":status"), ngx_string("206") }, + { ngx_string(":status"), ngx_string("302") }, + { ngx_string(":status"), ngx_string("400") }, + { ngx_string(":status"), ngx_string("403") }, + { ngx_string(":status"), ngx_string("421") }, + { ngx_string(":status"), ngx_string("425") }, + { ngx_string(":status"), ngx_string("500") }, + { ngx_string("accept-language"), ngx_string("") }, + { ngx_string("access-control-allow-credentials"), + ngx_string("FALSE") }, + { ngx_string("access-control-allow-credentials"), + ngx_string("TRUE") }, + { ngx_string("access-control-allow-headers"), + ngx_string("*") }, + { ngx_string("access-control-allow-methods"), + ngx_string("get") }, + { ngx_string("access-control-allow-methods"), + ngx_string("get, post, options") }, + { ngx_string("access-control-allow-methods"), + ngx_string("options") }, + { ngx_string("access-control-expose-headers"), + ngx_string("content-length") }, + { ngx_string("access-control-request-headers"), + ngx_string("content-type") }, + { ngx_string("access-control-request-method"), + ngx_string("get") }, + { ngx_string("access-control-request-method"), + ngx_string("post") }, + { ngx_string("alt-svc"), ngx_string("clear") }, + { ngx_string("horization"), ngx_string("") }, + { ngx_string("content-security-policy"), + ngx_string("script-src") }, + { ngx_string("early-data"), ngx_string("1") }, + { ngx_string("expect-ct"), ngx_string("") }, + { ngx_string("forwarded"), ngx_string("") }, + { ngx_string("if-range"), ngx_string("") }, + { ngx_string("origin"), ngx_string("") }, + { ngx_string("purpose"), ngx_string("prefetch") }, + { ngx_string("server"), ngx_string("") }, + { ngx_string("timing-allow-origin"), ngx_string("*") }, + { ngx_string("upgrade-insecure-requests"), + ngx_string("1") }, + { ngx_string("user-agent"), ngx_string("") }, + { ngx_string("x-forwarded-for"), ngx_string("") }, + { ngx_string("x-frame-options"), ngx_string("deny") }, + { ngx_string("x-frame-options"), ngx_string("sameorigin") } +}; + + +ngx_int_t +ngx_http_v3_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic, + ngx_uint_t index, ngx_str_t *value) +{ + ngx_array_t *dt; + ngx_http_v3_header_t *ref, *h; + + ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 ref insert %s[$ui] \"%V\"", + dynamic ? "dynamic" : "static", index, value); + + ref = ngx_http_v3_lookup_table(c, dynamic, index); + if (ref == NULL) { + return NGX_ERROR; + } + + dt = ngx_http_v3_get_dynamic_table(c); + if (dt == NULL) { + return NGX_ERROR; + } + + h = ngx_array_push(dt); + if (h == NULL) { + return NGX_ERROR; + } + + h->name = ref->name; + h->value = *value; + + if (ngx_http_v3_new_header(c) != NGX_OK) { + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_insert(ngx_connection_t *c, ngx_str_t *name, + ngx_str_t *value) +{ + ngx_array_t *dt; + ngx_http_v3_header_t *h; + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 insert \"%V\":\"%V\"", name, value); + + dt = ngx_http_v3_get_dynamic_table(c); + if (dt == NULL) { + return NGX_ERROR; + } + + h = ngx_array_push(dt); + if (h == NULL) { + return NGX_ERROR; + } + + h->name = *name; + h->value = *value; + + if (ngx_http_v3_new_header(c) != NGX_OK) { + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_set_capacity(ngx_connection_t *c, ngx_uint_t capacity) +{ + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 set capacity %ui", capacity); + + /* XXX ignore capacity */ + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_duplicate(ngx_connection_t *c, ngx_uint_t index) +{ + ngx_array_t *dt; + ngx_http_v3_header_t *ref, *h; + + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 duplicate %ui", index); + + ref = ngx_http_v3_lookup_table(c, 1, index); + if (ref == NULL) { + return NGX_ERROR; + } + + dt = ngx_http_v3_get_dynamic_table(c); + if (dt == NULL) { + return NGX_ERROR; + } + + h = ngx_array_push(dt); + if (h == NULL) { + return NGX_ERROR; + } + + *h = *ref; + + if (ngx_http_v3_new_header(c) != NGX_OK) { + return NGX_ERROR; + } + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_ack_header(ngx_connection_t *c, ngx_uint_t stream_id) +{ + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 ack header %ui", stream_id); + + /* XXX */ + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_cancel_stream(ngx_connection_t *c, ngx_uint_t stream_id) +{ + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 cancel stream %ui", stream_id); + + /* XXX */ + + return NGX_OK; +} + + +ngx_int_t +ngx_http_v3_inc_insert_count(ngx_connection_t *c, ngx_uint_t inc) +{ + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 increment insert count %ui", inc); + + /* XXX */ + + return NGX_OK; +} + + +static ngx_array_t * +ngx_http_v3_get_dynamic_table(ngx_connection_t *c) +{ + ngx_connection_t *pc; + ngx_http_v3_connection_t *h3c; + + pc = c->qs->parent; + h3c = pc->data; + + if (h3c->dynamic) { + return h3c->dynamic; + } + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 create dynamic table"); + + h3c->dynamic = ngx_array_create(pc->pool, 1, sizeof(ngx_http_v3_header_t)); + + return h3c->dynamic; +} + + +ngx_http_v3_header_t * +ngx_http_v3_lookup_table(ngx_connection_t *c, ngx_uint_t dynamic, + ngx_uint_t index) +{ + ngx_uint_t nelts; + ngx_array_t *dt; + ngx_http_v3_header_t *table; + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 lookup %s[%ui]", + dynamic ? "dynamic" : "static", index); + + if (dynamic) { + dt = ngx_http_v3_get_dynamic_table(c); + if (dt == NULL) { + return NULL; + } + + table = dt->elts; + nelts = dt->nelts; + + } else { + table = ngx_http_v3_static_table; + nelts = sizeof(ngx_http_v3_static_table) + / sizeof(ngx_http_v3_static_table[0]); + } + + if (index >= nelts) { + ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 lookup out of bounds: %ui", nelts); + return NULL; + } + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 lookup \"%V\":\"%V\"", + &table[index].name, &table[index].value); + + return &table[index]; +} + + +ngx_int_t +ngx_http_v3_check_insert_count(ngx_connection_t *c, ngx_uint_t insert_count) +{ + size_t n; + ngx_http_v3_connection_t *h3c; + + h3c = c->qs->parent->data; + n = h3c->dynamic ? h3c->dynamic->nelts : 0; + + ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, + "http3 check insert count %ui/%ui", insert_count, n); + + if (n < insert_count) { + /* XXX how to get notified? */ + /* XXX wake all streams on any arrival to the encoder stream? */ + return NGX_AGAIN; + } + + return NGX_OK; +} + + +static ngx_int_t +ngx_http_v3_new_header(ngx_connection_t *c) +{ + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 new dynamic header"); + + /* XXX report all waiting streams of a new header */ + + return NGX_OK; +} |
