]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: document and test behavior of ServeMux with ports
authorGuilherme Goncalves <guilhermeaugustosg@gmail.com>
Sat, 23 Jun 2018 22:32:26 +0000 (22:32 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 25 Jun 2018 19:12:51 +0000 (19:12 +0000)
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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/serve_test.go
src/net/http/server.go

index 4e5741ed9084e53cf71127692fbb9303870ffd7d..de76f5eab0c958de2ddfd93f8e4aeb589c1c7f93 100644 (file)
@@ -5924,6 +5924,28 @@ func TestServerCloseListenerOnce(t *testing.T) {
        }
 }
 
+// Issue 23351: document and test behavior of ServeMux with ports
+func TestStripPortFromHost(t *testing.T) {
+       mux := NewServeMux()
+
+       mux.HandleFunc("example.com/", func(w ResponseWriter, r *Request) {
+               fmt.Fprintf(w, "OK")
+       })
+       mux.HandleFunc("example.com:9000/", func(w ResponseWriter, r *Request) {
+               fmt.Fprintf(w, "uh-oh!")
+       })
+
+       req := httptest.NewRequest("GET", "http://example.com:9000/", nil)
+       rw := httptest.NewRecorder()
+
+       mux.ServeHTTP(rw, req)
+
+       response := rw.Body.String()
+       if response != "OK" {
+               t.Errorf("Response gotten was %q", response)
+       }
+}
+
 func BenchmarkResponseStatusLine(b *testing.B) {
        b.ReportAllocs()
        b.RunParallel(func(pb *testing.PB) {
index c244b372fc224e04f3cbe5d87fd5fc869d1a5af7..edc19c3a4c5d5d05f5ad0d1d66688f69f5c8579d 100644 (file)
@@ -2137,9 +2137,9 @@ func RedirectHandler(url string, code int) Handler {
 // "/codesearch" and "codesearch.google.com/" without also taking over
 // requests for "http://www.google.com/".
 //
-// ServeMux also takes care of sanitizing the URL request path,
-// redirecting any request containing . or .. elements or repeated slashes
-// to an equivalent, cleaner URL.
+// ServeMux also takes care of sanitizing the URL request path and the Host
+// header, stripping the port number and redirecting any request containing . or
+// .. elements or repeated slashes to an equivalent, cleaner URL.
 type ServeMux struct {
        mu    sync.RWMutex
        m     map[string]muxEntry