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
e7c84221
Commit
e7c84221
authored
Jul 20, 2016
by
Garrett Squire
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow multiple values for an HTTP header and
add a test to ensure this works.
parent
beae16f0
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
0 deletions
+36
-0
caddyhttp/header/header.go
caddyhttp/header/header.go
+4
-0
caddyhttp/header/header_test.go
caddyhttp/header/header_test.go
+32
-0
No files found.
caddyhttp/header/header.go
View file @
e7c84221
...
...
@@ -24,8 +24,12 @@ func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
for
_
,
rule
:=
range
h
.
Rules
{
if
httpserver
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
rule
.
Path
)
{
for
_
,
header
:=
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
,
"-"
)
{
w
.
Header
()
.
Del
(
strings
.
TrimLeft
(
header
.
Name
,
"-"
))
}
else
if
strings
.
HasPrefix
(
header
.
Name
,
"+"
)
{
w
.
Header
()
.
Add
(
strings
.
TrimLeft
(
header
.
Name
,
"+"
),
replacer
.
Replace
(
header
.
Value
))
}
else
{
w
.
Header
()
.
Set
(
header
.
Name
,
replacer
.
Replace
(
header
.
Value
))
}
...
...
caddyhttp/header/header_test.go
View file @
e7c84221
...
...
@@ -4,6 +4,8 @@ import (
"net/http"
"net/http/httptest"
"os"
"reflect"
"sort"
"testing"
"github.com/mholt/caddy/caddyhttp/httpserver"
...
...
@@ -55,3 +57,33 @@ func TestHeader(t *testing.T) {
}
}
}
func
TestMultipleHeaders
(
t
*
testing
.
T
)
{
he
:=
Headers
{
Next
:
httpserver
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
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"
},
}},
},
}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/a"
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"Could not create HTTP request: %v"
,
err
)
}
rec
:=
httptest
.
NewRecorder
()
he
.
ServeHTTP
(
rec
,
req
)
desiredHeaders
:=
[]
string
{
"</css/main.css>; rel=preload"
,
"</images/image.png>; rel=preload"
}
actualHeaders
:=
rec
.
HeaderMap
[
http
.
CanonicalHeaderKey
(
"Link"
)]
sort
.
Strings
(
actualHeaders
)
if
!
reflect
.
DeepEqual
(
desiredHeaders
,
actualHeaders
)
{
t
.
Errorf
(
"Expected header to contain: %v but got: %v"
,
desiredHeaders
,
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