Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
net/http: Host headers with ports never match routes in ServeMux #10463
So, in https://howsmyssl.com (jmhodges/howsmyssl), we redirect all incoming requests that don't match the domain www.howsmyssl.com to the correct domain. This captures folks typing in
That is, it uses code like
However, it appears that there are some clients that include the port in HTTPS requests and, on redirect, always re-include the port from the first request's Host header. Those requests will fall into a redirect loop with the above code. For instance, curl will just reuse a user-supplied Host header
This has caused a user to create a bug against howsmyssl because certain libraries and tools can't avoid re-including the port and fall into redirect loops.
So, I tried to fix this with routing by adding
to the code. However, it seems http.ServeMux will never match the port included in the route and, instead, only uses the incoming URI's host (not the Host header).
One might say to just 404 in the default case, but this will just cause the
I have a gross hack to strip the port from all incoming requests' Host headers, before it hits the muxer, but this problem seems like one common to a lot of HTTPS services and could be better handled there.
Stripping all ports from Host headers seems ill-advised, but adding ports as a part of the muxer seems like it would work okay.