Refactored HTTP/3 parser.

This commit is contained in:
Roman Arutyunyan 2020-03-18 13:46:35 +03:00
parent 023dbc3cfb
commit 01dc7445f0
10 changed files with 1941 additions and 761 deletions

View File

@ -412,8 +412,10 @@ if [ $HTTP = YES ]; then
ngx_module_name=ngx_http_v3_module
ngx_module_incs=src/http/v3
ngx_module_deps=src/http/v3/ngx_http_v3.h
ngx_module_deps="src/http/v3/ngx_http_v3.h \
src/http/v3/ngx_http_v3_parse.h"
ngx_module_srcs="src/http/v3/ngx_http_v3.c \
src/http/v3/ngx_http_v3_parse.c \
src/http/v3/ngx_http_v3_tables.c \
src/http/v3/ngx_http_v3_streams.c \
src/http/v3/ngx_http_v3_request.c \

View File

@ -1163,7 +1163,7 @@ ngx_http_process_request_line(ngx_event_t *rev)
switch (r->http_version) {
#if (NGX_HTTP_V3)
case NGX_HTTP_VERSION_30:
rc = ngx_http_v3_parse_header(r, r->header_in, 1);
rc = ngx_http_v3_parse_header(r, r->header_in);
break;
#endif
@ -1510,7 +1510,7 @@ ngx_http_process_request_headers(ngx_event_t *rev)
switch (r->http_version) {
#if (NGX_HTTP_V3)
case NGX_HTTP_VERSION_30:
rc = ngx_http_v3_parse_header(r, r->header_in, 0);
rc = ngx_http_v3_parse_header(r, r->header_in);
break;
#endif
@ -1547,11 +1547,17 @@ ngx_http_process_request_headers(ngx_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';
if (h->key.data[h->key.len]) {
h->key.data[h->key.len] = '\0';
}
h->value.len = r->header_end - r->header_start;
h->value.data = r->header_start;
h->value.data[h->value.len] = '\0';
if (h->value.data[h->value.len]) {
h->value.data[h->value.len] = '\0';
}
h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
if (h->lowcase_key == NULL) {

View File

@ -595,14 +595,7 @@ struct ngx_http_request_s {
u_char *port_end;
#if (NGX_HTTP_V3)
ngx_uint_t h3_length;
ngx_uint_t h3_index;
ngx_uint_t h3_insert_count;
ngx_uint_t h3_sign;
ngx_uint_t h3_delta_base;
ngx_uint_t h3_huffman;
ngx_uint_t h3_dynamic;
ngx_uint_t h3_offset;
void *h3_parse;
#endif
unsigned http_minor:16;

View File

@ -94,83 +94,3 @@ ngx_http_v3_encode_prefix_int(u_char *p, uint64_t value, ngx_uint_t prefix)
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;
}

View File

@ -12,13 +12,31 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_http_v3_parse.h>
#define NGX_HTTP_V3_STREAM 0x48335354 /* "H3ST" */
#define NGX_HTTP_V3_VARLEN_INT_LEN 4
#define NGX_HTTP_V3_PREFIX_INT_LEN 11
#define NGX_HTTP_V3_VARLEN_INT_LEN 4
#define NGX_HTTP_V3_PREFIX_INT_LEN 11
#define NGX_HTTP_V3_STREAM_CONTROL 0x00
#define NGX_HTTP_V3_STREAM_PUSH 0x01
#define NGX_HTTP_V3_STREAM_ENCODER 0x02
#define NGX_HTTP_V3_STREAM_DECODER 0x03
#define NGX_HTTP_V3_FRAME_DATA 0x00
#define NGX_HTTP_V3_FRAME_HEADERS 0x01
#define NGX_HTTP_V3_FRAME_CANCEL_PUSH 0x03
#define NGX_HTTP_V3_FRAME_SETTINGS 0x04
#define NGX_HTTP_V3_FRAME_PUSH_PROMISE 0x05
#define NGX_HTTP_V3_FRAME_GOAWAY 0x07
#define NGX_HTTP_V3_FRAME_MAX_PUSH_ID 0x0d
#define NGX_HTTP_V3_PARAM_MAX_TABLE_CAPACITY 0x01
#define NGX_HTTP_V3_PARAM_MAX_HEADER_LIST_SIZE 0x06
#define NGX_HTTP_V3_PARAM_BLOCKED_STREAMS 0x07
typedef struct {
@ -28,8 +46,11 @@ typedef struct {
ngx_connection_t *client_encoder;
ngx_connection_t *client_decoder;
ngx_connection_t *client_control;
ngx_connection_t *server_encoder;
ngx_connection_t *server_decoder;
ngx_connection_t *server_control;
} ngx_http_v3_connection_t;
@ -39,18 +60,12 @@ typedef struct {
} ngx_http_v3_header_t;
ngx_int_t ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b,
ngx_uint_t pseudo);
ngx_int_t ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b);
ngx_chain_t *ngx_http_v3_create_header(ngx_http_request_t *r);
uintptr_t ngx_http_v3_encode_varlen_int(u_char *p, uint64_t value);
uintptr_t ngx_http_v3_encode_prefix_int(u_char *p, uint64_t value,
ngx_uint_t prefix);
uint64_t ngx_http_v3_decode_varlen_int(u_char *p);
int64_t ngx_http_v3_decode_prefix_int(u_char **src, size_t len,
ngx_uint_t prefix);
ngx_int_t ngx_http_v3_decode_huffman(ngx_connection_t *c, ngx_str_t *s);
void ngx_http_v3_handle_client_uni_stream(ngx_connection_t *c);
@ -67,6 +82,8 @@ ngx_http_v3_header_t *ngx_http_v3_lookup_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_set_param(ngx_connection_t *c, uint64_t id,
uint64_t value);
ngx_int_t ngx_http_v3_client_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic,
ngx_uint_t index, ngx_str_t *value);

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,145 @@
/*
* Copyright (C) Roman Arutyunyan
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_HTTP_V3_PARSE_H_INCLUDED_
#define _NGX_HTTP_V3_PARSE_H_INCLUDED_
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_uint_t state;
uint64_t value;
} ngx_http_v3_parse_varlen_int_t;
typedef struct {
ngx_uint_t state;
ngx_uint_t mask;
uint64_t value;
} ngx_http_v3_parse_prefix_int_t;
typedef struct {
ngx_uint_t state;
uint64_t id;
ngx_http_v3_parse_varlen_int_t vlint;
} ngx_http_v3_parse_settings_t;
typedef struct {
ngx_uint_t state;
ngx_uint_t insert_count;
ngx_uint_t delta_base;
ngx_uint_t sign;
ngx_uint_t base;
ngx_http_v3_parse_prefix_int_t pint;
} ngx_http_v3_parse_header_block_prefix_t;
typedef struct {
ngx_uint_t state;
ngx_uint_t length;
ngx_uint_t huffman;
ngx_str_t value;
u_char *last;
u_char huffstate;
} ngx_http_v3_parse_literal_t;
typedef struct {
ngx_uint_t state;
ngx_uint_t index;
ngx_uint_t base;
ngx_uint_t dynamic;
ngx_str_t name;
ngx_str_t value;
ngx_http_v3_parse_prefix_int_t pint;
ngx_http_v3_parse_literal_t literal;
} ngx_http_v3_parse_header_t;
typedef struct {
ngx_uint_t state;
ngx_http_v3_parse_header_t header;
} ngx_http_v3_parse_header_rep_t;
typedef struct {
ngx_uint_t state;
ngx_uint_t length;
ngx_http_v3_parse_varlen_int_t vlint;
ngx_http_v3_parse_header_block_prefix_t prefix;
ngx_http_v3_parse_header_rep_t header_rep;
} ngx_http_v3_parse_headers_t;
typedef struct {
ngx_uint_t state;
ngx_http_v3_parse_header_t header;
ngx_http_v3_parse_prefix_int_t pint;
} ngx_http_v3_parse_encoder_t;
typedef struct {
ngx_uint_t state;
ngx_http_v3_parse_prefix_int_t pint;
} ngx_http_v3_parse_decoder_t;
typedef struct {
ngx_uint_t state;
ngx_uint_t type;
ngx_uint_t length;
ngx_http_v3_parse_varlen_int_t vlint;
ngx_http_v3_parse_settings_t settings;
} ngx_http_v3_parse_control_t;
ngx_int_t ngx_http_v3_parse_varlen_int(ngx_connection_t *c,
ngx_http_v3_parse_varlen_int_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_prefix_int(ngx_connection_t *c,
ngx_http_v3_parse_prefix_int_t *st, ngx_uint_t prefix, u_char ch);
ngx_int_t ngx_http_v3_parse_headers(ngx_connection_t *c,
ngx_http_v3_parse_headers_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_block_prefix(ngx_connection_t *c,
ngx_http_v3_parse_header_block_prefix_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_rep(ngx_connection_t *c,
ngx_http_v3_parse_header_rep_t *st, ngx_uint_t base, u_char ch);
ngx_int_t ngx_http_v3_parse_literal(ngx_connection_t *c,
ngx_http_v3_parse_literal_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_ri(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_lri(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_l(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_pbi(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_lpbi(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch);
ngx_int_t ngx_http_v3_parse_settings(ngx_connection_t *c,
ngx_http_v3_parse_settings_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_encoder(ngx_connection_t *c, void *data, u_char ch);
ngx_int_t ngx_http_v3_parse_header_inr(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_header_iwnr(ngx_connection_t *c,
ngx_http_v3_parse_header_t *st, u_char ch);
ngx_int_t ngx_http_v3_parse_decoder(ngx_connection_t *c, void *data, u_char ch);
#endif /* _NGX_HTTP_V3_PARSE_H_INCLUDED_ */

View File

@ -10,15 +10,6 @@
#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);
@ -46,6 +37,110 @@ struct {
};
ngx_int_t
ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b)
{
ngx_int_t rc;
ngx_str_t *name, *value;
ngx_connection_t *c;
ngx_http_v3_parse_headers_t *st;
enum {
sw_start = 0,
sw_prev,
sw_headers,
sw_last,
sw_done
};
c = r->connection;
st = r->h3_parse;
if (st == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header");
st = ngx_pcalloc(c->pool, sizeof(ngx_http_v3_parse_headers_t));
if (st == NULL) {
goto failed;
}
r->h3_parse = st;
}
switch (r->state) {
case sw_prev:
r->state = sw_headers;
return NGX_OK;
case sw_done:
goto done;
case sw_last:
r->state = sw_done;
return NGX_OK;
default:
break;
}
for ( /* void */ ; b->pos < b->last; b->pos++) {
rc = ngx_http_v3_parse_headers(c, st, *b->pos);
if (rc == NGX_ERROR) {
goto failed;
}
if (rc == NGX_AGAIN) {
continue;
}
name = &st->header_rep.header.name;
value = &st->header_rep.header.value;
if (r->state == sw_start
&& ngx_http_v3_process_pseudo_header(r, name, value) != NGX_OK)
{
if (rc == NGX_DONE) {
r->state = sw_last;
} else {
r->state = sw_prev;
}
} else if (rc == NGX_DONE) {
r->state = sw_done;
}
if (r->state == sw_start) {
continue;
}
r->header_name_start = name->data;
r->header_name_end = name->data + name->len;
r->header_start = value->data;
r->header_end = value->data + value->len;
r->header_hash = ngx_hash_key(name->data, name->len);
/* XXX r->lowcase_index = i; */
return NGX_OK;
}
return NGX_AGAIN;
failed:
return r->state == sw_start ? NGX_HTTP_PARSE_INVALID_REQUEST
: NGX_HTTP_PARSE_INVALID_HEADER;
done:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 parse header done");
return NGX_HTTP_PARSE_HEADER_DONE;
}
#if 0
ngx_int_t
ngx_http_v3_parse_header(ngx_http_request_t *r, ngx_buf_t *b, ngx_uint_t pseudo)
{
@ -167,11 +262,14 @@ again:
break;
}
/* fall through */
length &= 0x3fffff;
state = sw_header_block;
break;
case sw_length_3:
length &= 0x3fffff;
length = (length << 8) + ch;
length &= 0x3fffffff;
state = sw_header_block;
break;
@ -567,6 +665,7 @@ failed:
return NGX_HTTP_PARSE_INVALID_REQUEST;
}
#endif
static ngx_int_t
@ -576,6 +675,10 @@ ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name,
ngx_uint_t i;
ngx_connection_t *c;
if (name->len == 0 || name->data[0] != ':') {
return NGX_DECLINED;
}
c = r->connection;
if (name->len == 7 && ngx_strncmp(name->data, ":method", 7) == 0) {
@ -635,14 +738,10 @@ ngx_http_v3_process_pseudo_header(ngx_http_request_t *r, ngx_str_t *name,
return NGX_OK;
}
if (name->len && name->data[0] == ':') {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 unknown pseudo header \"%V\" \"%V\"",
name, value);
return NGX_OK;
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 unknown pseudo header \"%V\" \"%V\"", name, value);
return NGX_DONE;
return NGX_OK;
}
@ -789,7 +888,7 @@ ngx_http_v3_create_header(ngx_http_request_t *r)
b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 25, 4);
*b->last = 0;
b->last = (u_char *) ngx_http_v3_encode_prefix_int(b->last, 3, 7);
b->last = ngx_sprintf(b->last, "%03ui ", r->headers_out.status);
b->last = ngx_sprintf(b->last, "%03ui", r->headers_out.status);
}
if (r->headers_out.server == NULL) {

View File

@ -10,42 +10,30 @@
#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 ngx_int_t (*ngx_http_v3_handler_pt)(ngx_connection_t *c, void *data,
u_char ch);
typedef struct {
uint32_t signature; /* QSTR */
u_char buf[4];
uint32_t signature; /* QSTR */
ngx_uint_t len;
ngx_uint_t type;
ngx_uint_t state;
ngx_uint_t index;
ngx_uint_t offset;
ngx_http_v3_handler_pt handler;
void *data;
ngx_str_t name;
ngx_str_t value;
unsigned client:1;
unsigned dynamic:1;
unsigned huffman:1;
ngx_uint_t type;
ngx_uint_t client; /* unsigned client:1; */
} ngx_http_v3_uni_stream_t;
static void ngx_http_v3_close_uni_stream(ngx_connection_t *c);
static void ngx_http_v3_uni_stream_cleanup(void *data);
static void ngx_http_v3_read_uni_stream_type(ngx_event_t *rev);
static void ngx_http_v3_dummy_stream_handler(ngx_event_t *rev);
static void ngx_http_v3_client_encoder_handler(ngx_event_t *rev);
static void ngx_http_v3_client_decoder_handler(ngx_event_t *rev);
static void ngx_http_v3_uni_read_handler(ngx_event_t *rev);
static ngx_connection_t *ngx_http_v3_create_uni_stream(ngx_connection_t *c,
ngx_uint_t type);
static ngx_connection_t *ngx_http_v3_get_server_encoder(ngx_connection_t *c);
static ngx_connection_t *ngx_http_v3_get_server_decoder(ngx_connection_t *c);
static ngx_connection_t *ngx_http_v3_get_control(ngx_connection_t *c);
static ngx_connection_t *ngx_http_v3_get_encoder(ngx_connection_t *c);
static ngx_connection_t *ngx_http_v3_get_decoder(ngx_connection_t *c);
void
@ -56,8 +44,13 @@ ngx_http_v3_handle_client_uni_stream(ngx_connection_t *c)
c->log->connection = c->number;
/* XXX */
(void) ngx_http_v3_get_control(c);
(void) ngx_http_v3_get_encoder(c);
(void) ngx_http_v3_get_decoder(c);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 new uni stream id:0x%uXL", c->qs->id);
"http3 new uni stream id:0x%uxL", c->qs->id);
us = ngx_pcalloc(c->pool, sizeof(ngx_http_v3_uni_stream_t));
if (us == NULL) {
@ -81,7 +74,7 @@ ngx_http_v3_handle_client_uni_stream(ngx_connection_t *c)
cln->data = c;
c->read->handler = ngx_http_v3_read_uni_stream_type;
c->read->handler(c->read);
ngx_http_v3_read_uni_stream_type(c->read);
}
@ -115,7 +108,7 @@ ngx_http_v3_uni_stream_cleanup(void *data)
switch (us->type) {
case NGX_HTTP_V3_ENCODER_STREAM:
case NGX_HTTP_V3_STREAM_ENCODER:
if (us->client) {
h3c->client_encoder = NULL;
@ -125,7 +118,7 @@ ngx_http_v3_uni_stream_cleanup(void *data)
break;
case NGX_HTTP_V3_DECODER_STREAM:
case NGX_HTTP_V3_STREAM_DECODER:
if (us->client) {
h3c->client_decoder = NULL;
@ -133,6 +126,16 @@ ngx_http_v3_uni_stream_cleanup(void *data)
h3c->server_decoder = NULL;
}
break;
case NGX_HTTP_V3_STREAM_CONTROL:
if (us->client) {
h3c->client_control = NULL;
} else {
h3c->server_control = NULL;
}
break;
}
}
@ -141,81 +144,153 @@ ngx_http_v3_uni_stream_cleanup(void *data)
static void
ngx_http_v3_read_uni_stream_type(ngx_event_t *rev)
{
u_char *p;
ssize_t n, len;
u_char ch;
ssize_t n;
ngx_connection_t *c;
ngx_http_v3_connection_t *h3c;
ngx_http_v3_uni_stream_t *us;
ngx_http_v3_uni_stream_t *st;
c = rev->data;
us = c->data;
st = c->data;
h3c = c->qs->parent->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 read stream type");
while (rev->ready) {
p = &us->buf[us->len];
if (us->len == 0) {
len = 1;
} else {
len = (us->buf[0] >> 6) + 1 - us->len;
}
n = c->recv(c, p, len);
n = c->recv(c, &ch, 1);
if (n == NGX_ERROR) {
goto failed;
}
if (n == NGX_AGAIN || n != 1) {
break;
}
st->type = ch;
switch (st->type) {
case NGX_HTTP_V3_STREAM_ENCODER:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 encoder stream");
if (h3c->client_encoder) {
goto failed;
}
h3c->client_encoder = c;
st->handler = ngx_http_v3_parse_encoder;
n = sizeof(ngx_http_v3_parse_encoder_t);
break;
case NGX_HTTP_V3_STREAM_DECODER:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 decoder stream");
if (h3c->client_decoder) {
goto failed;
}
h3c->client_decoder = c;
st->handler = ngx_http_v3_parse_decoder;
n = sizeof(ngx_http_v3_parse_decoder_t);
break;
case NGX_HTTP_V3_STREAM_CONTROL:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 control stream");
if (h3c->client_control) {
goto failed;
}
h3c->client_control = c;
st->handler = ngx_http_v3_parse_control;
n = sizeof(ngx_http_v3_parse_control_t);
break;
default:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 stream 0x%02xi", st->type);
n = 0;
}
if (n) {
st->data = ngx_pcalloc(c->pool, n);
if (st->data == NULL) {
goto failed;
}
}
rev->handler = ngx_http_v3_uni_read_handler;
ngx_http_v3_uni_read_handler(rev);
return;
}
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
goto failed;
}
return;
failed:
ngx_http_v3_close_uni_stream(c);
}
static void
ngx_http_v3_uni_read_handler(ngx_event_t *rev)
{
u_char buf[128];
ssize_t n;
ngx_int_t rc, i;
ngx_connection_t *c;
ngx_http_v3_uni_stream_t *st;
c = rev->data;
st = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 read handler");
while (rev->ready) {
n = c->recv(c, buf, sizeof(buf));
if (n == NGX_ERROR || n == 0) {
goto failed;
}
if (n == NGX_AGAIN) {
break;
}
us->len += n;
if (n != len) {
break;
if (st->handler == NULL) {
continue;
}
if ((us->buf[0] >> 6) + 1 == us->len) {
us->type = ngx_http_v3_decode_varlen_int(us->buf);
for (i = 0; i < n; i++) {
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 stream type:%ui", us->type);
rc = st->handler(c, st->data, buf[i]);
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;
if (rc == NGX_ERROR) {
goto failed;
}
rev->handler(rev);
return;
if (rc == NGX_DONE) {
goto done;
}
/* rc == NGX_AGAIN */
}
}
@ -225,556 +300,9 @@ ngx_http_v3_read_uni_stream_type(ngx_event_t *rev)
return;
failed:
done:
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;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 read done");
failed:
@ -835,7 +363,7 @@ failed:
static ngx_connection_t *
ngx_http_v3_get_server_encoder(ngx_connection_t *c)
ngx_http_v3_get_control(ngx_connection_t *c)
{
ngx_http_v3_connection_t *h3c;
@ -843,7 +371,7 @@ ngx_http_v3_get_server_encoder(ngx_connection_t *c)
if (h3c->server_encoder == NULL) {
h3c->server_encoder = ngx_http_v3_create_uni_stream(c,
NGX_HTTP_V3_ENCODER_STREAM);
NGX_HTTP_V3_STREAM_CONTROL);
}
return h3c->server_encoder;
@ -851,18 +379,34 @@ ngx_http_v3_get_server_encoder(ngx_connection_t *c)
static ngx_connection_t *
ngx_http_v3_get_server_decoder(ngx_connection_t *c)
ngx_http_v3_get_encoder(ngx_connection_t *c)
{
ngx_http_v3_connection_t *h3c;
h3c = c->qs->parent->data;
if (h3c->server_decoder == NULL) {
h3c->server_decoder = ngx_http_v3_create_uni_stream(c,
NGX_HTTP_V3_DECODER_STREAM);
if (h3c->server_encoder == NULL) {
h3c->server_encoder = ngx_http_v3_create_uni_stream(c,
NGX_HTTP_V3_STREAM_ENCODER);
}
return h3c->server_decoder;
return h3c->server_encoder;
}
static ngx_connection_t *
ngx_http_v3_get_decoder(ngx_connection_t *c)
{
ngx_http_v3_connection_t *h3c;
h3c = c->qs->parent->data;
if (h3c->server_encoder == NULL) {
h3c->server_encoder = ngx_http_v3_create_uni_stream(c,
NGX_HTTP_V3_STREAM_DECODER);
}
return h3c->server_encoder;
}
@ -878,7 +422,7 @@ ngx_http_v3_client_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic,
"http3 client ref insert, %s[%ui] \"%V\"",
dynamic ? "dynamic" : "static", index, value);
ec = ngx_http_v3_get_server_encoder(c);
ec = ngx_http_v3_get_encoder(c);
if (ec == NULL) {
return NGX_ERROR;
}
@ -923,7 +467,7 @@ ngx_http_v3_client_insert(ngx_connection_t *c, ngx_str_t *name,
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 client insert \"%V\":\"%V\"", name, value);
ec = ngx_http_v3_get_server_encoder(c);
ec = ngx_http_v3_get_encoder(c);
if (ec == NULL) {
return NGX_ERROR;
}
@ -972,7 +516,7 @@ ngx_http_v3_client_set_capacity(ngx_connection_t *c, ngx_uint_t capacity)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 client set capacity %ui", capacity);
ec = ngx_http_v3_get_server_encoder(c);
ec = ngx_http_v3_get_encoder(c);
if (ec == NULL) {
return NGX_ERROR;
}
@ -999,7 +543,7 @@ ngx_http_v3_client_duplicate(ngx_connection_t *c, ngx_uint_t index)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 client duplicate %ui", index);
ec = ngx_http_v3_get_server_encoder(c);
ec = ngx_http_v3_get_encoder(c);
if (ec == NULL) {
return NGX_ERROR;
}
@ -1026,7 +570,7 @@ ngx_http_v3_client_ack_header(ngx_connection_t *c, ngx_uint_t stream_id)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 client ack header %ui", stream_id);
dc = ngx_http_v3_get_server_decoder(c);
dc = ngx_http_v3_get_decoder(c);
if (dc == NULL) {
return NGX_ERROR;
}
@ -1053,7 +597,7 @@ ngx_http_v3_client_cancel_stream(ngx_connection_t *c, ngx_uint_t stream_id)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 client cancel stream %ui", stream_id);
dc = ngx_http_v3_get_server_decoder(c);
dc = ngx_http_v3_get_decoder(c);
if (dc == NULL) {
return NGX_ERROR;
}
@ -1080,7 +624,7 @@ ngx_http_v3_client_inc_insert_count(ngx_connection_t *c, ngx_uint_t inc)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 client increment insert count %ui", inc);
dc = ngx_http_v3_get_server_decoder(c);
dc = ngx_http_v3_get_decoder(c);
if (dc == NULL) {
return NGX_ERROR;
}

View File

@ -383,3 +383,33 @@ ngx_http_v3_new_header(ngx_connection_t *c)
return NGX_OK;
}
ngx_int_t
ngx_http_v3_set_param(ngx_connection_t *c, uint64_t id, uint64_t value)
{
switch (id) {
case NGX_HTTP_V3_PARAM_MAX_TABLE_CAPACITY:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 param QPACK_MAX_TABLE_CAPACITY:%uL", value);
break;
case NGX_HTTP_V3_PARAM_MAX_HEADER_LIST_SIZE:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 param SETTINGS_MAX_HEADER_LIST_SIZE:%uL", value);
break;
case NGX_HTTP_V3_PARAM_BLOCKED_STREAMS:
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 param QPACK_BLOCKED_STREAMS:%uL", value);
break;
default:
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http3 param #%uL:%uL", id, value);
}
return NGX_OK;
}