-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(request-termination) delayed response
- Loading branch information
Showing
2 changed files
with
73 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters