diff options
-rw-r--r-- | src/net/http/request.go | 4 | ||||
-rw-r--r-- | src/net/http/server.go | 33 |
2 files changed, 21 insertions, 16 deletions
diff --git a/src/net/http/request.go b/src/net/http/request.go index 699b31a14e..13f367c1a8 100644 --- a/src/net/http/request.go +++ b/src/net/http/request.go @@ -762,7 +762,7 @@ func validMethod(method string) bool { // exact value (instead of -1), GetBody is populated (so 307 and 308 // redirects can replay the body), and Body is set to NoBody if the // ContentLength is 0. -func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { +func NewRequest(method, url string, body io.Reader) (*Request, error) { if method == "" { // We document that "" means "GET" for Request.Method, and people have // relied on that from NewRequest, so keep that working. @@ -772,7 +772,7 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) { if !validMethod(method) { return nil, fmt.Errorf("net/http: invalid method %q", method) } - u, err := url.Parse(urlStr) + u, err := parseURL(url) // Just url.Parse (url is shadowed for godoc). if err != nil { return nil, err } diff --git a/src/net/http/server.go b/src/net/http/server.go index e18a245943..a2c3acef50 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -1958,13 +1958,14 @@ func StripPrefix(prefix string, h Handler) Handler { }) } -// Redirect replies to the request with a redirect to urlStr, +// Redirect replies to the request with a redirect to url, // which may be a path relative to the request path. // // The provided code should be in the 3xx range and is usually // StatusMovedPermanently, StatusFound or StatusSeeOther. -func Redirect(w ResponseWriter, r *Request, urlStr string, code int) { - if u, err := url.Parse(urlStr); err == nil { +func Redirect(w ResponseWriter, r *Request, url string, code int) { + // parseURL is just url.Parse (url is shadowed for godoc). + if u, err := parseURL(url); err == nil { // If url was relative, make absolute by // combining with request path. // The browser would probably do this for us, @@ -1988,39 +1989,43 @@ func Redirect(w ResponseWriter, r *Request, urlStr string, code int) { } // no leading http://server - if urlStr == "" || urlStr[0] != '/' { + if url == "" || url[0] != '/' { // make relative path absolute olddir, _ := path.Split(oldpath) - urlStr = olddir + urlStr + url = olddir + url } var query string - if i := strings.Index(urlStr, "?"); i != -1 { - urlStr, query = urlStr[:i], urlStr[i:] + if i := strings.Index(url, "?"); i != -1 { + url, query = url[:i], url[i:] } // clean up but preserve trailing slash - trailing := strings.HasSuffix(urlStr, "/") - urlStr = path.Clean(urlStr) - if trailing && !strings.HasSuffix(urlStr, "/") { - urlStr += "/" + trailing := strings.HasSuffix(url, "/") + url = path.Clean(url) + if trailing && !strings.HasSuffix(url, "/") { + url += "/" } - urlStr += query + url += query } } - w.Header().Set("Location", hexEscapeNonASCII(urlStr)) + w.Header().Set("Location", hexEscapeNonASCII(url)) w.WriteHeader(code) // RFC 2616 recommends that a short note "SHOULD" be included in the // response because older user agents may not understand 301/307. // Shouldn't send the response for POST or HEAD; that leaves GET. if r.Method == "GET" { - note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n" + note := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code] + "</a>.\n" fmt.Fprintln(w, note) } } +// parseURL is just url.Parse. It exists only so that url.Parse can be called +// in places where url is shadowed for godoc. See https://golang.org/cl/49930. +var parseURL = url.Parse + var htmlReplacer = strings.NewReplacer( "&", "&", "<", "<", |