Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 23:00 UTC
rewrite: Add `method` Caddyfile directive (#4528)
Francis Lavoie committed
Jan 18, 2022 at 14:17 UTC
bcb7a19cd3fbc680a017bc2f5aafa71ff77e87e3
3 files changed
+48
-1
caddyconfig/httpcaddyfile/directives.go
+2
-1
@@ -45,7 +45,8 @@ var directiveOrder = []string{
45
46
"redir",
47
48
- // URI manipulation
48
+ // incoming request manipulation
49
+ "method",
50
"rewrite",
51
"uri",
52
"try_files",
caddytest/integration/caddyfile_adapt/method_directive.txt
new
+27
@@ -0,0 +1,27 @@
1
+:8080 {
2
+ method FOO
3
+}
4
+----------
5
+{
6
+ "apps": {
7
+ "http": {
8
+ "servers": {
9
+ "srv0": {
10
+ "listen": [
11
+ ":8080"
12
+ ],
13
+ "routes": [
14
+ {
15
+ "handle": [
16
+ {
17
+ "handler": "rewrite",
18
+ "method": "FOO"
19
+ }
20
+ ]
21
+ }
22
+ ]
23
+ }
24
+ }
25
+ }
26
+ }
27
+}
\ No newline at end of file
modules/caddyhttp/rewrite/caddyfile.go
+19
@@ -27,6 +27,7 @@ import (
27
28
func init() {
29
httpcaddyfile.RegisterHandlerDirective("rewrite", parseCaddyfileRewrite)
30
+ httpcaddyfile.RegisterHandlerDirective("method", parseCaddyfileMethod)
31
httpcaddyfile.RegisterHandlerDirective("uri", parseCaddyfileURI)
32
httpcaddyfile.RegisterDirective("handle_path", parseCaddyfileHandlePath)
33
}
@@ -51,6 +52,24 @@ func parseCaddyfileRewrite(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler,
52
return rewr, nil
53
}
54
55
+// parseCaddyfileMethod sets up a basic method rewrite handler from Caddyfile tokens. Syntax:
56
+//
57
+// method [<matcher>] <method>
58
+//
59
+func parseCaddyfileMethod(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
60
+ var rewr Rewrite
61
+ for h.Next() {
62
+ if !h.NextArg() {
63
+ return nil, h.ArgErr()
64
+ }
65
+ rewr.Method = h.Val()
66
+ if h.NextArg() {
67
+ return nil, h.ArgErr()
68
+ }
69
+ }
70
+ return rewr, nil
71
+}
72
+
73
// parseCaddyfileURI sets up a handler for manipulating (but not "rewriting") the
74
// URI from Caddyfile tokens. Syntax:
75
//