Skip to content

Commit b116dce

Browse files
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
1 parent 16d5b22 commit b116dce

File tree

8 files changed

+30
-11
lines changed

8 files changed

+30
-11
lines changed

caddyconfig/httpcaddyfile/shorthands.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,27 @@ func NewShorthandReplacer() ShorthandReplacer {
5252
// be used in the Caddyfile, and the right is the replacement.
5353
func placeholderShorthands() []string {
5454
return []string{
55-
"{dir}", "{http.request.uri.path.dir}",
56-
"{file}", "{http.request.uri.path.file}",
5755
"{host}", "{http.request.host}",
5856
"{hostport}", "{http.request.hostport}",
5957
"{port}", "{http.request.port}",
58+
"{orig_method}", "{http.request.orig_method}",
59+
"{orig_uri}", "{http.request.orig_uri}",
60+
"{orig_path}", "{http.request.orig_uri.path}",
61+
"{orig_dir}", "{http.request.orig_uri.path.dir}",
62+
"{orig_file}", "{http.request.orig_uri.path.file}",
63+
"{orig_query}", "{http.request.orig_uri.query}",
64+
"{orig_?query}", "{http.request.orig_uri.prefixed_query}",
6065
"{method}", "{http.request.method}",
66+
"{uri}", "{http.request.uri}",
6167
"{path}", "{http.request.uri.path}",
68+
"{dir}", "{http.request.uri.path.dir}",
69+
"{file}", "{http.request.uri.path.file}",
6270
"{query}", "{http.request.uri.query}",
71+
"{?query}", "{http.request.uri.prefixed_query}",
6372
"{remote}", "{http.request.remote}",
6473
"{remote_host}", "{http.request.remote.host}",
6574
"{remote_port}", "{http.request.remote.port}",
6675
"{scheme}", "{http.request.scheme}",
67-
"{uri}", "{http.request.uri}",
6876
"{uuid}", "{http.request.uuid}",
6977
"{tls_cipher}", "{http.request.tls.cipher_suite}",
7078
"{tls_version}", "{http.request.tls.version}",

caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.caddyfiletest

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ route {
88
}
99
not path */
1010
}
11-
redir @canonicalPath {http.request.orig_uri.path}/ 308
11+
redir @canonicalPath {orig_path}/{orig_?query} 308
1212

1313
# If the requested file does not exist, try index files
1414
@indexFiles {
@@ -17,7 +17,7 @@ route {
1717
split_path .php
1818
}
1919
}
20-
rewrite @indexFiles {http.matchers.file.relative}
20+
rewrite @indexFiles {file_match.relative}
2121

2222
# Proxy PHP files to the FastCGI responder
2323
@phpFiles {
@@ -50,7 +50,7 @@ route {
5050
"handler": "static_response",
5151
"headers": {
5252
"Location": [
53-
"{http.request.orig_uri.path}/"
53+
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
5454
]
5555
},
5656
"status_code": 308

caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.caddyfiletest

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"handler": "static_response",
4343
"headers": {
4444
"Location": [
45-
"{http.request.orig_uri.path}/"
45+
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
4646
]
4747
},
4848
"status_code": 308

caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.caddyfiletest

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ php_fastcgi @test localhost:9000
3333
"handler": "static_response",
3434
"headers": {
3535
"Location": [
36-
"{http.request.orig_uri.path}/"
36+
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
3737
]
3838
},
3939
"status_code": 308

caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.caddyfiletest

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ php_fastcgi localhost:9000 {
4343
"handler": "static_response",
4444
"headers": {
4545
"Location": [
46-
"{http.request.orig_uri.path}/"
46+
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
4747
]
4848
},
4949
"status_code": 308

caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.caddyfiletest

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ php_fastcgi localhost:9000 {
4646
"handler": "static_response",
4747
"headers": {
4848
"Location": [
49-
"{http.request.orig_uri.path}/"
49+
"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
5050
]
5151
},
5252
"status_code": 308

modules/caddyhttp/replacer.go

+11
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
186186
return path.Ext(req.URL.Path), true
187187
case "http.request.uri.query":
188188
return req.URL.RawQuery, true
189+
case "http.request.uri.prefixed_query":
190+
if req.URL.RawQuery == "" {
191+
return "", true
192+
}
193+
return "?" + req.URL.RawQuery, true
189194
case "http.request.duration":
190195
start := GetVar(req.Context(), "start_time").(time.Time)
191196
return time.Since(start), true
@@ -239,6 +244,12 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
239244
case "http.request.orig_uri.query":
240245
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
241246
return or.URL.RawQuery, true
247+
case "http.request.orig_uri.prefixed_query":
248+
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
249+
if or.URL.RawQuery == "" {
250+
return "", true
251+
}
252+
return "?" + or.URL.RawQuery, true
242253
}
243254

244255
// remote IP range/prefix (e.g. keep top 24 bits of 1.2.3.4 => "1.2.3.0/24")

modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
343343
}
344344
redirHandler := caddyhttp.StaticResponse{
345345
StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
346-
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
346+
Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"}},
347347
}
348348
redirRoute := caddyhttp.Route{
349349
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},

0 commit comments

Comments
 (0)