HTTP: improved r.subrequest() error handling after
d34fcb0 (0.8.5).
This reverts code changes introduced in
d34fcb0. To fix the original
problem JS errors are not propagated as nginx errors in r.subrequest().
This also fixes a problem of a lost write event when the njs handler
making r.subrequest() is called from a lua handler as a subrequest:
nginx.conf:
...
location = /subrequest {
access_by_lua_file scripts/subrequest.lua;
proxy_pass http://localhost:8080/pass;
}
location = /nested/subrequest {
internal;
js_content main.getToken;
}
scripts/subrequest.lua:
local res = ngx.location.capture('/nested/subrequest')
ngx.log(ngx.STDERR, "Subrequest status: " .. res.status)
if res.status ~= 200 then
ngx.log(ngx.STDERR, "Subrequest failed with status: " .. res.status)
return nil
end
return res.body
main.js:
async function getToken(r) {
var reply = await r.subrequest('/api/auth');
r.error("reply.status :: " + reply.status);
var status = reply.status;
r.return(status, "{"result":"OK!"}");
}
export default { getToken };
$ curl localhost:8080/subrequest
error.log (before the fix):
...
*1 http js event finalize rc: 0
*1 post event
0000B4BCA64C5FF0
*1 http log handler
*1 http request count:3 blk:0
timer delta: 0
posted event
0000B4BCA64C5FF0
*1 delete posted event
0000B4BCA64C5FF0
*1 http run request: "/subrequest?"
*1 access phase: 11
*1 lua access handler, uri:"/subrequest" c:2
*1 lua run write event handler: timedout:0, ready:1, writing_raw_req_socket:0
*1 useless lua write event handler <<<---
error.log (after the fix):
...
*1 http js event finalize rc: 0
*1 http posted request: "/nested/subrequest?"
*1 http js content write event handler
*1 http js content rc: 0
*1 http finalize request: 0, "/nested/subrequest?" a:0, c:3
*1 lua run post subrequest handler, rc:0 c:3
*1 http log handler
*1 http wake parent request: "/subrequest?"
*1 http posted request: "/subrequest?"
*1 access phase: 11
*1 lua access handler, uri:"/subrequest" c:2
*1 lua run subrequests done, resuming lua thread