Skip to content

Commit

Permalink
refactor(plugins) removing ngx.req.raw_header() for HTTP/2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco authored and p0pr0ck5 committed Jun 21, 2017
1 parent fc9a96b commit a89c0c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
25 changes: 22 additions & 3 deletions kong/plugins/galileo/alf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ local req_start_time = ngx.req.start_time
local req_get_method = ngx.req.get_method
local req_get_headers = ngx.req.get_headers
local req_get_uri_args = ngx.req.get_uri_args
local req_raw_header = ngx.req.raw_header
local encode_base64 = ngx.encode_base64
local http_version = ngx.req.http_version
local setmetatable = setmetatable
Expand All @@ -35,6 +34,7 @@ local os_date = os.date
local pairs = pairs
local type = type
local gsub = string.gsub
local fmt = string.format

local _M = {
_VERSION = "2.0.0",
Expand Down Expand Up @@ -80,6 +80,22 @@ local function hash_to_array(t)
return arr
end

-- Calculate an approximation of header size (it doesn't calculate white
-- space that may be sorrounding values, other than that it's accurate)
local function calculate_headers_size(request_line, headers)
local size = 0
for k, v in pairs(headers) do
if type(v) == "table" then
for _, y in ipairs(v) do
size = size + #k + 2 + #tostring(y) + 2 --First 2 is semicolon + space
end
else
size = size + #k + 2 + #tostring(v) + 2 --First 2 is semicolon + space
end
end
return #request_line + 2 + size + 2 -- 2 it's \r\n, 4 it's trailing \r\n\r\n
end

local function get_header(t, name, default)
local v = t[name]
if not v then
Expand Down Expand Up @@ -111,6 +127,7 @@ function _M:add_entry(_ngx, req_body_str, resp_body_str)
local var = _ngx.var
local ctx = _ngx.ctx
local http_version = "HTTP/" .. http_version()
local method = req_get_method()
local request_headers = req_get_headers()
local request_content_len = get_header(request_headers, "content-length", 0)
local request_transfer_encoding = get_header(request_headers, "transfer-encoding")
Expand Down Expand Up @@ -182,11 +199,13 @@ function _M:add_entry(_ngx, req_body_str, resp_body_str)
clientIPAddress = var.remote_addr,
request = {
httpVersion = http_version,
method = req_get_method(),
method = method,
url = var.scheme .. "://" .. var.host .. var.request_uri,
queryString = hash_to_array(req_get_uri_args()),
headers = hash_to_array(request_headers),
headersSize = #req_raw_header(),
headersSize = calculate_headers_size(
fmt("%s %s %s", method, var.request_uri, http_version),
request_headers),
postData = post_data,
bodyCaptured = req_has_body,
bodySize = req_body_size,
Expand Down
4 changes: 3 additions & 1 deletion kong/plugins/hmac-auth/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local ngx_sha1 = ngx.hmac_sha1
local ngx_set_header = ngx.req.set_header
local ngx_get_headers = ngx.req.get_headers
local ngx_log = ngx.log
local fmt = string.format

local split = utils.split

Expand Down Expand Up @@ -68,7 +69,8 @@ local function create_hash(request, hmac_params, headers)
if not header_value then
if header == "request-line" then
-- request-line in hmac headers list
signing_string = signing_string .. split(request.raw_header(), "\r\n")[1]
local request_line = fmt("%s %s HTTP/%s", ngx.req.get_method(), ngx.var.uri, ngx.req.http_version())
signing_string = signing_string .. request_line
else
signing_string = signing_string .. header .. ":"
end
Expand Down
2 changes: 1 addition & 1 deletion spec/03-plugins/09-galileo/01-alf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("ALF serializer", function()
local entry = assert(alf:add_entry(_ngx))
assert.contains({name = "accept", value = "application/json"}, entry.request.headers)
assert.contains({name = "host", value = "mockbin.com"}, entry.request.headers)
assert.equal(84, entry.request.headersSize)
assert.equal(118, entry.request.headersSize)
end)
it("handles headers with multiple values", function()
local alf = alf_serializer.new()
Expand Down
9 changes: 4 additions & 5 deletions spec/03-plugins/09-galileo/ngx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ return {
get_method = function() return "GET" end,
http_version = function() return 1.1 end,
raw_header = function ()
return [[
GET /request/path HTTP/1.1\r
Host: mockbin.com\r
Accept: application/json\r
\r\n\r\n]]
return "GET /request/path HTTP/1.1\r\n"..
"Host: mockbin.com\r\n"..
"Accept: application/json\r\n"..
"Accept: application/x-www-form-urlencoded\r\n\r\n"
end,
get_headers = function()
return {
Expand Down

0 comments on commit a89c0c6

Please sign in to comment.