Svip
Svip

Reputation: 3128

How do I get Go's net/http package to stop removing double slashes?

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

Upvotes: 4

Views: 2813

Answers (1)

Himanshu
Himanshu

Reputation: 12675

Go for gorilla mux to route the server using the url. By default the DefaultSeveMux uses Clean which modify the url against various unwanted characters. That's the reason the url changed whn you are using // double slash.

func Clean(path string) string

While if you do not want to sanitize the url. Gorilla mux provide a method SkipClean() which when set to true. It will not sanitize the url.

func (r *Router) SkipClean(value bool) *Router {
    r.skipClean = value
    return r
}

It is mentioned in the document of gorilla mux for SkipClean() as:

SkipClean defines the path cleaning behaviour for new routes. The initial value is false. Users should be careful about which routes are not cleaned When true, if the route path is "/path//to", it will remain with the double slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/ When false, the path will be cleaned, so /fetch/http://xkcd.com/534/ will become /fetch/http/xkcd.com/534

Upvotes: 2

Related Questions