Skip to content

Commit

Permalink
fix(request-termination) delayed response
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed Jun 15, 2018
1 parent 79c8a80 commit 55d0479
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
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

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

0 comments on commit 55d0479

Please sign in to comment.