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
a2900e46
Commit
a2900e46
authored
Nov 02, 2016
by
Tw
Committed by
Matt Holt
Nov 01, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
header: only register deletion operation (#1212)
fix issue #1183 Signed-off-by:
Tw
<
tw19881113@gmail.com
>
parent
08c17c7c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
18 deletions
+13
-18
caddyhttp/header/header.go
caddyhttp/header/header.go
+8
-17
caddyhttp/header/header_test.go
caddyhttp/header/header_test.go
+5
-1
No files found.
caddyhttp/header/header.go
View file @
a2900e46
...
@@ -32,9 +32,9 @@ func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
...
@@ -32,9 +32,9 @@ func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
if
strings
.
HasPrefix
(
header
.
Name
,
"-"
)
{
if
strings
.
HasPrefix
(
header
.
Name
,
"-"
)
{
rww
.
delHeader
(
strings
.
TrimLeft
(
header
.
Name
,
"-"
))
rww
.
delHeader
(
strings
.
TrimLeft
(
header
.
Name
,
"-"
))
}
else
if
strings
.
HasPrefix
(
header
.
Name
,
"+"
)
{
}
else
if
strings
.
HasPrefix
(
header
.
Name
,
"+"
)
{
rww
.
addHeader
(
strings
.
TrimLeft
(
header
.
Name
,
"+"
),
replacer
.
Replace
(
header
.
Value
))
rww
.
Header
()
.
Add
(
strings
.
TrimLeft
(
header
.
Name
,
"+"
),
replacer
.
Replace
(
header
.
Value
))
}
else
{
}
else
{
rww
.
setHeader
(
header
.
Name
,
replacer
.
Replace
(
header
.
Value
))
rww
.
Header
()
.
Set
(
header
.
Name
,
replacer
.
Replace
(
header
.
Value
))
}
}
}
}
}
}
...
@@ -95,24 +95,15 @@ func (rww *responseWriterWrapper) WriteHeader(status int) {
...
@@ -95,24 +95,15 @@ func (rww *responseWriterWrapper) WriteHeader(status int) {
rww
.
w
.
WriteHeader
(
status
)
rww
.
w
.
WriteHeader
(
status
)
}
}
// addHeader registers a http.Header.Add operation
// delHeader deletes the existing header according to the key
func
(
rww
*
responseWriterWrapper
)
addHeader
(
key
,
value
string
)
{
// Also it will delete that header added later.
rww
.
ops
=
append
(
rww
.
ops
,
func
(
h
http
.
Header
)
{
h
.
Add
(
key
,
value
)
})
}
// delHeader registers a http.Header.Del operation
func
(
rww
*
responseWriterWrapper
)
delHeader
(
key
string
)
{
func
(
rww
*
responseWriterWrapper
)
delHeader
(
key
string
)
{
rww
.
ops
=
append
(
rww
.
ops
,
func
(
h
http
.
Header
)
{
// remove the existing one if any
h
.
Del
(
key
)
rww
.
Header
()
.
Del
(
key
)
})
}
// setHeader registers a http.Header.Set operation
// register a future deletion
func
(
rww
*
responseWriterWrapper
)
setHeader
(
key
,
value
string
)
{
rww
.
ops
=
append
(
rww
.
ops
,
func
(
h
http
.
Header
)
{
rww
.
ops
=
append
(
rww
.
ops
,
func
(
h
http
.
Header
)
{
h
.
Set
(
key
,
value
)
h
.
Del
(
key
)
})
})
}
}
...
...
caddyhttp/header/header_test.go
View file @
a2900e46
...
@@ -25,6 +25,7 @@ func TestHeader(t *testing.T) {
...
@@ -25,6 +25,7 @@ func TestHeader(t *testing.T) {
{
"/a"
,
"Foo"
,
"Bar"
},
{
"/a"
,
"Foo"
,
"Bar"
},
{
"/a"
,
"Bar"
,
""
},
{
"/a"
,
"Bar"
,
""
},
{
"/a"
,
"Baz"
,
""
},
{
"/a"
,
"Baz"
,
""
},
{
"/a"
,
"Server"
,
""
},
{
"/a"
,
"ServerName"
,
hostname
},
{
"/a"
,
"ServerName"
,
hostname
},
{
"/b"
,
"Foo"
,
""
},
{
"/b"
,
"Foo"
,
""
},
{
"/b"
,
"Bar"
,
"Removed in /a"
},
{
"/b"
,
"Bar"
,
"Removed in /a"
},
...
@@ -32,7 +33,7 @@ func TestHeader(t *testing.T) {
...
@@ -32,7 +33,7 @@ func TestHeader(t *testing.T) {
he
:=
Headers
{
he
:=
Headers
{
Next
:
httpserver
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
Next
:
httpserver
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
w
.
Header
()
.
Set
(
"Bar"
,
"Removed in /a"
)
w
.
Header
()
.
Set
(
"Bar"
,
"Removed in /a"
)
fmt
.
Fprint
(
w
,
"This is a test"
)
w
.
WriteHeader
(
http
.
StatusOK
)
return
0
,
nil
return
0
,
nil
}),
}),
Rules
:
[]
Rule
{
Rules
:
[]
Rule
{
...
@@ -40,6 +41,7 @@ func TestHeader(t *testing.T) {
...
@@ -40,6 +41,7 @@ func TestHeader(t *testing.T) {
{
Name
:
"Foo"
,
Value
:
"Bar"
},
{
Name
:
"Foo"
,
Value
:
"Bar"
},
{
Name
:
"ServerName"
,
Value
:
"{hostname}"
},
{
Name
:
"ServerName"
,
Value
:
"{hostname}"
},
{
Name
:
"-Bar"
},
{
Name
:
"-Bar"
},
{
Name
:
"-Server"
},
}},
}},
},
},
}
}
...
@@ -50,6 +52,8 @@ func TestHeader(t *testing.T) {
...
@@ -50,6 +52,8 @@ func TestHeader(t *testing.T) {
}
}
rec
:=
httptest
.
NewRecorder
()
rec
:=
httptest
.
NewRecorder
()
// preset header
rec
.
Header
()
.
Set
(
"Server"
,
"Caddy"
)
he
.
ServeHTTP
(
rec
,
req
)
he
.
ServeHTTP
(
rec
,
req
)
...
...
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