Skip to content

Commit f1e8803

Browse files
kennygrantbradfitz
authored andcommitted
net/http: strip port from host in mux Handler
This change strips the port in mux.Handler before attempting to match handlers and adds a test for a request with port. CONNECT requests continue to use the original path and port. Fixes #10463 Change-Id: Iff3a2ca2b7f1d884eca05a7262ad6b7dffbcc30f Reviewed-on: https://go-review.googlesource.com/38194 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 369d108 commit f1e8803

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/net/http/serve_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ var serveMuxTests = []struct {
337337
{"GET", "codesearch.google.com", "/search/", 203, "codesearch.google.com/"},
338338
{"GET", "codesearch.google.com", "/search/foo", 203, "codesearch.google.com/"},
339339
{"GET", "codesearch.google.com", "/", 203, "codesearch.google.com/"},
340+
{"GET", "codesearch.google.com:443", "/", 203, "codesearch.google.com/"},
340341
{"GET", "images.google.com", "/search", 201, "/search"},
341342
{"GET", "images.google.com", "/search/", 404, ""},
342343
{"GET", "images.google.com", "/search/foo", 404, ""},

src/net/http/server.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,19 @@ func cleanPath(p string) string {
21672167
return np
21682168
}
21692169

2170+
// stripHostPort returns h without any trailing ":<port>".
2171+
func stripHostPort(h string) string {
2172+
// If no port on host, return unchanged
2173+
if strings.IndexByte(h, ':') == -1 {
2174+
return h
2175+
}
2176+
host, _, err := net.SplitHostPort(h)
2177+
if err != nil {
2178+
return h // on error, return unchanged
2179+
}
2180+
return host
2181+
}
2182+
21702183
// Find a handler on a handler map given a path string.
21712184
// Most-specific (longest) pattern wins.
21722185
func (mux *ServeMux) match(path string) (h Handler, pattern string) {
@@ -2195,7 +2208,10 @@ func (mux *ServeMux) match(path string) (h Handler, pattern string) {
21952208
// consulting r.Method, r.Host, and r.URL.Path. It always returns
21962209
// a non-nil handler. If the path is not in its canonical form, the
21972210
// handler will be an internally-generated handler that redirects
2198-
// to the canonical path.
2211+
// to the canonical path. If the host contains a port, it is ignored
2212+
// when matching handlers.
2213+
//
2214+
// The path and host are used unchanged for CONNECT requests.
21992215
//
22002216
// Handler also returns the registered pattern that matches the
22012217
// request or, in the case of internally-generated redirects,
@@ -2204,16 +2220,24 @@ func (mux *ServeMux) match(path string) (h Handler, pattern string) {
22042220
// If there is no registered handler that applies to the request,
22052221
// Handler returns a ``page not found'' handler and an empty pattern.
22062222
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
2207-
if r.Method != "CONNECT" {
2208-
if p := cleanPath(r.URL.Path); p != r.URL.Path {
2209-
_, pattern = mux.handler(r.Host, p)
2210-
url := *r.URL
2211-
url.Path = p
2212-
return RedirectHandler(url.String(), StatusMovedPermanently), pattern
2213-
}
2223+
2224+
// CONNECT requests are not canonicalized.
2225+
if r.Method == "CONNECT" {
2226+
return mux.handler(r.Host, r.URL.Path)
2227+
}
2228+
2229+
// All other requests have any port stripped and path cleaned
2230+
// before passing to mux.handler.
2231+
host := stripHostPort(r.Host)
2232+
path := cleanPath(r.URL.Path)
2233+
if path != r.URL.Path {
2234+
_, pattern = mux.handler(host, path)
2235+
url := *r.URL
2236+
url.Path = path
2237+
return RedirectHandler(url.String(), StatusMovedPermanently), pattern
22142238
}
22152239

2216-
return mux.handler(r.Host, r.URL.Path)
2240+
return mux.handler(host, r.URL.Path)
22172241
}
22182242

22192243
// handler is the main implementation of Handler.

0 commit comments

Comments
 (0)