Skip to content

Commit

Permalink
Merge pull request #59 from 3scale/hostname-rewrite
Browse files Browse the repository at this point in the history
extract port from host
  • Loading branch information
mikz authored Sep 9, 2016
2 parents 6f1b935 + c23c379 commit ca9ff9d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
18 changes: 18 additions & 0 deletions spec/configuration_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,22 @@ describe('Configuration object', function()
assert.same(nil, configuration.decode(''))
end)
end)

describe('.url', function()
it('works with port', function()
assert.same({'https', false, false, 'example.com', '8443'}, configuration.url('https://example.com:8443'))
end)

it('works with user', function()
assert.same({'https', 'user', false, 'example.com', false }, configuration.url('https://user@example.com'))
end)

it('works with user and password', function()
assert.same({'https', 'user', 'password', 'example.com', false }, configuration.url('https://user:password@example.com'))
end)

it('works with port and path', function()
assert.same({'http', false, false, 'example.com', '8080', '/path'}, configuration.url('http://example.com:8080/path'))
end)
end)
end)
22 changes: 13 additions & 9 deletions src/configuration.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,10 @@ function _M.download(endpoint)
return nil, err
end

local scheme, user, pass, host, path = unpack(url)
local url = table.concat({ scheme, '://', host, path }, '')
local scheme, user, pass, host, port, path = unpack(url)
if port then host = table.concat({host, port}, ':') end

local url = table.concat({ scheme, '://', host, path or '/admin/api/nginx/spec.json' }, '')

local http = require "resty.http"
local httpc = http.new()
Expand Down Expand Up @@ -278,20 +280,20 @@ function _M.url(endpoint)
return nil, 'missing endpoint'
end

local match = ngx.re.match(endpoint, "^(https?):\\/\\/(?:(.+)@)?([^\\/\\s]+)(\\/.+)?$")
local match = ngx.re.match(endpoint, "^(https?):\\/\\/(?:(.+)@)?([^\\/\\s]+?)(?::(\\d+))?(\\/.+)?$")

if not match then
return nil, 'invalid endpoint' -- TODO: maybe improve the error message?
end

local scheme, userinfo, host, path = unpack(match)
local scheme, userinfo, host, port, path = unpack(match)

if path == '/' then path = nil end

local match = ngx.re.match(tostring(userinfo), "^([^:\\s]+)?(?::(.*))?$")
local user, pass = unpack(match)
local match = userinfo and ngx.re.match(tostring(userinfo), "^([^:\\s]+)?(?::(.*))?$")
local user, pass = unpack(match or {})

return { scheme, user or nil, pass or nil, host, path or '/admin/api/nginx/spec.json' }
return { scheme, user or false, pass or false, host, port or false, path or nil }
end


Expand All @@ -302,9 +304,11 @@ function _M.curl(endpoint)
return nil, err
end

local scheme, user, pass, host, path = unpack(url)
local scheme, user, pass, host, port, path = unpack(url)

if port then host = table.concat({host, port}, ':') end

local url = table.concat({ scheme, '://', table.concat({user or '', pass or ''}, ':'), '@', host, path }, '')
local url = table.concat({ scheme, '://', table.concat({user or '', pass or ''}, ':'), '@', host, path or '/admin/api/nginx/spec.json' }, '')

local config, exit, code = util.system('curl --silent --show-error --fail --max-time 3 ' .. url)

Expand Down
2 changes: 1 addition & 1 deletion t/003-apicast.t
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ It asks backend and then forwards the request to the api.
}
location /api-backend/ {
echo 'yay, api backend: $host';
echo 'yay, api backend: $http_host';
}
--- request
GET /?user_key=value
Expand Down

0 comments on commit ca9ff9d

Please sign in to comment.