Consider the following very basic "net/http"-program:
package main
import (
"net/http"
"log"
)
func entry(w http.ResponseWriter, req *http.Request) {
log.Println(req.URL.Path)
path := []byte(req.URL.Path)
w.Write(path)
}
func main() {
http.HandleFunc("/", entry)
err := http.ListenAndServe("localhost:10000", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
When accessing the URL, http://localhost:10000/a//b
, the DefaultServerMux
, which is what the default HandleFunc()
and ListenAndServe()
uses, redirects it to /a/b
, effectively removing the double slash.
The documentation for ServerMux
does specify that it 'sanitises' URL requests:
ServeMux also takes care of sanitizing the URL request path, redirecting any request containing . or .. elements or repeated slashes to an equivalent, cleaner URL.
But what if I don't want it to? I have a scenario where //
!= /
in my URLs. I could find another solution. But is there a way to still use Go's "net/http" package, while it not sanitising my URLs like this? Preferably with as little re-writing as possible.
(I will probably find a different solution than having //
and /
being distinct, since I am probably happy with other features that ServerMux
provides (in case a solution requires me to use another multiplexer), but now I am curious whether there is a solution with Go's standard "net/http" package.)