This commit is contained in:
Roman Arutyunyan 2020-03-13 19:36:33 +03:00
parent 365b77b587
commit 7739b6073b
18 changed files with 3016 additions and 72 deletions

View File

@ -7,8 +7,8 @@ echo "creating $NGX_MAKEFILE"
mkdir -p $NGX_OBJS/src/core $NGX_OBJS/src/event $NGX_OBJS/src/event/modules \
$NGX_OBJS/src/os/unix $NGX_OBJS/src/os/win32 \
$NGX_OBJS/src/http $NGX_OBJS/src/http/v2 $NGX_OBJS/src/http/modules \
$NGX_OBJS/src/http/modules/perl \
$NGX_OBJS/src/http $NGX_OBJS/src/http/v2 $NGX_OBJS/src/http/v3 \
$NGX_OBJS/src/http/modules $NGX_OBJS/src/http/modules/perl \
$NGX_OBJS/src/mail \
$NGX_OBJS/src/stream \
$NGX_OBJS/src/misc

View File

@ -403,6 +403,27 @@ if [ $HTTP = YES ]; then
ngx_module_type=HTTP
if [ $HTTP_V3 = YES ]; then
have=NGX_HTTP_V3 . auto/have
have=NGX_HTTP_HEADERS . auto/have
# XXX for Huffman
HTTP_V2=YES
ngx_module_name=ngx_http_v3_module
ngx_module_incs=src/http/v3
ngx_module_deps=src/http/v3/ngx_http_v3.h
ngx_module_srcs="src/http/v3/ngx_http_v3.c \
src/http/v3/ngx_http_v3_tables.c \
src/http/v3/ngx_http_v3_streams.c \
src/http/v3/ngx_http_v3_request.c \
src/http/v3/ngx_http_v3_module.c"
ngx_module_libs=
ngx_module_link=$HTTP_V3
. auto/module
fi
if [ $HTTP_V2 = YES ]; then
have=NGX_HTTP_V2 . auto/have
have=NGX_HTTP_HEADERS . auto/have

View File

@ -59,6 +59,7 @@ HTTP_CHARSET=YES
HTTP_GZIP=YES
HTTP_SSL=NO
HTTP_V2=NO
HTTP_V3=YES
HTTP_SSI=YES
HTTP_REALIP=NO
HTTP_XSLT=NO

View File

@ -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) {

View File

@ -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;
}
pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, c->log);
if (pool == NULL) {
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;
}
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;

View File

@ -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? */

View File

@ -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

View File

@ -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
}

View File

@ -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;
}

View File

@ -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) {

View File

@ -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;
if (c->qs->unidirectional) {
ngx_http_v3_handle_client_uni_stream(c);
return;
}
hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
if (hc == NULL) {
ngx_http_close_connection(c);
return;
}
ngx_memcpy(hc, h3c, sizeof(ngx_http_connection_t));
c->data = hc;
ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
if (ctx == NULL) {
ngx_http_close_connection(c);
return;
}
ctx->connection = c;
ctx->request = NULL;
ctx->current_request = NULL;
c->log->connection = c->number;
c->log->handler = ngx_http_log_error;
c->log->data = ctx;
c->log->action = "waiting for request";
c->log_error = NGX_ERROR_INFO;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"quic stream: 0x%uXL", qs->id);
ssize_t n;
ngx_buf_t b;
"http3 new stream id:0x%uXL", c->qs->id);
u_char buf[512];
rev = c->read;
rev->handler = ngx_http_wait_request_handler;
c->write->handler = ngx_http_empty_handler;
b.start = buf;
b.end = buf + 512;
b.pos = b.last = b.start;
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");
return;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"quic stream: 0x%uXL %ui bytes read", qs->id, n);
b.last += n;
n = c->send(c, b.start, n);
if (n < 0) {
ngx_log_error(NGX_LOG_INFO, c->log, 0, "stream write failed");
return;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"quic stream: 0x%uXL %ui bytes written", qs->id, n);
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);

View File

@ -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;
};

176
src/http/v3/ngx_http_v3.c Normal file
View File

@ -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;
}

89
src/http/v3/ngx_http_v3.h Normal file
View File

@ -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_ */

View File

@ -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
};

View File

@ -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;
}

File diff suppressed because it is too large Load Diff

View File

@ -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;
}