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
5490ff20
Commit
5490ff20
authored
Apr 28, 2018
by
comp500
Committed by
Łukasz Nowak
Nov 28, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add more tests and comments
parent
7497cb26
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
0 deletions
+60
-0
caddyhttp/proxy/proxy.go
caddyhttp/proxy/proxy.go
+2
-0
caddyhttp/proxy/upstream.go
caddyhttp/proxy/upstream.go
+3
-0
caddyhttp/proxy/upstream_test.go
caddyhttp/proxy/upstream_test.go
+55
-0
No files found.
caddyhttp/proxy/proxy.go
View file @
5490ff20
...
...
@@ -401,9 +401,11 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer,
for
ruleField
,
ruleValues
:=
range
replacements
{
for
_
,
ruleValue
:=
range
ruleValues
{
// Replace variables in replacement string
replacement
:=
repl
.
Replace
(
ruleValue
.
to
)
original
:=
headers
.
Get
(
ruleField
)
if
len
(
replacement
)
>
0
&&
len
(
original
)
>
0
{
// Replace matches in original string with replacement string
replaced
:=
ruleValue
.
regexp
.
ReplaceAllString
(
original
,
replacement
)
headers
.
Set
(
ruleField
,
replaced
)
}
...
...
caddyhttp/proxy/upstream.go
View file @
5490ff20
...
...
@@ -79,11 +79,14 @@ type srvResolver interface {
LookupSRV
(
context
.
Context
,
string
,
string
,
string
)
(
string
,
[]
*
net
.
SRV
,
error
)
}
// headerReplacement stores a compiled regex matcher and a string replacer, for replacement rules
type
headerReplacement
struct
{
regexp
*
regexp
.
Regexp
to
string
}
// headerReplacements stores a mapping of canonical MIME header to headerReplacement
// Implements a subset of http.Header functions, to allow convenient addition and deletion of rules
type
headerReplacements
map
[
string
][]
headerReplacement
func
(
h
headerReplacements
)
Add
(
key
string
,
value
headerReplacement
)
{
...
...
caddyhttp/proxy/upstream_test.go
View file @
5490ff20
...
...
@@ -325,6 +325,61 @@ func TestParseBlockTransparent(t *testing.T) {
}
}
func
TestParseBlockRegex
(
t
*
testing
.
T
)
{
// tests for regex replacement of headers
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/"
,
nil
)
tests
:=
[]
struct
{
config
string
}{
// Test #1: transparent preset with replacement of Host
{
"proxy / localhost:8080 {
\n
transparent
\n
header_upstream Host (.*) NewHost
\n
}"
},
// Test #2: transparent preset with replacement of another param
{
"proxy / localhost:8080 {
\n
transparent
\n
header_upstream X-Test Tester
\n
header_upstream X-Test Test Host
\n
}"
},
// Test #3: transparent preset with multiple params
{
"proxy / localhost:8080 {
\n
transparent
\n
header_upstream X-Test Tester
\n
header_upstream X-Test Test Host
\n
header_upstream X-Test er ing
\n
}"
},
}
for
i
,
test
:=
range
tests
{
upstreams
,
err
:=
NewStaticUpstreams
(
caddyfile
.
NewDispenser
(
"Testfile"
,
strings
.
NewReader
(
test
.
config
)),
""
)
if
err
!=
nil
{
t
.
Errorf
(
"Expected no error. Got: %s"
,
err
.
Error
())
}
for
_
,
upstream
:=
range
upstreams
{
headers
:=
upstream
.
Select
(
r
)
.
UpstreamHeaderReplacements
switch
i
{
case
0
:
if
host
,
ok
:=
headers
[
"Host"
];
!
ok
||
host
[
0
]
.
to
!=
"NewHost"
{
t
.
Errorf
(
"Test %d: Incorrect Host replacement: %v"
,
i
+
1
,
host
[
0
])
}
case
1
:
if
v
,
ok
:=
headers
[
"X-Test"
];
!
ok
{
t
.
Errorf
(
"Test %d: Incorrect X-Test replacement"
,
i
+
1
)
}
else
{
if
v
[
0
]
.
to
!=
"Host"
{
t
.
Errorf
(
"Test %d: Incorrect X-Test replacement: %v"
,
i
+
1
,
v
[
0
])
}
}
case
2
:
if
v
,
ok
:=
headers
[
"X-Test"
];
!
ok
{
t
.
Errorf
(
"Test %d: Incorrect X-Test replacement"
,
i
+
1
)
}
else
{
if
v
[
0
]
.
to
!=
"Host"
{
t
.
Errorf
(
"Test %d: Incorrect X-Test replacement: %v"
,
i
+
1
,
v
[
0
])
}
if
v
[
1
]
.
to
!=
"ing"
{
t
.
Errorf
(
"Test %d: Incorrect X-Test replacement: %v"
,
i
+
1
,
v
[
1
])
}
}
default
:
t
.
Error
(
"Testing error"
)
}
}
}
}
func
TestHealthSetUp
(
t
*
testing
.
T
)
{
// tests for insecure skip verify
tests
:=
[]
struct
{
...
...
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