Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
net/http: cannot register ServeMux handler for all CONNECT requests regardless of host or path #9561
Go version 1.4 and head (4e03bbb), all platforms.
It doesn't appear to be possible to register a ServeMux handler that works for all CONNECT requests, regardless of hostname or path.
Steps to reproduce
The program prints something like
and returns a 200 response.
The program prints nothing and returns 404.
As http.ServeMux.handler notes, its path parameter is in canonical form, except for CONNECT requests. In this case the path is the empty string, so it never matches against a ServeMux entry that has a non-empty path.
It is possible to register a ServeMux handler for a particular host, as in
and this receives CONNECT requests as expected. But it doesn't appear to be possible to register a handler as a generic fallback for CONNECT requests, as one might hope to do with
A possible workaround is "don't use ServeMux for this", but perhaps there is a fix.
ServeMux.Handler might fix this by explicitly using "/" as the path argument to ServeMux.handler for a CONNECT request with an empty path. E.g.:
I'm not sure if that's a reasonable approach.
I don't think ServeMux needs to do everything. It's very easy to write your own mux, and CONNECT is a pretty advanced case. Anybody needing CONNECT (like @kr) knows how to write their own.
In the interest of minimizing changes in the standard library, I'm going to close this. Feel free to argue if you feel strongly.