@marcel91200 / caddy Mirror · read-only / commits / aa6c5fde
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

httpcaddyfile: Unify strip_prefix, strip_suffix, uri_replace directives (#3157)

* rewrite: strip_prefix, strip_suffix, uri_replace -> uri (closes #3140) * Add period, to satisfy @whitestrake :) and my own OCD * Restore implied / prefix

Matt Holt committed Mar 19, 2020 at 11:51 UTC aa6c5fde07574b48593ddcbf95ca6988c2172b4a
2 files changed +52 -79
caddyconfig/httpcaddyfile/directives.go
+1 -3
@@ -33,9 +33,7 @@ var directiveOrder = []string{
33
34 "root",
35
36 - "strip_prefix",
37 - "strip_suffix",
38 - "uri_replace",
36 + "uri",
37 "try_files",
38
39 // middleware handlers that typically wrap responses
modules/caddyhttp/rewrite/caddyfile.go
+51 -76
@@ -24,9 +24,7 @@ import (
24
25 func init() {
26 httpcaddyfile.RegisterHandlerDirective("rewrite", parseCaddyfileRewrite)
27 - httpcaddyfile.RegisterHandlerDirective("strip_prefix", parseCaddyfileStripPrefix)
28 - httpcaddyfile.RegisterHandlerDirective("strip_suffix", parseCaddyfileStripSuffix)
29 - httpcaddyfile.RegisterHandlerDirective("uri_replace", parseCaddyfileURIReplace)
27 + httpcaddyfile.RegisterHandlerDirective("uri", parseCaddyfileURI)
28 }
29
30 // parseCaddyfileRewrite sets up a basic rewrite handler from Caddyfile tokens. Syntax:
@@ -49,89 +47,66 @@ func parseCaddyfileRewrite(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler,
47 return rewr, nil
48 }
49
52 -// parseCaddyfileStripPrefix sets up a handler from Caddyfile tokens. Syntax:
50 +// parseCaddyfileURI sets up a handler for manipulating (but not "rewriting") the
51 +// URI from Caddyfile tokens. Syntax:
52 //
54 -// strip_prefix [<matcher>] <prefix>
53 +// uri [<matcher>] strip_prefix|strip_suffix|replace <target> [<replacement> [<limit>]]
54 //
56 -// The request path will be stripped the given prefix.
57 -func parseCaddyfileStripPrefix(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
55 +// If strip_prefix or strip_suffix are used, then <target> will be stripped
56 +// only if it is the beginning or the end, respectively, of the URI path. If
57 +// replace is used, then <target> will be replaced with <replacement> across
58 +// the whole URI, up to <limit> times (or unlimited if unspecified).
59 +func parseCaddyfileURI(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
60 var rewr Rewrite
59 - for h.Next() {
60 - if !h.NextArg() {
61 - return nil, h.ArgErr()
62 - }
63 - rewr.StripPathPrefix = h.Val()
64 - if !strings.HasPrefix(rewr.StripPathPrefix, "/") {
65 - rewr.StripPathPrefix = "/" + rewr.StripPathPrefix
66 - }
67 - if h.NextArg() {
68 - return nil, h.ArgErr()
69 - }
70 - }
71 - return rewr, nil
72 -}
73 -
74 -// parseCaddyfileStripSuffix sets up a handler from Caddyfile tokens. Syntax:
75 -//
76 -// strip_suffix [<matcher>] <suffix>
77 -//
78 -// The request path will be stripped the given suffix.
79 -func parseCaddyfileStripSuffix(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
80 - var rewr Rewrite
81 - for h.Next() {
82 - if !h.NextArg() {
83 - return nil, h.ArgErr()
84 - }
85 - rewr.StripPathSuffix = h.Val()
86 - if h.NextArg() {
87 - return nil, h.ArgErr()
88 - }
89 - }
90 - return rewr, nil
91 -}
92 -
93 -// parseCaddyfileURIReplace sets up a handler from Caddyfile tokens. Syntax:
94 -//
95 -// uri_replace [<matcher>] <find> <replace> [<limit>]
96 -//
97 -// Substring replacements will be performed on the request URI up to the
98 -// number specified by limit, if any (default = 0, or no limit).
99 -func parseCaddyfileURIReplace(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
100 - var rewr Rewrite
101 -
102 - var repls []replacer
103 -
61 for h.Next() {
62 args := h.RemainingArgs()
106 - var find, replace, lim string
107 - switch len(args) {
108 - case 3:
109 - lim = args[2]
110 - fallthrough
111 - case 2:
112 - find = args[0]
113 - replace = args[1]
114 - default:
63 + if len(args) < 2 {
64 return nil, h.ArgErr()
65 }
66 + switch args[0] {
67 + case "strip_prefix":
68 + if len(args) > 2 {
69 + return nil, h.ArgErr()
70 + }
71 + rewr.StripPathPrefix = args[1]
72 + if !strings.HasPrefix(rewr.StripPathPrefix, "/") {
73 + rewr.StripPathPrefix = "/" + rewr.StripPathPrefix
74 + }
75 + case "strip_suffix":
76 + if len(args) > 2 {
77 + return nil, h.ArgErr()
78 + }
79 + rewr.StripPathSuffix = args[1]
80 + case "replace":
81 + var find, replace, lim string
82 + switch len(args) {
83 + case 4:
84 + lim = args[3]
85 + fallthrough
86 + case 3:
87 + find = args[1]
88 + replace = args[2]
89 + default:
90 + return nil, h.ArgErr()
91 + }
92
118 - var limInt int
119 - if lim != "" {
120 - var err error
121 - limInt, err = strconv.Atoi(lim)
122 - if err != nil {
123 - return nil, h.Errf("limit must be an integer; invalid: %v", err)
93 + var limInt int
94 + if lim != "" {
95 + var err error
96 + limInt, err = strconv.Atoi(lim)
97 + if err != nil {
98 + return nil, h.Errf("limit must be an integer; invalid: %v", err)
99 + }
100 }
125 - }
101
127 - repls = append(repls, replacer{
128 - Find: find,
129 - Replace: replace,
130 - Limit: limInt,
131 - })
102 + rewr.URISubstring = append(rewr.URISubstring, replacer{
103 + Find: find,
104 + Replace: replace,
105 + Limit: limInt,
106 + })
107 + default:
108 + return nil, h.Errf("unrecognized URI manipulation '%s'", args[0])
109 + }
110 }
133 -
134 - rewr.URISubstring = repls
135 -
111 return rewr, nil
112 }