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
e28adf23
Commit
e28adf23
authored
Apr 28, 2018
by
comp500
Committed by
Łukasz Nowak
Aug 31, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests, fix a few bugs
parent
69d71a56
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
16 deletions
+40
-16
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+1
-1
caddyhttp/proxy/proxy_test.go
caddyhttp/proxy/proxy_test.go
+36
-12
caddyhttp/proxy/upstream.go
caddyhttp/proxy/upstream.go
+3
-3
No files found.
caddyhttp/proxy/proxy.go
View file @
e28adf23
...
...
@@ -225,7 +225,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
// headers coming back downstream
var
downHeaderUpdateFn
respUpdateFn
if
host
.
DownstreamHeaders
!=
nil
{
downHeaderUpdateFn
=
createRespHeaderUpdateFn
(
host
.
DownstreamHeaders
,
replacer
,
host
.
Up
streamHeaderReplacements
)
downHeaderUpdateFn
=
createRespHeaderUpdateFn
(
host
.
DownstreamHeaders
,
replacer
,
host
.
Down
streamHeaderReplacements
)
}
// Before we retry the request we have to make sure
...
...
caddyhttp/proxy/proxy_test.go
View file @
e28adf23
...
...
@@ -31,6 +31,7 @@ import (
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strings"
"sync"
...
...
@@ -723,6 +724,14 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
"Clear-Me"
:
{
""
},
"Host"
:
{
"{>Host}"
},
}
regex1
,
_
:=
regexp
.
Compile
(
"was originally"
)
regex2
,
_
:=
regexp
.
Compile
(
"this"
)
regex3
,
_
:=
regexp
.
Compile
(
"bad"
)
upstream
.
host
.
UpstreamHeaderReplacements
=
headerReplacements
{
"Regex-Me"
:
{
headerReplacement
{
regex1
,
"am now"
},
headerReplacement
{
regex2
,
"that"
}},
"Regexreplace-Me"
:
{
headerReplacement
{
regex3
,
"{hostname}"
}},
}
// set up proxy
p
:=
&
Proxy
{
Next
:
httpserver
.
EmptyNext
,
// prevents panic in some cases when test fails
...
...
@@ -739,18 +748,22 @@ func TestUpstreamHeadersUpdate(t *testing.T) {
r
.
Header
.
Add
(
"Remove-Me"
,
"Remove-Value"
)
r
.
Header
.
Add
(
"Replace-Me"
,
"Replace-Value"
)
r
.
Header
.
Add
(
"Host"
,
expectHost
)
r
.
Header
.
Add
(
"Regex-Me"
,
"I was originally this"
)
r
.
Header
.
Add
(
"Regexreplace-Me"
,
"The host is bad"
)
p
.
ServeHTTP
(
w
,
r
)
replacer
:=
httpserver
.
NewReplacer
(
r
,
nil
,
""
)
for
headerKey
,
expect
:=
range
map
[
string
][]
string
{
"Merge-Me"
:
{
"Initial"
,
"Merge-Value"
},
"Add-Me"
:
{
"Add-Value"
},
"Add-Empty"
:
nil
,
"Remove-Me"
:
nil
,
"Replace-Me"
:
{
replacer
.
Replace
(
"{hostname}"
)},
"Clear-Me"
:
nil
,
"Merge-Me"
:
{
"Initial"
,
"Merge-Value"
},
"Add-Me"
:
{
"Add-Value"
},
"Add-Empty"
:
nil
,
"Remove-Me"
:
nil
,
"Replace-Me"
:
{
replacer
.
Replace
(
"{hostname}"
)},
"Clear-Me"
:
nil
,
"Regex-Me"
:
{
"I am now that"
},
"Regexreplace-Me"
:
{
"The host is "
+
replacer
.
Replace
(
"{hostname}"
)},
}
{
if
got
:=
actualHeaders
[
headerKey
];
!
reflect
.
DeepEqual
(
got
,
expect
)
{
t
.
Errorf
(
"Upstream request does not contain expected %v header: expect %v, but got %v"
,
...
...
@@ -774,6 +787,8 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
w
.
Header
()
.
Add
(
"Replace-Me"
,
"Replace-Value"
)
w
.
Header
()
.
Add
(
"Content-Type"
,
"text/html"
)
w
.
Header
()
.
Add
(
"Overwrite-Me"
,
"Overwrite-Value"
)
w
.
Header
()
.
Add
(
"Regex-Me"
,
"I was originally this"
)
w
.
Header
()
.
Add
(
"Regexreplace-Me"
,
"The host is bad"
)
w
.
Write
([]
byte
(
"Hello, client"
))
}))
defer
backend
.
Close
()
...
...
@@ -785,6 +800,13 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
"-Remove-Me"
:
{
""
},
"Replace-Me"
:
{
"{hostname}"
},
}
regex1
,
_
:=
regexp
.
Compile
(
"was originally"
)
regex2
,
_
:=
regexp
.
Compile
(
"this"
)
regex3
,
_
:=
regexp
.
Compile
(
"bad"
)
upstream
.
host
.
DownstreamHeaderReplacements
=
headerReplacements
{
"Regex-Me"
:
{
headerReplacement
{
regex1
,
"am now"
},
headerReplacement
{
regex2
,
"that"
}},
"Regexreplace-Me"
:
{
headerReplacement
{
regex3
,
"{hostname}"
}},
}
// set up proxy
p
:=
&
Proxy
{
Next
:
httpserver
.
EmptyNext
,
// prevents panic in some cases when test fails
...
...
@@ -805,12 +827,14 @@ func TestDownstreamHeadersUpdate(t *testing.T) {
actualHeaders
:=
w
.
Header
()
for
headerKey
,
expect
:=
range
map
[
string
][]
string
{
"Merge-Me"
:
{
"Initial"
,
"Merge-Value"
},
"Add-Me"
:
{
"Add-Value"
},
"Remove-Me"
:
nil
,
"Replace-Me"
:
{
replacer
.
Replace
(
"{hostname}"
)},
"Content-Type"
:
{
"text/css"
},
"Overwrite-Me"
:
{
"Overwrite-Value"
},
"Merge-Me"
:
{
"Initial"
,
"Merge-Value"
},
"Add-Me"
:
{
"Add-Value"
},
"Remove-Me"
:
nil
,
"Replace-Me"
:
{
replacer
.
Replace
(
"{hostname}"
)},
"Content-Type"
:
{
"text/css"
},
"Overwrite-Me"
:
{
"Overwrite-Value"
},
"Regex-Me"
:
{
"I am now that"
},
"Regexreplace-Me"
:
{
"The host is "
+
replacer
.
Replace
(
"{hostname}"
)},
}
{
if
got
:=
actualHeaders
[
headerKey
];
!
reflect
.
DeepEqual
(
got
,
expect
)
{
t
.
Errorf
(
"Downstream response does not contain expected %s header: expect %v, but got %v"
,
...
...
caddyhttp/proxy/upstream.go
View file @
e28adf23
...
...
@@ -451,7 +451,7 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
}
u
.
upstreamHeaderReplacements
.
Add
(
header
,
headerReplacement
{
r
,
replaced
})
}
else
{
if
!
c
.
Args
(
&
header
,
&
value
)
{
if
len
(
value
)
==
0
{
// When removing a header, the value can be optional.
if
!
strings
.
HasPrefix
(
header
,
"-"
)
{
return
c
.
ArgErr
()
...
...
@@ -470,9 +470,9 @@ func parseBlock(c *caddyfile.Dispenser, u *staticUpstream, hasSrv bool) error {
if
err
!=
nil
{
return
c
.
ArgErr
()
}
u
.
up
streamHeaderReplacements
.
Add
(
header
,
headerReplacement
{
r
,
replaced
})
u
.
down
streamHeaderReplacements
.
Add
(
header
,
headerReplacement
{
r
,
replaced
})
}
else
{
if
!
c
.
Args
(
&
header
,
&
value
)
{
if
len
(
value
)
==
0
{
// When removing a header, the value can be optional.
if
!
strings
.
HasPrefix
(
header
,
"-"
)
{
return
c
.
ArgErr
()
...
...
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