Skip to content

Commit

Permalink
Add request_object:peer() for NGINX adapter
Browse files Browse the repository at this point in the history
host and port are extracted via
box.session.peer().

family, type and protocol are hardcoded.
  • Loading branch information
ASverdlov committed May 16, 2019
1 parent 45b33d9 commit 05e661d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions http/nginx_server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
}
Expand Down
2 changes: 1 addition & 1 deletion http/router/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
18 changes: 6 additions & 12 deletions test/http.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 05e661d

Please sign in to comment.