Merge pull request #5076 from caddyserver/fastcgi-redir

fastcgi: Redirect using original URI path (fix #5073) and rewrite: Only trim prefix if matched
This commit is contained in:
Matt Holt 2022-09-28 15:22:45 -06:00 committed by GitHub
commit 897a38958c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 9 deletions

View file

@ -8,7 +8,7 @@ route {
}
not path */
}
redir @canonicalPath {path}/ 308
redir @canonicalPath {http.request.orig_uri.path}/ 308
# If the requested file does not exist, try index files
@indexFiles {
@ -50,7 +50,7 @@ route {
"handler": "static_response",
"headers": {
"Location": [
"{http.request.uri.path}/"
"{http.request.orig_uri.path}/"
]
},
"status_code": 308

View file

@ -42,7 +42,7 @@
"handler": "static_response",
"headers": {
"Location": [
"{http.request.uri.path}/"
"{http.request.orig_uri.path}/"
]
},
"status_code": 308

View file

@ -30,7 +30,7 @@ php_fastcgi @api localhost:9000
"handler": "static_response",
"headers": {
"Location": [
"{http.request.uri.path}/"
"{http.request.orig_uri.path}/"
]
},
"status_code": 308

View file

@ -43,7 +43,7 @@ php_fastcgi localhost:9000 {
"handler": "static_response",
"headers": {
"Location": [
"{http.request.uri.path}/"
"{http.request.orig_uri.path}/"
]
},
"status_code": 308

View file

@ -46,7 +46,7 @@ php_fastcgi localhost:9000 {
"handler": "static_response",
"headers": {
"Location": [
"{http.request.uri.path}/"
"{http.request.orig_uri.path}/"
]
},
"status_code": 308

View file

@ -348,7 +348,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
}
redirHandler := caddyhttp.StaticResponse{
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
Headers: http.Header{"Location": []string{"{http.request.uri.path}/"}},
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
}
redirRoute := caddyhttp.Route{
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},

View file

@ -383,8 +383,13 @@ func trimPathPrefix(escapedPath, prefix string) string {
iPrefix++
}
// found matching prefix, trim it
return escapedPath[iPath:]
// if we iterated through the entire prefix, we found it, so trim it
if iPath >= len(prefix) {
return escapedPath[iPath:]
}
// otherwise we did not find the prefix
return escapedPath
}
func reverse(s string) string {

View file

@ -225,6 +225,16 @@ func TestRewrite(t *testing.T) {
input: newRequest(t, "GET", "/prefix/foo/bar"),
expect: newRequest(t, "GET", "/foo/bar"),
},
{
rule: Rewrite{StripPathPrefix: "/prefix"},
input: newRequest(t, "GET", "/prefix"),
expect: newRequest(t, "GET", ""),
},
{
rule: Rewrite{StripPathPrefix: "/prefix"},
input: newRequest(t, "GET", "/"),
expect: newRequest(t, "GET", "/"),
},
{
rule: Rewrite{StripPathPrefix: "/prefix"},
input: newRequest(t, "GET", "/prefix/foo%2Fbar"),