aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Arutyunyan <arut@nginx.com>2020-07-23 13:41:24 +0300
committerRoman Arutyunyan <arut@nginx.com>2020-07-23 13:41:24 +0300
commit6d064c94e0600f7ff15e2815784f9f89cc4858b4 (patch)
tree4ed79e2d168f9c145167a5fe924bc1e050f3ad00
parent77384356cea5750aee576b7d4a68aa0b0a1c13a1 (diff)
downloadnginx-6d064c94e0600f7ff15e2815784f9f89cc4858b4.tar.gz
nginx-6d064c94e0600f7ff15e2815784f9f89cc4858b4.zip
HTTP/3: server pushes.
New directives are added: - http3_max_concurrent_pushes - http3_push - http3_push_preload
-rw-r--r--src/http/ngx_http.h1
-rw-r--r--src/http/ngx_http_request.c3
-rw-r--r--src/http/v3/ngx_http_v3.h21
-rw-r--r--src/http/v3/ngx_http_v3_module.c133
-rw-r--r--src/http/v3/ngx_http_v3_parse.c36
-rw-r--r--src/http/v3/ngx_http_v3_request.c704
-rw-r--r--src/http/v3/ngx_http_v3_streams.c135
7 files changed, 1020 insertions, 13 deletions
diff --git a/src/http/ngx_http.h b/src/http/ngx_http.h
index 480802d40..2a3d81a37 100644
--- a/src/http/ngx_http.h
+++ b/src/http/ngx_http.h
@@ -93,6 +93,7 @@ ngx_int_t ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
void ngx_http_init_connection(ngx_connection_t *c);
void ngx_http_close_connection(ngx_connection_t *c);
+u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
#if (NGX_HTTP_SSL && defined SSL_CTRL_SET_TLSEXT_HOSTNAME)
int ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg);
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
index 82deef674..54e5e2866 100644
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -55,7 +55,6 @@ static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
static void ngx_http_log_request(ngx_http_request_t *r);
-static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
ngx_http_request_t *sr, u_char *buf, size_t len);
@@ -3838,7 +3837,7 @@ ngx_http_close_connection(ngx_connection_t *c)
}
-static u_char *
+u_char *
ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
u_char *p;
diff --git a/src/http/v3/ngx_http_v3.h b/src/http/v3/ngx_http_v3.h
index 28e0e6f11..aab27b3ac 100644
--- a/src/http/v3/ngx_http_v3.h
+++ b/src/http/v3/ngx_http_v3.h
@@ -50,6 +50,7 @@
#define NGX_HTTP_V3_DEFAULT_MAX_FIELD_SIZE 4096
#define NGX_HTTP_V3_DEFAULT_MAX_TABLE_CAPACITY 16384
#define NGX_HTTP_V3_DEFAULT_MAX_BLOCKED_STREAMS 16
+#define NGX_HTTP_V3_DEFAULT_MAX_CONCURRENT_PUSHES 10
/* HTTP/3 errors */
#define NGX_HTTP_V3_ERR_NO_ERROR 0x100
@@ -89,10 +90,18 @@ typedef struct {
size_t max_field_size;
size_t max_table_capacity;
ngx_uint_t max_blocked_streams;
+ ngx_uint_t max_concurrent_pushes;
} ngx_http_v3_srv_conf_t;
typedef struct {
+ ngx_flag_t push_preload;
+ ngx_flag_t push;
+ ngx_array_t *pushes;
+} ngx_http_v3_loc_conf_t;
+
+
+typedef struct {
ngx_str_t name;
ngx_str_t value;
} ngx_http_v3_header_t;
@@ -110,8 +119,15 @@ typedef struct {
typedef struct {
ngx_http_connection_t hc;
ngx_http_v3_dynamic_table_t table;
+
ngx_queue_t blocked;
ngx_uint_t nblocked;
+
+ ngx_queue_t pushing;
+ ngx_uint_t npushing;
+ uint64_t next_push_id;
+ uint64_t max_push_id;
+
ngx_uint_t settings_sent;
/* unsigned settings_sent:1; */
ngx_connection_t *known_streams[NGX_HTTP_V3_MAX_KNOWN_STREAM];
@@ -144,6 +160,8 @@ uintptr_t ngx_http_v3_encode_header_pbi(u_char *p, ngx_uint_t index);
uintptr_t ngx_http_v3_encode_header_lpbi(u_char *p, ngx_uint_t index,
u_char *data, size_t len);
+ngx_connection_t *ngx_http_v3_create_push_stream(ngx_connection_t *c,
+ uint64_t push_id);
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,
@@ -163,6 +181,9 @@ 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_set_max_push_id(ngx_connection_t *c,
+ uint64_t max_push_id);
+ngx_int_t ngx_http_v3_cancel_push(ngx_connection_t *c, uint64_t push_id);
ngx_int_t ngx_http_v3_client_ref_insert(ngx_connection_t *c, ngx_uint_t dynamic,
ngx_uint_t index, ngx_str_t *value);
diff --git a/src/http/v3/ngx_http_v3_module.c b/src/http/v3/ngx_http_v3_module.c
index 2483b9cb6..89748f5f4 100644
--- a/src/http/v3/ngx_http_v3_module.c
+++ b/src/http/v3/ngx_http_v3_module.c
@@ -16,6 +16,10 @@ static ngx_int_t ngx_http_v3_add_variables(ngx_conf_t *cf);
static void *ngx_http_v3_create_srv_conf(ngx_conf_t *cf);
static char *ngx_http_v3_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
+static void *ngx_http_v3_create_loc_conf(ngx_conf_t *cf);
+static char *ngx_http_v3_merge_loc_conf(ngx_conf_t *cf, void *parent,
+ void *child);
+static char *ngx_http_v3_push(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_command_t ngx_http_v3_commands[] = {
@@ -41,6 +45,27 @@ static ngx_command_t ngx_http_v3_commands[] = {
offsetof(ngx_http_v3_srv_conf_t, max_blocked_streams),
NULL },
+ { ngx_string("http3_max_concurrent_pushes"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_num_slot,
+ NGX_HTTP_SRV_CONF_OFFSET,
+ offsetof(ngx_http_v3_srv_conf_t, max_concurrent_pushes),
+ NULL },
+
+ { ngx_string("http3_push"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_http_v3_push,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
+ { ngx_string("http3_push_preload"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+ ngx_conf_set_flag_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_v3_loc_conf_t, push_preload),
+ NULL },
+
ngx_null_command
};
@@ -55,8 +80,8 @@ static ngx_http_module_t ngx_http_v3_module_ctx = {
ngx_http_v3_create_srv_conf, /* create server configuration */
ngx_http_v3_merge_srv_conf, /* merge server configuration */
- NULL, /* create location configuration */
- NULL /* merge location configuration */
+ ngx_http_v3_create_loc_conf, /* create location configuration */
+ ngx_http_v3_merge_loc_conf /* merge location configuration */
};
@@ -135,6 +160,7 @@ ngx_http_v3_create_srv_conf(ngx_conf_t *cf)
h3scf->max_field_size = NGX_CONF_UNSET_SIZE;
h3scf->max_table_capacity = NGX_CONF_UNSET_SIZE;
h3scf->max_blocked_streams = NGX_CONF_UNSET_UINT;
+ h3scf->max_concurrent_pushes = NGX_CONF_UNSET_UINT;
return h3scf;
}
@@ -158,5 +184,108 @@ ngx_http_v3_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
prev->max_blocked_streams,
NGX_HTTP_V3_DEFAULT_MAX_BLOCKED_STREAMS);
+ ngx_conf_merge_uint_value(conf->max_concurrent_pushes,
+ prev->max_concurrent_pushes,
+ NGX_HTTP_V3_DEFAULT_MAX_CONCURRENT_PUSHES);
+
+ return NGX_CONF_OK;
+}
+
+
+static void *
+ngx_http_v3_create_loc_conf(ngx_conf_t *cf)
+{
+ ngx_http_v3_loc_conf_t *h3lcf;
+
+ h3lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_v3_loc_conf_t));
+ if (h3lcf == NULL) {
+ return NULL;
+ }
+
+ /*
+ * set by ngx_pcalloc():
+ *
+ * h3lcf->pushes = NULL;
+ */
+
+ h3lcf->push_preload = NGX_CONF_UNSET;
+ h3lcf->push = NGX_CONF_UNSET;
+
+ return h3lcf;
+}
+
+
+static char *
+ngx_http_v3_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
+{
+ ngx_http_v3_loc_conf_t *prev = parent;
+ ngx_http_v3_loc_conf_t *conf = child;
+
+ ngx_conf_merge_value(conf->push, prev->push, 1);
+
+ if (conf->push && conf->pushes == NULL) {
+ conf->pushes = prev->pushes;
+ }
+
+ ngx_conf_merge_value(conf->push_preload, prev->push_preload, 0);
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_v3_push(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ ngx_http_v3_loc_conf_t *h3lcf = conf;
+
+ ngx_str_t *value;
+ ngx_http_complex_value_t *cv;
+ ngx_http_compile_complex_value_t ccv;
+
+ value = cf->args->elts;
+
+ if (ngx_strcmp(value[1].data, "off") == 0) {
+
+ if (h3lcf->pushes) {
+ return "\"off\" parameter cannot be used with URI";
+ }
+
+ if (h3lcf->push == 0) {
+ return "is duplicate";
+ }
+
+ h3lcf->push = 0;
+ return NGX_CONF_OK;
+ }
+
+ if (h3lcf->push == 0) {
+ return "URI cannot be used with \"off\" parameter";
+ }
+
+ h3lcf->push = 1;
+
+ if (h3lcf->pushes == NULL) {
+ h3lcf->pushes = ngx_array_create(cf->pool, 1,
+ sizeof(ngx_http_complex_value_t));
+ if (h3lcf->pushes == NULL) {
+ return NGX_CONF_ERROR;
+ }
+ }
+
+ cv = ngx_array_push(h3lcf->pushes);
+ if (cv == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+
+ ccv.cf = cf;
+ ccv.value = &value[1];
+ ccv.complex_value = cv;
+
+ if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+ return NGX_CONF_ERROR;
+ }
+
return NGX_CONF_OK;
}
diff --git a/src/http/v3/ngx_http_v3_parse.c b/src/http/v3/ngx_http_v3_parse.c
index 8a68fddd8..1a7aa17f8 100644
--- a/src/http/v3/ngx_http_v3_parse.c
+++ b/src/http/v3/ngx_http_v3_parse.c
@@ -933,6 +933,7 @@ ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch)
sw_first_type,
sw_type,
sw_length,
+ sw_cancel_push,
sw_settings,
sw_max_push_id,
sw_skip
@@ -988,6 +989,10 @@ ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch)
switch (st->type) {
+ case NGX_HTTP_V3_FRAME_CANCEL_PUSH:
+ st->state = sw_cancel_push;
+ break;
+
case NGX_HTTP_V3_FRAME_SETTINGS:
st->state = sw_settings;
break;
@@ -1004,6 +1009,26 @@ ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch)
break;
+ case sw_cancel_push:
+
+ rc = ngx_http_v3_parse_varlen_int(c, &st->vlint, ch);
+
+ if (--st->length == 0 && rc == NGX_AGAIN) {
+ return NGX_HTTP_V3_ERR_FRAME_ERROR;
+ }
+
+ if (rc != NGX_DONE) {
+ return rc;
+ }
+
+ rc = ngx_http_v3_cancel_push(c, st->vlint.value);
+ if (rc != NGX_OK) {
+ return rc;
+ }
+
+ st->state = sw_type;
+ break;
+
case sw_settings:
rc = ngx_http_v3_parse_settings(c, &st->settings, ch);
@@ -1025,12 +1050,19 @@ ngx_http_v3_parse_control(ngx_connection_t *c, void *data, u_char ch)
case sw_max_push_id:
rc = ngx_http_v3_parse_varlen_int(c, &st->vlint, ch);
+
+ if (--st->length == 0 && rc == NGX_AGAIN) {
+ return NGX_HTTP_V3_ERR_FRAME_ERROR;
+ }
+
if (rc != NGX_DONE) {
return rc;
}
- ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
- "http3 parse MAX_PUSH_ID:%uL", st->vlint.value);
+ rc = ngx_http_v3_set_max_push_id(c, st->vlint.value);
+ if (rc != NGX_OK) {
+ return rc;
+ }
st->state = sw_type;
break;
diff --git a/src/http/v3/ngx_http_v3_request.c b/src/http/v3/ngx_http_v3_request.c
index adc40d368..a0259be11 100644
--- a/src/http/v3/ngx_http_v3_request.c
+++ b/src/http/v3/ngx_http_v3_request.c
@@ -11,18 +11,37 @@
/* static table indices */
+#define NGX_HTTP_V3_HEADER_AUTHORITY 0
+#define NGX_HTTP_V3_HEADER_PATH_ROOT 1
#define NGX_HTTP_V3_HEADER_CONTENT_LENGTH_ZERO 4
#define NGX_HTTP_V3_HEADER_DATE 6
#define NGX_HTTP_V3_HEADER_LAST_MODIFIED 10
#define NGX_HTTP_V3_HEADER_LOCATION 12
+#define NGX_HTTP_V3_HEADER_METHOD_GET 17
+#define NGX_HTTP_V3_HEADER_SCHEME_HTTP 22
+#define NGX_HTTP_V3_HEADER_SCHEME_HTTPS 23
#define NGX_HTTP_V3_HEADER_STATUS_200 25
+#define NGX_HTTP_V3_HEADER_ACCEPT_ENCODING 31
#define NGX_HTTP_V3_HEADER_CONTENT_TYPE_TEXT_PLAIN 53
#define NGX_HTTP_V3_HEADER_VARY_ACCEPT_ENCODING 59
+#define NGX_HTTP_V3_HEADER_ACCEPT_LANGUAGE 72
#define NGX_HTTP_V3_HEADER_SERVER 92
+#define NGX_HTTP_V3_HEADER_USER_AGENT 95
static ngx_int_t ngx_http_v3_process_pseudo_header(ngx_http_request_t *r,
ngx_str_t *name, ngx_str_t *value);
+static ngx_int_t ngx_http_v3_push_resources(ngx_http_request_t *r,
+ ngx_chain_t ***out);
+static ngx_int_t ngx_http_v3_push_resource(ngx_http_request_t *r,
+ ngx_str_t *path, ngx_chain_t ***out);
+static ngx_int_t ngx_http_v3_create_push_request(
+ ngx_http_request_t *pr, ngx_str_t *path, uint64_t push_id);
+static ngx_int_t ngx_http_v3_set_push_header(ngx_http_request_t *r,
+ const char *name, ngx_str_t *value);
+static void ngx_http_v3_push_request_handler(ngx_event_t *ev);
+static ngx_chain_t *ngx_http_v3_create_push_promise(ngx_http_request_t *r,
+ ngx_str_t *path, uint64_t push_id);
struct {
@@ -431,7 +450,7 @@ ngx_http_v3_create_header(ngx_http_request_t *r)
ngx_buf_t *b;
ngx_str_t host;
ngx_uint_t i, port;
- ngx_chain_t *hl, *cl, *bl;
+ ngx_chain_t *out, *hl, *cl, **ll;
ngx_list_part_t *part;
ngx_table_elt_t *header;
ngx_connection_t *c;
@@ -443,6 +462,17 @@ ngx_http_v3_create_header(ngx_http_request_t *r)
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 create header");
+ out = NULL;
+ ll = &out;
+
+ if ((c->qs->id & NGX_QUIC_STREAM_UNIDIRECTIONAL) == 0
+ && r->method != NGX_HTTP_HEAD)
+ {
+ if (ngx_http_v3_push_resources(r, &ll) != NGX_OK) {
+ return NULL;
+ }
+ }
+
len = ngx_http_v3_encode_header_block_prefix(NULL, 0, 0, 0);
if (r->headers_out.status == NGX_HTTP_OK) {
@@ -796,6 +826,9 @@ ngx_http_v3_create_header(ngx_http_request_t *r)
hl->buf = b;
hl->next = cl;
+ *ll = hl;
+ ll = &cl->next;
+
if (r->headers_out.content_length_n >= 0 && !r->header_only) {
len = ngx_http_v3_encode_varlen_int(NULL, NGX_HTTP_V3_FRAME_DATA)
+ ngx_http_v3_encode_varlen_int(NULL,
@@ -811,17 +844,18 @@ ngx_http_v3_create_header(ngx_http_request_t *r)
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) {
+ cl = ngx_alloc_chain_link(c->pool);
+ if (cl == NULL) {
return NULL;
}
- bl->buf = b;
- bl->next = NULL;
- cl->next = bl;
+ cl->buf = b;
+ cl->next = NULL;
+
+ *ll = cl;
}
- return hl;
+ return out;
}
@@ -853,3 +887,659 @@ ngx_http_v3_create_trailers(ngx_http_request_t *r)
return cl;
}
+
+
+static ngx_int_t
+ngx_http_v3_push_resources(ngx_http_request_t *r, ngx_chain_t ***out)
+{
+ u_char *start, *end, *last;
+ ngx_str_t path;
+ ngx_int_t rc;
+ ngx_uint_t i, push;
+ ngx_table_elt_t **h;
+ ngx_http_v3_loc_conf_t *h3lcf;
+ ngx_http_complex_value_t *pushes;
+
+ h3lcf = ngx_http_get_module_loc_conf(r, ngx_http_v3_module);
+
+ if (h3lcf->pushes) {
+ pushes = h3lcf->pushes->elts;
+
+ for (i = 0; i < h3lcf->pushes->nelts; i++) {
+
+ if (ngx_http_complex_value(r, &pushes[i], &path) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ if (path.len == 0) {
+ continue;
+ }
+
+ if (path.len == 3 && ngx_strncmp(path.data, "off", 3) == 0) {
+ continue;
+ }
+
+ rc = ngx_http_v3_push_resource(r, &path, out);
+
+ if (rc == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ if (rc == NGX_ABORT) {
+ return NGX_OK;
+ }
+
+ /* NGX_OK, NGX_DECLINED */
+ }
+ }
+
+ if (!h3lcf->push_preload) {
+ return NGX_OK;
+ }
+
+ h = r->headers_out.link.elts;
+
+ for (i = 0; i < r->headers_out.link.nelts; i++) {
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "http3 parse link: \"%V\"", &h[i]->value);
+
+ start = h[i]->value.data;
+ end = h[i]->value.data + h[i]->value.len;
+
+ next_link:
+
+ while (start < end && *start == ' ') { start++; }
+
+ if (start == end || *start++ != '<') {
+ continue;
+ }
+
+ while (start < end && *start == ' ') { start++; }
+
+ for (last = start; last < end && *last != '>'; last++) {
+ /* void */
+ }
+
+ if (last == start || last == end) {
+ continue;
+ }
+
+ path.len = last - start;
+ path.data = start;
+
+ start = last + 1;
+
+ while (start < end && *start == ' ') { start++; }
+
+ if (start == end) {
+ continue;
+ }
+
+ if (*start == ',') {
+ start++;
+ goto next_link;
+ }
+
+ if (*start++ != ';') {
+ continue;
+ }
+
+ last = ngx_strlchr(start, end, ',');
+
+ if (last == NULL) {
+ last = end;
+ }
+
+ push = 0;
+
+ for ( ;; ) {
+
+ while (start < last && *start == ' ') { start++; }
+
+ if (last - start >= 6
+ && ngx_strncasecmp(start, (u_char *) "nopush", 6) == 0)
+ {
+ start += 6;
+
+ if (start == last || *start == ' ' || *start == ';') {
+ push = 0;
+ break;
+ }
+
+ goto next_param;
+ }
+
+ if (last - start >= 11
+ && ngx_strncasecmp(start, (u_char *) "rel=preload", 11) == 0)
+ {
+ start += 11;
+
+ if (start == last || *start == ' ' || *start == ';') {
+ push = 1;
+ }
+
+ goto next_param;
+ }
+
+ if (last - start >= 4
+ && ngx_strncasecmp(start, (u_char *) "rel=", 4) == 0)
+ {
+ start += 4;
+
+ while (start < last && *start == ' ') { start++; }
+
+ if (start == last || *start++ != '"') {
+ goto next_param;
+ }
+
+ for ( ;; ) {
+
+ while (start < last && *start == ' ') { start++; }
+
+ if (last - start >= 7
+ && ngx_strncasecmp(start, (u_char *) "preload", 7) == 0)
+ {
+ start += 7;
+
+ if (start < last && (*start == ' ' || *start == '"')) {
+ push = 1;
+ break;
+ }
+ }
+
+ while (start < last && *start != ' ' && *start != '"') {
+ start++;
+ }
+
+ if (start == last) {
+ break;
+ }
+
+ if (*start == '"') {
+ break;
+ }
+
+ start++;
+ }
+ }
+
+ next_param:
+
+ start = ngx_strlchr(start, last, ';');
+
+ if (start == NULL) {
+ break;
+ }
+
+ start++;
+ }
+
+ if (push) {
+ while (path.len && path.data[path.len - 1] == ' ') {
+ path.len--;
+ }
+ }
+
+ if (push && path.len
+ && !(path.len > 1 && path.data[0] == '/' && path.data[1] == '/'))
+ {
+ rc = ngx_http_v3_push_resource(r, &path, out);
+
+ if (rc == NGX_ERROR) {
+ return NGX_ERROR;
+ }
+
+ if (rc == NGX_ABORT) {
+ return NGX_OK;
+ }
+
+ /* NGX_OK, NGX_DECLINED */
+ }
+
+ if (last < end) {
+ start = last + 1;
+ goto next_link;
+ }
+ }
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_v3_push_resource(ngx_http_request_t *r, ngx_str_t *path,
+ ngx_chain_t ***ll)
+{
+ uint64_t push_id;
+ ngx_int_t rc;
+ ngx_chain_t *cl;
+ ngx_connection_t *c;
+ ngx_http_v3_srv_conf_t *h3scf;
+ ngx_http_v3_connection_t *h3c;
+
+ c = r->connection;
+ h3c = c->qs->parent->data;
+ h3scf = ngx_http_get_module_srv_conf(r, ngx_http_v3_module);
+
+ ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 push \"%V\" pushing:%ui/%ui id:%uL/%uL",
+ path, h3c->npushing, h3scf->max_concurrent_pushes,
+ h3c->next_push_id, h3c->max_push_id);
+
+ if (!ngx_path_separator(path->data[0])) {
+ ngx_log_error(NGX_LOG_WARN, c->log, 0,
+ "non-absolute path \"%V\" not pushed", path);
+ return NGX_DECLINED;
+ }
+
+ if (h3c->next_push_id > h3c->max_push_id) {
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 abort pushes due to max_push_id");
+ return NGX_ABORT;
+ }
+
+ if (h3c->npushing >= h3scf->max_concurrent_pushes) {
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 abort pushes due to max_concurrent_pushes");
+ return NGX_ABORT;
+ }
+
+ push_id = h3c->next_push_id++;
+
+ rc = ngx_http_v3_create_push_request(r, path, push_id);
+ if (rc != NGX_OK) {
+ return rc;
+ }
+
+ cl = ngx_http_v3_create_push_promise(r, path, push_id);
+ if (cl == NULL) {
+ return NGX_ERROR;
+ }
+
+ for (**ll = cl; **ll; *ll = &(**ll)->next);
+
+ return NGX_OK;
+}
+
+
+static ngx_int_t
+ngx_http_v3_create_push_request(ngx_http_request_t *pr, ngx_str_t *path,
+ uint64_t push_id)
+{
+ ngx_pool_t *pool;
+ ngx_connection_t *c, *pc;
+ ngx_http_request_t *r;
+ ngx_http_log_ctx_t *ctx;
+ ngx_http_connection_t *hc;
+ ngx_http_core_srv_conf_t *cscf;
+ ngx_http_v3_connection_t *h3c;
+
+ pc = pr->connection;
+
+ r = NULL;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
+ "http3 create push request id:%uL", push_id);
+
+ c = ngx_http_v3_create_push_stream(pc, push_id);
+ if (c == NULL) {
+ return NGX_ABORT;
+ }
+
+ hc = ngx_palloc(c->pool, sizeof(ngx_http_connection_t));
+ if (hc == NULL) {
+ goto failed;
+ }
+
+ h3c = c->qs->parent->data;
+ 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) {
+ goto failed;
+ }
+
+ ctx->connection = c;
+ ctx->request = NULL;
+ ctx->current_request = NULL;
+
+ c->log->handler = ngx_http_log_error;
+ c->log->data = ctx;
+ c->log->action = "processing pushed request headers";
+
+ c->log_error = NGX_ERROR_INFO;
+
+ r = ngx_http_create_request(c);
+ if (r == NULL) {
+ goto failed;
+ }
+
+ c->data = r;
+
+ ngx_str_set(&r->http_protocol, "HTTP/3.0");
+
+ r->method_name = ngx_http_core_get_method;
+ r->method = NGX_HTTP_GET;
+
+ cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
+
+ r->header_in = ngx_create_temp_buf(r->pool,
+ cscf->client_header_buffer_size);
+ if (r->header_in == NULL) {
+ goto failed;
+ }
+
+ if (ngx_list_init(&r->headers_in.headers, r->pool, 4,
+ sizeof(ngx_table_elt_t))
+ != NGX_OK)
+ {
+ goto failed;
+ }
+
+ r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
+
+ r->schema.data = ngx_pstrdup(r->pool, &pr->schema);
+ if (r->schema.data == NULL) {
+ goto failed;
+ }
+
+ r->schema.len = pr->schema.len;
+
+ r->uri_start = ngx_pstrdup(r->pool, path);
+ if (r->uri_start == NULL) {
+ goto failed;
+ }
+
+ r->uri_end = r->uri_start + path->len;
+
+ if (ngx_http_parse_uri(r) != NGX_OK) {
+ goto failed;
+ }
+
+ if (ngx_http_process_request_uri(r) != NGX_OK) {
+ goto failed;
+ }
+
+ if (ngx_http_v3_set_push_header(r, "host", &pr->headers_in.server)
+ != NGX_OK)
+ {
+ goto failed;
+ }
+
+ if (pr->headers_in.accept_encoding) {
+ if (ngx_http_v3_set_push_header(r, "accept-encoding",
+ &pr->headers_in.accept_encoding->value)
+ != NGX_OK)
+ {
+ goto failed;
+ }
+ }
+
+ if (pr->headers_in.accept_language) {
+ if (ngx_http_v3_set_push_header(r, "accept-language",
+ &pr->headers_in.accept_language->value)
+ != NGX_OK)
+ {
+ goto failed;
+ }
+ }
+
+ if (pr->headers_in.user_agent) {
+ if (ngx_http_v3_set_push_header(r, "user-agent",
+ &pr->headers_in.user_agent->value)
+ != NGX_OK)
+ {
+ goto failed;
+ }
+ }
+
+ c->read->handler = ngx_http_v3_push_request_handler;
+ c->read->handler = ngx_http_v3_push_request_handler;
+
+ ngx_post_event(c->read, &ngx_posted_events);
+
+ return NGX_OK;
+
+failed:
+
+ if (r) {
+ ngx_http_free_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ }
+
+ c->destroyed = 1;
+
+ pool = c->pool;
+
+ ngx_close_connection(c);
+
+ ngx_destroy_pool(pool);
+
+ return NGX_ERROR;
+}
+
+
+static ngx_int_t
+ngx_http_v3_set_push_header(ngx_http_request_t *r, const char *name,
+ ngx_str_t *value)
+{
+ u_char *p;
+ ngx_table_elt_t *h;
+ ngx_http_header_t *hh;
+ ngx_http_core_main_conf_t *cmcf;
+
+ ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "http3 push header \"%s\": \"%V\"", name, value);
+
+ cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
+
+ p = ngx_pnalloc(r->pool, value->len + 1);
+ if (p == NULL) {
+ return NGX_ERROR;
+ }
+
+ ngx_memcpy(p, value->data, value->len);
+ p[value->len] = '\0';
+
+ h = ngx_list_push(&r->headers_in.headers);
+ if (h == NULL) {
+ return NGX_ERROR;
+ }
+
+ h->key.data = (u_char *) name;
+ h->key.len = ngx_strlen(name);
+ h->hash = ngx_hash_key(h->key.data, h->key.len);
+ h->lowcase_key = (u_char *) name;
+ h->value.data = p;
+ h->value.len = value->len;
+
+ hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
+ h->lowcase_key, h->key.len);
+
+ if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ return NGX_OK;
+}
+
+
+static void
+ngx_http_v3_push_request_handler(ngx_event_t *ev)
+{
+ ngx_connection_t *c;
+ ngx_http_request_t *r;
+
+ c = ev->data;
+ r = c->data;
+
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http3 push request handler");
+
+ ngx_http_process_request(r);
+}
+
+
+static ngx_chain_t *
+ngx_http_v3_create_push_promise(ngx_http_request_t *r, ngx_str_t *path,
+ uint64_t push_id)
+{
+ size_t n, len;
+ ngx_buf_t *b;
+ ngx_chain_t *hl, *cl;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "http3 create push promise id:%uL", push_id);
+
+ len = ngx_http_v3_encode_varlen_int(NULL, push_id);
+
+ len += ngx_http_v3_encode_header_block_prefix(NULL, 0, 0, 0);
+
+ len += ngx_http_v3_encode_header_ri(NULL, 0,
+ NGX_HTTP_V3_HEADER_METHOD_GET);
+
+ len += ngx_http_v3_encode_header_lri(NULL, 0,
+ NGX_HTTP_V3_HEADER_AUTHORITY,
+ NULL, r->headers_in.server.len);
+
+ if (path->len == 1 && path->data[0] == '/') {
+ len += ngx_http_v3_encode_header_ri(NULL, 0,
+ NGX_HTTP_V3_HEADER_PATH_ROOT);
+
+ } else {
+ len += ngx_http_v3_encode_header_lri(NULL, 0,
+ NGX_HTTP_V3_HEADER_PATH_ROOT,
+ NULL, path->len);
+ }
+
+ if (r->schema.len == 5 && ngx_strncmp(r->schema.data, "https", 5) == 0) {
+ len += ngx_http_v3_encode_header_ri(NULL, 0,
+ NGX_HTTP_V3_HEADER_SCHEME_HTTPS);
+
+ } else if (r->schema.len == 4
+ && ngx_strncmp(r->schema.data, "http", 4) == 0)
+ {
+ len += ngx_http_v3_encode_header_ri(NULL, 0,
+ NGX_HTTP_V3_HEADER_SCHEME_HTTP);
+
+ } else {
+ len += ngx_http_v3_encode_header_lri(NULL, 0,
+ NGX_HTTP_V3_HEADER_SCHEME_HTTP,
+ NULL, r->schema.len);
+ }
+
+ if (r->headers_in.accept_encoding) {
+ len += ngx_http_v3_encode_header_lri(NULL, 0,
+ NGX_HTTP_V3_HEADER_ACCEPT_ENCODING, NULL,
+ r->headers_in.accept_encoding->value.len);
+ }
+
+ if (r->headers_in.accept_language) {
+ len += ngx_http_v3_encode_header_lri(NULL, 0,
+ NGX_HTTP_V3_HEADER_ACCEPT_LANGUAGE, NULL,
+ r->headers_in.accept_language->value.len);
+ }
+
+ if (r->headers_in.user_agent) {
+ len += ngx_http_v3_encode_header_lri(NULL, 0,
+ NGX_HTTP_V3_HEADER_USER_AGENT, NULL,
+ r->headers_in.user_agent->value.len);
+ }
+
+ b = ngx_create_temp_buf(r->pool, len);
+ if (b == NULL) {
+ return NULL;
+ }
+
+ b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last, push_id);
+
+ b->last = (u_char *) ngx_http_v3_encode_header_block_prefix(b->last,
+ 0, 0, 0);
+
+ b->last = (u_char *) ngx_http_v3_encode_header_ri(b->last, 0,
+ NGX_HTTP_V3_HEADER_METHOD_GET);
+
+ b->last = (u_char *) ngx_http_v3_encode_header_lri(b->last, 0,
+ NGX_HTTP_V3_HEADER_AUTHORITY,
+ r->headers_in.server.data,
+ r->headers_in.server.len);
+
+ if (path->len == 1 && path->data[0] == '/') {
+ b->last = (u_char *) ngx_http_v3_encode_header_ri(b->last, 0,
+ NGX_HTTP_V3_HEADER_PATH_ROOT);
+
+ } else {
+ b->last = (u_char *) ngx_http_v3_encode_header_lri(b->last, 0,
+ NGX_HTTP_V3_HEADER_PATH_ROOT,
+ path->data, path->len);
+ }
+
+ if (r->schema.len == 5 && ngx_strncmp(r->schema.data, "https", 5) == 0) {
+ b->last = (u_char *) ngx_http_v3_encode_header_ri(b->last, 0,
+ NGX_HTTP_V3_HEADER_SCHEME_HTTPS);
+
+ } else if (r->schema.len == 4
+ && ngx_strncmp(r->schema.data, "http", 4) == 0)
+ {
+ b->last = (u_char *) ngx_http_v3_encode_header_ri(b->last, 0,
+ NGX_HTTP_V3_HEADER_SCHEME_HTTP);
+
+ } else {
+ b->last = (u_char *) ngx_http_v3_encode_header_lri(b->last, 0,
+ NGX_HTTP_V3_HEADER_SCHEME_HTTP,
+ r->schema.data, r->schema.len);
+ }
+
+ if (r->headers_in.accept_encoding) {
+ b->last = (u_char *) ngx_http_v3_encode_header_lri(b->last, 0,
+ NGX_HTTP_V3_HEADER_ACCEPT_ENCODING,
+ r->headers_in.accept_encoding->value.data,
+ r->headers_in.accept_encoding->value.len);
+ }
+
+ if (r->headers_in.accept_language) {
+ b->last = (u_char *) ngx_http_v3_encode_header_lri(b->last, 0,
+ NGX_HTTP_V3_HEADER_ACCEPT_LANGUAGE,
+ r->headers_in.accept_language->value.data,
+ r->headers_in.accept_language->value.len);
+ }
+
+ if (r->headers_in.user_agent) {
+ b->last = (u_char *) ngx_http_v3_encode_header_lri(b->last, 0,
+ NGX_HTTP_V3_HEADER_USER_AGENT,
+ r->headers_in.user_agent->value.data,
+ r->headers_in.user_agent->value.len);
+ }
+
+ cl = ngx_alloc_chain_link(r->pool);
+ if (cl == NULL) {
+ return NULL;
+ }
+
+ cl->buf = b;
+ cl->next = NULL;
+
+ n = b->last - b->pos;
+
+ len = ngx_http_v3_encode_varlen_int(NULL, NGX_HTTP_V3_FRAME_PUSH_PROMISE)
+ + ngx_http_v3_encode_varlen_int(NULL, n);
+
+ b = ngx_create_temp_buf(r->pool, len);
+ if (b == NULL) {
+ return NULL;
+ }
+
+ b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last,
+ NGX_HTTP_V3_FRAME_PUSH_PROMISE);
+ b->last = (u_char *) ngx_http_v3_encode_varlen_int(b->last, n);
+
+ hl = ngx_alloc_chain_link(r->pool);
+ if (hl == NULL) {
+ return NULL;
+ }
+
+ hl->buf = b;
+ hl->next = cl;
+
+ return hl;
+}
diff --git a/src/http/v3/ngx_http_v3_streams.c b/src/http/v3/ngx_http_v3_streams.c
index 2ac3f7d74..8d5147f4d 100644
--- a/src/http/v3/ngx_http_v3_streams.c
+++ b/src/http/v3/ngx_http_v3_streams.c
@@ -21,10 +21,19 @@ typedef struct {
} ngx_http_v3_uni_stream_t;
+typedef struct {
+ ngx_queue_t queue;
+ uint64_t id;
+ ngx_connection_t *connection;
+ ngx_uint_t *npushing;
+} ngx_http_v3_push_t;
+
+
static void ngx_http_v3_close_uni_stream(ngx_connection_t *c);
static void ngx_http_v3_read_uni_stream_type(ngx_event_t *rev);
static void ngx_http_v3_uni_read_handler(ngx_event_t *rev);
static void ngx_http_v3_dummy_write_handler(ngx_event_t *wev);
+static void ngx_http_v3_push_cleanup(void *data);
static ngx_connection_t *ngx_http_v3_get_uni_stream(ngx_connection_t *c,
ngx_uint_t type);
static ngx_int_t ngx_http_v3_send_settings(ngx_connection_t *c);
@@ -50,6 +59,7 @@ ngx_http_v3_init_connection(ngx_connection_t *c)
h3c->hc = *hc;
ngx_queue_init(&h3c->blocked);
+ ngx_queue_init(&h3c->pushing);
c->data = h3c;
return NGX_OK;
@@ -321,6 +331,70 @@ ngx_http_v3_dummy_write_handler(ngx_event_t *wev)
/* XXX async & buffered stream writes */
+ngx_connection_t *
+ngx_http_v3_create_push_stream(ngx_connection_t *c, uint64_t push_id)
+{
+ u_char *p, buf[NGX_HTTP_V3_VARLEN_INT_LEN * 2];
+ size_t n;
+ ngx_connection_t *sc;
+ ngx_pool_cleanup_t *cln;
+ ngx_http_v3_push_t *push;
+ ngx_http_v3_connection_t *h3c;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 create push stream id:%uL", push_id);
+
+ sc = ngx_quic_open_stream(c, 0);
+ if (sc == NULL) {
+ return NULL;
+ }
+
+ p = buf;
+ p = (u_char *) ngx_http_v3_encode_varlen_int(p, NGX_HTTP_V3_STREAM_PUSH);
+ p = (u_char *) ngx_http_v3_encode_varlen_int(p, push_id);
+ n = p - buf;
+
+ if (sc->send(sc, buf, n) != (ssize_t) n) {
+ goto failed;
+ }
+
+ cln = ngx_pool_cleanup_add(sc->pool, sizeof(ngx_http_v3_push_t));
+ if (cln == NULL) {
+ goto failed;
+ }
+
+ h3c = c->qs->parent->data;
+ h3c->npushing++;
+
+ cln->handler = ngx_http_v3_push_cleanup;
+
+ push = cln->data;
+ push->id = push_id;
+ push->connection = sc;
+ push->npushing = &h3c->npushing;
+
+ ngx_queue_insert_tail(&h3c->pushing, &push->queue);
+
+ return sc;
+
+failed:
+
+ ngx_http_v3_close_uni_stream(sc);
+
+ return NULL;
+}
+
+
+static void
+ngx_http_v3_push_cleanup(void *data)
+{
+ ngx_http_v3_push_t *push = data;
+
+ ngx_queue_remove(&push->queue);
+ (*push->npushing)--;
+}
+
+
static ngx_connection_t *
ngx_http_v3_get_uni_stream(ngx_connection_t *c, ngx_uint_t type)
{
@@ -682,3 +756,64 @@ ngx_http_v3_client_inc_insert_count(ngx_connection_t *c, ngx_uint_t inc)
return NGX_OK;
}
+
+
+ngx_int_t
+ngx_http_v3_set_max_push_id(ngx_connection_t *c, uint64_t max_push_id)
+{
+ ngx_http_v3_connection_t *h3c;
+
+ h3c = c->qs->parent->data;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 MAX_PUSH_ID:%uL", max_push_id);
+
+ if (max_push_id < h3c->max_push_id) {
+ return NGX_HTTP_V3_ERR_ID_ERROR;
+ }
+
+ h3c->max_push_id = max_push_id;
+
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_http_v3_cancel_push(ngx_connection_t *c, uint64_t push_id)
+{
+ ngx_queue_t *q;
+ ngx_http_request_t *r;
+ ngx_http_v3_push_t *push;
+ ngx_http_v3_connection_t *h3c;
+
+ h3c = c->qs->parent->data;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+ "http3 CANCEL_PUSH:%uL", push_id);
+
+ if (push_id >= h3c->next_push_id) {
+ return NGX_HTTP_V3_ERR_ID_ERROR;
+ }
+
+ for (q = ngx_queue_head(&h3c->pushing);
+ q != ngx_queue_sentinel(&h3c->pushing);
+ q = ngx_queue_next(&h3c->pushing))
+ {
+ push = (ngx_http_v3_push_t *) q;
+
+ if (push->id != push_id) {
+ continue;
+ }
+
+ r = push->connection->data;
+
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "http3 cancel push");
+
+ ngx_http_finalize_request(r, NGX_HTTP_CLOSE);
+
+ break;
+ }
+
+ return NGX_OK;
+}