Skip to content

Commit

Permalink
refactor(serverHandler): simplify vhostContext
Browse files Browse the repository at this point in the history
  • Loading branch information
marjune163 committed May 12, 2024
1 parent faf4fb0 commit caa3ecf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 52 deletions.
6 changes: 1 addition & 5 deletions src/serverHandler/aliasHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ type aliasHandler struct {
headersUrls pathHeadersList
headersDirs pathHeadersList

vary string

postMiddlewares []middleware.Middleware
}

Expand Down Expand Up @@ -177,7 +175,7 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// final process
if session.wantJson {
h.json(w, r, data)
h.json(w, r, session, data)
} else if shouldServeAsContent(session.file, data.Item) {
h.content(w, r, session, data)
} else {
Expand Down Expand Up @@ -272,8 +270,6 @@ func newAliasHandler(
hideDirs: vhostCtx.hideDirs,
hideFiles: vhostCtx.hideFiles,

vary: vhostCtx.vary,

postMiddlewares: p.PostMiddlewares,
}
return h
Expand Down
4 changes: 2 additions & 2 deletions src/serverHandler/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func getJsonData(data *responseData) *jsonResponseData {
}
}

func (h *aliasHandler) json(w http.ResponseWriter, r *http.Request, data *responseData) {
func (h *aliasHandler) json(w http.ResponseWriter, r *http.Request, session *sessionContext, data *responseData) {
header := w.Header()
header.Set("Vary", h.vary)
header.Set("Vary", session.vary)
header.Set("X-Content-Type-Options", "nosniff")
header.Set("Content-Type", "application/json; charset=utf-8")
if lacksHeader(header, "Cache-Control") {
Expand Down
2 changes: 1 addition & 1 deletion src/serverHandler/sessionData.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (h *aliasHandler) getSessionData(r *http.Request) (session *sessionContext,
}

restrictAccess, allowAccess := h.isAllowAccess(r, vhostReqPath, fsPath, file, item)
vary := h.vary
vary := "accept-encoding"
if restrictAccess {
vary += ", referer, origin"
}
Expand Down
65 changes: 21 additions & 44 deletions src/serverHandler/vhostHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,10 @@ import (
)

type vhostContext struct {
users *user.List
theme theme.Theme
logger *serverLog.Logger
theme theme.Theme

shows *regexp.Regexp
showDirs *regexp.Regexp
showFiles *regexp.Regexp
hides *regexp.Regexp
hideDirs *regexp.Regexp
hideFiles *regexp.Regexp

users *user.List
authUrlsUsers pathIntsList
authDirsUsers pathIntsList
indexUrlsUsers pathIntsList
Expand All @@ -33,13 +26,18 @@ type vhostContext struct {
deleteUrlsUsers pathIntsList
deleteDirsUsers pathIntsList

shows *regexp.Regexp
showDirs *regexp.Regexp
showFiles *regexp.Regexp
hides *regexp.Regexp
hideDirs *regexp.Regexp
hideFiles *regexp.Regexp

restrictAccessUrls pathStringsList
restrictAccessDirs pathStringsList

headersUrls pathHeadersList
headersDirs pathHeadersList

vary string
}

func NewVhostHandler(
Expand Down Expand Up @@ -86,41 +84,22 @@ func NewVhostHandler(
return nil, errs
}

// auth/index/upload/mkdir/delete urls/dirs users
authUrlsUsers := pathUsernamesToPathUids(users, p.AuthUrlsUsers)
authDirsUsers := pathUsernamesToPathUids(users, p.AuthDirsUsers)
indexUrlsUsers := pathUsernamesToPathUids(users, p.IndexUrlsUsers)
indexDirsUsers := pathUsernamesToPathUids(users, p.IndexDirsUsers)
uploadUrlsUsers := pathUsernamesToPathUids(users, p.UploadUrlsUsers)
uploadDirsUsers := pathUsernamesToPathUids(users, p.UploadDirsUsers)
mkdirUrlsUsers := pathUsernamesToPathUids(users, p.MkdirUrlsUsers)
mkdirDirsUsers := pathUsernamesToPathUids(users, p.MkdirDirsUsers)
deleteUrlsUsers := pathUsernamesToPathUids(users, p.DeleteUrlsUsers)
deleteDirsUsers := pathUsernamesToPathUids(users, p.DeleteDirsUsers)

// restrict access
restrictAccessUrls := newRestrictAccesses(p.RestrictAccessUrls)
restrictAccessDirs := newRestrictAccesses(p.RestrictAccessDirs)

// `Vary` header
vary := "accept-encoding"

// alias param
vhostCtx := &vhostContext{
theme: theme,
logger: logger,

users: users,
authUrlsUsers: authUrlsUsers,
authDirsUsers: authDirsUsers,
indexUrlsUsers: indexUrlsUsers,
indexDirsUsers: indexDirsUsers,
uploadUrlsUsers: uploadUrlsUsers,
uploadDirsUsers: uploadDirsUsers,
mkdirUrlsUsers: mkdirUrlsUsers,
mkdirDirsUsers: mkdirDirsUsers,
deleteUrlsUsers: deleteUrlsUsers,
deleteDirsUsers: deleteDirsUsers,
authUrlsUsers: pathUsernamesToPathUids(users, p.AuthUrlsUsers),
authDirsUsers: pathUsernamesToPathUids(users, p.AuthDirsUsers),
indexUrlsUsers: pathUsernamesToPathUids(users, p.IndexUrlsUsers),
indexDirsUsers: pathUsernamesToPathUids(users, p.IndexDirsUsers),
uploadUrlsUsers: pathUsernamesToPathUids(users, p.UploadUrlsUsers),
uploadDirsUsers: pathUsernamesToPathUids(users, p.UploadDirsUsers),
mkdirUrlsUsers: pathUsernamesToPathUids(users, p.MkdirUrlsUsers),
mkdirDirsUsers: pathUsernamesToPathUids(users, p.MkdirDirsUsers),
deleteUrlsUsers: pathUsernamesToPathUids(users, p.DeleteUrlsUsers),
deleteDirsUsers: pathUsernamesToPathUids(users, p.DeleteDirsUsers),

shows: shows,
showDirs: showDirs,
Expand All @@ -129,13 +108,11 @@ func NewVhostHandler(
hideDirs: hideDirs,
hideFiles: hideFiles,

restrictAccessUrls: restrictAccessUrls,
restrictAccessDirs: restrictAccessDirs,
restrictAccessUrls: newRestrictAccesses(p.RestrictAccessUrls),
restrictAccessDirs: newRestrictAccesses(p.RestrictAccessDirs),

headersUrls: newPathHeaders(p.HeadersUrls),
headersDirs: newPathHeaders(p.HeadersDirs),

vary: vary,
}

handler = newMultiplexHandler(p, vhostCtx)
Expand Down

0 comments on commit caa3ecf

Please sign in to comment.