Skip to content

Commit 920b1bf

Browse files
committed
fix: fix: ignore request path name case on windows platform
1 parent 72c3e16 commit 920b1bf

File tree

6 files changed

+40
-37
lines changed

6 files changed

+40
-37
lines changed

src/param/cli.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package param
33
import (
44
"../goNixArgParser"
55
"../serverErrHandler"
6+
"../util"
67
"errors"
78
"fmt"
89
"io/ioutil"
@@ -193,7 +194,7 @@ func doParseCli() []*Param {
193194

194195
// root
195196
root, _ := result.GetString("root")
196-
root, _ = normalizeFsPath(root)
197+
root, _ = util.NormalizeFsPath(root)
197198
param.Root = root
198199

199200
// certificate

src/param/util.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package param
33
import (
44
"../util"
55
"path"
6-
"path/filepath"
76
"strings"
87
"unicode/utf8"
98
)
@@ -58,37 +57,6 @@ func normalizeUrlPaths(inputs []string) []string {
5857
return outputs
5958
}
6059

61-
func asciiToLowerCase(input string) string {
62-
buffer := []byte(input)
63-
length := len(buffer)
64-
65-
for i := 0; i < length; {
66-
r, w := utf8.DecodeRune(buffer[i:])
67-
if w == 1 && r >= 'A' && r <= 'Z' {
68-
buffer[i] += 'a' - 'A'
69-
}
70-
71-
i += w
72-
}
73-
74-
return string(buffer)
75-
}
76-
77-
func normalizeFsPath(input string) (string, error) {
78-
abs, err := filepath.Abs(input)
79-
if err != nil {
80-
return abs, err
81-
}
82-
83-
volume := filepath.VolumeName(abs)
84-
if len(volume) > 0 {
85-
// suppose on windows platform, ignore ascii case in path name
86-
abs = asciiToLowerCase(abs)
87-
}
88-
89-
return abs, err
90-
}
91-
9260
func normalizeFsPaths(inputs []string) []string {
9361
outputs := make([]string, 0, len(inputs))
9462

@@ -97,7 +65,7 @@ func normalizeFsPaths(inputs []string) []string {
9765
continue
9866
}
9967

100-
abs, err := normalizeFsPath(input)
68+
abs, err := util.NormalizeFsPath(input)
10169
if err != nil {
10270
continue
10371
}

src/serverHandler/responseData.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
292292
rootRelPath = "./"
293293
}
294294

295-
reqFsPath, _absErr := filepath.Abs(h.root + reqPath)
295+
reqFsPath, _absErr := util.NormalizeFsPath(h.root + reqPath)
296296
if _absErr != nil {
297297
reqFsPath = filepath.Clean(h.root + reqPath)
298298
}

src/util/ascii.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package util
2+
3+
import "unicode/utf8"
4+
5+
func AsciiToLowerCase(input string) string {
6+
buffer := []byte(input)
7+
length := len(buffer)
8+
9+
for i := 0; i < length; {
10+
r, w := utf8.DecodeRune(buffer[i:])
11+
if w == 1 && r >= 'A' && r <= 'Z' {
12+
buffer[i] += 'a' - 'A'
13+
}
14+
15+
i += w
16+
}
17+
18+
return string(buffer)
19+
}

src/param/util_test.go renamed to src/util/ascii_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package param
1+
package util
22

33
import "testing"
44

55
func TestAsciiToLowerCase(t *testing.T) {
66
str := "Hello, 你好"
7-
lower := asciiToLowerCase(str)
7+
lower := AsciiToLowerCase(str)
88
expect := "hello, 你好"
99
if lower != expect {
1010
t.Error(lower)

src/util/path.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,18 @@ func HasFsPrefixDir(fsPath, prefix string) bool {
4242

4343
return strings.HasPrefix(fsPath, prefix)
4444
}
45+
46+
func NormalizeFsPath(input string) (string, error) {
47+
abs, err := filepath.Abs(input)
48+
if err != nil {
49+
return abs, err
50+
}
51+
52+
volume := filepath.VolumeName(abs)
53+
if len(volume) > 0 {
54+
// suppose on windows platform, ignore ascii case in path name
55+
abs = AsciiToLowerCase(abs)
56+
}
57+
58+
return abs, err
59+
}

0 commit comments

Comments
 (0)