shithub: hugo

Download patch

ref: 83e46555dd15aa6082b17f9ebbf9f12eb1cf64d8
parent: 50c819cedee34c09d21f46def8a16d435d7271d1
author: Arun Sori <[email protected]>
date: Fri Oct 23 17:51:37 EDT 2015

Disable directory listing for Hugo file server

Fix #1400

--- a/commands/server.go
+++ b/commands/server.go
@@ -49,6 +49,26 @@
 	//Run: server,
 }
 
+type filesOnlyFs struct {
+	fs http.FileSystem
+}
+
+type noDirFile struct {
+	http.File
+}
+
+func (fs filesOnlyFs) Open(name string) (http.File, error) {
+	f, err := fs.fs.Open(name)
+	if err != nil {
+		return nil, err
+	}
+	return noDirFile{f}, nil
+}
+
+func (f noDirFile) Readdir(count int) ([]os.FileInfo, error) {
+	return nil, nil
+}
+
 func init() {
 	serverCmd.Flags().IntVarP(&serverPort, "port", "p", 1313, "port on which the server will listen")
 	serverCmd.Flags().StringVarP(&serverInterface, "bind", "", "127.0.0.1", "interface to which the server will bind")
@@ -125,7 +145,8 @@
 	jww.FEEDBACK.Println("Serving pages from " + helpers.AbsPathify(viper.GetString("PublishDir")))
 
 	httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS}
-	fileserver := http.FileServer(httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))
+	fs := filesOnlyFs{httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))}
+	fileserver := http.FileServer(fs)
 
 	// We're only interested in the path
 	u, err := url.Parse(viper.GetString("BaseURL"))