-
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.
Browse files
Browse the repository at this point in the history
* feat(plugin): request-termination A new plug-in that allows a request to be terminated and a specified HTTP status code and body returned. This is useful to temporarily return a status page for a service. For example if the service is unavailable due to scheduled maintenance.
- Loading branch information
Showing
9 changed files
with
372 additions
and
12 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
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
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
local responses = require "kong.tools.responses" | ||
local meta = require "kong.meta" | ||
|
||
local server_header = meta._NAME.."/"..meta._VERSION | ||
|
||
local RequestTerminationHandler = BasePlugin:extend() | ||
|
||
RequestTerminationHandler.PRIORITY = 1 | ||
|
||
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 | ||
|
||
if not content_type then | ||
content_type = "application/json; charset=utf-8"; | ||
end | ||
ngx.header["Content-Type"] = content_type | ||
ngx.header["Server"] = server_header | ||
|
||
ngx.say(body) | ||
|
||
return ngx.exit(status_code) | ||
else | ||
return responses.send(status_code, message) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
local Errors = require "kong.dao.errors" | ||
local utils = require "kong.tools.utils" | ||
|
||
return { | ||
no_consumer = true, | ||
fields = { | ||
status_code = { type = "number", default = 503 }, | ||
message = { type = "string" }, | ||
content_type = { type = "string" }, | ||
body = { type = "string" }, | ||
}, | ||
self_check = function(schema, plugin_t, dao, is_updating) | ||
if plugin_t.status_code then | ||
if plugin_t.status_code < 100 or plugin_t.status_code > 599 then | ||
return false, Errors.schema("status_code must be between 100..599") | ||
end | ||
end | ||
|
||
if plugin_t.message then | ||
if plugin_t.content_type or plugin_t.body then | ||
return false, Errors.schema("message cannot be used with content_type or body") | ||
end | ||
else | ||
if plugin_t.content_type and not plugin_t.body then | ||
return false, Errors.schema("content_type requires a body") | ||
end | ||
end | ||
|
||
return true | ||
end | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
local schemas_validation = require "kong.dao.schemas_validation" | ||
local schema = require "kong.plugins.request-termination.schema" | ||
|
||
local v = schemas_validation.validate_entity | ||
|
||
describe("Plugin: request-termination (schema)", function() | ||
it("should accept a valid status_code", function() | ||
assert(v({status_code = 404}, schema)) | ||
end) | ||
it("should accept a valid message", function() | ||
assert(v({message = "Not found"}, schema)) | ||
end) | ||
it("should accept a valid content_type", function() | ||
assert(v({content_type = "text/html",body = "<body><h1>Not found</h1>"}, schema)) | ||
end) | ||
it("should accept a valid body", function() | ||
assert(v({body = "<body><h1>Not found</h1>"}, schema)) | ||
end) | ||
|
||
describe("errors", function() | ||
it("status_code should only accept numbers", function() | ||
local ok, err = v({status_code = "abcd"}, schema) | ||
assert.same({status_code = "status_code is not a number"}, err) | ||
assert.False(ok) | ||
end) | ||
it("status_code < 100", function() | ||
local ok, _, err = v({status_code = "99"}, schema) | ||
assert.False(ok) | ||
assert.same("status_code must be between 100..599", err.message) | ||
end) | ||
it("status_code > 599", function() | ||
local ok, _, err = v({status_code = "600"}, schema) | ||
assert.False(ok) | ||
assert.same("status_code must be between 100..599", err.message) | ||
end) | ||
it("message with body", function() | ||
local ok, _, err = v({message = "error", body = "test"}, schema) | ||
assert.False(ok) | ||
assert.same("message cannot be used with content_type or body", err.message) | ||
end) | ||
it("message with body and content_type", function() | ||
local ok, _, err = v({message = "error", content_type="text/html", body = "test"}, schema) | ||
assert.False(ok) | ||
assert.same("message cannot be used with content_type or body", err.message) | ||
end) | ||
it("message with content_type", function() | ||
local ok, _, err = v({message = "error", content_type="text/html"}, schema) | ||
assert.False(ok) | ||
assert.same("message cannot be used with content_type or body", err.message) | ||
end) | ||
it("content_type without body", function() | ||
local ok, _, err = v({content_type="text/html"}, schema) | ||
assert.False(ok) | ||
assert.same("content_type requires a body", err.message) | ||
end) | ||
end) | ||
end) |
Oops, something went wrong.