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

fix(request-termination) delayed response #3513

Merged
merged 1 commit into from
Jun 15, 2018
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
74 changes: 57 additions & 17 deletions kong/plugins/request-termination/handler.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,80 @@
local BasePlugin = require "kong.plugins.base_plugin"
local singletons = require "kong.singletons"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local meta = require "kong.meta"

local server_header = meta._NAME .. "/" .. meta._VERSION

local ngx = ngx


local server_header = meta._SERVER_TOKENS


local RequestTerminationHandler = BasePlugin:extend()


RequestTerminationHandler.PRIORITY = 2
RequestTerminationHandler.VERSION = "0.1.0"
RequestTerminationHandler.VERSION = "0.1.1"


local function flush(ctx)
ctx = ctx or ngx.ctx

local response = ctx.delayed_response

local status = response.status_code
local content = response.content
local content_type = response.content_type
if not content_type then
content_type = "application/json; charset=utf-8";
end

ngx.status = status

if singletons.configuration.enabled_headers[constants.HEADERS.SERVER] then
ngx.header[constants.HEADERS.SERVER] = server_header

else
ngx.header[constants.HEADERS.SERVER] = nil
end
Copy link
Member

Choose a reason for hiding this comment

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

This plugin should already have tests ensuring that this header is set/unset when terminating a request, and I am not seeing them. Perhaps this underlying implementation change warrants adding those tests.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added


ngx.header["Content-Type"] = content_type
ngx.header["Content-Length"] = #content
ngx.print(content)

return ngx.exit(status)
end


function RequestTerminationHandler:new()
RequestTerminationHandler.super.new(self, "request-termination")
end


function RequestTerminationHandler:access(conf)
RequestTerminationHandler.super.access(self)

local status_code = conf.status_code
local content_type = conf.content_type
local body = conf.body
local message = conf.message
if body then
ngx.status = status_code
local status = conf.status_code
local content = conf.body

if not content_type then
content_type = "application/json; charset=utf-8";
end
ngx.header["Content-Type"] = content_type
ngx.header["Server"] = server_header
if content then
local ctx = ngx.ctx
if ctx.delay_response and not ctx.delayed_response then
ctx.delayed_response = {
status_code = status,
content = content,
content_type = conf.content_type,
}

ngx.say(body)
ctx.delayed_response_callback = flush

return ngx.exit(status_code)
else
return responses.send(status_code, message)
return
end
end

return responses.send(status, conf.message)
end


return RequestTerminationHandler
16 changes: 16 additions & 0 deletions spec/03-plugins/27-request-termination/02-access_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"
local meta = require "kong.meta"


local server_tokens = meta._SERVER_TOKENS


for _, strategy in helpers.each_strategy() do
Expand Down Expand Up @@ -182,5 +186,17 @@ for _, strategy in helpers.each_strategy() do
assert.same({ code = 1, message = "Service unavailable" }, json)
end)
end)

it("returns server tokens with Server header", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/status/200",
headers = {
["Host"] = "api1.request-termination.com"
}
})

assert.equal(server_tokens, res.headers["Server"])
end)
end)
end