Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
63f74911
Commit
63f74911
authored
Nov 03, 2016
by
Mateusz Gajewski
Committed by
Matt Holt
Nov 03, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use http.Header instead of custom type (#1214)
* Use http.Header * This initialization was just stupid
parent
e19a007b
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
37 deletions
+39
-37
caddyhttp/header/header.go
caddyhttp/header/header.go
+13
-14
caddyhttp/header/header_test.go
caddyhttp/header/header_test.go
+7
-8
caddyhttp/header/setup.go
caddyhttp/header/setup.go
+11
-9
caddyhttp/header/setup_test.go
caddyhttp/header/setup_test.go
+8
-6
No files found.
caddyhttp/header/header.go
View file @
63f74911
...
...
@@ -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
}
)
...
...
caddyhttp/header/header_test.go
View file @
63f74911
...
...
@@ -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"
},
}},
},
}
...
...
caddyhttp/header/setup.go
View file @
63f74911
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
.
V
alue
=
c
.
Val
()
v
alue
=
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
.
V
alue
=
c
.
Val
()
v
alue
=
c
.
Val
()
}
head
.
Headers
=
append
(
head
.
Headers
,
h
)
head
.
Headers
.
Add
(
name
,
value
)
}
if
isNewPattern
{
...
...
caddyhttp/header/setup_test.go
View file @
63f74911
...
...
@@ -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
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment