Skip to content

Commit 4e5d91d

Browse files
committed
refactor: produce status in response data
1 parent 0844360 commit 4e5d91d

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/serverHandler/aliasHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
145145
h.needAuth(w, r)
146146
}
147147
if !data.AuthSuccess {
148-
h.authFailed(w)
148+
h.authFailed(w, data)
149149
return
150150
}
151151

152152
if !data.AllowAccess {
153-
restrictAccess(w)
153+
restrictAccess(w, data)
154154
return
155155
}
156156

src/serverHandler/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ func (h *aliasHandler) verifyAuth(r *http.Request) (username string, success boo
2525
return
2626
}
2727

28-
func (h *aliasHandler) authFailed(w http.ResponseWriter) {
29-
w.WriteHeader(http.StatusUnauthorized)
28+
func (h *aliasHandler) authFailed(w http.ResponseWriter, data *responseData) {
29+
w.WriteHeader(data.Status)
3030
}

src/serverHandler/responseData.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
302302
reqPath := util.CleanUrlPath(rawReqPath[len(h.aliasPrefix):])
303303
reqFsPath, _ := util.NormalizeFsPath(h.root + reqPath)
304304

305+
status := http.StatusOK
306+
305307
needAuth := h.getNeedAuth(rawReqPath, reqFsPath)
306308
authUserName := ""
307309
authSuccess := true
@@ -311,6 +313,9 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
311313
if _authErr != nil {
312314
errs = append(errs, _authErr)
313315
}
316+
if !authSuccess {
317+
status = http.StatusUnauthorized
318+
}
314319
}
315320

316321
headers := h.getHeaders(rawReqPath, reqFsPath, authSuccess)
@@ -336,7 +341,6 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
336341
}
337342
wantJson := strings.HasPrefix(rawQuery, "json") || strings.Contains(rawQuery, "&json")
338343

339-
status := http.StatusOK
340344
isRoot := rawReqPath == "/"
341345

342346
currDirRelPath := getCurrDirRelPath(rawReqPath, prefixReqPath)
@@ -371,6 +375,9 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
371375
}
372376

373377
allowAccess := h.isAllowAccess(r, rawReqPath, reqFsPath, file, item)
378+
if !allowAccess {
379+
status = http.StatusForbidden
380+
}
374381

375382
itemName := getItemName(item, r)
376383

src/serverHandler/restrictAccess.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func (h *aliasHandler) isAllowAccess(r *http.Request, reqUrlPath, reqFsPath stri
7878
return false
7979
}
8080

81-
func restrictAccess(w http.ResponseWriter) {
82-
w.WriteHeader(http.StatusForbidden)
83-
w.Write([]byte("403 Forbidden"))
81+
func restrictAccess(w http.ResponseWriter, data *responseData) {
82+
w.WriteHeader(data.Status)
8483
}

0 commit comments

Comments
 (0)