[nginx] HTTP/2: support for unbuffered upload of request body.

Valentin Bartenev vbart at nginx.com
Fri Apr 1 12:57:31 UTC 2016


details:   http://hg.nginx.org/nginx/rev/9d66d7ed2abb
branches:  
changeset: 6497:9d66d7ed2abb
user:      Valentin Bartenev <vbart at nginx.com>
date:      Fri Apr 01 15:57:10 2016 +0300
description:
HTTP/2: support for unbuffered upload of request body.

diffstat:

 src/http/ngx_http_request_body.c |   13 +++-
 src/http/v2/ngx_http_v2.c        |  129 ++++++++++++++++++++++++++++++++++++++-
 src/http/v2/ngx_http_v2.h        |    1 +
 3 files changed, 139 insertions(+), 4 deletions(-)

diffs (232 lines):

diff -r 887cca40ba6a -r 9d66d7ed2abb src/http/ngx_http_request_body.c
--- a/src/http/ngx_http_request_body.c	Fri Apr 01 15:56:03 2016 +0300
+++ b/src/http/ngx_http_request_body.c	Fri Apr 01 15:57:10 2016 +0300
@@ -48,7 +48,6 @@ ngx_http_read_client_request_body(ngx_ht
 
 #if (NGX_HTTP_V2)
     if (r->stream) {
-        r->request_body_no_buffering = 0;
         rc = ngx_http_v2_read_request_body(r, post_handler);
         goto done;
     }
@@ -215,6 +214,18 @@ ngx_http_read_unbuffered_request_body(ng
 {
     ngx_int_t  rc;
 
+#if (NGX_HTTP_V2)
+    if (r->stream) {
+        rc = ngx_http_v2_read_unbuffered_request_body(r);
+
+        if (rc == NGX_OK) {
+            r->reading_body = 0;
+        }
+
+        return rc;
+    }
+#endif
+
     if (r->connection->read->timedout) {
         r->connection->timedout = 1;
         return NGX_HTTP_REQUEST_TIME_OUT;
diff -r 887cca40ba6a -r 9d66d7ed2abb src/http/v2/ngx_http_v2.c
--- a/src/http/v2/ngx_http_v2.c	Fri Apr 01 15:56:03 2016 +0300
+++ b/src/http/v2/ngx_http_v2.c	Fri Apr 01 15:57:10 2016 +0300
@@ -3395,6 +3395,8 @@ ngx_http_v2_run_request(ngx_http_request
         return;
     }
 
+    r->headers_in.chunked = (r->headers_in.content_length_n == -1);
+
     ngx_http_process_request(r);
 }
 
@@ -3440,6 +3442,18 @@ ngx_http_v2_read_request_body(ngx_http_r
 
     len = r->headers_in.content_length_n;
 
+    if (r->request_body_no_buffering && !stream->in_closed) {
+        r->request_body_in_file_only = 0;
+
+        if (len < 0 || len > (off_t) clcf->client_body_buffer_size) {
+            len = clcf->client_body_buffer_size;
+        }
+
+        if (len > NGX_HTTP_V2_MAX_WINDOW) {
+            len = NGX_HTTP_V2_MAX_WINDOW;
+        }
+    }
+
     if (len >= 0 && len <= (off_t) clcf->client_body_buffer_size
         && !r->request_body_in_file_only)
     {
@@ -3458,12 +3472,18 @@ ngx_http_v2_read_request_body(ngx_http_r
     }
 
     if (stream->in_closed) {
+        r->request_body_no_buffering = 0;
         return ngx_http_v2_process_request_body(r, NULL, 0, 1);
     }
 
-    stream->no_flow_control = 1;
-
-    stream->recv_window = NGX_HTTP_V2_MAX_WINDOW;
+    if (r->request_body_no_buffering) {
+        stream->no_flow_control = 0;
+        stream->recv_window = (size_t) len;
+
+    } else {
+        stream->no_flow_control = 1;
+        stream->recv_window = NGX_HTTP_V2_MAX_WINDOW;
+    }
 
     if (ngx_http_v2_send_window_update(stream->connection, stream->node->id,
                                        stream->recv_window)
@@ -3525,6 +3545,11 @@ ngx_http_v2_process_request_body(ngx_htt
             ngx_del_timer(fc->read);
         }
 
+        if (r->request_body_no_buffering) {
+            ngx_post_event(fc->read, &ngx_posted_events);
+            return NGX_OK;
+        }
+
         rc = ngx_http_v2_filter_request_body(r);
 
         if (rc != NGX_OK) {
@@ -3553,6 +3578,11 @@ ngx_http_v2_process_request_body(ngx_htt
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
     ngx_add_timer(fc->read, clcf->client_body_timeout);
 
+    if (r->request_body_no_buffering) {
+        ngx_post_event(fc->read, &ngx_posted_events);
+        return NGX_OK;
+    }
+
     if (buf->sync) {
         return ngx_http_v2_filter_request_body(r);
     }
@@ -3573,6 +3603,11 @@ ngx_http_v2_filter_request_body(ngx_http
     rb = r->request_body;
     buf = rb->buf;
 
+    if (buf->pos == buf->last && rb->rest) {
+        cl = NULL;
+        goto update;
+    }
+
     cl = ngx_chain_get_free_buf(r->pool, &rb->free);
     if (cl == NULL) {
         return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -3634,6 +3669,9 @@ ngx_http_v2_filter_request_body(ngx_http
     }
 
     b->tag = (ngx_buf_tag_t) &ngx_http_v2_filter_request_body;
+    b->flush = r->request_body_no_buffering;
+
+update:
 
     rc = ngx_http_top_request_body_filter(r, cl);
 
@@ -3676,6 +3714,91 @@ ngx_http_v2_read_client_request_body_han
 }
 
 
+ngx_int_t
+ngx_http_v2_read_unbuffered_request_body(ngx_http_request_t *r)
+{
+    size_t                     window;
+    ngx_buf_t                 *buf;
+    ngx_int_t                  rc;
+    ngx_connection_t          *fc;
+    ngx_http_v2_stream_t      *stream;
+    ngx_http_v2_connection_t  *h2c;
+    ngx_http_core_loc_conf_t  *clcf;
+
+    stream = r->stream;
+    fc = r->connection;
+
+    if (fc->read->timedout) {
+        if (stream->recv_window) {
+            stream->skip_data = 1;
+            fc->timedout = 1;
+
+            return NGX_HTTP_REQUEST_TIME_OUT;
+        }
+
+        fc->read->timedout = 0;
+    }
+
+    if (fc->error) {
+        stream->skip_data = 1;
+        return NGX_HTTP_BAD_REQUEST;
+    }
+
+    rc = ngx_http_v2_filter_request_body(r);
+
+    if (rc != NGX_OK) {
+        stream->skip_data = 1;
+        return rc;
+    }
+
+    if (!r->request_body->rest) {
+        return NGX_OK;
+    }
+
+    if (r->request_body->busy != NULL) {
+        return NGX_AGAIN;
+    }
+
+    buf = r->request_body->buf;
+
+    buf->pos = buf->start;
+    buf->last = buf->start;
+
+    window = buf->end - buf->start;
+    h2c = stream->connection;
+
+    if (h2c->state.stream == stream) {
+        window -= h2c->state.length;
+    }
+
+    if (window == stream->recv_window) {
+        return NGX_AGAIN;
+    }
+
+    if (ngx_http_v2_send_window_update(h2c, stream->node->id,
+                                       window - stream->recv_window)
+        == NGX_ERROR)
+    {
+        stream->skip_data = 1;
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    if (ngx_http_v2_send_output_queue(h2c) == NGX_ERROR) {
+        stream->skip_data = 1;
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    if (stream->recv_window == 0) {
+        clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+        ngx_add_timer(fc->read, clcf->client_body_timeout);
+    }
+
+    stream->recv_window = window;
+
+    return NGX_AGAIN;
+}
+
+
 static ngx_int_t
 ngx_http_v2_terminate_stream(ngx_http_v2_connection_t *h2c,
     ngx_http_v2_stream_t *stream, ngx_uint_t status)
diff -r 887cca40ba6a -r 9d66d7ed2abb src/http/v2/ngx_http_v2.h
--- a/src/http/v2/ngx_http_v2.h	Fri Apr 01 15:56:03 2016 +0300
+++ b/src/http/v2/ngx_http_v2.h	Fri Apr 01 15:57:10 2016 +0300
@@ -260,6 +260,7 @@ void ngx_http_v2_request_headers_init(vo
 
 ngx_int_t ngx_http_v2_read_request_body(ngx_http_request_t *r,
     ngx_http_client_body_handler_pt post_handler);
+ngx_int_t ngx_http_v2_read_unbuffered_request_body(ngx_http_request_t *r);
 
 void ngx_http_v2_close_stream(ngx_http_v2_stream_t *stream, ngx_int_t rc);
 



More information about the nginx-devel mailing list