Commit 63f74911 authored by Mateusz Gajewski's avatar Mateusz Gajewski Committed by Matt Holt

Use http.Header instead of custom type (#1214)

* Use http.Header

* This initialization was just stupid
parent e19a007b
......@@ -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
}
)
......
......@@ -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: "</images/image.png>; rel=preload"},
{Name: "+Link", Value: "</css/main.css>; rel=preload"},
{Path: "/a", Headers: http.Header{
"+Link": []string{"</images/image.png>; rel=preload", "</css/main.css>; rel=preload"},
}},
},
}
......
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 {
......
......@@ -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)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment