Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
Revert "fileserver: Redirect within the original URL (#4179)"
This reverts commit f9b54454a19e2b070159ce8d2af76d819658244e. /cc @diamondburned (see #4205)
Matthew Holt committed
Jun 14, 2021 at 09:03 UTC
8848df9c5d372a559d01512b7a4ef00e38867b55
2 files changed
+10
-13
modules/caddyhttp/fileserver/browse.go
+4
-5
@@ -47,11 +47,10 @@ func (fsrv *FileServer) serveBrowse(root, dirPath string, w http.ResponseWriter,
47
// URL doesn't end in a trailing slash because hrefs like
48
// "/b/c" on a path like "/a" end up going to "/b/c" instead
49
// of "/a/b/c" - so we have to redirect in this case
50
- oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
51
- if !strings.HasSuffix(oldReq.URL.Path, "/") {
52
- fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", oldReq.URL.Path))
53
- oldReq.URL.Path += "/"
54
- http.Redirect(w, r, oldReq.URL.String(), http.StatusMovedPermanently)
50
+ if !strings.HasSuffix(r.URL.Path, "/") {
51
+ fsrv.logger.Debug("redirecting to trailing slash to preserve hrefs", zap.String("request_path", r.URL.Path))
52
+ r.URL.Path += "/"
53
+ http.Redirect(w, r, r.URL.String(), http.StatusMovedPermanently)
54
return nil
55
}
56
modules/caddyhttp/fileserver/staticfiles.go
+6
-8
@@ -243,14 +243,12 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
243
// trailing slash - not enforcing this can break relative hrefs
244
// in HTML (see https://github.com/caddyserver/caddy/issues/2741)
245
if fsrv.CanonicalURIs == nil || *fsrv.CanonicalURIs {
246
- oldReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
247
-
248
- if implicitIndexFile && !strings.HasSuffix(oldReq.URL.Path, "/") {
249
- fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", oldReq.URL.Path))
250
- return redirect(w, r, oldReq.URL.Path+"/")
251
- } else if !implicitIndexFile && strings.HasSuffix(oldReq.URL.Path, "/") {
252
- fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", oldReq.URL.Path))
253
- return redirect(w, r, oldReq.URL.Path[:len(oldReq.URL.Path)-1])
246
+ if implicitIndexFile && !strings.HasSuffix(r.URL.Path, "/") {
247
+ fsrv.logger.Debug("redirecting to canonical URI (adding trailing slash for directory)", zap.String("path", r.URL.Path))
248
+ return redirect(w, r, r.URL.Path+"/")
249
+ } else if !implicitIndexFile && strings.HasSuffix(r.URL.Path, "/") {
250
+ fsrv.logger.Debug("redirecting to canonical URI (removing trailing slash for file)", zap.String("path", r.URL.Path))
251
+ return redirect(w, r, r.URL.Path[:len(r.URL.Path)-1])
252
}
253
}
254