From 125ed11c0abac7580125cb745b725a6fdd8be207 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 30 Apr 2015 13:24:58 -0700 Subject: [PATCH] net/http: document ServeFile and FileServer index.html redirect behavior Fixes #9876 Change-Id: I97a354fde827dfccc9e373fadea2e37d094439b0 Reviewed-on: https://go-review.googlesource.com/9538 Reviewed-by: Rob Pike --- src/net/http/fs.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/net/http/fs.go b/src/net/http/fs.go index 40bf1b3ef3..75720234c2 100644 --- a/src/net/http/fs.go +++ b/src/net/http/fs.go @@ -443,7 +443,13 @@ func localRedirect(w ResponseWriter, r *Request, newPath string) { w.WriteHeader(StatusMovedPermanently) } -// ServeFile replies to the request with the contents of the named file or directory. +// ServeFile replies to the request with the contents of the named +// file or directory. +// +// As a special case, ServeFile redirects any request where r.URL.Path +// ends in "/index.html" to the same path, without the final +// "index.html". To avoid such redirects either modify the path or +// use ServeContent. func ServeFile(w ResponseWriter, r *Request, name string) { dir, file := filepath.Split(name) serveFile(w, r, Dir(dir), file, false) @@ -460,6 +466,10 @@ type fileHandler struct { // use http.Dir: // // http.Handle("/", http.FileServer(http.Dir("/tmp"))) +// +// As a special case, the returned file server redirects any request +// ending in "/index.html" to the same path, without the final +// "index.html". func FileServer(root FileSystem) Handler { return &fileHandler{root} } -- 2.48.1