Skip to content

Commit 073d2f8

Browse files
committed
refactor(util): extract path equal function
1 parent c93949e commit 073d2f8

File tree

10 files changed

+21
-167
lines changed

10 files changed

+21
-167
lines changed

src/param/strUtil.go

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,7 @@ func splitKeyValue(input string) (sep rune, sepLen int, k, v string, ok bool) {
5353
return sep, sepLen, k, v, true
5454
}
5555

56-
func normalizePathRestrictAccessesAccurate(inputs []string, normalizePath func(string) string) map[string][]string {
57-
maps := make(map[string][]string, len(inputs))
58-
59-
for i := range inputs {
60-
reqPath, hosts, ok := splitKeyValues(inputs[i])
61-
if !ok {
62-
continue
63-
}
64-
65-
normalizedPath := normalizePath(reqPath)
66-
normalizedHosts := util.ExtractHostsFromUrls(hosts)
67-
maps[normalizedPath] = append(maps[normalizedPath], normalizedHosts...)
68-
}
69-
70-
return maps
71-
}
72-
73-
func normalizePathRestrictAccessesNoCase(inputs []string, normalizePath func(string) string) map[string][]string {
56+
func normalizePathRestrictAccesses(inputs []string, normalizePath func(string) string) map[string][]string {
7457
maps := make(map[string][]string, len(inputs))
7558

7659
for i := range inputs {
@@ -83,7 +66,7 @@ func normalizePathRestrictAccessesNoCase(inputs []string, normalizePath func(str
8366
normalizedHosts := util.ExtractHostsFromUrls(hosts)
8467

8568
for existingPath := range maps {
86-
if strings.EqualFold(existingPath, normalizedPath) {
69+
if util.IsPathEqual(existingPath, normalizedPath) {
8770
normalizedPath = existingPath
8871
break
8972
}
@@ -95,29 +78,7 @@ func normalizePathRestrictAccessesNoCase(inputs []string, normalizePath func(str
9578
return maps
9679
}
9780

98-
func normalizePathHeadersMapAccurate(inputs []string, normalizePath func(string) string) map[string][][2]string {
99-
maps := make(map[string][][2]string, len(inputs))
100-
101-
for _, input := range inputs {
102-
sep, sepLen, reqPath, header, ok := splitKeyValue(input)
103-
if !ok {
104-
continue
105-
}
106-
sepIndex := strings.IndexRune(header, sep)
107-
if sepIndex <= 0 || sepIndex+sepLen == len(header) {
108-
continue
109-
}
110-
111-
normalizedPath := normalizePath(reqPath)
112-
headerName := header[:sepIndex]
113-
headerValue := header[sepIndex+1:]
114-
maps[normalizedPath] = append(maps[normalizedPath], [2]string{headerName, headerValue})
115-
}
116-
117-
return maps
118-
}
119-
120-
func normalizePathHeadersMapNoCase(inputs []string, normalizePath func(string) string) map[string][][2]string {
81+
func normalizePathHeadersMap(inputs []string, normalizePath func(string) string) map[string][][2]string {
12182
maps := make(map[string][][2]string, len(inputs))
12283

12384
for _, input := range inputs {
@@ -135,7 +96,7 @@ func normalizePathHeadersMapNoCase(inputs []string, normalizePath func(string) s
13596
headerValue := header[sepIndex+1:]
13697

13798
for existingPath := range maps {
138-
if strings.EqualFold(existingPath, normalizedPath) {
99+
if util.IsPathEqual(existingPath, normalizedPath) {
139100
normalizedPath = existingPath
140101
break
141102
}
@@ -147,24 +108,7 @@ func normalizePathHeadersMapNoCase(inputs []string, normalizePath func(string) s
147108
return maps
148109
}
149110

150-
func normalizePathMapsAccurate(inputs []string) map[string]string {
151-
maps := make(map[string]string, len(inputs))
152-
153-
for _, input := range inputs {
154-
_, _, urlPath, fsPath, ok := splitKeyValue(input)
155-
if !ok {
156-
continue
157-
}
158-
159-
urlPath = util.CleanUrlPath(urlPath)
160-
fsPath = filepath.Clean(fsPath)
161-
maps[urlPath] = fsPath
162-
}
163-
164-
return maps
165-
}
166-
167-
func normalizePathMapsNoCase(inputs []string) map[string]string {
111+
func normalizePathMaps(inputs []string) map[string]string {
168112
maps := make(map[string]string, len(inputs))
169113

170114
for _, input := range inputs {
@@ -177,7 +121,7 @@ func normalizePathMapsNoCase(inputs []string) map[string]string {
177121
fsPath = filepath.Clean(fsPath)
178122

179123
for existingUrl := range maps {
180-
if strings.EqualFold(existingUrl, urlPath) {
124+
if util.IsPathEqual(existingUrl, urlPath) {
181125
urlPath = existingUrl
182126
break
183127
}

src/param/strUtil_test.go

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func TestSplitKeyValue(t *testing.T) {
115115
}
116116
}
117117

118-
func TestNormalizePathRestrictAccessesAccurate(t *testing.T) {
119-
results := normalizePathRestrictAccessesAccurate([]string{
118+
func TestNormalizePathRestrictAccesses(t *testing.T) {
119+
results := normalizePathRestrictAccesses([]string{
120120
":/foo:host1:host2",
121121
":/foo/:host3:host4",
122122
":/bar",
@@ -133,28 +133,10 @@ func TestNormalizePathRestrictAccessesAccurate(t *testing.T) {
133133
}
134134
}
135135

136-
func TestNormalizePathRestrictAccessesNoCase(t *testing.T) {
137-
results := normalizePathRestrictAccessesNoCase([]string{
138-
":/foo:host1:host2",
139-
":/FOO/:host3:host4",
140-
":/bar",
141-
}, path.Clean)
142-
143-
if len(results) != 2 {
144-
t.Error()
145-
}
146-
if !expectStrings(results["/foo"], "host1", "host2", "host3", "host4") {
147-
t.Error()
148-
}
149-
if len(results["/bar"]) != 0 {
150-
t.Error()
151-
}
152-
}
153-
154-
func TestNormalizePathHeadersMapAccurate(t *testing.T) {
136+
func TestNormalizePathHeadersMap(t *testing.T) {
155137
var result map[string][][2]string
156138

157-
result = normalizePathHeadersMapAccurate([]string{
139+
result = normalizePathHeadersMap([]string{
158140
":/foo:X-header1:X-Value1",
159141
":/foo/:X-header2:X-Value2",
160142
":/bar:X-header3:X-Value3",
@@ -186,50 +168,15 @@ func TestNormalizePathHeadersMapAccurate(t *testing.T) {
186168
}
187169
}
188170

189-
func TestNormalizePathHeadersMapNoCase(t *testing.T) {
190-
var result map[string][][2]string
191-
192-
result = normalizePathHeadersMapNoCase([]string{
193-
":/foo:X-header1:X-Value1",
194-
":/FOO/:X-header2:X-Value2",
195-
":/bar:X-header3:X-Value3",
196-
":baz",
197-
":baz:",
198-
":baz:X-Not-Valid",
199-
":baz:X-Not-Valid:",
200-
}, path.Clean)
201-
202-
if len(result) != 2 {
203-
t.Error(result)
204-
}
205-
206-
if len(result["/foo"]) != 2 {
207-
t.Error(result["/foo"])
208-
}
209-
if result["/foo"][0][0] != "X-header1" || result["/foo"][0][1] != "X-Value1" {
210-
t.Error(result["/foo"][0])
211-
}
212-
if result["/foo"][1][0] != "X-header2" || result["/foo"][1][1] != "X-Value2" {
213-
t.Error(result["/foo"][0])
214-
}
215-
216-
if len(result["/bar"]) != 1 {
217-
t.Error(result["/foo"])
218-
}
219-
if result["/bar"][0][0] != "X-header3" || result["/bar"][0][1] != "X-Value3" {
220-
t.Error(result["/foo"][0])
221-
}
222-
}
223-
224-
func TestNormalizePathMapsAccurate(t *testing.T) {
171+
func TestNormalizePathMaps(t *testing.T) {
225172
var maps map[string]string
226173

227-
maps = normalizePathMapsAccurate([]string{":/data/lib://usr/lib"})
174+
maps = normalizePathMaps([]string{":/data/lib://usr/lib"})
228175
if maps["/data/lib"] != "/usr/lib" {
229176
t.Error(maps)
230177
}
231178

232-
maps = normalizePathMapsAccurate([]string{":/data/lib://usr/lib", "@foo@bar/baz"})
179+
maps = normalizePathMaps([]string{":/data/lib://usr/lib", "@foo@bar/baz"})
233180
if len(maps) != 2 {
234181
t.Error(maps)
235182
}
@@ -241,22 +188,6 @@ func TestNormalizePathMapsAccurate(t *testing.T) {
241188
}
242189
}
243190

244-
func TestNormalizePathMapsNoCase(t *testing.T) {
245-
var maps map[string]string
246-
maps = normalizePathMapsNoCase([]string{":/data/lib://usr/lib"})
247-
if maps["/data/lib"] != "/usr/lib" {
248-
t.Error(maps)
249-
}
250-
251-
maps = normalizePathMapsNoCase([]string{":/data/lib://usr/lib", "#/Data/Lib#/tmp/"})
252-
if len(maps) != 1 {
253-
t.Error(maps)
254-
}
255-
if maps["/Data/Lib"] != "/tmp" {
256-
t.Error(maps)
257-
}
258-
}
259-
260191
func TestNormalizeFilenames(t *testing.T) {
261192
files := []string{"", "abc/def.txt", "hello.txt"}
262193
normalized := normalizeFilenames(files)

src/param/strUtil_unix.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/param/strUtil_windows.go

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/serverHandler/archive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func matchSelection(info os.FileInfo, selections []string) (matchName, matchPref
1818

1919
name := info.Name()
2020
for _, selName := range selections {
21-
if isNameEqual(selName, name) {
21+
if util.IsPathEqual(selName, name) {
2222
matchName = true
2323
continue
2424
}
@@ -29,7 +29,7 @@ func matchSelection(info os.FileInfo, selections []string) (matchName, matchPref
2929
}
3030

3131
selNamePart1 := selName[:slashIndex]
32-
if isNameEqual(selNamePart1, name) {
32+
if util.IsPathEqual(selNamePart1, name) {
3333
childSel := selName[slashIndex+1:]
3434
if len(childSel) > 0 {
3535
matchPrefix = true

src/serverHandler/init_unix.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33

44
package serverHandler
55

6-
import (
7-
"../util"
8-
)
9-
106
type alias = aliasAccurate
117

128
var createAlias = createAliasAccurate
13-
var isNameEqual = util.IsStrEqualAccurate
149

1510
var createRenamedFileInfo = createRenamedFileInfoAccurate
1611
var createPlaceholderFileInfo = createPlaceholderFileInfoAccurate

src/serverHandler/init_windows.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33

44
package serverHandler
55

6-
import (
7-
"../util"
8-
)
9-
106
type alias = aliasNoCase
117

128
var createAlias = createAliasNoCase
13-
var isNameEqual = util.IsStrEqualNoCase
149

1510
var createRenamedFileInfo = createRenamedFileInfoNoCase
1611
var createPlaceholderFileInfo = createPlaceholderFileInfoNoCase

src/serverHandler/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package serverHandler
22

33
import (
44
"../acceptHeaders"
5+
"../util"
56
"compress/flate"
67
"compress/gzip"
78
"io"
@@ -78,7 +79,7 @@ func isVirtual(info os.FileInfo) bool {
7879

7980
func containsItem(infos []os.FileInfo, name string) bool {
8081
for i := range infos {
81-
if isNameEqual(infos[i].Name(), name) {
82+
if util.IsPathEqual(infos[i].Name(), name) {
8283
return true
8384
}
8485
}

src/util/path_unix.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package util
55

66
import "path/filepath"
77

8+
var IsPathEqual = IsStrEqualAccurate
9+
810
var HasUrlPrefixDir = HasUrlPrefixDirAccurate
911
var HasFsPrefixDir = HasFsPrefixDirAccurate
1012

src/util/path_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"strings"
99
)
1010

11+
var IsPathEqual = IsStrEqualNoCase
12+
1113
var HasUrlPrefixDir = HasUrlPrefixDirNoCase
1214
var HasFsPrefixDir = HasFsPrefixDirNoCase
1315

0 commit comments

Comments
 (0)