Skip to content

Commit 2880d11

Browse files
committed
fix(serverHandler): enable dir slash for directory only
1 parent 3405ce4 commit 2880d11

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

src/serverHandler/responseData.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
353353
status = getStatusByErr(_statErr)
354354
}
355355

356-
needDirSlashRedirect := h.forceDirSlash > 0 && prefixReqPath[len(prefixReqPath)-1] != '/' && !shouldServeAsContent(file, item)
356+
needDirSlashRedirect := h.forceDirSlash > 0 && prefixReqPath[len(prefixReqPath)-1] != '/' && item != nil && item.IsDir()
357357

358358
indexFile, indexItem, _statIdxErr := h.statIndexFile(rawReqPath, reqFsPath, item, authSuccess && !needDirSlashRedirect)
359359
if _statIdxErr != nil {
@@ -390,6 +390,11 @@ func (h *aliasHandler) getResponseData(r *http.Request) (data *responseData, fsP
390390
errs = append(errs, _dereferenceErrs...)
391391
}
392392

393+
// update `needDirSlashRedirect` for dangling intermediate alias directory
394+
if !needDirSlashRedirect && h.forceDirSlash > 0 && len(subItems) > 0 && prefixReqPath[len(prefixReqPath)-1] != '/' {
395+
needDirSlashRedirect = true
396+
}
397+
393398
subItems = h.FilterItems(subItems)
394399
rawSortBy, sortState := sortInfos(subItems, rawQuery, h.defaultSort)
395400

test/case/041.force.dir.slash.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ assert $(curl_head_status 'http://127.0.0.1:3003/foo/bar/lorem/ipsum/a') '301'
3030
fail "incorrect redirect location"
3131
assert $(curl_head_status 'http://127.0.0.1:3003/foo/bar/lorem/ipsum/a/') '200'
3232

33+
assert $(curl_head_status 'http://127.0.0.1:3003/foo/bar/lorem/ipsum/a/not-exist') '404'
34+
3335
jobs -p | xargs kill &> /dev/null

test/case/041.force.dir.slash.status.code.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ assert $(curl_head_status 'http://127.0.0.1:3003/foo/bar/lorem/ipsum/a') '302'
3030
fail "incorrect redirect location"
3131
assert $(curl_head_status 'http://127.0.0.1:3003/foo/bar/lorem/ipsum/a/') '200'
3232

33+
assert $(curl_head_status 'http://127.0.0.1:3003/foo/bar/lorem/ipsum/a/not-exist') '404'
34+
3335
jobs -p | xargs kill &> /dev/null

0 commit comments

Comments
 (0)