Skip to content

Commit

Permalink
fix(echo): modify response body correctly (apache#2561)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacewander authored Oct 29, 2020
1 parent b23475f commit 960077f
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 8 deletions.
11 changes: 8 additions & 3 deletions apisix/plugins/echo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ end
function _M.body_filter(conf, ctx)
if conf.body then
ngx.arg[1] = conf.body
ngx.arg[2] = true
end

if conf.before_body then
if conf.before_body and not ctx.plugin_echo_body_set then
ngx.arg[1] = conf.before_body .. ngx.arg[1]
ctx.plugin_echo_body_set = true
end

if conf.after_body then
if ngx.arg[2] and conf.after_body then
ngx.arg[1] = ngx.arg[1] .. conf.after_body
end
ngx.arg[2] = true
end


Expand All @@ -101,6 +102,10 @@ end


function _M.header_filter(conf, ctx)
if conf.body or conf.before_body or conf.after_body then
core.response.clear_header_as_body_modified()
end

if not conf.headers then
return
end
Expand Down
12 changes: 11 additions & 1 deletion t/lib/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ local function inject_headers()
end

function _M.hello()
ngx.say("hello world")
local s = "hello world"
ngx.header['Content-Length'] = #s + 1
ngx.say(s)
end

function _M.hello_chunked()
ngx.print("hell")
ngx.flush(true)
ngx.print("o w")
ngx.flush(true)
ngx.say("orld")
end

function _M.hello1()
Expand Down
1 change: 1 addition & 0 deletions t/node/remote-addr-ipv6.t
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ qr{connected: 1
request sent: 59
received: HTTP/1.1 200 OK
received: Content-Type: text/plain
received: Content-Length: 12
received: Connection: close
received: Server: APISIX/\d\.\d+(\.\d+)?
received: Server: \w+
Expand Down
107 changes: 103 additions & 4 deletions t/plugin/echo.t
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ done
"echo": {
"before_body": "before the body modification ",
"body":"hello upstream",
"headers": {
"after_body": " after the body modification.",
"headers": {
"Location":"https://www.iresty.com",
"Authorization": "userpass"
},
"auth_value" : "userpass"
},
"auth_value" : "userpass"
}
},
"upstream": {
Expand Down Expand Up @@ -145,7 +146,7 @@ GET /hello
--- more_headers
Authorization: userpass
--- response_body chomp
before the body modification hello upstream
before the body modification hello upstream after the body modification.
--- response_headers
Location: https://www.iresty.com
Authorization: userpass
Expand Down Expand Up @@ -536,3 +537,101 @@ GET /t
additional properties forbidden, found invalid_att
--- no_error_log
[error]



=== TEST 15: set body with chunked upstream
--- 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": {
"echo": {
"body":"hello upstream"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello_chunked"
}]]
)

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



=== TEST 16: access
--- request
GET /hello_chunked
--- response_body chomp
hello upstream
--- no_error_log
[error]



=== TEST 17: add before/after body with chunked upstream
--- 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": {
"echo": {
"before_body": "before the body modification ",
"after_body": " after the body modification."
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello_chunked"
}]]
)

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



=== TEST 18: access
--- request
GET /hello_chunked
--- response_body chomp
before the body modification hello world
after the body modification.
--- no_error_log
[error]
1 change: 1 addition & 0 deletions t/plugin/redirect.t
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ ssl handshake: userdata
sent http request: 58 bytes.
received: HTTP/1.1 200 OK
received: Content-Type: text/plain
received: Content-Length: 12
received: Connection: close
received: Server: APISIX/\d\.\d+(\.\d+)?
received: Server: \w+
Expand Down
1 change: 1 addition & 0 deletions t/router/multi-ssl-certs.t
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ ssl handshake: userdata
sent http request: 62 bytes.
received: HTTP/1.1 200 OK
received: Content-Type: text/plain
received: Content-Length: 12
received: Connection: close
received: Server: APISIX/\d\.\d+(\.\d+)?
received: Server: \w+
Expand Down
3 changes: 3 additions & 0 deletions t/router/radixtree-sni.t
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ ssl handshake: userdata
sent http request: 62 bytes.
received: HTTP/1.1 200 OK
received: Content-Type: text/plain
received: Content-Length: 12
received: Connection: close
received: Server: APISIX/\d\.\d+(\.\d+)?
received: Server: \w+
Expand Down Expand Up @@ -313,6 +314,7 @@ ssl handshake: userdata
sent http request: 62 bytes.
received: HTTP/1.1 200 OK
received: Content-Type: text/plain
received: Content-Length: 12
received: Connection: close
received: Server: APISIX/\d\.\d+(\.\d+)?
received: Server: \w+
Expand Down Expand Up @@ -427,6 +429,7 @@ ssl handshake: userdata
sent http request: 58 bytes.
received: HTTP/1.1 200 OK
received: Content-Type: text/plain
received: Content-Length: 12
received: Connection: close
received: Server: APISIX/\d\.\d+(\.\d+)?
received: Server: \w+
Expand Down

0 comments on commit 960077f

Please sign in to comment.