Problem with post_action when used with return 202; and POST request

Maxim Dounin mdounin at mdounin.ru
Wed Aug 10 23:10:16 UTC 2011


Hello!

On Wed, Aug 10, 2011 at 02:18:41PM +0300, Pyry Hakulinen wrote:

> Hi,
> 
> We are trying to use post_action in a location block that only has
> "return 202;". Everything works fine for GET requests and with small
> POST requests, but if the client request body + headers are over 1kB,
> nginx will not trigger the post_action handler.
> 
> Test configuration, and debug log attached. Following commands can be
> used to trigger the error:
> 
> dd if=/dev/zero of=request_body count=2
> wget -d --post-file=request_body localhost:8022
> 
> Is this a bug or just the way it should work? I tested it with nginx
> 1.0.5 and 1.1.0, both appear to be working the same way.
> 
> I worked around it by creating a small module that just calls
> ngx_http_read_client_request_body() and returns 202 from the callback.
> But I feel that it might not be the right solution for this. (module
> also attached)
> 
> Also while debugging I found another possibly related bug, if you do a
> POST request against location / { return 202; } block, it will
> incorrectly result in 2 lines in access log:
> 
> 127.0.0.1 - - [10/Aug/2011:14:07:40 +0300] "POST / HTTP/1.1" 202 0 "-"
> "curl/7.21.0 (x86_64-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o
> zlib/1.2.3.4 libidn/1.18"
> 127.0.0.1 - - [10/Aug/2011:14:07:40 +0300] "foobar=foo" 400 172 "-" "-"
> 
> Happy to provide more information if needed. Thanks,
> Pyry

Thank you for your report.  Please try the attached patch.

Maxim Dounin
-------------- next part --------------
# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1313016955 -14400
# Node ID 34a89aff5be7c994a37989837cd8d329c8f36500
# Parent  561a37709f6d7f31424a04d7e2c4855a7464a933
Fix "return 202" not discarding body.

Big POST (not fully preread) to a

    location / {
        return 202;
    }

resulted in incorrect behaviour due "return" code path not calling
ngx_http_discard_request_body().  The same applies to all "return" used
with 2xx/3xx codes except 201 and 204, and to all "return ... text" uses.

Fix is to add appropriate call ngx_http_send_response() function where
it looks appropriate.  Remove now redundant discard body call from empty
gif module.

Reported by Pyry Hakulinen, see
http://mailman.nginx.org/pipermail/nginx/2011-August/028503.html

diff --git a/src/http/modules/ngx_http_empty_gif_module.c b/src/http/modules/ngx_http_empty_gif_module.c
--- a/src/http/modules/ngx_http_empty_gif_module.c
+++ b/src/http/modules/ngx_http_empty_gif_module.c
@@ -111,19 +111,12 @@ static ngx_str_t  ngx_http_gif_type = ng
 static ngx_int_t
 ngx_http_empty_gif_handler(ngx_http_request_t *r)
 {
-    ngx_int_t                 rc;
     ngx_http_complex_value_t  cv;
 
     if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
         return NGX_HTTP_NOT_ALLOWED;
     }
 
-    rc = ngx_http_discard_request_body(r);
-
-    if (rc != NGX_OK) {
-        return rc;
-    }
-
     ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));
 
     cv.value.len = sizeof(ngx_empty_gif);
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -1776,6 +1776,10 @@ ngx_http_send_response(ngx_http_request_
     ngx_buf_t    *b;
     ngx_chain_t   out;
 
+    if (ngx_http_discard_request_body(r) != NGX_OK) {
+        return NGX_HTTP_INTERNAL_SERVER_ERROR;
+    }
+
     r->headers_out.status = status;
 
     if (status == NGX_HTTP_NO_CONTENT) {


More information about the nginx mailing list