{"action":"opened","issue":{"url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/756","repository_url":"https://api.github.com/repos/openresty/lua-nginx-module","labels_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/756/labels{/name}","comments_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/756/comments","events_url":"https://api.github.com/repos/openresty/lua-nginx-module/issues/756/events","html_url":"https://github.com/openresty/lua-nginx-module/issues/756","id":153255571,"number":756,"title":"Bad request handling when subrequest reads the main request's body","user":{"login":"aviramc","id":3400063,"avatar_url":"https://avatars.githubusercontent.com/u/3400063?v=3","gravatar_id":"","url":"https://api.github.com/users/aviramc","html_url":"https://github.com/aviramc","followers_url":"https://api.github.com/users/aviramc/followers","following_url":"https://api.github.com/users/aviramc/following{/other_user}","gists_url":"https://api.github.com/users/aviramc/gists{/gist_id}","starred_url":"https://api.github.com/users/aviramc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aviramc/subscriptions","organizations_url":"https://api.github.com/users/aviramc/orgs","repos_url":"https://api.github.com/users/aviramc/repos","events_url":"https://api.github.com/users/aviramc/events{/privacy}","received_events_url":"https://api.github.com/users/aviramc/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-05T15:22:17Z","updated_at":"2016-05-05T15:22:17Z","closed_at":null,"body":"After upgrading to the latest ngx_lua, I've noticed the following bug:\r\n* A client connects to Nginx with a keep-alive connection.\r\n* The client performs a request with a body. The Lua handler doesn't read the body, and performs a subrequest.\r\n* The subrequest reads the request body.\r\n* The subrequest and main requests end normally.\r\n* The client sends another request on the same connections (keep-alive). **Nginx returns a '400 - bad request' error**.\r\n\r\nLooking at the code, it appears that the correction to issue #493 produces the issue; look at commit 6fa6e97cc45f89eff2f9f4d64752f30ea18e3d16 or specifically, in the file ngx_http_lua_util.c line 493:\r\nIf the request body has been read by the subrequest, the main request's request_body field is NULL, and therefore the code thinks it should discard it, and so it reads extra bytes as the given Content-Length header. This causes the following request to start at an offset, to be parsed incorrectly, and to produce a \"400 bad request\" error.\r\n\r\nI'm not sure how to correct the issue. In my own repository, I've done the following:\r\n* Added to the module's ctx object the field 'request_body_forwarded', a boolean field.\r\n* In 'ngx_http_lua_subrequest.c' line 660, setting the flag to 1.\r\n* In 'ngx_http_lua_util.c line 493', consider this flag as well, meaning discard the body only if this flag is false.\r\nThis seems to fix the issue, but I'm not sure this is the proper fix.\r\n@agentzh , what do you think?\r\n\r\nIn order to reproduce, use the following configuration:\r\n```\r\nserver {\r\n       listen 80;\r\n       server_name localhost;\r\n\r\n       location / {\r\n         content_by_lua \"\r\n         local sr = ngx.location.capture('/sub', {method = ngx.HTTP_POST})\r\n         ngx.say('sr status: ' .. tostring(sr.status))\r\n\";\r\n       }\r\n\r\n       location /sub {\r\n         content_by_lua \"\r\n         local req_body = ngx.req.read_body()\r\n         ngx.say('subrequest! got body: ')\r\n         ngx.say(req_body)\r\n         \";\r\n       }\r\n}\r\n```\r\n\r\nAnd the following curl command:\r\n`curl -v http://127.0.0.1/blah?[0-1] --data-binary 1234567`\r\n\r\nThe second command should fail with a 400 error."}}