Skip to content

Commit a1d2849

Browse files
committed
fix(param): trim whitespace around header name/value
1 parent a7391f5 commit a1d2849

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

src/param/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Param struct {
7272
RestrictAccessDirs [][]string
7373

7474
GlobalHeaders [][2]string // [][name, value]
75-
// [][path, (name, value)...]
75+
// [][path, name1, value1, ..., nameN, valueN]
7676
HeadersUrls [][]string
7777
HeadersDirs [][]string
7878

@@ -210,10 +210,15 @@ func (param *Param) Normalize() (errs []error) {
210210
errs = append(errs, es...)
211211

212212
// headers
213+
TrimKVs(param.GlobalHeaders)
214+
213215
param.HeadersUrls, es = normalizeAllPathValues(param.HeadersUrls, false, util.NormalizeUrlPath, normalizeHeaders)
214216
errs = append(errs, es...)
217+
TrimValuesAfterKey(param.HeadersUrls)
218+
215219
param.HeadersDirs, es = normalizeAllPathValues(param.HeadersDirs, false, filepath.Abs, normalizeHeaders)
216220
errs = append(errs, es...)
221+
TrimValuesAfterKey(param.HeadersDirs)
217222

218223
// hsts & https
219224
if param.Hsts {

src/param/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ func NormalizeFsPaths(inputs []string) []string {
135135
return outputs
136136
}
137137

138+
// TrimValuesAfterKey
139+
// only trim values in [][key, value, value, ...]
140+
func TrimValuesAfterKey(inputs [][]string) {
141+
for i := range inputs {
142+
util.InPlaceTrim(inputs[i][1:])
143+
}
144+
}
145+
146+
func TrimKVs(inputs [][2]string) {
147+
for i := range inputs {
148+
util.InPlaceTrim(inputs[i][:])
149+
}
150+
}
151+
138152
func NormalizeRedirectCode(code int) int {
139153
if code <= 300 || code > 399 {
140154
return 301

src/util/str.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package util
22

33
import (
4+
"strings"
45
"unicode"
56
"unicode/utf8"
67
)
@@ -29,6 +30,12 @@ eachValue:
2930
return inputs[:endIndex]
3031
}
3132

33+
func InPlaceTrim(inputs []string) {
34+
for i := range inputs {
35+
inputs[i] = strings.TrimSpace(inputs[i])
36+
}
37+
}
38+
3239
func EscapeControllingRune(str string) []byte {
3340
runeBytes := make([]byte, 4)
3441
buf := make([]byte, 0, len(str))

src/util/str_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func TestInPlaceDedup(t *testing.T) {
2222
}
2323
}
2424

25+
func TestInPlaceTrim(t *testing.T) {
26+
inputs := []string{"a", "b", " c", "d ", "\te f \n"}
27+
InPlaceTrim(inputs)
28+
29+
if !reflect.DeepEqual(inputs, []string{"a", "b", "c", "d", "e f"}) {
30+
t.Error(inputs)
31+
}
32+
}
33+
2534
func TestReplaceControllingRune(t *testing.T) {
2635
var str string
2736
buf := make([]byte, 0, 64)

test/case/032.custom.header.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ source "$root"/lib.bash
55
"$ghfs" -l 3003 -r "$fs"/vhost1 -a :shortcut/vhost2:"$fs"/vhost2/ \
66
--global-header foo:bar \
77
--global-header 'set-cookie:name1=value1' \
8-
--global-header 'set-cookie:name2=value2' \
9-
--header '|hello|X-Hello-Name|X-Hello-Value' \
8+
--global-header ' set-cookie : name2=value2 ' \
9+
--header '|hello| X-Hello-Name | X-Hello-Value ' \
1010
--header '|/shortcut|X-Sc-Name|X-Sc-Value' \
1111
--header-dir ":$fs/vhost1/world:X-World-Name:X-World-Value" \
1212
--header-dir ":$fs/vhost2:X-Vh2-Name:X-Vh2-Value" \

0 commit comments

Comments
 (0)