Skip to content

Commit 37f05cc

Browse files
Guilherme Goncalvesbradfitz
Guilherme Goncalves
authored andcommitted
net/http: document and test behavior of ServeMux with ports
Beginning on Go 1.9, ServeMux has been dropping the port number from the Host header and in the path pattern. This commit explicitly mentions the change in behavior and adds a simple test case to ensure consistency. Fixes #23351. Change-Id: I0270c8bd96cda92c13ac6437cdf66c2807b3042d Reviewed-on: https://go-review.googlesource.com/120557 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 444e7dd commit 37f05cc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/net/http/serve_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -5924,6 +5924,28 @@ func TestServerCloseListenerOnce(t *testing.T) {
59245924
}
59255925
}
59265926

5927+
// Issue 23351: document and test behavior of ServeMux with ports
5928+
func TestStripPortFromHost(t *testing.T) {
5929+
mux := NewServeMux()
5930+
5931+
mux.HandleFunc("example.com/", func(w ResponseWriter, r *Request) {
5932+
fmt.Fprintf(w, "OK")
5933+
})
5934+
mux.HandleFunc("example.com:9000/", func(w ResponseWriter, r *Request) {
5935+
fmt.Fprintf(w, "uh-oh!")
5936+
})
5937+
5938+
req := httptest.NewRequest("GET", "http://example.com:9000/", nil)
5939+
rw := httptest.NewRecorder()
5940+
5941+
mux.ServeHTTP(rw, req)
5942+
5943+
response := rw.Body.String()
5944+
if response != "OK" {
5945+
t.Errorf("Response gotten was %q", response)
5946+
}
5947+
}
5948+
59275949
func BenchmarkResponseStatusLine(b *testing.B) {
59285950
b.ReportAllocs()
59295951
b.RunParallel(func(pb *testing.PB) {

src/net/http/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2137,9 +2137,9 @@ func RedirectHandler(url string, code int) Handler {
21372137
// "/codesearch" and "codesearch.google.com/" without also taking over
21382138
// requests for "http://www.google.com/".
21392139
//
2140-
// ServeMux also takes care of sanitizing the URL request path,
2141-
// redirecting any request containing . or .. elements or repeated slashes
2142-
// to an equivalent, cleaner URL.
2140+
// ServeMux also takes care of sanitizing the URL request path and the Host
2141+
// header, stripping the port number and redirecting any request containing . or
2142+
// .. elements or repeated slashes to an equivalent, cleaner URL.
21432143
type ServeMux struct {
21442144
mu sync.RWMutex
21452145
m map[string]muxEntry

0 commit comments

Comments
 (0)