Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

bugfix: when the parent request takes a request body, the subrequest …

…does not take any bodies, and the subrequest's method is neither "PUT" nor "POST", then the subrequest will no longer inherit the parent request's request body. thanks 欧远宁 for reporting this issue.
  • Loading branch information...
commit aa4bd387801343188c618b814eab06c5c2205402 1 parent 9026d59
@agentzh agentzh authored
Showing with 160 additions and 2 deletions.
  1. +13 −0 src/ngx_http_lua_subrequest.c
  2. +147 −2 t/020-subrequest.t
View
13 src/ngx_http_lua_subrequest.c
@@ -552,6 +552,19 @@ ngx_http_lua_adjust_subrequest(ngx_http_request_t *sr, ngx_uint_t method,
if (rc != NGX_OK) {
return NGX_ERROR;
}
+
+ } else if (method != NGX_HTTP_PUT && method != NGX_HTTP_POST
+ && r->headers_in.content_length_n > 0)
+ {
+
+ rc = ngx_http_lua_set_content_length_header(sr, 0);
+ if (rc != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+#if 1
+ sr->request_body = NULL;
+#endif
}
sr->method = method;
View
149 t/020-subrequest.t
@@ -9,9 +9,8 @@ workers(1);
#master_process_enabled(1);
repeat_each(2);
-#repeat_each(1);
-plan tests => repeat_each() * (blocks() * 2 + 3);
+plan tests => repeat_each() * (blocks() * 2 + 7);
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
@@ -835,3 +834,149 @@ lua reuse free buf chain, but reallocate memory because
--- no_error_log
[error]
+
+
+=== TEST 33: main POST, sub GET (main does not read the body)
+--- config
+ location /other {
+ default_type 'foo/bar';
+ content_by_lua '
+ ngx.req.read_body()
+ ngx.say(ngx.var.request_method)
+ ngx.say(ngx.req.get_body_data())
+ ';
+ }
+
+ location /foo {
+ proxy_pass http://127.0.0.1:$server_port/other;
+ #proxy_pass http://127.0.0.1:8892/other;
+ }
+
+ location /lua {
+ content_by_lua '
+ res = ngx.location.capture("/foo",
+ { method = ngx.HTTP_GET });
+
+ ngx.print(res.body)
+ ';
+ }
+--- request
+POST /lua
+hello, world
+--- response_body
+GET
+nil
+--- no_error_log
+[error]
+
+
+
+=== TEST 34: main POST, sub GET (main has read the body)
+--- config
+ location /other {
+ default_type 'foo/bar';
+ content_by_lua '
+ ngx.req.read_body()
+ ngx.say(ngx.var.request_method)
+ ngx.say(ngx.req.get_body_data())
+ ';
+ }
+
+ location /foo {
+ proxy_pass http://127.0.0.1:$server_port/other;
+ #proxy_pass http://127.0.0.1:8892/other;
+ }
+
+ location /lua {
+ content_by_lua '
+ ngx.req.read_body()
+
+ res = ngx.location.capture("/foo",
+ { method = ngx.HTTP_GET });
+
+ ngx.print(res.body)
+ ';
+ }
+--- request
+POST /lua
+hello, world
+--- response_body
+GET
+nil
+--- no_error_log
+[error]
+
+
+
+=== TEST 35: main POST, sub POST (inherit bodies directly)
+--- config
+ location /other {
+ default_type 'foo/bar';
+ content_by_lua '
+ ngx.req.read_body()
+ ngx.say(ngx.var.request_method)
+ ngx.say(ngx.req.get_body_data())
+ ';
+ }
+
+ location /foo {
+ proxy_pass http://127.0.0.1:$server_port/other;
+ #proxy_pass http://127.0.0.1:8892/other;
+ }
+
+ location /lua {
+ content_by_lua '
+ ngx.req.read_body()
+
+ res = ngx.location.capture("/foo",
+ { method = ngx.HTTP_POST });
+
+ ngx.print(res.body)
+ ';
+ }
+--- request
+POST /lua
+hello, world
+--- response_body
+POST
+hello, world
+--- no_error_log
+[error]
+
+
+
+=== TEST 36: main POST, sub PUT (inherit bodies directly)
+--- config
+ location /other {
+ default_type 'foo/bar';
+ content_by_lua '
+ ngx.req.read_body()
+ ngx.say(ngx.var.request_method)
+ ngx.say(ngx.req.get_body_data())
+ ';
+ }
+
+ location /foo {
+ proxy_pass http://127.0.0.1:$server_port/other;
+ #proxy_pass http://127.0.0.1:8892/other;
+ }
+
+ location /lua {
+ content_by_lua '
+ ngx.req.read_body()
+
+ res = ngx.location.capture("/foo",
+ { method = ngx.HTTP_PUT });
+
+ ngx.print(res.body)
+ ';
+ }
+--- request
+POST /lua
+hello, world
+--- response_body
+PUT
+hello, world
+--- no_error_log
+[error]
+
Please sign in to comment.
Something went wrong with that request. Please try again.