Skip to content

Commit aa4bd38

Browse files
committed
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.
1 parent 9026d59 commit aa4bd38

File tree

2 files changed

+160
-2
lines changed

2 files changed

+160
-2
lines changed

src/ngx_http_lua_subrequest.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,19 @@ ngx_http_lua_adjust_subrequest(ngx_http_request_t *sr, ngx_uint_t method,
552552
if (rc != NGX_OK) {
553553
return NGX_ERROR;
554554
}
555+
556+
} else if (method != NGX_HTTP_PUT && method != NGX_HTTP_POST
557+
&& r->headers_in.content_length_n > 0)
558+
{
559+
560+
rc = ngx_http_lua_set_content_length_header(sr, 0);
561+
if (rc != NGX_OK) {
562+
return NGX_ERROR;
563+
}
564+
565+
#if 1
566+
sr->request_body = NULL;
567+
#endif
555568
}
556569

557570
sr->method = method;

t/020-subrequest.t

Lines changed: 147 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ workers(1);
99
#master_process_enabled(1);
1010

1111
repeat_each(2);
12-
#repeat_each(1);
1312

14-
plan tests => repeat_each() * (blocks() * 2 + 3);
13+
plan tests => repeat_each() * (blocks() * 2 + 7);
1514

1615
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
1716

@@ -835,3 +834,149 @@ lua reuse free buf chain, but reallocate memory because
835834
--- no_error_log
836835
[error]
837836

837+
838+
839+
=== TEST 33: main POST, sub GET (main does not read the body)
840+
--- config
841+
location /other {
842+
default_type 'foo/bar';
843+
content_by_lua '
844+
ngx.req.read_body()
845+
ngx.say(ngx.var.request_method)
846+
ngx.say(ngx.req.get_body_data())
847+
';
848+
}
849+
850+
location /foo {
851+
proxy_pass http://127.0.0.1:$server_port/other;
852+
#proxy_pass http://127.0.0.1:8892/other;
853+
}
854+
855+
location /lua {
856+
content_by_lua '
857+
res = ngx.location.capture("/foo",
858+
{ method = ngx.HTTP_GET });
859+
860+
ngx.print(res.body)
861+
';
862+
}
863+
--- request
864+
POST /lua
865+
hello, world
866+
--- response_body
867+
GET
868+
nil
869+
--- no_error_log
870+
[error]
871+
872+
873+
874+
=== TEST 34: main POST, sub GET (main has read the body)
875+
--- config
876+
location /other {
877+
default_type 'foo/bar';
878+
content_by_lua '
879+
ngx.req.read_body()
880+
ngx.say(ngx.var.request_method)
881+
ngx.say(ngx.req.get_body_data())
882+
';
883+
}
884+
885+
location /foo {
886+
proxy_pass http://127.0.0.1:$server_port/other;
887+
#proxy_pass http://127.0.0.1:8892/other;
888+
}
889+
890+
location /lua {
891+
content_by_lua '
892+
ngx.req.read_body()
893+
894+
res = ngx.location.capture("/foo",
895+
{ method = ngx.HTTP_GET });
896+
897+
ngx.print(res.body)
898+
';
899+
}
900+
--- request
901+
POST /lua
902+
hello, world
903+
--- response_body
904+
GET
905+
nil
906+
--- no_error_log
907+
[error]
908+
909+
910+
911+
=== TEST 35: main POST, sub POST (inherit bodies directly)
912+
--- config
913+
location /other {
914+
default_type 'foo/bar';
915+
content_by_lua '
916+
ngx.req.read_body()
917+
ngx.say(ngx.var.request_method)
918+
ngx.say(ngx.req.get_body_data())
919+
';
920+
}
921+
922+
location /foo {
923+
proxy_pass http://127.0.0.1:$server_port/other;
924+
#proxy_pass http://127.0.0.1:8892/other;
925+
}
926+
927+
location /lua {
928+
content_by_lua '
929+
ngx.req.read_body()
930+
931+
res = ngx.location.capture("/foo",
932+
{ method = ngx.HTTP_POST });
933+
934+
ngx.print(res.body)
935+
';
936+
}
937+
--- request
938+
POST /lua
939+
hello, world
940+
--- response_body
941+
POST
942+
hello, world
943+
--- no_error_log
944+
[error]
945+
946+
947+
948+
=== TEST 36: main POST, sub PUT (inherit bodies directly)
949+
--- config
950+
location /other {
951+
default_type 'foo/bar';
952+
content_by_lua '
953+
ngx.req.read_body()
954+
ngx.say(ngx.var.request_method)
955+
ngx.say(ngx.req.get_body_data())
956+
';
957+
}
958+
959+
location /foo {
960+
proxy_pass http://127.0.0.1:$server_port/other;
961+
#proxy_pass http://127.0.0.1:8892/other;
962+
}
963+
964+
location /lua {
965+
content_by_lua '
966+
ngx.req.read_body()
967+
968+
res = ngx.location.capture("/foo",
969+
{ method = ngx.HTTP_PUT });
970+
971+
ngx.print(res.body)
972+
';
973+
}
974+
--- request
975+
POST /lua
976+
hello, world
977+
--- response_body
978+
PUT
979+
hello, world
980+
--- no_error_log
981+
[error]
982+

0 commit comments

Comments
 (0)