aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/event/ngx_event.c36
-rw-r--r--src/event/ngx_event_quic.c23
-rw-r--r--src/event/ngx_event_quic.h1
-rw-r--r--src/http/ngx_http.h3
-rw-r--r--src/http/ngx_http_core_module.c6
-rw-r--r--src/http/ngx_http_header_filter_module.c15
-rw-r--r--src/http/ngx_http_parse.c3
-rw-r--r--src/http/ngx_http_request.c178
-rw-r--r--src/http/ngx_http_request.h13
-rw-r--r--src/http/v3/ngx_http_v3.c176
-rw-r--r--src/http/v3/ngx_http_v3.h89
-rw-r--r--src/http/v3/ngx_http_v3_module.c46
-rw-r--r--src/http/v3/ngx_http_v3_request.c971
-rw-r--r--src/http/v3/ngx_http_v3_streams.c1097
-rw-r--r--src/http/v3/ngx_http_v3_tables.c385
15 files changed, 2982 insertions, 60 deletions
diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c
index 402a7f5e2..f0ab73afe 100644
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -268,6 +268,22 @@ ngx_process_events_and_timers(ngx_cycle_t *cycle)
ngx_int_t
ngx_handle_read_event(ngx_event_t *rev, ngx_uint_t flags)
{
+ ngx_connection_t *c;
+
+ c = rev->data;
+
+ if (c->qs) {
+
+ if (!rev->active && !rev->ready) {
+ rev->active = 1;
+
+ } else if (rev->active && (rev->ready || (flags & NGX_CLOSE_EVENT))) {
+ rev->active = 0;
+ }
+
+ return NGX_OK;
+ }
+
if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
/* kqueue, epoll */
@@ -338,14 +354,26 @@ ngx_handle_write_event(ngx_event_t *wev, size_t lowat)
{
ngx_connection_t *c;
- if (lowat) {
- c = wev->data;
+ c = wev->data;
+ if (lowat) {
if (ngx_send_lowat(c, lowat) == NGX_ERROR) {
return NGX_ERROR;
}
}
+ if (c->qs) {
+
+ if (!wev->active && !wev->ready) {
+ wev->active = 1;
+
+ } else if (wev->active && wev->ready) {
+ wev->active = 0;
+ }
+
+ return NGX_OK;
+ }
+
if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
/* kqueue, epoll */
@@ -916,6 +944,10 @@ ngx_send_lowat(ngx_connection_t *c, size_t lowat)
{
int sndlowat;
+ if (c->qs) {
+ return NGX_OK;
+ }
+
#if (NGX_HAVE_LOWAT_EVENT)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
diff --git a/src/event/ngx_event_quic.c b/src/event/ngx_event_quic.c
index 880dda023..248cc9087 100644
--- a/src/event/ngx_event_quic.c
+++ b/src/event/ngx_event_quic.c
@@ -1909,6 +1909,7 @@ ngx_quic_stream_recv(ngx_connection_t *c, u_char *buf, size_t size)
b = sn->b;
if (b->last - b->pos == 0) {
+ c->read->ready = 0;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
"quic recv() not ready");
return NGX_AGAIN; // ?
@@ -2029,6 +2030,7 @@ ngx_quic_payload_handler(ngx_connection_t *c, ngx_quic_header_t *pkt)
u_char *end, *p;
ssize_t len;
ngx_buf_t *b;
+ ngx_log_t *log;
ngx_uint_t ack_this;
ngx_pool_t *pool;
ngx_event_t *rev, *wev;
@@ -2129,21 +2131,38 @@ ngx_quic_payload_handler(ngx_connection_t *c, ngx_quic_header_t *pkt)
return NGX_ERROR;
}
+ sn->c = ngx_get_connection(-1, c->log); // TODO: free on connection termination
+ if (sn->c == NULL) {
+ return NGX_ERROR;
+ }
+
pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, c->log);
if (pool == NULL) {
+ /* XXX free connection */
return NGX_ERROR;
}
- sn->c = ngx_get_connection(-1, c->log); // TODO: free on connection termination
- if (sn->c == NULL) {
+ log = ngx_palloc(pool, sizeof(ngx_log_t));
+ if (log == NULL) {
+ /* XXX free pool and connection */
return NGX_ERROR;
}
+ *log = *c->log;
+ pool->log = log;
+
+ sn->c->log = log;
sn->c->pool = pool;
+ sn->c->listening = c->listening;
+ sn->c->sockaddr = c->sockaddr;
+ sn->c->local_sockaddr = c->local_sockaddr;
+
rev = sn->c->read;
wev = sn->c->write;
+ rev->ready = 1;
+
rev->log = c->log;
wev->log = c->log;
diff --git a/src/event/ngx_event_quic.h b/src/event/ngx_event_quic.h
index f3ff3da77..6a60c8703 100644
--- a/src/event/ngx_event_quic.h
+++ b/src/event/ngx_event_quic.h
@@ -14,6 +14,7 @@ struct ngx_quic_stream_s {
uint64_t id;
ngx_uint_t unidirectional:1;
ngx_connection_t *parent;
+ void *data;
};
/* TODO: get rid somehow of ssl argument? */
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index 8b43857ee..8772001c0 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -38,6 +38,9 @@ typedef u_char *(*ngx_http_log_handler_pt)(ngx_http_request_t *r,
#if (NGX_HTTP_V2)
#include <ngx_http_v2.h>
#endif
+#if (NGX_HTTP_V3)
+#include <ngx_http_v3.h>
+#endif
#if (NGX_HTTP_CACHE)
#include <ngx_http_cache.h>
#endif
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
index 5c210bebd..576c679d7 100644
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -809,7 +809,7 @@ ngx_http_handler(ngx_http_request_t *r)
if (!r->internal) {
switch (r->headers_in.connection_type) {
case 0:
- r->keepalive = (r->http_version > NGX_HTTP_VERSION_10);
+ r->keepalive = (r->http_version == NGX_HTTP_VERSION_11);
break;
case NGX_HTTP_CONNECTION_CLOSE:
@@ -4000,14 +4000,14 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
}
if (ngx_strcmp(value[n].data, "http3") == 0) {
-#if (NGX_HTTP_SSL)
+#if (NGX_HTTP_V3)
lsopt.http3 = 1;
lsopt.type = SOCK_DGRAM;
continue;
#else
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the \"http3\" parameter requires "
- "ngx_http_ssl_module");
+ "ngx_http_v3_module");
return NGX_CONF_ERROR;
#endif
}
diff --git a/src/http/ngx_http_header_filter_module.c b/src/http/ngx_http_header_filter_module.c
index 9b8940590..02e251f79 100644
--- a/src/http/ngx_http_header_filter_module.c
+++ b/src/http/ngx_http_header_filter_module.c
@@ -179,6 +179,21 @@ ngx_http_header_filter(ngx_http_request_t *r)
return NGX_OK;
}
+#if (NGX_HTTP_V3)
+
+ if (r->http_version == NGX_HTTP_VERSION_30) {
+ ngx_chain_t *cl;
+
+ cl = ngx_http_v3_create_header(r);
+ if (cl == NULL) {
+ return NGX_ERROR;
+ }
+
+ return ngx_http_write_filter(r, cl);
+ }
+
+#endif
+
if (r->http_version < NGX_HTTP_VERSION_10) {
return NGX_OK;
}
diff --git a/src/http/ngx_http_parse.c b/src/http/ngx_http_parse.c
index cfc42f9dd..28aa8b0dd 100644
--- a/src/http/ngx_http_parse.c
+++ b/src/http/ngx_http_parse.c
@@ -144,6 +144,7 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
/* HTTP methods: GET, HEAD, POST */
case sw_start:
r->request_start = p;
+ r->method_start = p;
if (ch == CR || ch == LF) {
break;
@@ -158,7 +159,7 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
case sw_method:
if (ch == ' ') {
- r->method_end = p - 1;
+ r->method_end = p;
m = r->request_start;
switch (p - m) {
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 55af26bf8..ef305faf4 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -64,7 +64,9 @@ static void ngx_http_ssl_handshake(ngx_event_t *rev);
static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
#endif
+#if (NGX_HTTP_V3)
static void ngx_http_quic_stream_handler(ngx_connection_t *c);
+#endif
static char *ngx_http_client_errors[] = {
@@ -219,7 +221,15 @@ ngx_http_init_connection(ngx_connection_t *c)
ngx_http_in6_addr_t *addr6;
#endif
- hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
+#if (NGX_HTTP_V3)
+ if (c->type == SOCK_DGRAM) {
+ hc = ngx_pcalloc(c->pool, sizeof(ngx_http_v3_connection_t));
+ hc->quic = 1;
+
+ } else
+#endif
+ hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
+
if (hc == NULL) {
ngx_http_close_connection(c);
return;
@@ -329,11 +339,9 @@ ngx_http_init_connection(ngx_connection_t *c)
rev->ready = 1;
}
-#if (NGX_HTTP_SSL)
- if (hc->addr_conf->http3) {
- ngx_http_ssl_srv_conf_t *sscf;
-
- hc->quic = 1;
+#if (NGX_HTTP_V3)
+ if (hc->quic) {
+ ngx_http_ssl_srv_conf_t *sscf;
sscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_ssl_module);
@@ -390,46 +398,63 @@ ngx_http_init_connection(ngx_connection_t *c)
}
+#if (NGX_HTTP_V3)
+
static void
ngx_http_quic_stream_handler(ngx_connection_t *c)
{
- ngx_quic_stream_t *qs = c->qs;
+ ngx_event_t *rev;
+ ngx_connection_t *pc;
+ ngx_http_log_ctx_t *ctx;
+ ngx_http_connection_t *hc;
+ ngx_http_v3_connection_t *h3c;
- // STUB for stream read/write
+ pc = c->qs->parent;
+ h3c = pc->data;
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "quic stream: 0x%uXL", qs->id);
- ssize_t n;
- ngx_buf_t b;
+ if (c->qs->unidirectional) {
+ ngx_http_v3_handle_client_uni_stream(c);
+ return;
+ }
- u_char buf[512];
+ hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
+ if (hc == NULL) {
+ ngx_http_close_connection(c);
+ return;
+ }
- b.start = buf;
- b.end = buf + 512;
- b.pos = b.last = b.start;
+ ngx_memcpy(hc, h3c, sizeof(ngx_http_connection_t));
+ c->data = hc;
- n = c->recv(c, b.pos, b.end - b.start);
- if (n < 0) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0, "stream read failed");
+ ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
+ if (ctx == NULL) {
+ ngx_http_close_connection(c);
return;
}
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "quic stream: 0x%uXL %ui bytes read", qs->id, n);
+ ctx->connection = c;
+ ctx->request = NULL;
+ ctx->current_request = NULL;
- b.last += n;
+ c->log->connection = c->number;
+ c->log->handler = ngx_http_log_error;
+ c->log->data = ctx;
+ c->log->action = "waiting for request";
- n = c->send(c, b.start, n);
+ c->log_error = NGX_ERROR_INFO;
- if (n < 0) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0, "stream write failed");
- return;
- }
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 new stream id:0x%uXL", c->qs->id);
- ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "quic stream: 0x%uXL %ui bytes written", qs->id, n);
+ rev = c->read;
+ rev->handler = ngx_http_wait_request_handler;
+ c->write->handler = ngx_http_empty_handler;
+
+ rev->handler(rev);
}
+#endif
+
static void
ngx_http_wait_request_handler(ngx_event_t *rev)
@@ -679,6 +704,13 @@ ngx_http_alloc_request(ngx_connection_t *c)
r->method = NGX_HTTP_UNKNOWN;
r->http_version = NGX_HTTP_VERSION_10;
+#if (NGX_HTTP_V3)
+ if (hc->quic) {
+ r->http_version = NGX_HTTP_VERSION_30;
+ r->filter_need_in_memory = 1;
+ }
+#endif
+
r->headers_in.content_length_n = -1;
r->headers_in.keep_alive_n = -1;
r->headers_out.content_length_n = -1;
@@ -1128,7 +1160,16 @@ ngx_http_process_request_line(ngx_event_t *rev)
}
}
- rc = ngx_http_parse_request_line(r, r->header_in);
+ switch (r->http_version) {
+#if (NGX_HTTP_V3)
+ case NGX_HTTP_VERSION_30:
+ rc = ngx_http_v3_parse_header(r, r->header_in, 1);
+ break;
+#endif
+
+ default: /* HTTP/1.x */
+ rc = ngx_http_parse_request_line(r, r->header_in);
+ }
if (rc == NGX_OK) {
@@ -1141,8 +1182,8 @@ ngx_http_process_request_line(ngx_event_t *rev)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http request line: \"%V\"", &r->request_line);
- r->method_name.len = r->method_end - r->request_start + 1;
- r->method_name.data = r->request_line.data;
+ r->method_name.len = r->method_end - r->method_start;
+ r->method_name.data = r->method_start;
if (r->http_protocol.data) {
r->http_protocol.len = r->request_end - r->http_protocol.data;
@@ -1213,6 +1254,15 @@ ngx_http_process_request_line(ngx_event_t *rev)
break;
}
+ if (rc == NGX_DONE) {
+ if (ngx_handle_read_event(rev, 0) != NGX_OK) {
+ ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return;
+ }
+
+ break;
+ }
+
if (rc != NGX_AGAIN) {
/* there was error while a request line parsing */
@@ -1403,7 +1453,7 @@ ngx_http_process_request_headers(ngx_event_t *rev)
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
- rc = NGX_AGAIN;
+ rc = NGX_OK;
for ( ;; ) {
@@ -1457,11 +1507,21 @@ ngx_http_process_request_headers(ngx_event_t *rev)
/* the host header could change the server configuration context */
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
- rc = ngx_http_parse_header_line(r, r->header_in,
- cscf->underscores_in_headers);
+ switch (r->http_version) {
+#if (NGX_HTTP_V3)
+ case NGX_HTTP_VERSION_30:
+ rc = ngx_http_v3_parse_header(r, r->header_in, 0);
+ break;
+#endif
+
+ default: /* HTTP/1.x */
+ rc = ngx_http_parse_header_line(r, r->header_in,
+ cscf->underscores_in_headers);
+ }
if (rc == NGX_OK) {
+ /* XXX */
r->request_length += r->header_in->pos - r->header_name_start;
if (r->invalid_header && cscf->ignore_invalid_headers) {
@@ -1487,11 +1547,11 @@ ngx_http_process_request_headers(ngx_event_t *rev)
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';
+ //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';
+ //h->value.data[h->value.len] = '\0';
h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
if (h->lowcase_key == NULL) {
@@ -1642,7 +1702,7 @@ ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
return NGX_OK;
}
- old = request_line ? r->request_start : r->header_name_start;
+ old = request_line ? r->request_start : r->header_name_start; /* XXX */
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
@@ -1721,45 +1781,59 @@ ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
r->request_end = new + (r->request_end - old);
}
- r->method_end = new + (r->method_end - old);
+ if (r->method_start >= old && r->method_start < r->header_in->pos) {
+ r->method_start = new + (r->method_start - old);
+ r->method_end = new + (r->method_end - old);
+ }
- r->uri_start = new + (r->uri_start - old);
- r->uri_end = new + (r->uri_end - old);
+ if (r->uri_start >= old && r->uri_start < r->header_in->pos) {
+ r->uri_start = new + (r->uri_start - old);
+ r->uri_end = new + (r->uri_end - old);
+ }
- if (r->schema_start) {
+ if (r->schema_start >= old && r->schema_start < r->header_in->pos) {
r->schema_start = new + (r->schema_start - old);
r->schema_end = new + (r->schema_end - old);
}
- if (r->host_start) {
+ if (r->host_start >= old && r->host_start < r->header_in->pos) {
r->host_start = new + (r->host_start - old);
if (r->host_end) {
r->host_end = new + (r->host_end - old);
}
}
- if (r->port_start) {
+ if (r->port_start >= old && r->port_start < r->header_in->pos) {
r->port_start = new + (r->port_start - old);
r->port_end = new + (r->port_end - old);
}
- if (r->uri_ext) {
+ if (r->uri_ext >= old && r->uri_ext < r->header_in->pos) {
r->uri_ext = new + (r->uri_ext - old);
}
- if (r->args_start) {
+ if (r->args_start >= old && r->args_start < r->header_in->pos) {
r->args_start = new + (r->args_start - old);
}
- if (r->http_protocol.data) {
+ if (r->http_protocol.data >= old
+ && r->http_protocol.data < r->header_in->pos)
+ {
r->http_protocol.data = new + (r->http_protocol.data - old);
}
} else {
- r->header_name_start = new;
- r->header_name_end = new + (r->header_name_end - old);
- r->header_start = new + (r->header_start - old);
- r->header_end = new + (r->header_end - old);
+ if (r->header_name_start >= old
+ && r->header_name_start < r->header_in->pos)
+ {
+ r->header_name_start = new;
+ r->header_name_end = new + (r->header_name_end - old);
+ }
+
+ if (r->header_start >= old && r->header_start < r->header_in->pos) {
+ r->header_start = new + (r->header_start - old);
+ r->header_end = new + (r->header_end - old);
+ }
}
r->header_in = b;
@@ -1984,7 +2058,7 @@ ngx_http_process_request_header(ngx_http_request_t *r)
return NGX_ERROR;
}
- if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
+ if (r->headers_in.host == NULL && r->http_version == NGX_HTTP_VERSION_11) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent HTTP/1.1 request without \"Host\" header");
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
index 8cc5d6432..04d88db7b 100644
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -24,6 +24,7 @@
#define NGX_HTTP_VERSION_10 1000
#define NGX_HTTP_VERSION_11 1001
#define NGX_HTTP_VERSION_20 2000
+#define NGX_HTTP_VERSION_30 3000
#define NGX_HTTP_UNKNOWN 0x0001
#define NGX_HTTP_GET 0x0002
@@ -584,6 +585,7 @@ struct ngx_http_request_s {
u_char *args_start;
u_char *request_start;
u_char *request_end;
+ u_char *method_start;
u_char *method_end;
u_char *schema_start;
u_char *schema_end;
@@ -592,6 +594,17 @@ struct ngx_http_request_s {
u_char *port_start;
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;
+#endif
+
unsigned http_minor:16;
unsigned http_major:16;
};
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;
+}