You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Today I accidentally passed a gocraft/web subrouter to the net/http package. This was an accident on my part, but resulted in some strange behavior. It appears that for each request, it was performed twice. I was not even aware a subrouter could be pased to net/http.
This example demonstrates my mistake, but does not replicate the behavior.
As you can see, subrouter can be used as it implements http.Handler. However, all I got was a panic when I used curl to get "http://localhost:8080/foo/bar".
ericu@ericu-desktop:~$ go run ./serve_subrouter.go
2014/11/05 22:18:28 http: panic serving [::1]:51597: runtime error: makeslice: cap out of range
goroutine 20 [running]:
net/http.func·011()
/home/ericu/builds/go/src/pkg/net/http/server.go:1100 +0xb7
runtime.panic(0x6cb260, 0x8bc0fc)
/home/ericu/builds/go/src/pkg/runtime/panic.c:248 +0x18d
github.com/gocraft/web.(*Router).ServeHTTP(0xc208058180, 0x7fe673d358c8, 0xc20803cbe0, 0xc208029040)
/home/ericu/gotmp/src/github.com/gocraft/web/router_serve.go:32 +0x82
net/http.serverHandler.ServeHTTP(0xc208004180, 0x7fe673d358c8, 0xc20803cbe0, 0xc208029040)
/home/ericu/builds/go/src/pkg/net/http/server.go:1673 +0x19f
net/http.(*conn).serve(0xc20803e200)
/home/ericu/builds/go/src/pkg/net/http/server.go:1174 +0xa7e
created by net/http.(*Server).Serve
/home/ericu/builds/go/src/pkg/net/http/server.go:1721 +0x313
^Cexit status 2
ericu@ericu-desktop:~$ go run ./serve_subrouter.go
2014/11/05 22:18:42 http: panic serving [::1]:51598: runtime error: makeslice: cap out of range
goroutine 20 [running]:
net/http.func·011()
/home/ericu/builds/go/src/pkg/net/http/server.go:1100 +0xb7
runtime.panic(0x6cb260, 0x8bc0fc)
/home/ericu/builds/go/src/pkg/runtime/panic.c:248 +0x18d
github.com/gocraft/web.(*Router).ServeHTTP(0xc208056180, 0x7f74ab7559c8, 0xc20803cbe0, 0xc208029450)
/home/ericu/gotmp/src/github.com/gocraft/web/router_serve.go:32 +0x82
net/http.serverHandler.ServeHTTP(0xc208004180, 0x7f74ab7559c8, 0xc20803cbe0, 0xc208029450)
/home/ericu/builds/go/src/pkg/net/http/server.go:1673 +0x19f
net/http.(*conn).serve(0xc20803e200)
/home/ericu/builds/go/src/pkg/net/http/server.go:1174 +0xa7e
created by net/http.(*Server).Serve
/home/ericu/builds/go/src/pkg/net/http/server.go:1721 +0x313
The obvious solution is not to pass subrouter to net/http. But it seems this mistake could be avoided by making the subrouter not implement the http.Handler type. This would generate a very obvious compile time error. If I come up with some patches to do this, would you be willing to accept them?
The text was updated successfully, but these errors were encountered:
hydrogen18
changed the title
Subrouters implement http.Handler but don't do anything useful.
Subrouters implement http.Handler but doesn't do anything useful.
Nov 6, 2014
hydrogen18
changed the title
Subrouters implement http.Handler but doesn't do anything useful.
Subrouters implement http.Handler but don't do anything useful.
Nov 14, 2014
@hydrogen18 Sorry for the delay in responding. I might be willing to accept them, but I worry that fixing it will introduce more complexity than it solves.
I've run into this a couple times. Thank you @hydrogen18 for spotting the issue -- I created a subrouter with the name of the original router, renamed the original router to something else, in which case I ended up passing the subrouter to http.ListenAndServe. Oops!
Today I accidentally passed a
gocraft/web
subrouter to thenet/http
package. This was an accident on my part, but resulted in some strange behavior. It appears that for each request, it was performed twice. I was not even aware a subrouter could be pased tonet/http
.This example demonstrates my mistake, but does not replicate the behavior.
As you can see,
subrouter
can be used as it implementshttp.Handler
. However, all I got was a panic when I usedcurl
to get "http://localhost:8080/foo/bar".The obvious solution is not to pass
subrouter
tonet/http
. But it seems this mistake could be avoided by making the subrouter not implement thehttp.Handler
type. This would generate a very obvious compile time error. If I come up with some patches to do this, would you be willing to accept them?The text was updated successfully, but these errors were encountered: