From 63f749112be4182e4ad9ec2f9cfaa96c01c083a0 Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 3 Nov 2016 19:24:26 +0100 Subject: Use http.Header instead of custom type (#1214) * Use http.Header * This initialization was just stupid --- caddyhttp/header/header.go | 27 +++++++++++++-------------- caddyhttp/header/header_test.go | 15 +++++++-------- caddyhttp/header/setup.go | 20 +++++++++++--------- caddyhttp/header/setup_test.go | 14 ++++++++------ 4 files changed, 39 insertions(+), 37 deletions(-) (limited to 'caddyhttp') diff --git a/caddyhttp/header/header.go b/caddyhttp/header/header.go index 28f95d45f..9421ea1e0 100644 --- a/caddyhttp/header/header.go +++ b/caddyhttp/header/header.go @@ -26,15 +26,21 @@ func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) rww := &responseWriterWrapper{w: w} for _, rule := range h.Rules { if httpserver.Path(r.URL.Path).Matches(rule.Path) { - for _, header := range rule.Headers { + for name := range rule.Headers { + // One can either delete a header, add multiple values to a header, or simply // set a header. - if strings.HasPrefix(header.Name, "-") { - rww.delHeader(strings.TrimLeft(header.Name, "-")) - } else if strings.HasPrefix(header.Name, "+") { - rww.Header().Add(strings.TrimLeft(header.Name, "+"), replacer.Replace(header.Value)) + + if strings.HasPrefix(name, "-") { + rww.delHeader(strings.TrimLeft(name, "-")) + } else if strings.HasPrefix(name, "+") { + for _, value := range rule.Headers[name] { + rww.Header().Add(strings.TrimLeft(name, "+"), replacer.Replace(value)) + } } else { - rww.Header().Set(header.Name, replacer.Replace(header.Value)) + for _, value := range rule.Headers[name] { + rww.Header().Set(name, replacer.Replace(value)) + } } } } @@ -44,16 +50,9 @@ func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) type ( // Rule groups a slice of HTTP headers by a URL pattern. - // TODO: use http.Header type instead? Rule struct { Path string - Headers []Header - } - - // Header represents a single HTTP header, simply a name and value. - Header struct { - Name string - Value string + Headers http.Header } ) diff --git a/caddyhttp/header/header_test.go b/caddyhttp/header/header_test.go index 5c3b8304c..3bf67196a 100644 --- a/caddyhttp/header/header_test.go +++ b/caddyhttp/header/header_test.go @@ -37,11 +37,11 @@ func TestHeader(t *testing.T) { return 0, nil }), Rules: []Rule{ - {Path: "/a", Headers: []Header{ - {Name: "Foo", Value: "Bar"}, - {Name: "ServerName", Value: "{hostname}"}, - {Name: "-Bar"}, - {Name: "-Server"}, + {Path: "/a", Headers: http.Header{ + "Foo": []string{"Bar"}, + "ServerName": []string{"{hostname}"}, + "-Bar": []string{""}, + "-Server": []string{}, }}, }, } @@ -71,9 +71,8 @@ func TestMultipleHeaders(t *testing.T) { return 0, nil }), Rules: []Rule{ - {Path: "/a", Headers: []Header{ - {Name: "+Link", Value: "; rel=preload"}, - {Name: "+Link", Value: "; rel=preload"}, + {Path: "/a", Headers: http.Header{ + "+Link": []string{"; rel=preload", "; rel=preload"}, }}, }, } diff --git a/caddyhttp/header/setup.go b/caddyhttp/header/setup.go index 17dc8b403..18ba944b7 100644 --- a/caddyhttp/header/setup.go +++ b/caddyhttp/header/setup.go @@ -1,6 +1,8 @@ package header import ( + "net/http" + "github.com/mholt/caddy" "github.com/mholt/caddy/caddyhttp/httpserver" ) @@ -31,6 +33,7 @@ func headersParse(c *caddy.Controller) ([]Rule, error) { for c.NextLine() { var head Rule + head.Headers = http.Header{} var isNewPattern bool if !c.NextArg() { @@ -54,27 +57,26 @@ func headersParse(c *caddy.Controller) ([]Rule, error) { for c.NextBlock() { // A block of headers was opened... - - h := Header{Name: c.Val()} + name := c.Val() + value := "" if c.NextArg() { - h.Value = c.Val() + value = c.Val() } - head.Headers = append(head.Headers, h) + head.Headers.Add(name, value) } if c.NextArg() { // ... or single header was defined as an argument instead. - h := Header{Name: c.Val()} - - h.Value = c.Val() + name := c.Val() + value := c.Val() if c.NextArg() { - h.Value = c.Val() + value = c.Val() } - head.Headers = append(head.Headers, h) + head.Headers.Add(name, value) } if isNewPattern { diff --git a/caddyhttp/header/setup_test.go b/caddyhttp/header/setup_test.go index 3c544d1c3..c1591bfed 100644 --- a/caddyhttp/header/setup_test.go +++ b/caddyhttp/header/setup_test.go @@ -2,6 +2,8 @@ package header import ( "fmt" + "net/http" + "reflect" "testing" "github.com/mholt/caddy" @@ -39,15 +41,15 @@ func TestHeadersParse(t *testing.T) { }{ {`header /foo Foo "Bar Baz"`, false, []Rule{ - {Path: "/foo", Headers: []Header{ - {Name: "Foo", Value: "Bar Baz"}, + {Path: "/foo", Headers: http.Header{ + "Foo": []string{"Bar Baz"}, }}, }}, {`header /bar { Foo "Bar Baz" Baz Qux }`, false, []Rule{ - {Path: "/bar", Headers: []Header{ - {Name: "Foo", Value: "Bar Baz"}, - {Name: "Baz", Value: "Qux"}, + {Path: "/bar", Headers: http.Header{ + "Foo": []string{"Bar Baz"}, + "Baz": []string{"Qux"}, }}, }}, } @@ -77,7 +79,7 @@ func TestHeadersParse(t *testing.T) { expectedHeaders := fmt.Sprintf("%v", expectedRule.Headers) actualHeaders := fmt.Sprintf("%v", actualRule.Headers) - if actualHeaders != expectedHeaders { + if !reflect.DeepEqual(actualRule.Headers, expectedRule.Headers) { t.Errorf("Test %d, rule %d: Expected headers %s, but got %s", i, j, expectedHeaders, actualHeaders) } -- cgit v1.2.3