如何避免在http响应中发送.git
之类的点文件夹或.gitignore
之类的点文件?
如果用户请求此类文件,我想发送错误。
以下是部分示例片段。该代码检查所请求的文件是否为.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)
})