Skip to content

Commit 2abd8ae

Browse files
dmitshurbradfitz
authored andcommitted
net/http: improve signature of Redirect, NewRequest
In CL https://golang.org/cl/4893043 (6 years ago), a new package named "url" was created (it is currently known as "net/url"). During that change, some identifier name collisions were introduced, and two parameters in net/http were renamed to "urlStr". Since that time, Go has continued to put high emphasis on the quality and readability of the documentation. Sometimes, that means making small sacrifices in the implementation details of a package to ensure that the godoc reads better, since that's what the majority of users interact with. See https://golang.org/s/style#named-result-parameters: > Clarity of docs is always more important than saving a line or two > in your function. I think the "urlStr" parameter name is suboptimal for godoc purposes, and just "url" would be better. During the review of https://golang.org/cl/4893043, it was also noted by @rsc that having to rename parameters named "url" was suboptimal: > It's unfortunate that naming the package url means > you can't have a parameter or variable named url. However, at the time, the name of the url package was still being decided, and uri was an alternative name under consideration. The reason urlStr was chosen is because it was a lesser evil compared to naming the url package uri instead: > Let's not get hung up on URI vs. URL, but I'd like s/uri/urlStr/ even for just > that the "i" in "uri" looks very similar to the "l" in "url" in many fonts. > Please let's go with urlStr instead of uri. Now that we have the Go 1 compatibility guarantee, the name of the net/url package is fixed. However, it's possible to improve the signature of Redirect, NewRequest functions in net/http package for godoc purposes by creating a package global alias to url.Parse, and renaming urlStr parameter to url in the exported funcs. This CL does so. Updates #21077. Change-Id: Ibcc10e3825863a663e6ad91b6eb47b1862a299a6 Reviewed-on: https://go-review.googlesource.com/49930 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 9e859d5 commit 2abd8ae

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/net/http/request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ func validMethod(method string) bool {
762762
// exact value (instead of -1), GetBody is populated (so 307 and 308
763763
// redirects can replay the body), and Body is set to NoBody if the
764764
// ContentLength is 0.
765-
func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
765+
func NewRequest(method, url string, body io.Reader) (*Request, error) {
766766
if method == "" {
767767
// We document that "" means "GET" for Request.Method, and people have
768768
// relied on that from NewRequest, so keep that working.
@@ -772,7 +772,7 @@ func NewRequest(method, urlStr string, body io.Reader) (*Request, error) {
772772
if !validMethod(method) {
773773
return nil, fmt.Errorf("net/http: invalid method %q", method)
774774
}
775-
u, err := url.Parse(urlStr)
775+
u, err := parseURL(url) // Just url.Parse (url is shadowed for godoc).
776776
if err != nil {
777777
return nil, err
778778
}

src/net/http/server.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,13 +1958,14 @@ func StripPrefix(prefix string, h Handler) Handler {
19581958
})
19591959
}
19601960

1961-
// Redirect replies to the request with a redirect to urlStr,
1961+
// Redirect replies to the request with a redirect to url,
19621962
// which may be a path relative to the request path.
19631963
//
19641964
// The provided code should be in the 3xx range and is usually
19651965
// StatusMovedPermanently, StatusFound or StatusSeeOther.
1966-
func Redirect(w ResponseWriter, r *Request, urlStr string, code int) {
1967-
if u, err := url.Parse(urlStr); err == nil {
1966+
func Redirect(w ResponseWriter, r *Request, url string, code int) {
1967+
// parseURL is just url.Parse (url is shadowed for godoc).
1968+
if u, err := parseURL(url); err == nil {
19681969
// If url was relative, make absolute by
19691970
// combining with request path.
19701971
// The browser would probably do this for us,
@@ -1988,39 +1989,43 @@ func Redirect(w ResponseWriter, r *Request, urlStr string, code int) {
19881989
}
19891990

19901991
// no leading http://server
1991-
if urlStr == "" || urlStr[0] != '/' {
1992+
if url == "" || url[0] != '/' {
19921993
// make relative path absolute
19931994
olddir, _ := path.Split(oldpath)
1994-
urlStr = olddir + urlStr
1995+
url = olddir + url
19951996
}
19961997

19971998
var query string
1998-
if i := strings.Index(urlStr, "?"); i != -1 {
1999-
urlStr, query = urlStr[:i], urlStr[i:]
1999+
if i := strings.Index(url, "?"); i != -1 {
2000+
url, query = url[:i], url[i:]
20002001
}
20012002

20022003
// clean up but preserve trailing slash
2003-
trailing := strings.HasSuffix(urlStr, "/")
2004-
urlStr = path.Clean(urlStr)
2005-
if trailing && !strings.HasSuffix(urlStr, "/") {
2006-
urlStr += "/"
2004+
trailing := strings.HasSuffix(url, "/")
2005+
url = path.Clean(url)
2006+
if trailing && !strings.HasSuffix(url, "/") {
2007+
url += "/"
20072008
}
2008-
urlStr += query
2009+
url += query
20092010
}
20102011
}
20112012

2012-
w.Header().Set("Location", hexEscapeNonASCII(urlStr))
2013+
w.Header().Set("Location", hexEscapeNonASCII(url))
20132014
w.WriteHeader(code)
20142015

20152016
// RFC 2616 recommends that a short note "SHOULD" be included in the
20162017
// response because older user agents may not understand 301/307.
20172018
// Shouldn't send the response for POST or HEAD; that leaves GET.
20182019
if r.Method == "GET" {
2019-
note := "<a href=\"" + htmlEscape(urlStr) + "\">" + statusText[code] + "</a>.\n"
2020+
note := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code] + "</a>.\n"
20202021
fmt.Fprintln(w, note)
20212022
}
20222023
}
20232024

2025+
// parseURL is just url.Parse. It exists only so that url.Parse can be called
2026+
// in places where url is shadowed for godoc. See https://golang.org/cl/49930.
2027+
var parseURL = url.Parse
2028+
20242029
var htmlReplacer = strings.NewReplacer(
20252030
"&", "&amp;",
20262031
"<", "&lt;",

0 commit comments

Comments
 (0)