-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http: document and test behavior of ServeMux with ports #23351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Unfortunately, the time for Go 1.9 bug reports was about 8 months ago during the beta & release candidate periods. If we changed the behavior in Go 1.9, it's difficult to impossible to change again and we might be stuck with the current behavior. Please try Go 1.10beta1, which is still in beta and maybe easier to maybe change. /cc @kennygrant |
@bradfitz I'm wondering if there should be checking/normalization code added to |
I don't know. I'm barely familiar with the ServeMux code. I've always let other people who care about it more handle it. I'll see if that continues to be the case with this bug. |
Could you report your results with Go 1.10? |
The behavior in |
In case other people stumble on this report. A partial workaround is to make sure the handler is registered twice (one without the port and one with):
|
Sorry about that. This change was made in response to this problem - #10463 - specifying ports wasn't working well with some clients and causing a redirect loop. I don't think we currently have any tests for patterns with a port and I didn't anticipate or test this use of port in patterns, since ListenAndServe specifies the port to listen on. For code which has to support older versions of Go at the same time, I agree it's awkward. For code using > 1.8, this isn't an issue, as it can just use routes without ports. Possible solutions: Strip ports from handlers in mux.Handle and make explicit the lack of support for port matching in patterns Not sure what the best resolution for this would be (code + doc or just doc), but any change would probably be best in 1.11 to give time for testing. |
Thanks for the response @kennygrant I did read the background on the original issue. It's not clear to me yet what solution would be best for Go Authors. Like I said, since this shipped in 1.9, authors will always have to be aware of this issue. Even if 1.10 introduces a change to mitigate the impact, authors still have to consider 1.9 for quite a while. To ignore that for a moment though. It seems like the original fix could have done a 3 step lookup for the handler, instead of simply stripping the port.
Resolving from most to least specific seems in keeping with other code in
If it is too late, then it seems like (I guess one thing to consider if The reason I ran into this is that I have a project that does depend on host routing and since the user configures those hosts, they may include ports. I plan to stop using the |
I agree ports should be mentioned in the main servemux doc and yes if it accepts host it might seem reasonable to allow ports so it should be documented at least. stripHostPort could maybe be moved to within mux.handler I think to do what you suggest, to have something like:
From a quick glance I think that could work - but we'd have to add some tests for hosts with port in the patterns and let others test it in the wild. That would change behaviour on connect requests though. I'll leave it to others who know more to weigh in on their preferred solution, there is no rush if this is for 1.11. |
At this point we shouldn't change anything, but we should document the current Go 1.9+ behavior and add tests. |
Hi folks, I'd love to give this one a shot for my first contribution. Unless anybody here would rather do it themselves, I should have a CL coming up soon. Thanks! |
Please feel free to send the CL. Thanks for your time. |
Change https://golang.org/cl/120557 mentions this issue: |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?1.9.2 and 1.10beta1
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
Consider this Go program:
If you use Go 1.8.5:
go run example.go
If you use Go 1.10beta1 and Go 1.9.2:
go run example.go
What did you expect to see?
I expect that compiling the example in 1.9.2 should behave as it did in 1.8.5.
More specifically, I would expect Go 1.9.2 to call the handler that I explicitly added for the host
localhost:8080
.What did you see instead?
Instead, this program called with Go 1.9.2 returns a 404 instead of calling the handler that I added for the host localhost:8080.
More Details
This change seems to have been reviewed at: https://go-review.googlesource.com/c/go/+/38194
This change strips the port from the host only when it is trying to locate a handler. It does not strip the port when the user adds a handler for a host that includes a port number. As a result, trying to add handlers for hosts that include the port no longer works.
To make matters worse, these change makes it hard to write http code for hosts with ports that works on both Go 1.9 and Go <1.9.
In Go 1.9, the route has to be setup with
m.HandleFunc("localhost", ...)
And in Go < 1.9, the route has to be setup with
m.HandleFunc("localhost:8080", ...)
.To make the code work on all versions of Go, the handler function needs to be registered for both "localhost" and "localhost:8080"
The text was updated successfully, but these errors were encountered: