Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: return right response code when http_to_https enabled in redirect plugin. #2311

Merged
merged 6 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apisix/plugins/redirect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local tab_insert = table.insert
local tab_concat = table.concat
local re_gmatch = ngx.re.gmatch
local ipairs = ipairs
local ngx = ngx

local lrucache = core.lrucache.new({
ttl = 300, count = 100
Expand Down Expand Up @@ -133,7 +134,13 @@ function _M.rewrite(conf, ctx)
-- TODO: add test case
-- PR: https://github.com/apache/apisix/pull/1958
uri = "https://$host$request_uri"
ret_code = 301
local method_name = ngx.req.get_method()
gy09535 marked this conversation as resolved.
Show resolved Hide resolved
if method_name == "GET" or method_name == "HEAD" then
ret_code = 301
gy09535 marked this conversation as resolved.
Show resolved Hide resolved
else
-- https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
ret_code = 308
end
end

if uri and ret_code then
Expand Down
74 changes: 74 additions & 0 deletions t/plugin/redirect.t
Original file line number Diff line number Diff line change
Expand Up @@ -676,3 +676,77 @@ close: 1 nil}
--- no_error_log
[error]
[alert]



=== TEST 26: add plugin with new uri: /test/add
--- 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,
[[{
"methods":["POST","GET","HEAD"],
"plugins": {
"redirect": {
"http_to_https": true,
"ret_code": 307
}
},
"host": "test.com",
"uri": "/hello-https"
}]]
)

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



=== TEST 27: http to https post redirect
--- request
POST /hello-https
--- more_headers
Host: test.com
--- response_headers
Location: https://test.com/hello-https
--- error_code: 308
--- no_error_log
[error]
gy09535 marked this conversation as resolved.
Show resolved Hide resolved



=== TEST 28: http to https get redirect
--- request
GET /hello-https
--- more_headers
Host: test.com
--- response_headers
Location: https://test.com/hello-https
--- error_code: 301
--- no_error_log
[error]



=== TEST 29: http to https head redirect
--- request
HEAD /hello-https
--- more_headers
Host: test.com
--- response_headers
Location: https://test.com/hello-https
--- error_code: 301
--- no_error_log
[error]