duanliang1019 2018-07-04 08:56
浏览 184
已采纳

HTTP静态文件处理程序不断显示目录列表

When I implement a static server handler, if I access to root path, it will show the whole directory, like this:

access to root folder

My code is:

package main

import (
    "flag"
    "log"
    "net/http"
    "strings"
)

func main() {
    port := flag.String("p", "3000", "port to serve on")
    directory := flag.String("d", ".", "the directory of static file to host")
    flag.Parse()

    http.Handle("/statics/", http.StripPrefix(strings.TrimRight("/statics/", "/"), http.FileServer(http.Dir(*directory))))

    log.Printf("Serving %s on HTTP port: %s
", *directory, *port)
    log.Fatal(http.ListenAndServe(":"+*port, nil))
}

go to : http://locahost:3000/statics/

  • 写回答

1条回答 默认 最新

  • dty9731 2018-07-04 09:39
    关注

    Fortunately or not, the default behavior of http.FileServer is that if the path denotes a directory without an index.html file, a directory listing will be served. And it does not provide a simple way to turn that off. But...

    http.FileServer operates on a virtual filesystem described by the http.FileSystem interface.

    This interface has a single method which tells how to open a file, and obtain an http.File "view" of it:

    type FileSystem interface {
            Open(name string) (File, error)
    }
    

    If you want to disable directory listing, all you have to do is provide your own implementation of http.FileSystem which when a directory is targeted, you simply report / return an error. That's all it takes.

    Of course you don't have to do this all by yourself. You may create your own FileSystem which uses / utilizes http.Dir, which is the default implementation that uses the native file system (restricted to a specific directory tree).

    type myfs struct {
        http.Dir
    }
    
    func (m myfs) Open(name string) (result http.File, err error) {
        f, err := m.Dir.Open(name)
        if err != nil {
            return
        }
    
        fi, err := f.Stat()
        if err != nil {
            return
        }
        if fi.IsDir() {
            // Return a response that would have been if directory would not exist:
            return m.Dir.Open("does-not-exist")
        }
        return f, nil
    }
    

    Using the above custom implementation:

    handler := http.FileServer(myfs{http.Dir(*directory)})
    http.Handle(
        "/statics/",
        http.StripPrefix(strings.TrimRight("/statics/", "/"), handler),
    )
    

    And that's all. Attempting to browse http://locahost:3000/statics/ will result in the default response:

    404 page not found
    

    Notes:

    The above implementation does a second Dir.Open() call to return an error, which is always the same. To "speed things up", we can store this response and just reuse it:

    var notFoundFile, notFoundErr = http.Dir("dummy").Open("does-not-exist")
    

    And when we detect a directory in our myfs.Open() method:

    if fi.IsDir() {
        // Return a response that "belogns" to opening a non-existing folder:
        return notFoundFile, notFoundErr
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大