From c5a23d5cdea12b4df49ff8d7f1adbe867ab6aaf6 Mon Sep 17 00:00:00 2001 From: astaxie Date: Mon, 24 Jun 2013 23:24:13 +0800 Subject: [PATCH] fix #81 --- beego.go | 1 + config.go | 3 +++ router.go | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/beego.go b/beego.go index 54bbecc4..602b575d 100644 --- a/beego.go +++ b/beego.go @@ -39,6 +39,7 @@ var ( UseFcgi bool MaxMemory int64 EnableGzip bool // enable gzip + DirectoryIndex bool //ebable DirectoryIndex default is false ) func init() { diff --git a/config.go b/config.go index 01e9be99..0019afce 100644 --- a/config.go +++ b/config.go @@ -174,6 +174,9 @@ func ParseConfig() (err error) { if ar, err := AppConfig.Bool("enablegzip"); err == nil { EnableGzip = ar } + if ar, err := AppConfig.Bool("directoryindex"); err == nil { + DirectoryIndex = ar + } } return nil } diff --git a/router.go b/router.go index b78d8885..1a4a0388 100644 --- a/router.go +++ b/router.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "net/url" + "os" "reflect" "regexp" "runtime" @@ -317,6 +318,21 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) } if strings.HasPrefix(r.URL.Path, prefix) { file := staticDir + r.URL.Path[len(prefix):] + finfo, err := os.Stat(file) + if err != nil { + return + } + //if the request is dir and DirectoryIndex is false then + if finfo.IsDir() && !DirectoryIndex { + if h, ok := ErrorMaps["403"]; ok { + h(w, r) + } else { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.WriteHeader(403) + fmt.Fprintln(w, "403 Forbidden") + return + } + } http.ServeFile(w, r, file) w.started = true return