Skip to content

Commit

Permalink
map: Reimplement; multiple outputs; optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Oct 2, 2020
1 parent 023d702 commit 25d2b4b
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 146 deletions.
4 changes: 2 additions & 2 deletions caddyconfig/httpcaddyfile/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func parseBind(h Helper) ([]ConfigValue, error) {
// load <paths...>
// ca <acme_ca_endpoint>
// ca_root <pem_file>
// dns <provider_name>
// dns <provider_name> [...]
// on_demand
// eab <key_id> <mac_key>
// issuer <module_name> ...
// issuer <module_name> [...]
// }
//
func parseTLS(h Helper) ([]ConfigValue, error) {
Expand Down
2 changes: 1 addition & 1 deletion caddytest/caddytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func (tc *Tester) AssertResponse(req *http.Request, expectedStatusCode int, expe

body := string(bytes)

if !strings.Contains(body, expectedBody) {
if body != expectedBody {
tc.t.Errorf("requesting \"%s\" expected response body \"%s\" but got \"%s\"", req.RequestURI, expectedBody, body)
}

Expand Down
131 changes: 62 additions & 69 deletions caddytest/integration/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func TestMap(t *testing.T) {

// arrange
tester := caddytest.NewTester(t)
tester.InitServer(`{
Expand All @@ -18,25 +17,24 @@ func TestMap(t *testing.T) {
localhost:9080 {
map http.request.method dest-name {
map {http.request.method} {dest-1} {dest-2} {
default unknown
G.T get-called
POST post-called
~G.T get-called
POST post-called foobar
}
respond /version 200 {
body "hello from localhost {dest-name}"
body "hello from localhost {dest-1} {dest-2}"
}
}
`, "caddyfile")

// act and assert
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called")
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called")
tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called ")
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called foobar")
}

func TestMapRespondWithDefault(t *testing.T) {

// arrange
tester := caddytest.NewTester(t)
tester.InitServer(`{
Expand All @@ -46,9 +44,9 @@ func TestMapRespondWithDefault(t *testing.T) {
localhost:9080 {
map http.request.method dest-name {
map {http.request.method} {dest-name} {
default unknown
GET get-called
GET get-called
}
respond /version 200 {
Expand All @@ -63,80 +61,75 @@ func TestMapRespondWithDefault(t *testing.T) {
}

func TestMapAsJson(t *testing.T) {

// arrange
tester := caddytest.NewTester(t)
tester.InitServer(`{
tester.InitServer(`
{
"apps": {
"http": {
"http_port": 9080,
"https_port": 9443,
"servers": {
"srv0": {
"listen": [
":9080"
],
"routes": [
{
"handle": [
{
"handler": "subroute",
"http_port": 9080,
"https_port": 9443,
"servers": {
"srv0": {
"listen": [
":9080"
],
"routes": [
{
"handle": [
{
"handler": "map",
"source": "http.request.method",
"destination": "dest-name",
"default": "unknown",
"items": [
"handle": [
{
"expression": "GET",
"value": "get-called"
},
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "map",
"source": "{http.request.method}",
"destinations": ["dest-name"],
"defaults": ["unknown"],
"mappings": [
{
"input": "GET",
"outputs": ["get-called"]
},
{
"input": "POST",
"outputs": ["post-called"]
}
]
}
]
},
{
"handle": [
{
"body": "hello from localhost {dest-name}",
"handler": "static_response",
"status_code": 200
}
],
"match": [
{
"path": ["/version"]
}
]
}
]
}
],
"match": [
{
"expression": "POST",
"value": "post-called"
"host": ["localhost"]
}
]
}
]
},
{
"handle": [
{
"body": "hello from localhost {dest-name}",
"handler": "static_response",
"status_code": 200
}
],
"match": [
{
"path": [
"/version"
]
}
]
],
"terminal": true
}
]
}
],
"match": [
{
"host": [
"localhost"
]
}
],
"terminal": true
}
]
}
}
}
}
}
`, "json")
}`, "json")

tester.AssertGetResponse("http://localhost:9080/version", 200, "hello from localhost get-called")
tester.AssertPostResponseBody("http://localhost:9080/version", []string{}, bytes.NewBuffer([]byte{}), 200, "hello from localhost post-called")
Expand Down
5 changes: 2 additions & 3 deletions caddytest/integration/sni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestDefaultSNI(t *testing.T) {

// act and assert
// makes a request with no sni
tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a")
tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a.caddy.localhost")
}

func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) {
Expand Down Expand Up @@ -204,7 +204,6 @@ func TestDefaultSNIWithNamedHostAndExplicitIP(t *testing.T) {
}

func TestDefaultSNIWithPortMappingOnly(t *testing.T) {

// arrange
tester := caddytest.NewTester(t)
tester.InitServer(`
Expand Down Expand Up @@ -273,7 +272,7 @@ func TestDefaultSNIWithPortMappingOnly(t *testing.T) {

// act and assert
// makes a request with no sni
tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a")
tester.AssertGetResponse("https://127.0.0.1:9443/version", 200, "hello from a.caddy.localhost")
}

func TestHttpOnlyOnDomainWithSNI(t *testing.T) {
Expand Down
86 changes: 57 additions & 29 deletions modules/caddyhttp/map/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package maphandler

import (
"strings"

"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
Expand All @@ -23,49 +25,75 @@ func init() {
httpcaddyfile.RegisterHandlerDirective("map", parseCaddyfile)
}

// parseCaddyfile sets up the handler for a map from Caddyfile tokens. Syntax:
// parseCaddyfile sets up the map handler from Caddyfile tokens. Syntax:
//
// map <source> <dest> {
// [default <default>] - used if not match is found
// [<regexp> <replacement>] - regular expression to match against the source find and the matching replacement value
// ...
// map [<matcher>] <source> <destinations...> {
// [~]<input> <outputs...>
// default <defaults...>
// }
//
// The map takes a source variable and maps it into the dest variable. The mapping process
// will check the source variable for the first successful match against a list of regular expressions.
// If a successful match is found the dest variable will contain the replacement value.
// If no successful match is found and the default is specified then the dest will contain the default value.
// If the input value is prefixed with a tilde (~), then the input will be parsed as a
// regular expression.
//
// The number of outputs for each mapping must not be more than the number of destinations.
// However, for convenience, there may be fewer outputs than destinations and any missing
// outputs will be filled in implicitly.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
m := new(Handler)
var handler Handler

for h.Next() {
// first see if source and dest are configured
if h.NextArg() {
m.Source = h.Val()
if h.NextArg() {
m.Destination = h.Val()
}
// source
if !h.NextArg() {
return nil, h.ArgErr()
}
handler.Source = h.Val()

// destinations
handler.Destinations = h.RemainingArgs()
if len(handler.Destinations) == 0 {
return nil, h.Err("missing destination argument(s)")
}

// load the rules
// mappings
for h.NextBlock(0) {
expression := h.Val()
if expression == "default" {
args := h.RemainingArgs()
if len(args) != 1 {
return m, h.ArgErr()
// defaults are a special case
if h.Val() == "default" {
if len(handler.Defaults) > 0 {
return nil, h.Err("defaults already defined")
}
m.Default = args[0]
} else {
args := h.RemainingArgs()
if len(args) != 1 {
return m, h.ArgErr()
handler.Defaults = h.RemainingArgs()
for len(handler.Defaults) < len(handler.Destinations) {
handler.Defaults = append(handler.Defaults, "")
}
m.Items = append(m.Items, Item{Expression: expression, Value: args[0]})
continue
}

// every other line maps one input to one or more outputs
in := h.Val()
outs := h.RemainingArgs()

// cannot have more outputs than destinations
if len(outs) > len(handler.Destinations) {
return nil, h.Err("too many outputs")
}

// for convenience, can have fewer outputs than destinations, but the
// underlying handler won't accept that, so we fill in empty values
for len(outs) < len(handler.Destinations) {
outs = append(outs, "")
}

// create the mapping
mapping := Mapping{Outputs: outs}
if strings.HasPrefix(in, "~") {
mapping.InputRegexp = in[1:]
} else {
mapping.Input = in
}

handler.Mappings = append(handler.Mappings, mapping)
}
}

return m, nil
return handler, nil
}
Loading

0 comments on commit 25d2b4b

Please sign in to comment.