-1

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
deepnum
  • 21
  • 1
  • 3
    you wrap the file server handler, check for the request uri, if it ends in anything undesirable, return an error, otherwise, execute the http file system handler. –  Nov 17 '19 at 12:17
  • Give me some code example – deepnum Nov 17 '19 at 12:18

1 Answers1

1

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)
    }))

    ...
}
Momchil Atanasov
  • 479
  • 4
  • 10
  • This only detect .gitignore. But i something that can identity any .file or file in a dot folder – deepnum Nov 17 '19 at 14:59
  • There is a problem. http.HandlerFunc is a type not a function. On type conversion it accepts a function and return a HanerFunc which implement a Handler – deepnum Nov 17 '19 at 15:35
  • @deepnum, yes, `HandlerFunc` accepts a function and converts it into an `http.Handler`. I had a mistake in the code, which is now corrected. What the code does is that it registers a handler that checks (the `if` statement) whether the targeted resource is a `.gitignore` file. If it is, then a `NotFound` status code is returned and the request is no longer processed. Otherwise, the original http handler is called, which in your case I presume is an `http.FileServer`. – Momchil Atanasov Nov 17 '19 at 16:12
  • @deepnum, I have extended the code to show how you could detect a `.file` or file in dot dir. Now, I haven't tested it nor checked whether it compiles but you should be able to adapt the idea on your own. – Momchil Atanasov Nov 17 '19 at 16:19
  • 2nd argument of http.Handle accepts a handler interface and http.HandlerFunc implements http.Handler interface. – deepnum Nov 17 '19 at 16:22
  • @deepnum, `http.HandlerFunc` implements the `http.Handler` interface, which is exactly what the second argument to [http.Handle](https://golang.org/pkg/net/http/#example_Handle) should be. I tested the code locally, and it had a problem with the `isPrivate` function, which is now corrected. The http side of the code is working, however. Are you sure you didn't accidentally type `http.HandleFunc` instead of `http.HandlerFunc`? – Momchil Atanasov Nov 17 '19 at 17:23