Skip to content

Commit 0997d0e

Browse files
committed
refactor(serverHandler/aliasHandler): extract hierarchy availability
1 parent 5b3112b commit 0997d0e

File tree

6 files changed

+62
-99
lines changed

6 files changed

+62
-99
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,13 @@ type aliasHandler struct {
4040
dirIndexes []string
4141
aliases aliases
4242

43-
globalAuth bool
44-
authUrls []string
45-
authUrlsUsers pathIntsList
46-
authDirs []string
47-
authDirsUsers pathIntsList
48-
49-
globalIndex bool
50-
indexUrls []string
51-
indexUrlsUsers pathIntsList
52-
indexDirs []string
53-
indexDirsUsers pathIntsList
54-
55-
globalUpload bool
56-
uploadUrls []string
57-
uploadUrlsUsers pathIntsList
58-
uploadDirs []string
59-
uploadDirsUsers pathIntsList
60-
61-
globalMkdir bool
62-
mkdirUrls []string
63-
mkdirUrlsUsers pathIntsList
64-
mkdirDirs []string
65-
mkdirDirsUsers pathIntsList
66-
67-
globalDelete bool
68-
deleteUrls []string
69-
deleteUrlsUsers pathIntsList
70-
deleteDirs []string
71-
deleteDirsUsers pathIntsList
72-
73-
globalArchive bool
74-
archiveUrls []string
75-
archiveDirs []string
76-
77-
globalCors bool
78-
corsUrls []string
79-
corsDirs []string
43+
auth *hierarchyAvailability
44+
index *hierarchyAvailability
45+
upload *hierarchyAvailability
46+
mkdir *hierarchyAvailability
47+
delete *hierarchyAvailability
48+
archive *hierarchyAvailability
49+
cors *hierarchyAvailability
8050

8151
globalRestrictAccess []string
8252
restrictAccessUrls pathStringsList
@@ -217,43 +187,13 @@ func newAliasHandler(
217187
dirIndexes: p.DirIndexes,
218188
aliases: allAliases.filterSuccessor(currentAlias.url),
219189

220-
globalAuth: p.GlobalAuth || prefixMatched(p.AuthUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.AuthDirs, util.HasFsPrefixDir, currentAlias.fs),
221-
authUrls: filterSuccessor(p.AuthUrls, util.HasUrlPrefixDir, currentAlias.url),
222-
authUrlsUsers: vhostCtx.authUrlsUsers.filterSuccessor(true, util.HasUrlPrefixDir, currentAlias.url),
223-
authDirs: filterSuccessor(p.AuthDirs, util.HasFsPrefixDir, currentAlias.fs),
224-
authDirsUsers: vhostCtx.authDirsUsers.filterSuccessor(true, util.HasFsPrefixDir, currentAlias.fs),
225-
226-
globalIndex: prefixMatched(p.IndexUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.IndexDirs, util.HasFsPrefixDir, currentAlias.fs),
227-
indexUrls: filterSuccessor(p.IndexUrls, util.HasUrlPrefixDir, currentAlias.url),
228-
indexUrlsUsers: vhostCtx.indexUrlsUsers.filterSuccessor(true, util.HasUrlPrefixDir, currentAlias.url),
229-
indexDirs: filterSuccessor(p.IndexDirs, util.HasFsPrefixDir, currentAlias.fs),
230-
indexDirsUsers: vhostCtx.indexDirsUsers.filterSuccessor(true, util.HasFsPrefixDir, currentAlias.fs),
231-
232-
globalUpload: p.GlobalUpload || prefixMatched(p.UploadUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.UploadDirs, util.HasFsPrefixDir, currentAlias.fs),
233-
uploadUrls: filterSuccessor(p.UploadUrls, util.HasUrlPrefixDir, currentAlias.url),
234-
uploadUrlsUsers: vhostCtx.uploadUrlsUsers.filterSuccessor(true, util.HasUrlPrefixDir, currentAlias.url),
235-
uploadDirs: filterSuccessor(p.UploadDirs, util.HasFsPrefixDir, currentAlias.fs),
236-
uploadDirsUsers: vhostCtx.uploadDirsUsers.filterSuccessor(true, util.HasFsPrefixDir, currentAlias.fs),
237-
238-
globalMkdir: p.GlobalMkdir || prefixMatched(p.MkdirUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.MkdirDirs, util.HasFsPrefixDir, currentAlias.fs),
239-
mkdirUrls: filterSuccessor(p.MkdirUrls, util.HasUrlPrefixDir, currentAlias.url),
240-
mkdirUrlsUsers: vhostCtx.mkdirUrlsUsers.filterSuccessor(true, util.HasUrlPrefixDir, currentAlias.url),
241-
mkdirDirs: filterSuccessor(p.MkdirDirs, util.HasFsPrefixDir, currentAlias.fs),
242-
mkdirDirsUsers: vhostCtx.mkdirDirsUsers.filterSuccessor(true, util.HasFsPrefixDir, currentAlias.fs),
243-
244-
globalDelete: p.GlobalDelete || prefixMatched(p.DeleteUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.DeleteDirs, util.HasFsPrefixDir, currentAlias.fs),
245-
deleteUrls: filterSuccessor(p.DeleteUrls, util.HasUrlPrefixDir, currentAlias.url),
246-
deleteUrlsUsers: vhostCtx.deleteUrlsUsers.filterSuccessor(true, util.HasUrlPrefixDir, currentAlias.url),
247-
deleteDirs: filterSuccessor(p.DeleteDirs, util.HasFsPrefixDir, currentAlias.fs),
248-
deleteDirsUsers: vhostCtx.deleteDirsUsers.filterSuccessor(true, util.HasFsPrefixDir, currentAlias.fs),
249-
250-
globalArchive: p.GlobalArchive || prefixMatched(p.ArchiveUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.ArchiveDirs, util.HasFsPrefixDir, currentAlias.fs),
251-
archiveUrls: filterSuccessor(p.ArchiveUrls, util.HasUrlPrefixDir, currentAlias.url),
252-
archiveDirs: filterSuccessor(p.ArchiveDirs, util.HasFsPrefixDir, currentAlias.fs),
253-
254-
globalCors: p.GlobalCors || prefixMatched(p.CorsUrls, util.HasUrlPrefixDir, currentAlias.url) || prefixMatched(p.CorsDirs, util.HasFsPrefixDir, currentAlias.fs),
255-
corsUrls: filterSuccessor(p.CorsUrls, util.HasUrlPrefixDir, currentAlias.url),
256-
corsDirs: filterSuccessor(p.CorsDirs, util.HasFsPrefixDir, currentAlias.fs),
190+
auth: newHierarchyAvailability(currentAlias.url, currentAlias.fs, p.GlobalAuth, p.AuthUrls, vhostCtx.authUrlsUsers, p.AuthDirs, vhostCtx.authDirsUsers),
191+
index: newHierarchyAvailability(currentAlias.url, currentAlias.fs, false, p.IndexUrls, vhostCtx.indexUrlsUsers, p.IndexDirs, vhostCtx.indexDirsUsers),
192+
upload: newHierarchyAvailability(currentAlias.url, currentAlias.fs, p.GlobalUpload, p.UploadUrls, vhostCtx.uploadUrlsUsers, p.UploadDirs, vhostCtx.uploadDirsUsers),
193+
mkdir: newHierarchyAvailability(currentAlias.url, currentAlias.fs, p.GlobalMkdir, p.MkdirUrls, vhostCtx.mkdirUrlsUsers, p.MkdirDirs, vhostCtx.mkdirDirsUsers),
194+
delete: newHierarchyAvailability(currentAlias.url, currentAlias.fs, p.GlobalDelete, p.DeleteUrls, vhostCtx.deleteUrlsUsers, p.DeleteDirs, vhostCtx.deleteDirsUsers),
195+
archive: newHierarchyAvailability(currentAlias.url, currentAlias.fs, p.GlobalArchive, p.ArchiveUrls, nil, p.ArchiveDirs, nil),
196+
cors: newHierarchyAvailability(currentAlias.url, currentAlias.fs, p.GlobalCors, p.CorsUrls, nil, p.CorsDirs, nil),
257197

258198
globalRestrictAccess: globalRestrictAccess,
259199
restrictAccessUrls: vhostCtx.restrictAccessUrls.filterSuccessor(false, util.HasUrlPrefixDir, currentAlias.url),

src/serverHandler/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (h *aliasHandler) visitTreeNode(
129129
}
130130
}
131131

132-
func (h *aliasHandler) archive(
132+
func (h *aliasHandler) archiveFiles(
133133
w http.ResponseWriter,
134134
r *http.Request,
135135
session *sessionContext,

src/serverHandler/archiveTar.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, session *sess
6969
h.logError(err)
7070
}()
7171

72-
h.archive(
72+
h.archiveFiles(
7373
w,
7474
r,
7575
session,
@@ -109,7 +109,7 @@ func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, session *sess
109109
h.logError(err)
110110
}()
111111

112-
h.archive(
112+
h.archiveFiles(
113113
w,
114114
r,
115115
session,

src/serverHandler/archiveZip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, session *sess
5454
h.logError(err)
5555
}()
5656

57-
h.archive(
57+
h.archiveFiles(
5858
w,
5959
r,
6060
session,

src/serverHandler/auth.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ func (h *aliasHandler) needAuth(rawQuery, vhostReqPath, reqFsPath string) (needA
1414
return true, true
1515
}
1616

17-
if h.globalAuth {
17+
if h.auth.global {
1818
return true, false
1919
}
2020

21-
if hasUrlOrDirPrefix(h.authUrls, vhostReqPath, h.authDirs, reqFsPath) {
21+
if hasUrlOrDirPrefix(h.auth.urls, vhostReqPath, h.auth.dirs, reqFsPath) {
2222
return true, false
2323
}
2424

25-
if matchPath, _ := hasUrlOrDirPrefixUsers(h.authUrlsUsers, vhostReqPath, h.authDirsUsers, reqFsPath, -1); matchPath {
25+
if matchPath, _ := hasUrlOrDirPrefixUsers(h.auth.urlsUsers, vhostReqPath, h.auth.dirsUsers, reqFsPath, -1); matchPath {
2626
return true, false
2727
}
2828

@@ -38,8 +38,8 @@ func (h *aliasHandler) verifyAuth(r *http.Request, vhostReqPath, reqFsPath strin
3838

3939
if hasAuthReq {
4040
userid, username, success := h.users.Auth(inputUser, inputPass)
41-
if success && userid >= 0 && (len(h.authUrlsUsers) > 0 || len(h.authDirsUsers) > 0) {
42-
if matchPrefix, match := hasUrlOrDirPrefixUsers(h.authUrlsUsers, vhostReqPath, h.authDirsUsers, reqFsPath, userid); matchPrefix {
41+
if success && userid >= 0 && (len(h.auth.urlsUsers) > 0 || len(h.auth.dirsUsers) > 0) {
42+
if matchPrefix, match := hasUrlOrDirPrefixUsers(h.auth.urlsUsers, vhostReqPath, h.auth.dirsUsers, reqFsPath, userid); matchPrefix {
4343
success = match
4444
}
4545
}

src/serverHandler/perm.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ import (
55
"os"
66
)
77

8+
type hierarchyAvailability struct {
9+
global bool
10+
urls []string
11+
urlsUsers pathIntsList
12+
dirs []string
13+
dirsUsers pathIntsList
14+
}
15+
16+
func newHierarchyAvailability(
17+
baseUrl, baseDir string,
18+
global bool,
19+
allUrls []string, allUrlsUsers pathIntsList,
20+
allDirs []string, allDirsUsers pathIntsList,
21+
) *hierarchyAvailability {
22+
return &hierarchyAvailability{
23+
global: global || prefixMatched(allUrls, util.HasUrlPrefixDir, baseUrl) || prefixMatched(allDirs, util.HasFsPrefixDir, baseDir),
24+
urls: filterSuccessor(allUrls, util.HasUrlPrefixDir, baseUrl),
25+
urlsUsers: allUrlsUsers.filterSuccessor(true, util.HasUrlPrefixDir, baseUrl),
26+
dirs: filterSuccessor(allDirs, util.HasFsPrefixDir, baseDir),
27+
dirsUsers: allDirsUsers.filterSuccessor(true, util.HasFsPrefixDir, baseDir),
28+
}
29+
}
30+
831
func hasUrlOrDirPrefix(urls []string, reqUrl string, dirs []string, reqDir string) bool {
932
for _, url := range urls {
1033
if util.HasUrlPrefixDir(reqUrl, url) {
@@ -58,16 +81,16 @@ func hasUrlOrDirPrefixUsers(urlsUsers pathIntsList, reqUrl string, dirsUsers pat
5881
}
5982

6083
func (h *aliasHandler) getCanIndex(rawReqPath, reqFsPath string, userId int) bool {
61-
if h.globalIndex {
84+
if h.index.global {
6285
return true
6386
}
6487

65-
if hasUrlOrDirPrefix(h.indexUrls, rawReqPath, h.indexDirs, reqFsPath) {
88+
if hasUrlOrDirPrefix(h.index.urls, rawReqPath, h.index.dirs, reqFsPath) {
6689
return true
6790
}
6891

6992
if userId >= 0 {
70-
if _, match := hasUrlOrDirPrefixUsers(h.indexUrlsUsers, rawReqPath, h.indexDirsUsers, reqFsPath, userId); match {
93+
if _, match := hasUrlOrDirPrefixUsers(h.index.urlsUsers, rawReqPath, h.index.dirsUsers, reqFsPath, userId); match {
7194
return true
7295
}
7396
}
@@ -80,16 +103,16 @@ func (h *aliasHandler) getCanUpload(info os.FileInfo, rawReqPath, reqFsPath stri
80103
return false
81104
}
82105

83-
if h.globalUpload {
106+
if h.upload.global {
84107
return true
85108
}
86109

87-
if hasUrlOrDirPrefix(h.uploadUrls, rawReqPath, h.uploadDirs, reqFsPath) {
110+
if hasUrlOrDirPrefix(h.upload.urls, rawReqPath, h.upload.dirs, reqFsPath) {
88111
return true
89112
}
90113

91114
if userId >= 0 {
92-
if _, match := hasUrlOrDirPrefixUsers(h.uploadUrlsUsers, rawReqPath, h.uploadDirsUsers, reqFsPath, userId); match {
115+
if _, match := hasUrlOrDirPrefixUsers(h.upload.urlsUsers, rawReqPath, h.upload.dirsUsers, reqFsPath, userId); match {
93116
return true
94117
}
95118
}
@@ -102,16 +125,16 @@ func (h *aliasHandler) getCanMkdir(info os.FileInfo, rawReqPath, reqFsPath strin
102125
return false
103126
}
104127

105-
if h.globalMkdir {
128+
if h.mkdir.global {
106129
return true
107130
}
108131

109-
if hasUrlOrDirPrefix(h.mkdirUrls, rawReqPath, h.mkdirDirs, reqFsPath) {
132+
if hasUrlOrDirPrefix(h.mkdir.urls, rawReqPath, h.mkdir.dirs, reqFsPath) {
110133
return true
111134
}
112135

113136
if userId >= 0 {
114-
if _, match := hasUrlOrDirPrefixUsers(h.mkdirUrlsUsers, rawReqPath, h.mkdirDirsUsers, reqFsPath, userId); match {
137+
if _, match := hasUrlOrDirPrefixUsers(h.mkdir.urlsUsers, rawReqPath, h.mkdir.dirsUsers, reqFsPath, userId); match {
115138
return true
116139
}
117140
}
@@ -124,16 +147,16 @@ func (h *aliasHandler) getCanDelete(info os.FileInfo, rawReqPath, reqFsPath stri
124147
return false
125148
}
126149

127-
if h.globalDelete {
150+
if h.delete.global {
128151
return true
129152
}
130153

131-
if hasUrlOrDirPrefix(h.deleteUrls, rawReqPath, h.deleteDirs, reqFsPath) {
154+
if hasUrlOrDirPrefix(h.delete.urls, rawReqPath, h.delete.dirs, reqFsPath) {
132155
return true
133156
}
134157

135158
if userId >= 0 {
136-
if _, match := hasUrlOrDirPrefixUsers(h.deleteUrlsUsers, rawReqPath, h.deleteDirsUsers, reqFsPath, userId); match {
159+
if _, match := hasUrlOrDirPrefixUsers(h.delete.urlsUsers, rawReqPath, h.delete.dirsUsers, reqFsPath, userId); match {
137160
return true
138161
}
139162
}
@@ -146,17 +169,17 @@ func (h *aliasHandler) getCanArchive(subInfos []os.FileInfo, rawReqPath, reqFsPa
146169
return false
147170
}
148171

149-
if h.globalArchive {
172+
if h.archive.global {
150173
return true
151174
}
152175

153-
return hasUrlOrDirPrefix(h.archiveUrls, rawReqPath, h.archiveDirs, reqFsPath)
176+
return hasUrlOrDirPrefix(h.archive.urls, rawReqPath, h.archive.dirs, reqFsPath)
154177
}
155178

156179
func (h *aliasHandler) getCanCors(rawReqPath, reqFsPath string) bool {
157-
if h.globalCors {
180+
if h.cors.global {
158181
return true
159182
}
160183

161-
return hasUrlOrDirPrefix(h.corsUrls, rawReqPath, h.corsDirs, reqFsPath)
184+
return hasUrlOrDirPrefix(h.cors.urls, rawReqPath, h.cors.dirs, reqFsPath)
162185
}

0 commit comments

Comments
 (0)