diff --git a/README.md b/README.md index 2be68a8..34f9487 100644 --- a/README.md +++ b/README.md @@ -208,8 +208,10 @@ end * `req.headers` - normalized request headers. A normalized header is in the lower case, all headers joined together into a single string. * `req.peer` - a Lua table with information about the remote peer - (like `socket:peer()`). **NOTE**: not available when using NGINX TSGI - adapter. + (like `socket:peer()`). + **NOTE**: when router is being used with + nginx adapter, `req.peer` contains information on iproto connection with + nginx, not the original HTTP user-agent. * `tostring(req)` - returns a string representation of the request. * `req:request_line()` - returns the request body. * `req:read(delimiter|chunk|{delimiter = x, chunk = x}, timeout)` - reads the diff --git a/http/nginx_server/init.lua b/http/nginx_server/init.lua index c88cd70..42537ff 100644 --- a/http/nginx_server/init.lua +++ b/http/nginx_server/init.lua @@ -43,6 +43,10 @@ local function make_env(server, req) body = json.decode(req.body).params end + local hostport = box.session.peer(box.session.id()) -- luacheck: ignore + local hostport_parts = string.split(hostport, ':') -- luacheck: ignore + local peer_host, peer_port = hostport_parts[1], tonumber(hostport_parts[2]) + local env = { ['tsgi.version'] = '1', ['tsgi.url_scheme'] = 'http', -- no support for https @@ -62,6 +66,13 @@ local function make_env(server, req) ['PATH_INFO'] = path_info, ['QUERY_STRING'] = query_string, ['SERVER_PROTOCOL'] = req.proto, + [tsgi.KEY_PEER] = { + host = peer_host, + port = peer_port, + family = 'AF_INET', + type = 'SOCK_STREAM', + protocol = 'tcp', + }, [KEY_BODY] = body, -- http body string; used in `tsgi_input_read` } diff --git a/http/router/init.lua b/http/router/init.lua index 59cc1c5..2127e3f 100644 --- a/http/router/init.lua +++ b/http/router/init.lua @@ -26,7 +26,7 @@ local function request_from_env(env, router) -- luacheck: ignore local request = { router = router, env = env, - peer = env[tsgi.KEY_PEER], -- only for builtin server + peer = env[tsgi.KEY_PEER], method = env['REQUEST_METHOD'], path = env['PATH_INFO'], query = env['QUERY_STRING'], diff --git a/test/http.test.lua b/test/http.test.lua index 73b6ae3..a56a8bb 100755 --- a/test/http.test.lua +++ b/test/http.test.lua @@ -318,18 +318,12 @@ test:test("server requests", function(test) test:is(r.status, 500, 'die 500') --test:is(r.reason, 'Internal server error', 'die reason') - -- request.peer is not supported in NGINX TSGI - if is_builtin_test() then - router:route({ path = '/info' }, function(cx) - return cx:render({ json = cx.peer }) - end) - local r = json.decode(http_client.get('http://127.0.0.1:12345/info').body) - test:is(r.host, '127.0.0.1', 'peer.host') - test:isnumber(r.port, 'peer.port') - else - test:ok(true, 'peer.host - ignore on NGINX') - test:ok(true, 'peer.port - ignore on NGINX') - end + router:route({ path = '/info' }, function(cx) + return cx:render({ json = cx.peer }) + end) + local r = json.decode(http_client.get('http://127.0.0.1:12345/info').body) + test:is(r.host, '127.0.0.1', 'peer.host') + test:isnumber(r.port, 'peer.port') local r = router:route({method = 'POST', path = '/dit', file = 'helper.html.el'}, function(tx)