Commit cc928b7b authored by Russ Cox's avatar Russ Cox

http: fix text displayed in Redirect

In the case where r.Method == "POST", was
calling Printf with an argument but "" format string,
causing a spurious %!EXTRA(...) message.

Also escape string properly in HTML generation.

R=r
CC=golang-dev
https://golang.org/cl/3923043
parent 3a43ff1a
...@@ -452,58 +452,63 @@ func NotFoundHandler() Handler { return HandlerFunc(NotFound) } ...@@ -452,58 +452,63 @@ func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
// Redirect replies to the request with a redirect to url, // Redirect replies to the request with a redirect to url,
// which may be a path relative to the request path. // which may be a path relative to the request path.
func Redirect(w ResponseWriter, r *Request, url string, code int) { func Redirect(w ResponseWriter, r *Request, url string, code int) {
// RFC2616 recommends that a short note "SHOULD" be included in the if u, err := ParseURL(url); err == nil {
// response because older user agents may not understand 301/307. // If url was relative, make absolute by
note := "<a href=\"%v\">" + statusText[code] + "</a>.\n" // combining with request path.
if r.Method == "POST" { // The browser would probably do this for us,
note = "" // but doing it ourselves is more reliable.
}
// NOTE(rsc): RFC 2616 says that the Location
u, err := ParseURL(url) // line must be an absolute URI, like
if err != nil { // "http://www.google.com/redirect/",
goto finish // not a path like "/redirect/".
} // Unfortunately, we don't know what to
// put in the host name section to get the
// If url was relative, make absolute by // client to connect to us again, so we can't
// combining with request path. // know the right absolute URI to send back.
// The browser would probably do this for us, // Because of this problem, no one pays attention
// but doing it ourselves is more reliable. // to the RFC; they all send back just a new path.
// So do we.
// NOTE(rsc): RFC 2616 says that the Location oldpath := r.URL.Path
// line must be an absolute URI, like if oldpath == "" { // should not happen, but avoid a crash if it does
// "http://www.google.com/redirect/", oldpath = "/"
// not a path like "/redirect/".
// Unfortunately, we don't know what to
// put in the host name section to get the
// client to connect to us again, so we can't
// know the right absolute URI to send back.
// Because of this problem, no one pays attention
// to the RFC; they all send back just a new path.
// So do we.
oldpath := r.URL.Path
if oldpath == "" { // should not happen, but avoid a crash if it does
oldpath = "/"
}
if u.Scheme == "" {
// no leading http://server
if url == "" || url[0] != '/' {
// make relative path absolute
olddir, _ := path.Split(oldpath)
url = olddir + url
} }
if u.Scheme == "" {
// no leading http://server
if url == "" || url[0] != '/' {
// make relative path absolute
olddir, _ := path.Split(oldpath)
url = olddir + url
}
// clean up but preserve trailing slash // clean up but preserve trailing slash
trailing := url[len(url)-1] == '/' trailing := url[len(url)-1] == '/'
url = path.Clean(url) url = path.Clean(url)
if trailing && url[len(url)-1] != '/' { if trailing && url[len(url)-1] != '/' {
url += "/" url += "/"
}
} }
} }
finish:
w.SetHeader("Location", url) w.SetHeader("Location", url)
w.WriteHeader(code) w.WriteHeader(code)
fmt.Fprintf(w, note, url)
// RFC2616 recommends that a short note "SHOULD" be included in the
// response because older user agents may not understand 301/307.
note := "<a href=\"" + htmlEscape(url) + "\">" + statusText[code] + "</a>.\n"
if r.Method == "POST" {
note = ""
}
fmt.Fprintln(w, note)
}
func htmlEscape(s string) string {
s = strings.Replace(s, "&", "&amp;", -1)
s = strings.Replace(s, "<", "&lt;", -1)
s = strings.Replace(s, ">", "&gt;", -1)
s = strings.Replace(s, "\"", "&quot;", -1)
s = strings.Replace(s, "'", "&apos;", -1)
return s
} }
// Redirect to a fixed URL // Redirect to a fixed URL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment