From aaec7e469c4c849a36490f8471752b550e4a6465 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 14 Mar 2018 21:57:25 -0600 Subject: [PATCH] httpserver: Add {labelN} placeholders for parts of hostnames For example, {label1} would match "sub" in "sub.example.com" or whatever value is in the wildcard spot of "*.example.com". Useful for rewrite! --- caddyhttp/httpserver/replacer.go | 14 ++++++++++++++ caddyhttp/httpserver/replacer_test.go | 7 ++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/caddyhttp/httpserver/replacer.go b/caddyhttp/httpserver/replacer.go index 211c0946..d6b658f2 100644 --- a/caddyhttp/httpserver/replacer.go +++ b/caddyhttp/httpserver/replacer.go @@ -375,6 +375,20 @@ func (r *replacer) getSubstitution(key string) string { } elapsedDuration := time.Since(r.responseRecorder.start) return strconv.FormatInt(convertToMilliseconds(elapsedDuration), 10) + default: + // {labelN} + if strings.HasPrefix(key, "{label") { + nStr := key[6 : len(key)-1] // get the integer N in "{labelN}" + n, err := strconv.Atoi(nStr) + if err != nil || n < 1 { + return r.emptyValue + } + labels := strings.Split(r.request.Host, ".") + if n > len(labels) { + return r.emptyValue + } + return labels[n-1] + } } return r.emptyValue diff --git a/caddyhttp/httpserver/replacer_test.go b/caddyhttp/httpserver/replacer_test.go index a1127524..d1c52262 100644 --- a/caddyhttp/httpserver/replacer_test.go +++ b/caddyhttp/httpserver/replacer_test.go @@ -53,7 +53,7 @@ func TestReplace(t *testing.T) { recordRequest := NewResponseRecorder(w) reader := strings.NewReader(`{"username": "dennis"}`) - request, err := http.NewRequest("POST", "http://localhost/?foo=bar", reader) + request, err := http.NewRequest("POST", "http://localhost.local/?foo=bar", reader) if err != nil { t.Fatalf("Failed to make request: %v", err) } @@ -87,7 +87,7 @@ func TestReplace(t *testing.T) { expect string }{ {"This hostname is {hostname}", "This hostname is " + hostname}, - {"This host is {host}.", "This host is localhost."}, + {"This host is {host}.", "This host is localhost.local."}, {"This request method is {method}.", "This request method is POST."}, {"The response status is {status}.", "The response status is 200."}, {"{when}", "02/Jan/2006:15:04:05 +0000"}, @@ -97,7 +97,7 @@ func TestReplace(t *testing.T) { {"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."}, {"The Custom response header is {Custom placeholder", "Bad {>Custom placeholder"}, - {"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost\\r\\n" + + {"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost.local\\r\\n" + "Cookie: foo=bar; taste=delicious\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\n" + "Shorterval: 1\\r\\n\\r\\n."}, {"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."}, @@ -112,6 +112,7 @@ func TestReplace(t *testing.T) { {"Query string is {query}", "Query string is foo=bar"}, {"Query string value for foo is {?foo}", "Query string value for foo is bar"}, {"Missing query string argument is {?missing}", "Missing query string argument is "}, + {"Label1 is {label1} and label2 is {label2} but label 3 is {label3}. {label4}", "Label1 is localhost and label2 is local but label 3 is -. -"}, } for _, c := range testCases {