Skip to content

Commit

Permalink
plugin(response-rewrite): rewrite binary data to client by base64 (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
lilien1010 authored and SaberMaster committed Jun 30, 2020
1 parent 792d11b commit 5117cb6
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
20 changes: 19 additions & 1 deletion apisix/plugins/response-rewrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ local schema = {
description = "new body for repsonse",
type = "string",
},
body_base64 = {
description = "whether new body for repsonse need base64 decode before return",
type = "boolean",
default = false,
},
status_code = {
description = "new status code for repsonse",
type = "integer",
Expand Down Expand Up @@ -77,6 +82,13 @@ function _M.check_schema(conf)
end
end

if conf.body_base64 then
local body = ngx.decode_base64(conf.body)
if not body then
return false, 'invalid base64 content'
end
end

return true
end

Expand All @@ -85,7 +97,13 @@ do

function _M.body_filter(conf, ctx)
if conf.body then
ngx.arg[1] = conf.body

if conf.body_base64 then
ngx.arg[1] = ngx.decode_base64(conf.body)
else
ngx.arg[1] = conf.body
end

ngx.arg[2] = true
end
end
Expand Down
1 change: 1 addition & 0 deletions doc/plugins/response-rewrite-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
|------- |-----|------|
|status_code |可选| 修改上游返回状态码|
|body |可选| 修改上游返回的 `body` 内容,如果设置了新内容,header 里面的 content-length 字段也会被去掉|
|body_base64 |可选| 布尔类型,描述 `body` 字段是否需要 base64 解码之后再返回给客户端,用在某些图片和 Protobuffer 场景|
|headers |可选| 返回给客户端的 `headers`,这里可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可|


Expand Down
1 change: 1 addition & 0 deletions doc/plugins/response-rewrite.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ response rewrite plugin, rewrite the content from upstream.
|------- |-----|------|
|status_code |optional| New `status code` to client|
|body |optional| New `body` to client, and the content-length will be reset too.|
|body_base64 |optional| This is a boolean value,identify if `body` in configuration need base64 decoded before rewrite to client.|
|headers |optional| Set the new `headers` for client, can set up multiple. If it exists already from upstream, will rewrite the header, otherwise will add the header. You can set the corresponding value to an empty string to remove a header. |

## How To Enable
Expand Down
77 changes: 75 additions & 2 deletions t/plugin/response-rewrite.t
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ property "body" validation failed: wrong type: expected string, got number
"plugins": {
"response-rewrite": {
"headers" : {
"X-Server-id": 3,
"X-Server-id": 3,
"X-Server-status": "on",
"Content-Type": ""
},
Expand Down Expand Up @@ -169,7 +169,7 @@ passed
if not res then
ngx.say(err)
return
end
end

if res.headers['Content-Type'] then
ngx.say('fail content-type should not be exist, now is'..res.headers['Content-Type'])
Expand Down Expand Up @@ -354,3 +354,76 @@ GET /t
invalid type as header value
--- no_error_log
[error]



=== TEST 12: set body in base64
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"response-rewrite": {
"body": "SGVsbG8K",
"body_base64": true
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



=== TEST 13: check base64 content
--- request
GET /hello
--- response_body
Hello
--- no_error_log
[error]



=== TEST 14: set body with not well formed base64
--- config
location /t {
content_by_lua_block {
local plugin = require("apisix.plugins.response-rewrite")
local ok, err = plugin.check_schema({
body = "1",
body_base64 = true
})
if not ok then
ngx.say(err)
return
end
}
}
--- request
GET /t
--- response_body
invalid base64 content
--- no_error_log
[error]

0 comments on commit 5117cb6

Please sign in to comment.