julienschmidt / httprouter

A high performance HTTP request router that scales well
https://pkg.go.dev/github.com/julienschmidt/httprouter
BSD 3-Clause "New" or "Revised" License
16.64k stars 1.47k forks source link

Negroni: Fix multiple slashes (CleanPath) #159

Closed asambeka closed 4 years ago

asambeka commented 8 years ago

While building the RestFul web service, faced this issue where following two URLs are handled differently: http://www.myhost.com/v1/service/action http://www.myhost.com//v1/service/action httprouter.Router is using pattern /v1/:controller/:method

I believe "//" should be treated as "/" while parsing. I am using latest code.

julienschmidt commented 8 years ago

CleanPath should take care of that. // should not be treated as / while parsing as it would create a different path for the same ressource, but the former should be corrected. That is what CleanPath does.

julienschmidt commented 8 years ago

CleanPath is used be the default NotFound handler, if you use a custom one, you can use it manually.

asambeka commented 8 years ago

I am using this with Negroni. Here is the snippet,

mux := httprouter.New()
v1vPattern := "/v1/:application/*object
mux.GET(v1vPattern, VGet)
mux.PUT(v1vPattern, VPut)
mux.DELETE(v1vPattern, Delete)
nhandle := negroni.Classic()
nhandle.UseHandler(mux)
nhandle.Run(HOST_PORT)

I don't have NotFound handler (unless negroni is doming something different). Any suggestions?

julienschmidt commented 8 years ago

How about mux.NotFound?

julienschmidt commented 8 years ago

I have never used Negroni myself, but I think quite a few people do. Maybe someone of the people watching this repository can help you?

asambeka commented 8 years ago

In the router.go I can see CleanPath being called line 367. If CleanPath is being called why is this not working? This appears to be a bug in CleanPlath.

julienschmidt commented 4 years ago

CleanPath successfully corrects //v1/service/action to /v1/service/action. If there was / is a bug, it does not seem to be in httprouter.