Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http-ng: fix HOST header #1264

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fixed issues with URI when using Routing Policy [PR #1245](https://github.com/3scale/APIcast/pull/1245) [THREESCALE-6410](https://issues.redhat.com/browse/THREESCALE-6410)
- Fixed typo on TLS jsonschema [PR #1260](https://github.com/3scale/APIcast/pull/1260) [THREESCALE-6390](https://issues.redhat.com/browse/THREESCALE-6390)
- Fixed host header format on http_ng resty [PR #1264](https://github.com/3scale/APIcast/pull/1264) [THREESCALE-2235](https://issues.redhat.com/browse/THREESCALE-2235)


### Added
Expand Down
20 changes: 15 additions & 5 deletions gateway/src/resty/http_ng/request.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local find = string.find
local sub = string.sub
local assert = assert
local setmetatable = setmetatable
local resty_url = require 'resty.url'
local format = string.format


------------
-- @module middleware
Expand All @@ -20,13 +23,20 @@ local request = { }

request.headers = require 'resty.http_ng.headers'

local function extract_host(url)
local _, last = find(url, '://', 0, true)
local len = find(url, '/', last + 1, true)
local function host_header(uri)
local port = uri.port
local default_port = resty_url.default_port(uri.scheme)

if len then len = len - 1 end
if port and port ~= default_port then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen in the JIRA issue that this strips the port only if it's the default one to fix some issue when using OIDC. However, this is a generic client, doesn't it affect other use cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not a issue at all.

return format('%s:%s', uri.host, port)
else
return uri.host
end
end

return sub(url, last + 1, len)
local function extract_host(url)
local uri = resty_url.parse(url)
return host_header(uri or {})
end

function request.extract_headers(req)
Expand Down
17 changes: 17 additions & 0 deletions spec/resty/http_ng/request_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ describe('request', function()
assert.equal('example.com',req.headers.Host)
end)

it('correct host heder format', function()
results = {
['http://foo.com'] = "foo.com",
['http://foo.com:80'] = "foo.com",
['http://foo.com:80/test'] = "foo.com",
['http://foo.com:8080/test'] = "foo.com:8080",
['https://foo.com/'] = "foo.com",
['https://foo.com:8043/'] = "foo.com:8043",
['https://foo.com:8043/test'] = "foo.com:8043",
}
for key, val in pairs(results) do
local req = request.new{url = key, method = 'GET' }
assert.equal(val, req.headers.Host)
end

end)

it('has version', function()
local req = request.new{url = 'http://example.com/path', method = 'GET' }

Expand Down