diff src/http/v2/ngx_http_v2_filter_module.c @ 7201:641306096f5b

HTTP/2: server push. Resources to be pushed are configured with the "http2_push" directive. Also, preload links from the Link response headers, as described in https://www.w3.org/TR/preload/#server-push-http-2, can be pushed, if enabled with the "http2_push_preload" directive. Only relative URIs with absolute paths can be pushed. The number of concurrent pushes is normally limited by a client, but cannot exceed a hard limit set by the "http2_max_concurrent_pushes" directive.
author Ruslan Ermilov <ru@nginx.com>
date Thu, 08 Feb 2018 09:55:03 +0300
parents 61d276dcd493
children a49af443656f
line wrap: on
line diff
--- a/src/http/v2/ngx_http_v2_filter_module.c
+++ b/src/http/v2/ngx_http_v2_filter_module.c
@@ -2,6 +2,7 @@
 /*
  * Copyright (C) Nginx, Inc.
  * Copyright (C) Valentin V. Bartenev
+ * Copyright (C) Ruslan Ermilov
  */
 
 
@@ -33,6 +34,13 @@
 #define NGX_HTTP_V2_ENCODE_RAW            0
 #define NGX_HTTP_V2_ENCODE_HUFF           0x80
 
+#define NGX_HTTP_V2_AUTHORITY_INDEX       1
+#define NGX_HTTP_V2_METHOD_GET_INDEX      2
+#define NGX_HTTP_V2_PATH_INDEX            4
+
+#define NGX_HTTP_V2_SCHEME_HTTP_INDEX     6
+#define NGX_HTTP_V2_SCHEME_HTTPS_INDEX    7
+
 #define NGX_HTTP_V2_STATUS_INDEX          8
 #define NGX_HTTP_V2_STATUS_200_INDEX      8
 #define NGX_HTTP_V2_STATUS_204_INDEX      9
@@ -53,12 +61,18 @@
 #define NGX_HTTP_V2_NO_TRAILERS           (ngx_http_v2_out_frame_t *) -1
 
 
+static ngx_int_t ngx_http_v2_push_resources(ngx_http_request_t *r);
+static ngx_int_t ngx_http_v2_push_resource(ngx_http_request_t *r,
+    ngx_str_t *path, ngx_str_t *authority);
+
 static u_char *ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len,
     u_char *tmp, ngx_uint_t lower);
 static u_char *ngx_http_v2_write_int(u_char *pos, ngx_uint_t prefix,
     ngx_uint_t value);
 static ngx_http_v2_out_frame_t *ngx_http_v2_create_headers_frame(
     ngx_http_request_t *r, u_char *pos, u_char *end, ngx_uint_t fin);
+static ngx_http_v2_out_frame_t *ngx_http_v2_create_push_frame(
+    ngx_http_request_t *r, u_char *pos, u_char *end);
 static ngx_http_v2_out_frame_t *ngx_http_v2_create_trailers_frame(
     ngx_http_request_t *r);
 
@@ -81,6 +95,8 @@ static ngx_inline ngx_int_t ngx_http_v2_
 
 static ngx_int_t ngx_http_v2_headers_frame_handler(
     ngx_http_v2_connection_t *h2c, ngx_http_v2_out_frame_t *frame);
+static ngx_int_t ngx_http_v2_push_frame_handler(
+    ngx_http_v2_connection_t *h2c, ngx_http_v2_out_frame_t *frame);
 static ngx_int_t ngx_http_v2_data_frame_handler(
     ngx_http_v2_connection_t *h2c, ngx_http_v2_out_frame_t *frame);
 static ngx_inline void ngx_http_v2_handle_frame(
@@ -241,6 +257,15 @@ ngx_http_v2_header_filter(ngx_http_reque
 
     h2c = stream->connection;
 
+    if (!h2c->push_disabled && !h2c->goaway
+        && stream->node->id % 2 == 1
+        && r->method != NGX_HTTP_HEAD)
+    {
+        if (ngx_http_v2_push_resources(r) != NGX_OK) {
+            return NGX_ERROR;
+        }
+    }
+
     len = h2c->table_update ? 1 : 0;
 
     len += status ? 1 : 1 + ngx_http_v2_literal_size("418");
@@ -638,7 +663,7 @@ ngx_http_v2_header_filter(ngx_http_reque
 
     ngx_http_v2_queue_blocked_frame(h2c, frame);
 
-    stream->queued = 1;
+    stream->queued++;
 
     cln = ngx_http_cleanup_add(r, 0);
     if (cln == NULL) {
@@ -655,6 +680,365 @@ ngx_http_v2_header_filter(ngx_http_reque
 }
 
 
+static ngx_int_t
+ngx_http_v2_push_resources(ngx_http_request_t *r)
+{
+    u_char                     *start, *end, *last;
+    ngx_int_t                   rc;
+    ngx_str_t                   path, authority;
+    ngx_uint_t                  i, push;
+    ngx_table_elt_t           **h;
+    ngx_connection_t           *fc;
+    ngx_http_v2_stream_t       *stream;
+    ngx_http_v2_loc_conf_t     *h2lcf;
+    ngx_http_v2_connection_t   *h2c;
+    ngx_http_complex_value_t   *pushes;
+
+    fc = r->connection;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0, "http2 push resources");
+
+    stream = r->stream;
+    h2c = stream->connection;
+
+    ngx_str_null(&authority);
+
+    h2lcf = ngx_http_get_module_loc_conf(r, ngx_http_v2_module);
+
+    if (h2lcf->pushes) {
+        pushes = h2lcf->pushes->elts;
+
+        for (i = 0; i < h2lcf->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_v2_push_resource(r, &path, &authority);
+
+            if (rc == NGX_ERROR) {
+                return NGX_ERROR;
+            }
+
+            if (rc == NGX_ABORT) {
+                return NGX_OK;
+            }
+
+            /* NGX_OK, NGX_DECLINED */
+        }
+    }
+
+    if (!h2lcf->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, fc->log, 0,
+                       "http2 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_v2_push_resource(r, &path, &authority);
+
+            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_v2_push_resource(ngx_http_request_t *r, ngx_str_t *path,
+    ngx_str_t *authority)
+{
+    u_char                    *start, *pos, *tmp;
+    size_t                     len;
+    ngx_table_elt_t           *host;
+    ngx_connection_t          *fc;
+    ngx_http_v2_stream_t      *stream;
+    ngx_http_v2_out_frame_t   *frame;
+    ngx_http_v2_connection_t  *h2c;
+
+    fc = r->connection;
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0, "http2 push resource");
+
+    stream = r->stream;
+    h2c = stream->connection;
+
+    if (!ngx_path_separator(path->data[0])) {
+        ngx_log_error(NGX_LOG_WARN, fc->log, 0,
+                      "non-absolute path \"%V\" not pushed", path);
+        return NGX_DECLINED;
+    }
+
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
+                  "http2 pushing:%ui limit:%ui",
+                  h2c->pushing, h2c->concurrent_pushes);
+
+    if (h2c->pushing >= h2c->concurrent_pushes) {
+        return NGX_ABORT;
+    }
+
+    if (h2c->last_push == 0x7ffffffe) {
+        return NGX_ABORT;
+    }
+
+    if (path->len > NGX_HTTP_V2_MAX_FIELD) {
+        return NGX_DECLINED;
+    }
+
+    host = r->headers_in.host;
+
+    if (authority->len == 0 && host) {
+
+        len = 1 + NGX_HTTP_V2_INT_OCTETS + host->value.len;
+
+        tmp = ngx_palloc(r->pool, len);
+        pos = ngx_pnalloc(r->pool, len);
+
+        if (pos == NULL || tmp == NULL) {
+            return NGX_ERROR;
+        }
+
+        authority->data = pos;
+
+        *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_AUTHORITY_INDEX);
+        pos = ngx_http_v2_write_value(pos, host->value.data, host->value.len,
+                                      tmp);
+
+        authority->len = pos - authority->data;
+    }
+
+    len = (h2c->table_update ? 1 : 0)
+          + 1
+          + 1 + NGX_HTTP_V2_INT_OCTETS + path->len
+          + authority->len
+          + 1;
+
+    tmp = ngx_palloc(r->pool, len);
+    pos = ngx_pnalloc(r->pool, len);
+
+    if (pos == NULL || tmp == NULL) {
+        return NGX_ERROR;
+    }
+
+    start = pos;
+
+    if (h2c->table_update) {
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                       "http2 table size update: 0");
+        *pos++ = (1 << 5) | 0;
+        h2c->table_update = 0;
+    }
+
+    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                   "http2 push header: \":method: GET\"");
+
+    *pos++ = ngx_http_v2_indexed(NGX_HTTP_V2_METHOD_GET_INDEX);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                   "http2 push header: \":path: %V\"", path);
+
+    *pos++ = ngx_http_v2_inc_indexed(NGX_HTTP_V2_PATH_INDEX);
+    pos = ngx_http_v2_write_value(pos, path->data, path->len, tmp);
+
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                   "http2 push header: \":authority: %V\"", &host->value);
+
+    pos = ngx_cpymem(pos, authority->data, authority->len);
+
+#if (NGX_HTTP_SSL)
+    if (fc->ssl) {
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                       "http2 push header: \":scheme: https\"");
+        *pos++ = ngx_http_v2_indexed(NGX_HTTP_V2_SCHEME_HTTPS_INDEX);
+
+    } else
+#endif
+    {
+        ngx_log_debug0(NGX_LOG_DEBUG_HTTP, fc->log, 0,
+                       "http2 push header: \":scheme: http\"");
+        *pos++ = ngx_http_v2_indexed(NGX_HTTP_V2_SCHEME_HTTP_INDEX);
+    }
+
+    frame = ngx_http_v2_create_push_frame(r, start, pos);
+    if (frame == NULL) {
+        return NGX_ERROR;
+    }
+
+    ngx_http_v2_queue_blocked_frame(h2c, frame);
+
+    stream->queued++;
+
+    return ngx_http_v2_push_stream(h2c, stream->node->id, pos - start,
+                                   path, &host->value);
+}
+
+
 static u_char *
 ngx_http_v2_string_encode(u_char *dst, u_char *src, size_t len, u_char *tmp,
     ngx_uint_t lower)
@@ -809,6 +1193,125 @@ ngx_http_v2_create_headers_frame(ngx_htt
 
 
 static ngx_http_v2_out_frame_t *
+ngx_http_v2_create_push_frame(ngx_http_request_t *r, u_char *pos, u_char *end)
+{
+    u_char                     type, flags;
+    size_t                     rest, frame_size, len;
+    ngx_buf_t                 *b;
+    ngx_chain_t               *cl, **ll;
+    ngx_http_v2_stream_t      *stream;
+    ngx_http_v2_out_frame_t   *frame;
+    ngx_http_v2_connection_t  *h2c;
+
+    stream = r->stream;
+    h2c = stream->connection;
+    rest = NGX_HTTP_V2_STREAM_ID_SIZE + (end - pos);
+
+    frame = ngx_palloc(r->pool, sizeof(ngx_http_v2_out_frame_t));
+    if (frame == NULL) {
+        return NULL;
+    }
+
+    frame->handler = ngx_http_v2_push_frame_handler;
+    frame->stream = stream;
+    frame->length = rest;
+    frame->blocked = 1;
+    frame->fin = 0;
+
+    ll = &frame->first;
+
+    type = NGX_HTTP_V2_PUSH_PROMISE_FRAME;
+    flags = NGX_HTTP_V2_NO_FLAG;
+    frame_size = h2c->frame_size;
+
+    for ( ;; ) {
+        if (rest <= frame_size) {
+            frame_size = rest;
+            flags |= NGX_HTTP_V2_END_HEADERS_FLAG;
+        }
+
+        b = ngx_create_temp_buf(r->pool,
+                                NGX_HTTP_V2_FRAME_HEADER_SIZE
+                                + ((type == NGX_HTTP_V2_PUSH_PROMISE_FRAME)
+                                   ? NGX_HTTP_V2_STREAM_ID_SIZE : 0));
+        if (b == NULL) {
+            return NULL;
+        }
+
+        b->last = ngx_http_v2_write_len_and_type(b->last, frame_size, type);
+        *b->last++ = flags;
+        b->last = ngx_http_v2_write_sid(b->last, stream->node->id);
+
+        b->tag = (ngx_buf_tag_t) &ngx_http_v2_module;
+
+        if (type == NGX_HTTP_V2_PUSH_PROMISE_FRAME) {
+            h2c->last_push += 2;
+
+            b->last = ngx_http_v2_write_sid(b->last, h2c->last_push);
+            len = frame_size - NGX_HTTP_V2_STREAM_ID_SIZE;
+
+        } else {
+            len = frame_size;
+        }
+
+        cl = ngx_alloc_chain_link(r->pool);
+        if (cl == NULL) {
+            return NULL;
+        }
+
+        cl->buf = b;
+
+        *ll = cl;
+        ll = &cl->next;
+
+        b = ngx_calloc_buf(r->pool);
+        if (b == NULL) {
+            return NULL;
+        }
+
+        b->pos = pos;
+
+        pos += len;
+
+        b->last = pos;
+        b->start = b->pos;
+        b->end = b->last;
+        b->temporary = 1;
+
+        cl = ngx_alloc_chain_link(r->pool);
+        if (cl == NULL) {
+            return NULL;
+        }
+
+        cl->buf = b;
+
+        *ll = cl;
+        ll = &cl->next;
+
+        rest -= frame_size;
+
+        if (rest) {
+            frame->length += NGX_HTTP_V2_FRAME_HEADER_SIZE;
+
+            type = NGX_HTTP_V2_CONTINUATION_FRAME;
+            continue;
+        }
+
+        cl->next = NULL;
+        frame->last = cl;
+
+        ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+                       "http2:%ui create PUSH_PROMISE frame %p: "
+                       "sid:%ui len:%uz",
+                       stream->node->id, frame, h2c->last_push,
+                       frame->length);
+
+        return frame;
+    }
+}
+
+
+static ngx_http_v2_out_frame_t *
 ngx_http_v2_create_trailers_frame(ngx_http_request_t *r)
 {
     u_char            *pos, *start, *tmp;
@@ -1377,6 +1880,60 @@ ngx_http_v2_headers_frame_handler(ngx_ht
 
 
 static ngx_int_t
+ngx_http_v2_push_frame_handler(ngx_http_v2_connection_t *h2c,
+    ngx_http_v2_out_frame_t *frame)
+{
+    ngx_chain_t           *cl, *ln;
+    ngx_http_v2_stream_t  *stream;
+
+    stream = frame->stream;
+    cl = frame->first;
+
+    for ( ;; ) {
+        if (cl->buf->pos != cl->buf->last) {
+            frame->first = cl;
+
+            ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
+                           "http2:%ui PUSH_PROMISE frame %p was sent partially",
+                           stream->node->id, frame);
+
+            return NGX_AGAIN;
+        }
+
+        ln = cl->next;
+
+        if (cl->buf->tag == (ngx_buf_tag_t) &ngx_http_v2_module) {
+            cl->next = stream->free_frame_headers;
+            stream->free_frame_headers = cl;
+
+        } else {
+            cl->next = stream->free_bufs;
+            stream->free_bufs = cl;
+        }
+
+        if (cl == frame->last) {
+            break;
+        }
+
+        cl = ln;
+    }
+
+    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, h2c->connection->log, 0,
+                   "http2:%ui PUSH_PROMISE frame %p was sent",
+                   stream->node->id, frame);
+
+    stream->request->header_size += NGX_HTTP_V2_FRAME_HEADER_SIZE
+                                    + frame->length;
+
+    ngx_http_v2_handle_frame(stream, frame);
+
+    ngx_http_v2_handle_stream(h2c, stream);
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_v2_data_frame_handler(ngx_http_v2_connection_t *h2c,
     ngx_http_v2_out_frame_t *frame)
 {