diff options
author | Toby Allen <[email protected]> | 2018-02-18 21:21:06 +0000 |
---|---|---|
committer | Matt Holt <[email protected]> | 2018-02-18 14:21:06 -0700 |
commit | 3b66865da5b9264d25ef45a709ff042a07df5b54 (patch) | |
tree | f6a7d9eadb0d94ffc70383ec019a912f746dba32 /caddyhttp | |
parent | 637b0b47ee67a20c89b9c0093576ee41228ee998 (diff) | |
download | caddy-3b66865da5b9264d25ef45a709ff042a07df5b54.tar.gz caddy-3b66865da5b9264d25ef45a709ff042a07df5b54.zip |
httpserver: Placeholder for response header fields (#2029)
* Allow Response Headers in logs
* Remove log line
* remove unneeded log import
* Check if rr is nil. Added test to check
* merge if statements
* remove temp file
Diffstat (limited to 'caddyhttp')
-rw-r--r-- | caddyhttp/httpserver/replacer.go | 10 | ||||
-rw-r--r-- | caddyhttp/httpserver/replacer_test.go | 43 |
2 files changed, 53 insertions, 0 deletions
diff --git a/caddyhttp/httpserver/replacer.go b/caddyhttp/httpserver/replacer.go index 371526bf6..211c09466 100644 --- a/caddyhttp/httpserver/replacer.go +++ b/caddyhttp/httpserver/replacer.go @@ -224,6 +224,16 @@ func (r *replacer) getSubstitution(key string) string { } } } + // search response headers then + if r.responseRecorder != nil && key[1] == '<' { + want := key[2 : len(key)-1] + for key, values := range r.responseRecorder.Header() { + // Header placeholders (case-insensitive) + if strings.EqualFold(key, want) { + return strings.Join(values, ",") + } + } + } // next check for cookies if key[1] == '~' { name := key[2 : len(key)-1] diff --git a/caddyhttp/httpserver/replacer_test.go b/caddyhttp/httpserver/replacer_test.go index 654028d60..a11275248 100644 --- a/caddyhttp/httpserver/replacer_test.go +++ b/caddyhttp/httpserver/replacer_test.go @@ -67,6 +67,9 @@ func TestReplace(t *testing.T) { request.Header.Set("CustomAdd", "caddy") request.Header.Set("Cookie", "foo=bar; taste=delicious") + // add some respons headers + recordRequest.Header().Set("Custom", "CustomResponseHeader") + hostname, err := os.Hostname() if err != nil { t.Fatalf("Failed to determine hostname: %v", err) @@ -92,10 +95,13 @@ func TestReplace(t *testing.T) { {"{when_unix}", "1136214252"}, {"The Custom header is {>Custom}.", "The Custom header is foobarbaz."}, {"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."}, + {"The Custom response header is {<Custom}.", "The Custom response header is CustomResponseHeader."}, + {"Bad {>Custom placeholder", "Bad {>Custom placeholder"}, {"The request is {request}.", "The request is POST /?foo=bar HTTP/1.1\\r\\nHost: localhost\\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..."}, + {"The cUsToM response header is {<CuSTom}.", "The cUsToM response header is CustomResponseHeader."}, {"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."}, {"Bad {host placeholder...", "Bad {host placeholder..."}, {"Bad {>Custom placeholder", "Bad {>Custom placeholder"}, @@ -138,6 +144,43 @@ func TestReplace(t *testing.T) { } } +func TestResponseRecorderNil(t *testing.T) { + + reader := strings.NewReader(`{"username": "dennis"}`) + + request, err := http.NewRequest("POST", "http://localhost/?foo=bar", reader) + if err != nil { + t.Fatalf("Failed to make request: %v", err) + } + + request.Header.Set("Custom", "foobarbaz") + repl := NewReplacer(request, nil, "-") + // add some headers after creating replacer + request.Header.Set("CustomAdd", "caddy") + request.Header.Set("Cookie", "foo=bar; taste=delicious") + + old := now + now = func() time.Time { + return time.Date(2006, 1, 2, 15, 4, 5, 02, time.FixedZone("hardcoded", -7)) + } + defer func() { + now = old + }() + testCases := []struct { + template string + expect string + }{ + {"The Custom response header is {<Custom}.", "The Custom response header is -."}, + } + + for _, c := range testCases { + if expected, actual := c.expect, repl.Replace(c.template); expected != actual { + t.Errorf("for template '%s', expected '%s', got '%s'", c.template, expected, actual) + } + } + +} + func TestSet(t *testing.T) { w := httptest.NewRecorder() recordRequest := NewResponseRecorder(w) |