]> git.kaiwu.me - nginx.git/commitdiff
Core: added limit to recv_chain().
authorRoman Arutyunyan <arut@nginx.com>
Tue, 28 Oct 2014 09:29:58 +0000 (12:29 +0300)
committerRoman Arutyunyan <arut@nginx.com>
Tue, 28 Oct 2014 09:29:58 +0000 (12:29 +0300)
src/event/ngx_event_openssl.c
src/event/ngx_event_openssl.h
src/event/ngx_event_pipe.c
src/os/unix/ngx_aio_read_chain.c
src/os/unix/ngx_os.h
src/os/unix/ngx_readv_chain.c
src/os/win32/ngx_os.h
src/os/win32/ngx_wsarecv_chain.c

index 975a8e00111b9c897185783623583b58da446a36..f34565277997e51127688502b4a4dbeee9121746 100644 (file)
@@ -1185,10 +1185,10 @@ ngx_ssl_handshake_handler(ngx_event_t *ev)
 
 
 ssize_t
-ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl)
+ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit)
 {
     u_char     *last;
-    ssize_t     n, bytes;
+    ssize_t     n, bytes, size;
     ngx_buf_t  *b;
 
     bytes = 0;
@@ -1197,8 +1197,19 @@ ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl)
     last = b->last;
 
     for ( ;; ) {
+        size = b->end - last;
 
-        n = ngx_ssl_recv(c, last, b->end - last);
+        if (limit) {
+            if (bytes >= limit) {
+                return bytes;
+            }
+
+            if (bytes + size > limit) {
+                size = (ssize_t) (limit - bytes);
+            }
+        }
+
+        n = ngx_ssl_recv(c, last, size);
 
         if (n > 0) {
             last += n;
index 408694035193effdb09883da33536088c94ecf85..08eff64456bb5519ea7ba6cb9c3c8a1ef2876924 100644 (file)
@@ -194,7 +194,7 @@ ngx_int_t ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool,
 ngx_int_t ngx_ssl_handshake(ngx_connection_t *c);
 ssize_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size);
 ssize_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size);
-ssize_t ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl);
+ssize_t ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit);
 ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
     off_t limit);
 void ngx_ssl_free_buffer(ngx_connection_t *c);
index 64fb07bde4207b681d4abd54476fd9bfd36c1f50..21f084417874c8aff840dbd566facd3c29d50a78 100644 (file)
@@ -270,7 +270,7 @@ ngx_event_pipe_read_upstream(ngx_event_pipe_t *p)
                 break;
             }
 
-            n = p->upstream->recv_chain(p->upstream, chain);
+            n = p->upstream->recv_chain(p->upstream, chain, 0);
 
             ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
                            "pipe recv chain: %z", n);
index 8c831b951297ac0ec2fe7fbe7298326b872fb23a..d8722b2c1c969c8a451c4ea4fd8cf11997de6868 100644 (file)
@@ -11,7 +11,7 @@
 
 
 ssize_t
-ngx_aio_read_chain(ngx_connection_t *c, ngx_chain_t *cl)
+ngx_aio_read_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit)
 {
     int           n;
     u_char       *buf, *prev;
index 1033d88251e582a0815d652c810b71b853514921..a1586426c13096e347cf4ef6a9f67262ac00d670 100644 (file)
@@ -17,7 +17,8 @@
 
 
 typedef ssize_t (*ngx_recv_pt)(ngx_connection_t *c, u_char *buf, size_t size);
-typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in);
+typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in,
+    off_t limit);
 typedef ssize_t (*ngx_send_pt)(ngx_connection_t *c, u_char *buf, size_t size);
 typedef ngx_chain_t *(*ngx_send_chain_pt)(ngx_connection_t *c, ngx_chain_t *in,
     off_t limit);
@@ -41,7 +42,7 @@ ngx_int_t ngx_os_signal_process(ngx_cycle_t *cycle, char *sig, ngx_int_t pid);
 
 
 ssize_t ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size);
-ssize_t ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *entry);
+ssize_t ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *entry, off_t limit);
 ssize_t ngx_udp_unix_recv(ngx_connection_t *c, u_char *buf, size_t size);
 ssize_t ngx_unix_send(ngx_connection_t *c, u_char *buf, size_t size);
 ngx_chain_t *ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in,
@@ -49,7 +50,7 @@ ngx_chain_t *ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in,
 
 #if (NGX_HAVE_AIO)
 ssize_t ngx_aio_read(ngx_connection_t *c, u_char *buf, size_t size);
-ssize_t ngx_aio_read_chain(ngx_connection_t *c, ngx_chain_t *cl);
+ssize_t ngx_aio_read_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit);
 ssize_t ngx_aio_write(ngx_connection_t *c, u_char *buf, size_t size);
 ngx_chain_t *ngx_aio_write_chain(ngx_connection_t *c, ngx_chain_t *in,
     off_t limit);
index 3cba80cccc6c32fbd5d99adf41329efb8c432cf4..3544b4b17ebe4f0f086123af81287dfc7e9215e9 100644 (file)
@@ -11,7 +11,7 @@
 
 
 ssize_t
-ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
+ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain, off_t limit)
 {
     u_char        *prev;
     ssize_t        n, size;
@@ -66,8 +66,20 @@ ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
     /* coalesce the neighbouring bufs */
 
     while (chain) {
+        n = chain->buf->end - chain->buf->last;
+
+        if (limit) {
+            if (size >= limit) {
+                break;
+            }
+
+            if (size + n > limit) {
+                n = (ssize_t) (limit - size);
+            }
+        }
+
         if (prev == chain->buf->last) {
-            iov->iov_len += chain->buf->end - chain->buf->last;
+            iov->iov_len += n;
 
         } else {
             if (vec.nelts >= IOV_MAX) {
@@ -80,10 +92,10 @@ ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
             }
 
             iov->iov_base = (void *) chain->buf->last;
-            iov->iov_len = chain->buf->end - chain->buf->last;
+            iov->iov_len = n;
         }
 
-        size += chain->buf->end - chain->buf->last;
+        size += n;
         prev = chain->buf->end;
         chain = chain->next;
     }
index 6f828fe69a953403917e020684808d8265303d82..8bce9a28969b788398e35e606b4309773f39ba9e 100644 (file)
@@ -17,7 +17,8 @@
 
 
 typedef ssize_t (*ngx_recv_pt)(ngx_connection_t *c, u_char *buf, size_t size);
-typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in);
+typedef ssize_t (*ngx_recv_chain_pt)(ngx_connection_t *c, ngx_chain_t *in,
+    off_t limit);
 typedef ssize_t (*ngx_send_pt)(ngx_connection_t *c, u_char *buf, size_t size);
 typedef ngx_chain_t *(*ngx_send_chain_pt)(ngx_connection_t *c, ngx_chain_t *in,
     off_t limit);
@@ -41,7 +42,7 @@ ssize_t ngx_overlapped_wsarecv(ngx_connection_t *c, u_char *buf, size_t size);
 ssize_t ngx_udp_wsarecv(ngx_connection_t *c, u_char *buf, size_t size);
 ssize_t ngx_udp_overlapped_wsarecv(ngx_connection_t *c, u_char *buf,
     size_t size);
-ssize_t ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain);
+ssize_t ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain, off_t limit);
 ssize_t ngx_wsasend(ngx_connection_t *c, u_char *buf, size_t size);
 ssize_t ngx_overlapped_wsasend(ngx_connection_t *c, u_char *buf, size_t size);
 ngx_chain_t *ngx_wsasend_chain(ngx_connection_t *c, ngx_chain_t *in,
index 7a85d8151be66500704aef2dbe055d22055824fc..2598e091c37ec786a0e5fbe1ce291bdcbeaf1951 100644 (file)
 
 
 ssize_t
-ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain)
+ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain, off_t limit)
 {
     int           rc;
     u_char       *prev;
     u_long        bytes, flags;
-    size_t        size;
+    size_t        n, size;
     ngx_err_t     err;
     ngx_array_t   vec;
     ngx_event_t  *rev;
@@ -41,8 +41,20 @@ ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain)
     /* coalesce the neighbouring bufs */
 
     while (chain) {
+        n = chain->buf->end - chain->buf->last;
+
+        if (limit) {
+            if (size >= (size_t) limit) {
+                break;
+            }
+
+            if (size + n > (size_t) limit) {
+                n = (size_t) limit - size;
+            }
+        }
+
         if (prev == chain->buf->last) {
-            wsabuf->len += chain->buf->end - chain->buf->last;
+            wsabuf->len += n;
 
         } else {
             wsabuf = ngx_array_push(&vec);
@@ -51,10 +63,10 @@ ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain)
             }
 
             wsabuf->buf = (char *) chain->buf->last;
-            wsabuf->len = chain->buf->end - chain->buf->last;
+            wsabuf->len = n;
         }
 
-        size += chain->buf->end - chain->buf->last;
+        size += n;
         prev = chain->buf->end;
         chain = chain->next;
     }