如何避免http响应中的点文件或点目录

问题描述 投票:0回答:1

如何避免在http响应中发送.git之类的点文件夹或.gitignore之类的点文件?

如果用户请求此类文件,我想发送错误。

http go unsafe
1个回答
0
投票

以下是部分示例片段。该代码检查所请求的文件是否为.gitignore。您将需要对此进行调整以适合您的代码,并扩展if检查以处理您的特定需求。

originalHandler := http.FileServer(http.Dir(".")) // or I presume something similar

http.Handle("/", http.HandlerFunc(resp http.ResponseWriter, req *http.Request) {
    if path.Base(req.URL.Path) == ".gitignore" {
        http.Error(resp, http.StatusText(http.StatusNotFound), http.StatusNotFound)
        return
    }

    originalHandler.ServeHTTP(resp, req)
})
© www.soinside.com 2019 - 2024. All rights reserved.