How can I avoid sending files in a dot folder like .git
or dot file like .gitignore
in a http response?
I want to send a error if user request this kind of files.
How can I avoid sending files in a dot folder like .git
or dot file like .gitignore
in a http response?
I want to send a error if user request this kind of files.
Following is an example partial snippet. The code checks whether the requested file is .gitignore
. You will need to adapt this to your code and to extend the if
check to handle your specific needs.
...
func isPrivate(urlPath string) bool {
for _, segment := range strings.Split(urlPath, "/") {
if strings.HasPrefix(segment, ".") {
return true
}
}
return false
}
...
func main() {
originalHandler := http.FileServer(http.Dir(".")) // or I presume something similar
http.Handle("/", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
if isPrivate(req.URL.Path) {
http.Error(resp, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
originalHandler.ServeHTTP(resp, req)
}))
...
}