@@ -25,6 +25,7 @@ type itemHtml struct {
2525}
2626
2727type responseData struct {
28+ prefixReqPath string
2829 rawReqPath string
2930 handlerReqPath string
3031
@@ -68,6 +69,8 @@ type responseData struct {
6869 SortState SortState
6970 Context * pathContext
7071
72+ NeedDirSlashRedirect bool
73+
7174 Lang string
7275 Trans * i18n.Translation
7376}
@@ -191,9 +194,9 @@ func (h *handler) mergeAlias(
191194 return subItems , aliasSubItems , errs
192195}
193196
194- func getCurrDirRelPath (reqPath , rawReqPath string ) string {
195- if len (reqPath ) == 1 && len (rawReqPath ) > 1 && rawReqPath [len (rawReqPath )- 1 ] != '/' {
196- return "./" + path .Base (rawReqPath ) + "/"
197+ func getCurrDirRelPath (reqPath , prefixReqPath string ) string {
198+ if len (reqPath ) == 1 && len (prefixReqPath ) > 1 && prefixReqPath [len (prefixReqPath )- 1 ] != '/' {
199+ return "./" + path .Base (prefixReqPath ) + "/"
197200 } else {
198201 return "./"
199202 }
@@ -291,6 +294,7 @@ func dereferenceSymbolLinks(reqFsPath string, subItems []os.FileInfo) (errs []er
291294func (h * handler ) getResponseData (r * http.Request ) * responseData {
292295 var errs []error
293296
297+ prefixReqPath := r .URL .RawPath
294298 rawReqPath := r .URL .Path
295299 tailSlash := rawReqPath [len (rawReqPath )- 1 ] == '/'
296300
@@ -334,7 +338,7 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
334338 status := http .StatusOK
335339 isRoot := rawReqPath == "/"
336340
337- currDirRelPath := getCurrDirRelPath (rawReqPath , r . URL . RawPath )
341+ currDirRelPath := getCurrDirRelPath (rawReqPath , prefixReqPath )
338342 pathEntries := getPathEntries (currDirRelPath , rawReqPath , tailSlash )
339343 var rootRelPath string
340344 if len (pathEntries ) > 0 {
@@ -349,7 +353,9 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
349353 status = getStatusByErr (_statErr )
350354 }
351355
352- indexFile , indexItem , _statIdxErr := h .statIndexFile (rawReqPath , reqFsPath , item , authSuccess )
356+ needDirSlashRedirect := h .forceDirSlash > 0 && prefixReqPath [len (prefixReqPath )- 1 ] != '/' && ! shouldServeAsContent (file , item )
357+
358+ indexFile , indexItem , _statIdxErr := h .statIndexFile (rawReqPath , reqFsPath , item , authSuccess && ! needDirSlashRedirect )
353359 if _statIdxErr != nil {
354360 errs = append (errs , _statIdxErr )
355361 status = getStatusByErr (_statIdxErr )
@@ -367,13 +373,13 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
367373
368374 itemName := getItemName (item , r )
369375
370- subItems , _readdirErr := readdir (file , item , authSuccess && ! isMutate && allowAccess && needResponseBody (r .Method ))
376+ subItems , _readdirErr := readdir (file , item , authSuccess && ! isMutate && ! needDirSlashRedirect && allowAccess && needResponseBody (r .Method ))
371377 if _readdirErr != nil {
372378 errs = append (errs , _readdirErr )
373379 status = http .StatusInternalServerError
374380 }
375381
376- subItems , aliasSubItems , _mergeErrs := h .mergeAlias (rawReqPath , item , subItems , authSuccess && allowAccess )
382+ subItems , aliasSubItems , _mergeErrs := h .mergeAlias (rawReqPath , item , subItems , authSuccess && ! needDirSlashRedirect && allowAccess )
377383 if len (_mergeErrs ) > 0 {
378384 errs = append (errs , _mergeErrs ... )
379385 status = http .StatusInternalServerError
@@ -407,6 +413,7 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
407413 }
408414
409415 return & responseData {
416+ prefixReqPath : prefixReqPath ,
410417 rawReqPath : rawReqPath ,
411418 handlerReqPath : reqPath ,
412419
@@ -449,5 +456,7 @@ func (h *handler) getResponseData(r *http.Request) *responseData {
449456 SubItemPrefix : subItemPrefix ,
450457 SortState : sortState ,
451458 Context : context ,
459+
460+ NeedDirSlashRedirect : needDirSlashRedirect ,
452461 }
453462}
0 commit comments