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
b6326d40
Commit
b6326d40
authored
Dec 31, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix for case-insensitive header replacements (#476)
parent
e2a3ec4c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
24 deletions
+27
-24
middleware/replacer.go
middleware/replacer.go
+13
-21
middleware/replacer_test.go
middleware/replacer_test.go
+14
-3
No files found.
middleware/replacer.go
View file @
b6326d40
...
@@ -98,28 +98,20 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
...
@@ -98,28 +98,20 @@ func NewReplacer(r *http.Request, rr *responseRecorder, emptyValue string) Repla
// the string with the replaced values.
// the string with the replaced values.
func
(
r
replacer
)
Replace
(
s
string
)
string
{
func
(
r
replacer
)
Replace
(
s
string
)
string
{
// Header replacements - these are case-insensitive, so we can't just use strings.Replace()
// Header replacements - these are case-insensitive, so we can't just use strings.Replace()
startPos
:=
strings
.
Index
(
s
,
headerReplacer
)
for
strings
.
Contains
(
s
,
headerReplacer
)
{
for
startPos
>
-
1
{
idxStart
:=
strings
.
Index
(
s
,
headerReplacer
)
// carefully find end of placeholder
endOffset
:=
idxStart
+
len
(
headerReplacer
)
endOffset
:=
strings
.
Index
(
s
[
startPos
+
1
:
],
"}"
)
idxEnd
:=
strings
.
Index
(
s
[
endOffset
:
],
"}"
)
if
endOffset
==
-
1
{
if
idxEnd
>
-
1
{
startPos
=
strings
.
Index
(
s
[
startPos
+
len
(
headerReplacer
)
:
],
headerReplacer
)
placeholder
:=
strings
.
ToLower
(
s
[
idxStart
:
endOffset
+
idxEnd
+
1
])
continue
replacement
:=
r
.
replacements
[
placeholder
]
if
replacement
==
""
{
replacement
=
r
.
emptyValue
}
s
=
s
[
:
idxStart
]
+
replacement
+
s
[
endOffset
+
idxEnd
+
1
:
]
}
else
{
break
}
}
endPos
:=
startPos
+
len
(
headerReplacer
)
+
endOffset
// look for replacement, case-insensitive
placeholder
:=
strings
.
ToLower
(
s
[
startPos
:
endPos
])
replacement
:=
r
.
replacements
[
placeholder
]
if
replacement
==
""
{
replacement
=
r
.
emptyValue
}
// do the replacement manually
s
=
s
[
:
startPos
]
+
replacement
+
s
[
endPos
:
]
// move to next one
startPos
=
strings
.
Index
(
s
[
endOffset
:
],
headerReplacer
)
}
}
// Regular replacements - these are easier because they're case-sensitive
// Regular replacements - these are easier because they're case-sensitive
...
...
middleware/replacer_test.go
View file @
b6326d40
...
@@ -45,7 +45,8 @@ func TestReplace(t *testing.T) {
...
@@ -45,7 +45,8 @@ func TestReplace(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatal
(
"Request Formation Failed
\n
"
)
t
.
Fatal
(
"Request Formation Failed
\n
"
)
}
}
request
.
Header
.
Set
(
"Custom"
,
"fooBar"
)
request
.
Header
.
Set
(
"Custom"
,
"foobarbaz"
)
request
.
Header
.
Set
(
"ShorterVal"
,
"1"
)
repl
:=
NewReplacer
(
request
,
recordRequest
,
"-"
)
repl
:=
NewReplacer
(
request
,
recordRequest
,
"-"
)
if
expected
,
actual
:=
"This host is localhost."
,
repl
.
Replace
(
"This host is {host}."
);
expected
!=
actual
{
if
expected
,
actual
:=
"This host is localhost."
,
repl
.
Replace
(
"This host is {host}."
);
expected
!=
actual
{
...
@@ -57,12 +58,12 @@ func TestReplace(t *testing.T) {
...
@@ -57,12 +58,12 @@ func TestReplace(t *testing.T) {
if
expected
,
actual
:=
"The response status is 200."
,
repl
.
Replace
(
"The response status is {status}."
);
expected
!=
actual
{
if
expected
,
actual
:=
"The response status is 200."
,
repl
.
Replace
(
"The response status is {status}."
);
expected
!=
actual
{
t
.
Errorf
(
"{status} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
t
.
Errorf
(
"{status} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
}
}
if
expected
,
actual
:=
"The Custom header is foo
Bar
."
,
repl
.
Replace
(
"The Custom header is {>Custom}."
);
expected
!=
actual
{
if
expected
,
actual
:=
"The Custom header is foo
barbaz
."
,
repl
.
Replace
(
"The Custom header is {>Custom}."
);
expected
!=
actual
{
t
.
Errorf
(
"{>Custom} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
t
.
Errorf
(
"{>Custom} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
}
}
// Test header case-insensitivity
// Test header case-insensitivity
if
expected
,
actual
:=
"The cUsToM header is foo
Bar
..."
,
repl
.
Replace
(
"The cUsToM header is {>cUsToM}..."
);
expected
!=
actual
{
if
expected
,
actual
:=
"The cUsToM header is foo
barbaz
..."
,
repl
.
Replace
(
"The cUsToM header is {>cUsToM}..."
);
expected
!=
actual
{
t
.
Errorf
(
"{>cUsToM} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
t
.
Errorf
(
"{>cUsToM} replacement: expected '%s', got '%s'"
,
expected
,
actual
)
}
}
...
@@ -80,6 +81,16 @@ func TestReplace(t *testing.T) {
...
@@ -80,6 +81,16 @@ func TestReplace(t *testing.T) {
if
expected
,
actual
:=
"Bad {>Custom placeholder"
,
repl
.
Replace
(
"Bad {>Custom placeholder"
);
expected
!=
actual
{
if
expected
,
actual
:=
"Bad {>Custom placeholder"
,
repl
.
Replace
(
"Bad {>Custom placeholder"
);
expected
!=
actual
{
t
.
Errorf
(
"bad header placeholder: expected '%s', got '%s'"
,
expected
,
actual
)
t
.
Errorf
(
"bad header placeholder: expected '%s', got '%s'"
,
expected
,
actual
)
}
}
// Test bad header placeholder with valid one later
if
expected
,
actual
:=
"Bad -"
,
repl
.
Replace
(
"Bad {>Custom placeholder {>ShorterVal}"
);
expected
!=
actual
{
t
.
Errorf
(
"bad header placeholders: expected '%s', got '%s'"
,
expected
,
actual
)
}
// Test shorter header value with multiple placeholders
if
expected
,
actual
:=
"Short value 1 then foobarbaz."
,
repl
.
Replace
(
"Short value {>ShorterVal} then {>Custom}."
);
expected
!=
actual
{
t
.
Errorf
(
"short value: expected '%s', got '%s'"
,
expected
,
actual
)
}
}
}
func
TestSet
(
t
*
testing
.
T
)
{
func
TestSet
(
t
*
testing
.
T
)
{
...
...
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