Skip to content

Commit 936c10d

Browse files
committed
feat(param): add allow CORS headers for file system paths
1 parent 2d1b467 commit 936c10d

File tree

4 files changed

+31
-35
lines changed

4 files changed

+31
-35
lines changed

src/param/cli.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ func init() {
4444
err = optionSet.AddFlag("globalcors", "--global-cors", "GHFS_GLOBAL_CORS", "enable CORS headers for all directories")
4545
serverErrHandler.CheckFatal(err)
4646

47-
err = optionSet.AddFlagValues("corsurls", "--cors", "", nil, "url path that enable CORS headers for specific directories")
47+
err = optionSet.AddFlagValues("corsurls", "--cors", "", nil, "url path that enable CORS headers")
48+
serverErrHandler.CheckFatal(err)
49+
50+
err = optionSet.AddFlagValues("corsdirs", "--cors-dir", "", nil, "file system path that enable CORS headers")
4851
serverErrHandler.CheckFatal(err)
4952

5053
err = optionSet.AddFlagsValue("key", []string{"-k", "--key"}, "GHFS_KEY", "", "TLS certificate key path")
@@ -192,6 +195,10 @@ func doParseCli() []*Param {
192195
arrCorsUrls, _ := result.GetStrings("corsurls")
193196
param.CorsUrls = normalizeUrlPaths(arrCorsUrls)
194197

198+
// normalize cors dirs
199+
arrCorsDirs, _ := result.GetStrings("corsdirs")
200+
param.CorsDirs = normalizeFsPaths(arrCorsDirs)
201+
195202
// shows
196203
shows, err := getWildcardRegexp(result.GetStrings("shows"))
197204
serverErrHandler.CheckFatal(err)

src/param/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Param struct {
2323

2424
GlobalCors bool
2525
CorsUrls []string
26+
CorsDirs []string
2627

2728
Key string
2829
Cert string

src/serverHandler/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type handler struct {
2424

2525
globalCors bool
2626
corsUrls []string
27+
corsDirs []string
2728

2829
shows *regexp.Regexp
2930
showDirs *regexp.Regexp
@@ -135,6 +136,7 @@ func NewHandler(
135136

136137
globalCors: p.GlobalCors,
137138
corsUrls: p.CorsUrls,
139+
corsDirs: p.CorsDirs,
138140

139141
shows: p.Shows,
140142
showDirs: p.ShowDirs,

src/serverHandler/pageData.go

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -246,66 +246,52 @@ func getItemName(item os.FileInfo, r *http.Request) (itemName string) {
246246
return
247247
}
248248

249-
func (h *handler) getCanUpload(item os.FileInfo, rawReqPath, reqFsPath string) bool {
250-
if item == nil || !item.IsDir() {
251-
return false
252-
}
253-
254-
if h.globalUpload {
255-
return true
256-
}
257-
258-
for _, uploadUrl := range h.uploadUrls {
259-
if util.HasUrlPrefixDir(rawReqPath, uploadUrl) {
249+
func hasUrlOrDirPrefix(urls []string, reqUrl string, dirs []string, reqDir string) bool {
250+
for _, url := range urls {
251+
if util.HasUrlPrefixDir(reqUrl, url) {
260252
return true
261253
}
262254
}
263255

264-
for _, uploadDir := range h.uploadDirs {
265-
if util.HasFsPrefixDir(reqFsPath, uploadDir) {
256+
for _, dir := range dirs {
257+
if util.HasFsPrefixDir(reqDir, dir) {
266258
return true
267259
}
268260
}
269261

270262
return false
271263
}
272264

273-
func (h *handler) getCanArchive(subItems []os.FileInfo, rawReqPath, reqFsPath string) bool {
274-
if len(subItems) == 0 {
265+
func (h *handler) getCanUpload(item os.FileInfo, rawReqPath, reqFsPath string) bool {
266+
if item == nil || !item.IsDir() {
275267
return false
276268
}
277269

278-
if h.globalArchive {
270+
if h.globalUpload {
279271
return true
280272
}
281273

282-
for _, archiveUrl := range h.archiveUrls {
283-
if util.HasUrlPrefixDir(rawReqPath, archiveUrl) {
284-
return true
285-
}
274+
return hasUrlOrDirPrefix(h.uploadUrls, rawReqPath, h.uploadDirs, reqFsPath)
275+
}
276+
277+
func (h *handler) getCanArchive(subItems []os.FileInfo, rawReqPath, reqFsPath string) bool {
278+
if len(subItems) == 0 {
279+
return false
286280
}
287281

288-
for _, archiveDir := range h.archiveDirs {
289-
if util.HasFsPrefixDir(reqFsPath, archiveDir) {
290-
return true
291-
}
282+
if h.globalArchive {
283+
return true
292284
}
293285

294-
return false
286+
return hasUrlOrDirPrefix(h.archiveUrls, rawReqPath, h.archiveDirs, reqFsPath)
295287
}
296288

297-
func (h *handler) getCanCors(rawReqPath string) bool {
289+
func (h *handler) getCanCors(rawReqPath, reqFsPath string) bool {
298290
if h.globalCors {
299291
return true
300292
}
301293

302-
for _, corsUrl := range h.corsUrls {
303-
if util.HasUrlPrefixDir(rawReqPath, corsUrl) {
304-
return true
305-
}
306-
}
307-
308-
return false
294+
return hasUrlOrDirPrefix(h.corsUrls, rawReqPath, h.corsDirs, reqFsPath)
309295
}
310296

311297
func (h *handler) getPageData(r *http.Request) (data *pageData, notFound, internalError bool) {
@@ -355,7 +341,7 @@ func (h *handler) getPageData(r *http.Request) (data *pageData, notFound, intern
355341

356342
canUpload := h.getCanUpload(item, rawReqPath, reqFsPath)
357343
canArchive := h.getCanArchive(subItems, rawReqPath, reqFsPath)
358-
canCors := h.getCanCors(rawReqPath)
344+
canCors := h.getCanCors(rawReqPath, reqFsPath)
359345

360346
data = &pageData{
361347
rawReqPath: rawReqPath,

0 commit comments

Comments
 (0)