Skip to content

Commit a431ba6

Browse files
committed
feat(serverHandler): split sessionContext from responseData
For data that needed by handler's process but not necessary for rendering, move to sessionContext. Otherwise keep stay in responseData.
1 parent b28de2a commit a431ba6

File tree

11 files changed

+124
-118
lines changed

11 files changed

+124
-118
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,79 +103,79 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
103103
}
104104

105105
// data
106-
data, fsPath := h.getResponseData(r)
107-
h.logErrors(data.errors)
108-
if data.File != nil {
106+
session, data := h.getSessionData(r)
107+
h.logErrors(session.errors)
108+
if session.file != nil {
109109
defer func() {
110-
data.File.Close()
110+
session.file.Close()
111111
}()
112112
}
113113

114-
if h.applyMiddlewares(h.inMiddlewares, w, r, data, fsPath) {
114+
if h.applyMiddlewares(h.inMiddlewares, w, r, session, data) {
115115
return
116116
}
117117

118-
if !data.AllowAccess {
119-
if !h.applyMiddlewares(h.postMiddlewares, w, r, data, fsPath) {
118+
if !session.allowAccess {
119+
if !h.applyMiddlewares(h.postMiddlewares, w, r, session, data) {
120120
h.page(w, r, data)
121121
}
122122
return
123123
}
124124

125-
if data.NeedAuth {
125+
if session.needAuth {
126126
h.notifyAuth(w, r)
127127
}
128128

129-
if data.AuthSuccess {
130-
if data.requestAuth {
131-
h.redirectWithoutRequestAuth(w, r, data)
129+
if session.authSuccess {
130+
if session.requestAuth {
131+
h.redirectWithoutRequestAuth(w, r, session, data)
132132
return
133133
}
134134

135-
if data.redirectAction == addSlashSuffix {
136-
redirect(w, r, data.prefixReqPath+"/", h.autoDirSlash)
135+
if session.redirectAction == addSlashSuffix {
136+
redirect(w, r, session.prefixReqPath+"/", h.autoDirSlash)
137137
return
138-
} else if data.redirectAction == removeSlashSuffix {
139-
redirect(w, r, data.prefixReqPath[:len(data.prefixReqPath)-1], h.autoDirSlash)
138+
} else if session.redirectAction == removeSlashSuffix {
139+
redirect(w, r, session.prefixReqPath[:len(session.prefixReqPath)-1], h.autoDirSlash)
140140
return
141141
}
142142

143143
if data.CanCors {
144144
cors(w)
145145
}
146146

147-
header(w, data.Headers)
147+
header(w, session.headers)
148148

149149
if data.IsMutate {
150-
h.mutate(w, r, data)
150+
h.mutate(w, r, session, data)
151151
return
152152
}
153153

154154
// archive
155155
if len(r.URL.RawQuery) >= 3 {
156156
switch r.URL.RawQuery[:3] {
157157
case "tar":
158-
h.tar(w, r, data)
158+
h.tar(w, r, session, data)
159159
return
160160
case "tgz":
161-
h.tgz(w, r, data)
161+
h.tgz(w, r, session, data)
162162
return
163163
case "zip":
164-
h.zip(w, r, data)
164+
h.zip(w, r, session, data)
165165
return
166166
}
167167
}
168168
}
169169

170-
if h.applyMiddlewares(h.postMiddlewares, w, r, data, fsPath) {
170+
if h.applyMiddlewares(h.postMiddlewares, w, r, session, data) {
171171
return
172172
}
173173

174174
// final process
175-
if data.wantJson {
175+
if session.wantJson {
176176
h.json(w, r, data)
177-
} else if shouldServeAsContent(data.File, data.Item) {
178-
h.content(w, r, data)
177+
} else if shouldServeAsContent(session.file, data.Item) {
178+
h.content(w, r, session, data)
179179
} else {
180180
h.page(w, r, data)
181181
}

src/serverHandler/archive.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,20 @@ func (h *aliasHandler) visitTreeNode(
132132
func (h *aliasHandler) archive(
133133
w http.ResponseWriter,
134134
r *http.Request,
135-
pageData *responseData,
135+
session *sessionContext,
136+
data *responseData,
136137
selections []string,
137138
fileSuffix string,
138139
contentType string,
139140
cbWriteFile archiveCallback,
140141
) {
141142
var itemName string
142-
_, hasAlias := h.aliases.byUrlPath(pageData.rawReqPath)
143+
_, hasAlias := h.aliases.byUrlPath(session.vhostReqPath)
143144
if hasAlias {
144-
itemName = path.Base(pageData.rawReqPath)
145+
itemName = path.Base(session.vhostReqPath)
145146
}
146147
if len(itemName) == 0 || itemName == "/" {
147-
itemName = pageData.ItemName
148+
itemName = data.ItemName
148149
}
149150

150151
targetFilename := itemName + fileSuffix
@@ -156,10 +157,10 @@ func (h *aliasHandler) archive(
156157

157158
h.visitTreeNode(
158159
r,
159-
pageData.rawReqPath,
160-
path.Clean(h.root+pageData.handlerReqPath),
160+
session.vhostReqPath,
161+
path.Clean(h.root+session.aliasReqPath),
161162
"",
162-
pageData.Item != nil, // not empty root
163+
data.Item != nil, // not empty root
163164
selections,
164165
func(f *os.File, fInfo os.FileInfo, relPath string) error {
165166
h.logArchive(targetFilename, relPath, r)

src/serverHandler/archiveTar.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func writeTar(tw *tar.Writer, f *os.File, fInfo os.FileInfo, archivePath string)
5252
return nil
5353
}
5454

55-
func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, pageData *responseData) {
56-
if !pageData.CanArchive {
55+
func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
56+
if !data.CanArchive {
5757
w.WriteHeader(http.StatusBadRequest)
5858
return
5959
}
@@ -72,7 +72,8 @@ func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, pageData *res
7272
h.archive(
7373
w,
7474
r,
75-
pageData,
75+
session,
76+
data,
7677
selections,
7778
".tar",
7879
"application/octet-stream",
@@ -82,8 +83,8 @@ func (h *aliasHandler) tar(w http.ResponseWriter, r *http.Request, pageData *res
8283
)
8384
}
8485

85-
func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, pageData *responseData) {
86-
if !pageData.CanArchive {
86+
func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
87+
if !data.CanArchive {
8788
w.WriteHeader(http.StatusBadRequest)
8889
return
8990
}
@@ -111,7 +112,8 @@ func (h *aliasHandler) tgz(w http.ResponseWriter, r *http.Request, pageData *res
111112
h.archive(
112113
w,
113114
r,
114-
pageData,
115+
session,
116+
data,
115117
selections,
116118
".tar.gz",
117119
"application/octet-stream",

src/serverHandler/archiveZip.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func writeZip(zw *zip.Writer, f *os.File, fInfo os.FileInfo, archivePath string)
3737
return nil
3838
}
3939

40-
func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, pageData *responseData) {
41-
if !pageData.CanArchive {
40+
func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
41+
if !data.CanArchive {
4242
w.WriteHeader(http.StatusBadRequest)
4343
return
4444
}
@@ -57,7 +57,8 @@ func (h *aliasHandler) zip(w http.ResponseWriter, r *http.Request, pageData *res
5757
h.archive(
5858
w,
5959
r,
60-
pageData,
60+
session,
61+
data,
6162
selections,
6263
".zip",
6364
"application/zip",

src/serverHandler/content.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/url"
66
)
77

8-
func (h *aliasHandler) content(w http.ResponseWriter, r *http.Request, data *responseData) {
8+
func (h *aliasHandler) content(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
99
header := w.Header()
1010
header.Set("Vary", h.vary)
1111
header.Set("X-Content-Type-Options", "nosniff")
@@ -14,7 +14,7 @@ func (h *aliasHandler) content(w http.ResponseWriter, r *http.Request, data *res
1414
}
1515

1616
item := data.Item
17-
file := data.File
17+
file := session.file
1818

1919
http.ServeContent(w, r, item.Name(), item.ModTime(), file)
2020
}

src/serverHandler/json.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ func getJsonData(data *responseData) *jsonResponseData {
5656
}
5757

5858
return &jsonResponseData{
59-
NeedAuth: data.NeedAuth,
60-
AuthUserName: data.AuthUserName,
61-
AuthSuccess: data.AuthSuccess,
6259
IsRoot: data.IsRoot,
6360
Path: data.Path,
6461
Paths: data.Paths,

src/serverHandler/middleware.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ import (
55
"net/http"
66
)
77

8-
func (h *aliasHandler) applyMiddlewares(mids []middleware.Middleware, w http.ResponseWriter, r *http.Request, data *responseData, fsPath string) (processed bool) {
8+
func (h *aliasHandler) applyMiddlewares(mids []middleware.Middleware, w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) (processed bool) {
99
if len(mids) == 0 {
1010
return
1111
}
1212

1313
context := &middleware.Context{
14-
PrefixReqPath: data.prefixReqPath,
15-
VhostReqPath: data.rawReqPath,
16-
AliasReqPath: data.handlerReqPath,
17-
AliasFsPath: fsPath,
14+
PrefixReqPath: session.prefixReqPath,
15+
VhostReqPath: session.vhostReqPath,
16+
AliasReqPath: session.aliasReqPath,
17+
AliasFsPath: session.fsPath,
1818
AliasFsRoot: h.root,
1919

20-
WantJson: data.wantJson,
20+
WantJson: session.wantJson,
2121

22-
RestrictAccess: data.RestrictAccess,
23-
AllowAccess: data.AllowAccess,
22+
AllowAccess: session.allowAccess,
2423

25-
NeedAuth: data.NeedAuth,
26-
AuthUserName: data.AuthUserName,
27-
AuthSuccess: data.AuthSuccess,
24+
NeedAuth: session.needAuth,
25+
AuthUserName: session.authUserName,
26+
AuthSuccess: session.authSuccess,
2827

2928
CanUpload: &data.CanUpload,
3029
CanMkdir: &data.CanMkdir,
@@ -37,8 +36,8 @@ func (h *aliasHandler) applyMiddlewares(mids []middleware.Middleware, w http.Res
3736
Logger: h.logger,
3837
}
3938

40-
if data.File != nil {
41-
context.File = &data.File
39+
if session.file != nil {
40+
context.File = &session.file
4241
}
4342
if data.Item != nil {
4443
context.FileInfo = &data.Item

src/serverHandler/mutate.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"strings"
66
)
77

8-
func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, data *responseData) {
8+
func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
99
if r.Method != http.MethodPost {
1010
w.WriteHeader(http.StatusMethodNotAllowed)
1111
return
@@ -16,19 +16,19 @@ func (h *aliasHandler) mutate(w http.ResponseWriter, r *http.Request, data *resp
1616
switch {
1717
case data.IsUpload:
1818
if data.CanUpload {
19-
success = h.saveUploadFiles(data.AuthUserName, h.root+data.handlerReqPath, data.CanMkdir, data.CanDelete, data.AliasSubItems, r)
19+
success = h.saveUploadFiles(session.authUserName, h.root+session.aliasReqPath, data.CanMkdir, data.CanDelete, data.AliasSubItems, r)
2020
}
2121
case data.IsMkdir:
2222
if data.CanMkdir && !h.logError(r.ParseForm()) {
23-
success = h.mkdirs(data.AuthUserName, h.root+data.handlerReqPath, r.Form["name"], data.AliasSubItems, r)
23+
success = h.mkdirs(session.authUserName, h.root+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
2424
}
2525
case data.IsDelete:
2626
if data.CanDelete && !h.logError(r.ParseForm()) {
27-
success = h.deleteItems(data.AuthUserName, h.root+data.handlerReqPath, r.Form["name"], data.AliasSubItems, r)
27+
success = h.deleteItems(session.authUserName, h.root+session.aliasReqPath, r.Form["name"], data.AliasSubItems, r)
2828
}
2929
}
3030

31-
if data.wantJson {
31+
if session.wantJson {
3232
header := w.Header()
3333
header.Set("Content-Type", "application/json; charset=utf-8")
3434
header.Set("Cache-Control", "public, max-age=0")

src/serverHandler/redirect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ func redirect(w http.ResponseWriter, r *http.Request, path string, code int) {
1010
http.Redirect(w, r, target, code)
1111
}
1212

13-
func (h *aliasHandler) redirectWithoutRequestAuth(w http.ResponseWriter, r *http.Request, data *responseData) {
13+
func (h *aliasHandler) redirectWithoutRequestAuth(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
1414
returnUrl := r.Header.Get("Referer")
1515
if len(returnUrl) == 0 {
16-
returnUrl = data.prefixReqPath + data.Context.QueryString()
16+
returnUrl = session.prefixReqPath + data.Context.QueryString()
1717
}
1818

1919
http.Redirect(w, r, returnUrl, http.StatusFound)

0 commit comments

Comments
 (0)