From cff99ba167a9eb6f134a576f0438390ddacba38d Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 15 Apr 2009 18:40:55 -0700 Subject: make Location translate relative path to absolute (HTTP requires absolute in protocol). add URL tests R=r DELTA=243 (242 added, 0 deleted, 1 changed) OCL=27472 CL=27523 --- src/lib/http/server.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'src/lib/http/server.go') diff --git a/src/lib/http/server.go b/src/lib/http/server.go index a8aef01f0e..267e9e41e4 100644 --- a/src/lib/http/server.go +++ b/src/lib/http/server.go @@ -269,6 +269,49 @@ func NotFoundHandler() Handler { // Redirect replies to the request with a redirect to url, // which may be a path relative to the request path. func Redirect(c *Conn, url string) { + u, err := ParseURL(url); + if err != nil { + // TODO report internal error instead? + c.SetHeader("Location", url); + c.WriteHeader(StatusMovedPermanently); + } + + // If url was relative, make absolute by + // combining with request path. + // The browser would probably do this for us, + // but doing it ourselves is more reliable. + + // NOTE(rsc): RFC 2616 says that the Location + // line must be an absolute URI, like + // "http://www.google.com/redirect/", + // 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 := c.Req.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, oldfile := path.Split(oldpath); + url = olddir + url; + } + + // clean up but preserve trailing slash + trailing := url[len(url) - 1] == '/'; + url = path.Clean(url); + if trailing && url[len(url) - 1] != '/' { + url += "/"; + } + } + c.SetHeader("Location", url); c.WriteHeader(StatusMovedPermanently); } -- cgit v1.3-5-g45d5