@@ -40,10 +40,8 @@ type responseData struct {
4040 rawReqPath string
4141 handlerReqPath string
4242
43- errors []error
44- HasForbiddenError bool
45- HasNotFoundError bool
46- HasInternalError bool
43+ errors []error
44+ Status int
4745
4846 IsRoot bool
4947 Path string
@@ -259,6 +257,18 @@ func sortSubItems(subItems []*subItem) {
259257 },
260258 )
261259}
260+ func getStatusByErr (err error ) int {
261+ switch {
262+ case os .IsPermission (err ):
263+ return http .StatusForbidden
264+ case os .IsNotExist (err ):
265+ return http .StatusNotFound
266+ case err != nil :
267+ return http .StatusInternalServerError
268+ default :
269+ return http .StatusOK
270+ }
271+ }
262272
263273func (h * handler ) getResponseData (r * http.Request ) (data * responseData ) {
264274 requestUri := r .URL .Path
@@ -267,10 +277,7 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
267277 rawReqPath := util .CleanUrlPath (requestUri )
268278 reqPath := util .CleanUrlPath (rawReqPath [len (h .urlPrefix ):]) // strip url prefix path
269279 errs := []error {}
270- forbidden := false
271- notFound := false
272- internalError := false
273-
280+ status := http .StatusOK
274281 isRoot := rawReqPath == "/"
275282
276283 pathEntries := getPathEntries (rawReqPath , tailSlash )
@@ -289,28 +296,21 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
289296 file , item , _statErr := stat (reqFsPath , ! h .emptyRoot )
290297 if _statErr != nil {
291298 errs = append (errs , _statErr )
292- switch {
293- case os .IsPermission (_statErr ):
294- forbidden = true
295- case os .IsNotExist (_statErr ):
296- notFound = true
297- default :
298- internalError = true
299- }
299+ status = getStatusByErr (_statErr )
300300 }
301301
302302 itemName := getItemName (item , r )
303303
304304 subInfos , _readdirErr := readdir (file , item , needResponseBody (r .Method ))
305305 if _readdirErr != nil {
306306 errs = append (errs , _readdirErr )
307- internalError = true
307+ status = http . StatusInternalServerError
308308 }
309309
310310 _mergeErrs := h .mergeAlias (rawReqPath , & subInfos )
311311 if len (_mergeErrs ) > 0 {
312312 errs = append (errs , _mergeErrs ... )
313- internalError = true
313+ status = http . StatusInternalServerError
314314 }
315315
316316 subInfos = h .FilterItems (subInfos )
@@ -329,10 +329,8 @@ func (h *handler) getResponseData(r *http.Request) (data *responseData) {
329329 rawReqPath : rawReqPath ,
330330 handlerReqPath : reqPath ,
331331
332- errors : errs ,
333- HasForbiddenError : forbidden ,
334- HasNotFoundError : notFound ,
335- HasInternalError : internalError ,
332+ errors : errs ,
333+ Status : status ,
336334
337335 IsRoot : isRoot ,
338336 Path : rawReqPath ,
0 commit comments