mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
caddyhttp: Add {?query}
placeholder (#6714)
* caddyhttp: Add `{prefixed_query}` placeholder * fastcgi: Preserve query during canonical redirect * Use orig_uri instead for the redirect, shorter Caddyfile shortcut
This commit is contained in:
parent
16d5b22349
commit
b116dcea3d
8 changed files with 30 additions and 11 deletions
|
@ -52,19 +52,27 @@ func NewShorthandReplacer() ShorthandReplacer {
|
||||||
// be used in the Caddyfile, and the right is the replacement.
|
// be used in the Caddyfile, and the right is the replacement.
|
||||||
func placeholderShorthands() []string {
|
func placeholderShorthands() []string {
|
||||||
return []string{
|
return []string{
|
||||||
"{dir}", "{http.request.uri.path.dir}",
|
|
||||||
"{file}", "{http.request.uri.path.file}",
|
|
||||||
"{host}", "{http.request.host}",
|
"{host}", "{http.request.host}",
|
||||||
"{hostport}", "{http.request.hostport}",
|
"{hostport}", "{http.request.hostport}",
|
||||||
"{port}", "{http.request.port}",
|
"{port}", "{http.request.port}",
|
||||||
|
"{orig_method}", "{http.request.orig_method}",
|
||||||
|
"{orig_uri}", "{http.request.orig_uri}",
|
||||||
|
"{orig_path}", "{http.request.orig_uri.path}",
|
||||||
|
"{orig_dir}", "{http.request.orig_uri.path.dir}",
|
||||||
|
"{orig_file}", "{http.request.orig_uri.path.file}",
|
||||||
|
"{orig_query}", "{http.request.orig_uri.query}",
|
||||||
|
"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
|
||||||
"{method}", "{http.request.method}",
|
"{method}", "{http.request.method}",
|
||||||
|
"{uri}", "{http.request.uri}",
|
||||||
"{path}", "{http.request.uri.path}",
|
"{path}", "{http.request.uri.path}",
|
||||||
|
"{dir}", "{http.request.uri.path.dir}",
|
||||||
|
"{file}", "{http.request.uri.path.file}",
|
||||||
"{query}", "{http.request.uri.query}",
|
"{query}", "{http.request.uri.query}",
|
||||||
|
"{?query}", "{http.request.uri.prefixed_query}",
|
||||||
"{remote}", "{http.request.remote}",
|
"{remote}", "{http.request.remote}",
|
||||||
"{remote_host}", "{http.request.remote.host}",
|
"{remote_host}", "{http.request.remote.host}",
|
||||||
"{remote_port}", "{http.request.remote.port}",
|
"{remote_port}", "{http.request.remote.port}",
|
||||||
"{scheme}", "{http.request.scheme}",
|
"{scheme}", "{http.request.scheme}",
|
||||||
"{uri}", "{http.request.uri}",
|
|
||||||
"{uuid}", "{http.request.uuid}",
|
"{uuid}", "{http.request.uuid}",
|
||||||
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
"{tls_cipher}", "{http.request.tls.cipher_suite}",
|
||||||
"{tls_version}", "{http.request.tls.version}",
|
"{tls_version}", "{http.request.tls.version}",
|
||||||
|
|
|
@ -8,7 +8,7 @@ route {
|
||||||
}
|
}
|
||||||
not path */
|
not path */
|
||||||
}
|
}
|
||||||
redir @canonicalPath {http.request.orig_uri.path}/ 308
|
redir @canonicalPath {orig_path}/{orig_?query} 308
|
||||||
|
|
||||||
# If the requested file does not exist, try index files
|
# If the requested file does not exist, try index files
|
||||||
@indexFiles {
|
@indexFiles {
|
||||||
|
@ -17,7 +17,7 @@ route {
|
||||||
split_path .php
|
split_path .php
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rewrite @indexFiles {http.matchers.file.relative}
|
rewrite @indexFiles {file_match.relative}
|
||||||
|
|
||||||
# Proxy PHP files to the FastCGI responder
|
# Proxy PHP files to the FastCGI responder
|
||||||
@phpFiles {
|
@phpFiles {
|
||||||
|
@ -50,7 +50,7 @@ route {
|
||||||
"handler": "static_response",
|
"handler": "static_response",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Location": [
|
"Location": [
|
||||||
"{http.request.orig_uri.path}/"
|
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"status_code": 308
|
"status_code": 308
|
||||||
|
|
|
@ -42,7 +42,7 @@
|
||||||
"handler": "static_response",
|
"handler": "static_response",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Location": [
|
"Location": [
|
||||||
"{http.request.orig_uri.path}/"
|
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"status_code": 308
|
"status_code": 308
|
||||||
|
|
|
@ -33,7 +33,7 @@ php_fastcgi @test localhost:9000
|
||||||
"handler": "static_response",
|
"handler": "static_response",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Location": [
|
"Location": [
|
||||||
"{http.request.orig_uri.path}/"
|
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"status_code": 308
|
"status_code": 308
|
||||||
|
|
|
@ -43,7 +43,7 @@ php_fastcgi localhost:9000 {
|
||||||
"handler": "static_response",
|
"handler": "static_response",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Location": [
|
"Location": [
|
||||||
"{http.request.orig_uri.path}/"
|
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"status_code": 308
|
"status_code": 308
|
||||||
|
|
|
@ -46,7 +46,7 @@ php_fastcgi localhost:9000 {
|
||||||
"handler": "static_response",
|
"handler": "static_response",
|
||||||
"headers": {
|
"headers": {
|
||||||
"Location": [
|
"Location": [
|
||||||
"{http.request.orig_uri.path}/"
|
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"status_code": 308
|
"status_code": 308
|
||||||
|
|
|
@ -186,6 +186,11 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
|
||||||
return path.Ext(req.URL.Path), true
|
return path.Ext(req.URL.Path), true
|
||||||
case "http.request.uri.query":
|
case "http.request.uri.query":
|
||||||
return req.URL.RawQuery, true
|
return req.URL.RawQuery, true
|
||||||
|
case "http.request.uri.prefixed_query":
|
||||||
|
if req.URL.RawQuery == "" {
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
return "?" + req.URL.RawQuery, true
|
||||||
case "http.request.duration":
|
case "http.request.duration":
|
||||||
start := GetVar(req.Context(), "start_time").(time.Time)
|
start := GetVar(req.Context(), "start_time").(time.Time)
|
||||||
return time.Since(start), true
|
return time.Since(start), true
|
||||||
|
@ -239,6 +244,12 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
|
||||||
case "http.request.orig_uri.query":
|
case "http.request.orig_uri.query":
|
||||||
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||||
return or.URL.RawQuery, true
|
return or.URL.RawQuery, true
|
||||||
|
case "http.request.orig_uri.prefixed_query":
|
||||||
|
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
|
||||||
|
if or.URL.RawQuery == "" {
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
return "?" + or.URL.RawQuery, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// remote IP range/prefix (e.g. keep top 24 bits of 1.2.3.4 => "1.2.3.0/24")
|
// remote IP range/prefix (e.g. keep top 24 bits of 1.2.3.4 => "1.2.3.0/24")
|
||||||
|
|
|
@ -343,7 +343,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
|
||||||
}
|
}
|
||||||
redirHandler := caddyhttp.StaticResponse{
|
redirHandler := caddyhttp.StaticResponse{
|
||||||
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
|
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
|
||||||
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
|
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"}},
|
||||||
}
|
}
|
||||||
redirRoute := caddyhttp.Route{
|
redirRoute := caddyhttp.Route{
|
||||||
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
|
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
|
||||||
|
|
Loading…
Reference in a new issue