From 99a6b2db67337622d1cd07e183073dbe793a74b2 Mon Sep 17 00:00:00 2001 From: Tw Date: Wed, 28 Sep 2016 16:54:53 +0800 Subject: [PATCH] replacer: evaluate header placeholder when replacing fix issue #1137 Signed-off-by: Tw --- caddyhttp/httpserver/replacer.go | 26 +++++++++++++------------- caddyhttp/httpserver/replacer_test.go | 5 ++++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/caddyhttp/httpserver/replacer.go b/caddyhttp/httpserver/replacer.go index 72487dab..4286e77e 100644 --- a/caddyhttp/httpserver/replacer.go +++ b/caddyhttp/httpserver/replacer.go @@ -91,20 +91,13 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla io.Closer }{io.TeeReader(r.Body, rb), io.Closer(r.Body)} } - rep := &replacer{ + return &replacer{ request: r, requestBody: rb, responseRecorder: rr, customReplacements: make(map[string]string), emptyValue: emptyValue, } - - // Header placeholders (case-insensitive) - for header, values := range r.Header { - rep.customReplacements["{>"+strings.ToLower(header)+"}"] = strings.Join(values, ",") - } - - return rep } func canLogRequest(r *http.Request) bool { @@ -143,10 +136,6 @@ func (r *replacer) Replace(s string) string { // get a replacement placeholder := s[idxStart : idxEnd+1] - // Header replacements - they are case-insensitive - if placeholder[1] == '>' { - placeholder = strings.ToLower(placeholder) - } replacement := r.getSubstitution(placeholder) // append prefix + replacement @@ -197,7 +186,18 @@ func (r *replacer) getSubstitution(key string) string { return value } - // search default replacements then + // search request headers then + if key[1] == '>' { + want := key[2 : len(key)-1] + for key, values := range r.request.Header { + // Header placeholders (case-insensitive) + if strings.EqualFold(key, want) { + return strings.Join(values, ",") + } + } + } + + // search default replacements in the end switch key { case "{method}": return r.request.Method diff --git a/caddyhttp/httpserver/replacer_test.go b/caddyhttp/httpserver/replacer_test.go index 9fcff8c9..e64073f9 100644 --- a/caddyhttp/httpserver/replacer_test.go +++ b/caddyhttp/httpserver/replacer_test.go @@ -45,6 +45,8 @@ func TestReplace(t *testing.T) { request.Header.Set("Custom", "foobarbaz") request.Header.Set("ShorterVal", "1") repl := NewReplacer(request, recordRequest, "-") + // add some headers after creating replacer + request.Header.Set("CustomAdd", "caddy") hostname, err := os.Hostname() if err != nil { @@ -60,7 +62,8 @@ func TestReplace(t *testing.T) { {"This request method is {method}.", "This request method is POST."}, {"The response status is {status}.", "The response status is 200."}, {"The Custom header is {>Custom}.", "The Custom header is foobarbaz."}, - {"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\nCustom: foobarbaz\\r\\nShorterval: 1\\r\\n\\r\\n."}, + {"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."}, + {"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\nShorterval: 1\\r\\n\\r\\n."}, {"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."}, {"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."}, {"Bad {host placeholder...", "Bad {host placeholder..."},