Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 05:45 UTC
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
Francis Lavoie committed
Dec 2, 2024 at 08:06 UTC
b116dcea3d022cd2b060a978c499ac17e5d0a2e1
8 files changed
+30
-11
caddyconfig/httpcaddyfile/shorthands.go
+11
-3
@@ -52,19 +52,27 @@ func NewShorthandReplacer() ShorthandReplacer {
52
// be used in the Caddyfile, and the right is the replacement.
53
func placeholderShorthands() []string {
54
return []string{
55
- "{dir}", "{http.request.uri.path.dir}",
56
- "{file}", "{http.request.uri.path.file}",
55
"{host}", "{http.request.host}",
56
"{hostport}", "{http.request.hostport}",
57
"{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}",
65
"{method}", "{http.request.method}",
66
+ "{uri}", "{http.request.uri}",
67
"{path}", "{http.request.uri.path}",
68
+ "{dir}", "{http.request.uri.path.dir}",
69
+ "{file}", "{http.request.uri.path.file}",
70
"{query}", "{http.request.uri.query}",
71
+ "{?query}", "{http.request.uri.prefixed_query}",
72
"{remote}", "{http.request.remote}",
73
"{remote_host}", "{http.request.remote.host}",
74
"{remote_port}", "{http.request.remote.port}",
75
"{scheme}", "{http.request.scheme}",
67
- "{uri}", "{http.request.uri}",
76
"{uuid}", "{http.request.uuid}",
77
"{tls_cipher}", "{http.request.tls.cipher_suite}",
78
"{tls_version}", "{http.request.tls.version}",
caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.caddyfiletest
+3
-3
@@ -8,7 +8,7 @@ route {
8
}
9
not path */
10
}
11
- redir @canonicalPath {http.request.orig_uri.path}/ 308
11
+ redir @canonicalPath {orig_path}/{orig_?query} 308
12
13
# If the requested file does not exist, try index files
14
@indexFiles {
@@ -17,7 +17,7 @@ route {
17
split_path .php
18
}
19
}
20
- rewrite @indexFiles {http.matchers.file.relative}
20
+ rewrite @indexFiles {file_match.relative}
21
22
# Proxy PHP files to the FastCGI responder
23
@phpFiles {
@@ -50,7 +50,7 @@ route {
50
"handler": "static_response",
51
"headers": {
52
"Location": [
53
- "{http.request.orig_uri.path}/"
53
+ "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
54
]
55
},
56
"status_code": 308
caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.caddyfiletest
+1
-1
@@ -42,7 +42,7 @@
42
"handler": "static_response",
43
"headers": {
44
"Location": [
45
- "{http.request.orig_uri.path}/"
45
+ "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
46
]
47
},
48
"status_code": 308
caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.caddyfiletest
+1
-1
@@ -33,7 +33,7 @@ php_fastcgi @test localhost:9000
33
"handler": "static_response",
34
"headers": {
35
"Location": [
36
- "{http.request.orig_uri.path}/"
36
+ "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
37
]
38
},
39
"status_code": 308
caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.caddyfiletest
+1
-1
@@ -43,7 +43,7 @@ php_fastcgi localhost:9000 {
43
"handler": "static_response",
44
"headers": {
45
"Location": [
46
- "{http.request.orig_uri.path}/"
46
+ "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
47
]
48
},
49
"status_code": 308
caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.caddyfiletest
+1
-1
@@ -46,7 +46,7 @@ php_fastcgi localhost:9000 {
46
"handler": "static_response",
47
"headers": {
48
"Location": [
49
- "{http.request.orig_uri.path}/"
49
+ "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"
50
]
51
},
52
"status_code": 308
modules/caddyhttp/replacer.go
+11
@@ -186,6 +186,11 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
186
return path.Ext(req.URL.Path), true
187
case "http.request.uri.query":
188
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
194
case "http.request.duration":
195
start := GetVar(req.Context(), "start_time").(time.Time)
196
return time.Since(start), true
@@ -239,6 +244,12 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
244
case "http.request.orig_uri.query":
245
or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request)
246
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
253
}
254
255
// 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
@@ -343,7 +343,7 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error
343
}
344
redirHandler := caddyhttp.StaticResponse{
345
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}"}},
347
}
348
redirRoute := caddyhttp.Route{
349
MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},