Skip to content

Commit

Permalink
Do the same for rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Jan 13, 2024
1 parent b04ab50 commit f7b1fe0
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
:8080

# With explicit wildcard matcher
route {
rewrite * /a
}

# With path matcher
route {
rewrite /path /b
}

# With named matcher
route {
@named method GET
rewrite @named /c
}

# With no matcher, assumed to be wildcard
route {
rewrite /d
}
----------
{
"apps": {
"http": {
"servers": {
"srv0": {
"listen": [
":8080"
],
"routes": [
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"group": "group0",
"handle": [
{
"handler": "rewrite",
"uri": "/a"
}
]
}
]
},
{
"handler": "subroute",
"routes": [
{
"group": "group1",
"handle": [
{
"handler": "rewrite",
"uri": "/b"
}
],
"match": [
{
"path": [
"/path"
]
}
]
}
]
},
{
"handler": "subroute",
"routes": [
{
"group": "group2",
"handle": [
{
"handler": "rewrite",
"uri": "/c"
}
],
"match": [
{
"method": [
"GET"
]
}
]
}
]
},
{
"handler": "subroute",
"routes": [
{
"group": "group3",
"handle": [
{
"handler": "rewrite",
"uri": "/d"
}
]
}
]
}
]
}
]
}
}
}
}
}
43 changes: 40 additions & 3 deletions modules/caddyhttp/rewrite/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func init() {
httpcaddyfile.RegisterHandlerDirective("rewrite", parseCaddyfileRewrite)
httpcaddyfile.RegisterDirective("rewrite", parseCaddyfileRewrite)
httpcaddyfile.RegisterHandlerDirective("method", parseCaddyfileMethod)
httpcaddyfile.RegisterHandlerDirective("uri", parseCaddyfileURI)
httpcaddyfile.RegisterDirective("handle_path", parseCaddyfileHandlePath)
Expand All @@ -38,7 +38,44 @@ func init() {
//
// Only URI components which are given in <to> will be set in the resulting URI.
// See the docs for the rewrite handler for more information.
func parseCaddyfileRewrite(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
func parseCaddyfileRewrite(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) {
// consume directive name
if !h.NextArg() {
return nil, h.ArgErr()
}

// count the tokens to determine what to do
argsCount := h.CountRemainingArgs()
if argsCount == 0 {
return nil, h.Errf("too few arguments; must have at least a rewrite URI")
}
if argsCount > 2 {
return nil, h.Errf("too many arguments; should only be a matcher and a URI")
}

// with only one arg, assume it's a rewrite URI with no matcher token
if argsCount == 1 {
if !h.NextArg() {
return nil, h.ArgErr()
}
return h.NewRoute(nil, Rewrite{URI: h.Val()}), nil
}

// parse the matcher token into a matcher set
userMatcherSet, err := h.ExtractMatcherSet()
if err != nil {
return nil, err
}

// consume directive name, again, because extracting matcher does a reset
if !h.NextArg() {
return nil, h.ArgErr()
}
// advance to the rewrite URI
if !h.NextArg() {
return nil, h.ArgErr()
}

var rewr Rewrite
for h.Next() {
if !h.NextArg() {
Expand All @@ -49,7 +86,7 @@ func parseCaddyfileRewrite(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler,
return nil, h.ArgErr()
}
}
return rewr, nil
return h.NewRoute(userMatcherSet, Rewrite{URI: h.Val()}), nil
}

// parseCaddyfileMethod sets up a basic method rewrite handler from Caddyfile tokens. Syntax:
Expand Down

0 comments on commit f7b1fe0

Please sign in to comment.