Skip to content

Commit a2401fb

Browse files
committed
refactor(param): decouple "alias" cli param deserialization and normalization
1 parent 500128c commit a2401fb

File tree

7 files changed

+107
-50
lines changed

7 files changed

+107
-50
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ ghfs [options]
111111
Use virtual empty directory as root directory.
112112
Useful to share alias directories only.
113113
114+
-a|--alias <separator><url-path><separator><fs-path> ...
115+
Set path alias.
116+
Mount a file system path to URL path.
117+
e.g. ":/doc:/usr/share/doc"
118+
114119
--prefix <path> ...
115120
Serve files under a specific sub url path.
116121
Could be useful if server is behind a reverse proxy and
@@ -140,11 +145,6 @@ ghfs [options]
140145
-I|--dir-index <file> ...
141146
Specify default index file for directory.
142147
143-
-a|--alias <separator><url-path><separator><fs-path> ...
144-
Set path alias.
145-
Mount a file system path to URL path.
146-
e.g. ":/doc:/usr/share/doc"
147-
148148
--global-restrict-access [<allowed-host> ...]
149149
Restrict access from third party host for all url paths, by detecting
150150
request header `Referer` or `Origin`.

README.zh-CN.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ ghfs [选项]
110110
使用空的虚拟目录作为根目录。
111111
在仅需挂载别名的情况下较实用。
112112
113+
-a|--alias <分隔符><URL路径><分隔符><文件系统路径> ...
114+
设置路径别名。
115+
将某个文件系统路径挂载到URL路径下。
116+
例如:“:/doc:/usr/share/doc”。
117+
113118
--prefix <path> ...
114119
在指定的URL子路径下提供服务。
115120
如果服务器在反向代理之后,且收到的请求并未去除代理路径前缀,可能较有用。
@@ -137,11 +142,6 @@ ghfs [选项]
137142
-I|--dir-index <文件> ...
138143
指定目录默认页面文件。
139144
140-
-a|--alias <分隔符><URL路径><分隔符><文件系统路径> ...
141-
设置路径别名。
142-
将某个文件系统路径挂载到URL路径下。
143-
例如:“:/doc:/usr/share/doc”。
144-
145145
--global-restrict-access [<允许的主机> ...]
146146
限制第三方主机对所有URL路径的访问,它是通过检测请求头中的`Referer`或`Origin`实现的。
147147
如果该请求头为空,仍然能够访问目录列表。

src/param/cli.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ func init() {
2626
err = options.AddFlags("emptyroot", []string{"-R", "--empty-root"}, "GHFS_EMPTY_ROOT", "use virtual empty root directory")
2727
serverError.CheckFatal(err)
2828

29+
err = options.AddFlagsValues("aliases", []string{"-a", "--alias"}, "", nil, "set alias path, <sep><url><sep><path>, e.g. :/doc:/usr/share/doc")
30+
serverError.CheckFatal(err)
31+
2932
err = options.AddFlagValues("prefixurls", "--prefix", "", nil, "serve files under URL path instead of /")
3033
serverError.CheckFatal(err)
3134

@@ -40,9 +43,6 @@ func init() {
4043
err = options.AddFlagsValues("dirindexes", []string{"-I", "--dir-index"}, "GHFS_DIR_INDEX", nil, "default index page for directory")
4144
serverError.CheckFatal(err)
4245

43-
err = options.AddFlagsValues("aliases", []string{"-a", "--alias"}, "", nil, "set alias path, <sep><url><sep><path>, e.g. :/doc:/usr/share/doc")
44-
serverError.CheckFatal(err)
45-
4646
err = options.AddFlagValues("globalrestrictaccess", "--global-restrict-access", "GHFS_GLOBAL_RESTRICT_ACCESS", []string{}, "restrict access to all url paths from current host, with optional extra allow list")
4747
serverError.CheckFatal(err)
4848

@@ -294,6 +294,10 @@ func ParseCli() (params []*Param, printVersion, printHelp bool, errs []error) {
294294
param.AccessLog, _ = result.GetString("accesslog")
295295
param.ErrorLog, _ = result.GetString("errorlog")
296296

297+
// aliases
298+
strAlias, _ := result.GetStrings("aliases")
299+
param.Aliases = splitAllKeyValue(strAlias)
300+
297301
// force dir slash
298302
if result.HasKey("forcedirslash") {
299303
strRedirectCode, _ := result.GetString("forcedirslash")
@@ -343,11 +347,6 @@ func ParseCli() (params []*Param, printVersion, printHelp bool, errs []error) {
343347
errs = append(errs, es...)
344348
param.Certificates = certs
345349

346-
// aliases
347-
arrAlias, _ := result.GetStrings("aliases")
348-
param.Aliases, es = normalizePathMaps(arrAlias)
349-
errs = append(errs, es...)
350-
351350
// upload/mkdir/delete/archive/cors/auth urls/dirs
352351
param.GlobalUpload = result.HasKey("globalupload")
353352
param.UploadUrls, _ = result.GetStrings("uploadurls")

src/param/main.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ type Param struct {
1616

1717
DefaultSort string
1818
DirIndexes []string
19-
Aliases map[string]string
19+
// value: [url-path, fs-path]
20+
Aliases [][2]string
2021

2122
GlobalRestrictAccess []string
2223
RestrictAccessUrls map[string][]string
@@ -83,21 +84,32 @@ type Param struct {
8384
}
8485

8586
func (param *Param) normalize() (errs []error) {
87+
var es []error
8688
var err error
8789

8890
// root
8991
param.Root, err = util.NormalizeFsPath(param.Root)
9092
errs = serverError.AppendError(errs, err)
9193

94+
// alias
95+
param.Aliases, es = normalizePathMaps(param.Aliases)
96+
errs = append(errs, es...)
97+
9298
// root & empty root && alias
93-
_, hasRootAlias := param.Aliases["/"]
94-
if hasRootAlias {
99+
rootAliasIndex := -1
100+
for i := range param.Aliases {
101+
if param.Aliases[i][0] == "/" {
102+
rootAliasIndex = i
103+
break
104+
}
105+
}
106+
if rootAliasIndex >= 0 {
95107
param.EmptyRoot = false
96108
} else if param.EmptyRoot {
97109
param.Root = os.DevNull
98-
param.Aliases["/"] = os.DevNull
110+
param.Aliases = append(param.Aliases, [2]string{"/", os.DevNull})
99111
} else {
100-
param.Aliases["/"] = param.Root
112+
param.Aliases = append(param.Aliases, [2]string{"/", param.Root})
101113
}
102114

103115
// url prefixes

src/param/strUtil.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ func splitKeyValue(input string) (sep rune, sepLen int, k, v string, ok bool) {
5252
return sep, sepLen, k, v, true
5353
}
5454

55+
func splitAllKeyValue(inputs []string) (results [][2]string) {
56+
results = make([][2]string, 0, len(inputs))
57+
for i := range inputs {
58+
_, _, k, v, ok := splitKeyValue(inputs[i])
59+
if ok {
60+
results = append(results, [2]string{k, v})
61+
}
62+
}
63+
return
64+
}
65+
5566
func normalizePathRestrictAccesses(
5667
inputs []string,
5768
normalizePath func(string) (string, error),
@@ -119,30 +130,31 @@ func normalizePathHeadersMap(
119130
return
120131
}
121132

122-
func normalizePathMaps(inputs []string) (maps map[string]string, errs []error) {
123-
maps = make(map[string]string, len(inputs))
124-
var err error
133+
func normalizePathMaps(inputs [][2]string) (results [][2]string, errs []error) {
134+
results = make([][2]string, 0, len(inputs))
125135

126-
for _, input := range inputs {
127-
_, _, urlPath, fsPath, ok := splitKeyValue(input)
128-
if !ok {
136+
eachInput:
137+
for i := range inputs {
138+
urlPath := inputs[i][0]
139+
fsPath := inputs[i][1]
140+
if len(urlPath) == 0 || len(fsPath) == 0 {
129141
continue
130142
}
131-
132143
urlPath = util.CleanUrlPath(urlPath)
133-
fsPath, err = util.NormalizeFsPath(fsPath)
144+
fsPath, err := util.NormalizeFsPath(fsPath)
134145
if err != nil {
135146
errs = append(errs, err)
147+
continue
136148
}
137149

138-
for existingUrl := range maps {
139-
if util.IsPathEqual(existingUrl, urlPath) {
140-
urlPath = existingUrl
141-
break
150+
for j := range results {
151+
if util.IsPathEqual(results[j][0], urlPath) {
152+
results[j][1] = fsPath
153+
continue eachInput
142154
}
143155
}
144156

145-
maps[urlPath] = fsPath
157+
results = append(results, [2]string{urlPath, fsPath})
146158
}
147159

148160
return

src/param/strUtil_test.go

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,19 @@ func TestSplitKeyValue(t *testing.T) {
116116
}
117117
}
118118

119+
func TestSplitAllKeyValue(t *testing.T) {
120+
results := splitAllKeyValue([]string{":foo:bar", "#lorem#ipsum"})
121+
if len(results) != 2 {
122+
t.Error(results)
123+
}
124+
if !expectStrings(results[0][:], "foo", "bar") {
125+
t.Error(results[0])
126+
}
127+
if !expectStrings(results[1][:], "lorem", "ipsum") {
128+
t.Error(results[1])
129+
}
130+
}
131+
119132
func TestNormalizePathRestrictAccesses(t *testing.T) {
120133
results, _ := normalizePathRestrictAccesses([]string{
121134
":/foo:host1:host2",
@@ -170,25 +183,46 @@ func TestNormalizePathHeadersMap(t *testing.T) {
170183
}
171184

172185
func TestNormalizePathMaps(t *testing.T) {
173-
var maps map[string]string
186+
var results [][2]string
174187
var fsPath string
175188

176-
maps, _ = normalizePathMaps([]string{":/data/lib://usr/lib"})
189+
results, _ = normalizePathMaps([][2]string{{"/data/lib", "//usr/lib"}})
190+
if len(results) != 1 {
191+
t.Error(results)
192+
}
177193
fsPath, _ = filepath.Abs("/usr/lib")
178-
if maps["/data/lib"] != fsPath {
179-
t.Error(maps)
194+
if !expectStrings(results[0][:], "/data/lib", fsPath) {
195+
t.Error(results[0])
180196
}
181197

182-
maps, _ = normalizePathMaps([]string{":/data/lib://usr/lib", "@foo@bar/baz"})
183-
if len(maps) != 2 {
184-
t.Error(maps)
198+
results, _ = normalizePathMaps([][2]string{{"/data/lib", "//usr/lib"}, {"foo", "bar/baz"}})
199+
if len(results) != 2 {
200+
t.Error(results)
201+
}
202+
fsPath, _ = filepath.Abs("/usr/lib")
203+
if !expectStrings(results[0][:], "/data/lib", fsPath) {
204+
t.Error(results[0])
205+
}
206+
fsPath, _ = filepath.Abs("bar/baz")
207+
if !expectStrings(results[1][:], "/foo", fsPath) {
208+
t.Error(results[1])
209+
}
210+
211+
results, _ = normalizePathMaps([][2]string{
212+
{"/data/lib", "//usr/lib"},
213+
{"foo", "bar/baz"},
214+
{"data/lib", "/usr/local/lib"},
215+
})
216+
if len(results) != 2 {
217+
t.Error(results)
185218
}
186-
if maps["/data/lib"] != "/usr/lib" {
187-
t.Error(maps)
219+
fsPath, _ = filepath.Abs("/usr/local/lib")
220+
if !expectStrings(results[0][:], "/data/lib", fsPath) {
221+
t.Error(results[0])
188222
}
189223
fsPath, _ = filepath.Abs("bar/baz")
190-
if maps["/foo"] != fsPath {
191-
t.Error(maps)
224+
if !expectStrings(results[1][:], "/foo", fsPath) {
225+
t.Error(results[1])
192226
}
193227
}
194228

src/serverHandler/aliases.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66

77
type aliases []alias
88

9-
func newAliases(entries map[string]string) aliases {
9+
func newAliases(entries [][2]string) aliases {
1010
aliases := make(aliases, 0, len(entries))
11-
for urlPath, fsPath := range entries {
12-
aliases = append(aliases, createAlias(urlPath, fsPath))
11+
for i := range entries {
12+
aliases = append(aliases, createAlias(entries[i][0], entries[i][1]))
1313
}
1414

1515
sort.Sort(aliases)

0 commit comments

Comments
 (0)