Commit 141821d5 authored by Russ Cox's avatar Russ Cox

http: change RawPath to mean raw path, not raw everything-after-scheme.

The new meaning is more useful for both websocket and http.

R=r, petar-m, ukai
CC=golang-dev, madari
https://golang.org/cl/582043
parent c75f891a
...@@ -39,7 +39,7 @@ var reqTests = []reqTest{ ...@@ -39,7 +39,7 @@ var reqTests = []reqTest{
URL: &URL{ URL: &URL{
Raw: "http://www.techcrunch.com/", Raw: "http://www.techcrunch.com/",
Scheme: "http", Scheme: "http",
RawPath: "//www.techcrunch.com/", RawPath: "/",
Authority: "www.techcrunch.com", Authority: "www.techcrunch.com",
Userinfo: "", Userinfo: "",
Host: "www.techcrunch.com", Host: "www.techcrunch.com",
......
...@@ -23,7 +23,7 @@ var reqWriteTests = []reqWriteTest{ ...@@ -23,7 +23,7 @@ var reqWriteTests = []reqWriteTest{
URL: &URL{ URL: &URL{
Raw: "http://www.techcrunch.com/", Raw: "http://www.techcrunch.com/",
Scheme: "http", Scheme: "http",
RawPath: "//www.techcrunch.com/", RawPath: "http://www.techcrunch.com/",
Authority: "www.techcrunch.com", Authority: "www.techcrunch.com",
Userinfo: "", Userinfo: "",
Host: "www.techcrunch.com", Host: "www.techcrunch.com",
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// Parse URLs (actually URIs, but that seems overly pedantic). // Parse URLs (actually URIs, but that seems overly pedantic).
// RFC 2396 // RFC 3986
package http package http
...@@ -239,10 +239,10 @@ func urlEscape(s string, doPlus bool) string { ...@@ -239,10 +239,10 @@ func urlEscape(s string, doPlus bool) string {
type URL struct { type URL struct {
Raw string // the original string Raw string // the original string
Scheme string // scheme Scheme string // scheme
RawPath string // //[userinfo@]host/path[?query][#fragment]
Authority string // [userinfo@]host Authority string // [userinfo@]host
Userinfo string // userinfo Userinfo string // userinfo
Host string // host Host string // host
RawPath string // /path[?query][#fragment]
Path string // /path Path string // /path
RawQuery string // query RawQuery string // query
Fragment string // fragment Fragment string // fragment
...@@ -306,18 +306,22 @@ func ParseURL(rawurl string) (url *URL, err os.Error) { ...@@ -306,18 +306,22 @@ func ParseURL(rawurl string) (url *URL, err os.Error) {
if url.Scheme, path, err = getscheme(rawurl); err != nil { if url.Scheme, path, err = getscheme(rawurl); err != nil {
goto Error goto Error
} }
url.RawPath = path
// RFC 2396: a relative URI (no scheme) has a ?query, // RFC 2396: a relative URI (no scheme) has a ?query,
// but absolute URIs only have query if path begins with / // but absolute URIs only have query if path begins with /
var query string
if url.Scheme == "" || len(path) > 0 && path[0] == '/' { if url.Scheme == "" || len(path) > 0 && path[0] == '/' {
path, url.RawQuery = split(path, '?', true) path, query = split(path, '?', false)
if len(query) > 1 {
url.RawQuery = query[1:]
}
} }
// Maybe path is //authority/path // Maybe path is //authority/path
if len(path) > 2 && path[0:2] == "//" { if len(path) > 2 && path[0:2] == "//" {
url.Authority, path = split(path[2:], '/', false) url.Authority, path = split(path[2:], '/', false)
} }
url.RawPath = path + query
// If there's no @, split's default is wrong. Check explicitly. // If there's no @, split's default is wrong. Check explicitly.
if strings.Index(url.Authority, "@") < 0 { if strings.Index(url.Authority, "@") < 0 {
...@@ -357,12 +361,17 @@ Error: ...@@ -357,12 +361,17 @@ Error:
// ParseURLReference is like ParseURL but allows a trailing #fragment. // ParseURLReference is like ParseURL but allows a trailing #fragment.
func ParseURLReference(rawurlref string) (url *URL, err os.Error) { func ParseURLReference(rawurlref string) (url *URL, err os.Error) {
// Cut off #frag. // Cut off #frag.
rawurl, frag := split(rawurlref, '#', true) rawurl, frag := split(rawurlref, '#', false)
if url, err = ParseURL(rawurl); err != nil { if url, err = ParseURL(rawurl); err != nil {
return nil, err return nil, err
} }
if url.Fragment, err = urlUnescape(frag, false); err != nil { url.Raw += frag
return nil, &URLError{"parse", rawurl, err} url.RawPath += frag
if len(frag) > 1 {
frag = frag[1:]
if url.Fragment, err = urlUnescape(frag, false); err != nil {
return nil, &URLError{"parse", rawurl, err}
}
} }
return url, nil return url, nil
} }
......
...@@ -27,10 +27,10 @@ var urltests = []URLTest{ ...@@ -27,10 +27,10 @@ var urltests = []URLTest{
URLTest{ URLTest{
"http://www.google.com", "http://www.google.com",
&URL{ &URL{
"http://www.google.com", Raw: "http://www.google.com",
"http", "//www.google.com", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"", "", "", Host: "www.google.com",
}, },
"", "",
}, },
...@@ -38,21 +38,25 @@ var urltests = []URLTest{ ...@@ -38,21 +38,25 @@ var urltests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/", "http://www.google.com/",
&URL{ &URL{
"http://www.google.com/", Raw: "http://www.google.com/",
"http", "//www.google.com/", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/", "", "", Host: "www.google.com",
RawPath: "/",
Path: "/",
}, },
"", "",
}, },
// path with hex escaping... note that space roundtrips to + // path with hex escaping
URLTest{ URLTest{
"http://www.google.com/file%20one%26two", "http://www.google.com/file%20one%26two",
&URL{ &URL{
"http://www.google.com/file%20one%26two", Raw: "http://www.google.com/file%20one%26two",
"http", "//www.google.com/file%20one%26two", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/file one&two", "", "", Host: "www.google.com",
RawPath: "/file%20one%26two",
Path: "/file one&two",
}, },
"http://www.google.com/file%20one%26two", "http://www.google.com/file%20one%26two",
}, },
...@@ -60,10 +64,13 @@ var urltests = []URLTest{ ...@@ -60,10 +64,13 @@ var urltests = []URLTest{
URLTest{ URLTest{
"ftp://webmaster@www.google.com/", "ftp://webmaster@www.google.com/",
&URL{ &URL{
"ftp://webmaster@www.google.com/", Raw: "ftp://webmaster@www.google.com/",
"ftp", "//webmaster@www.google.com/", Scheme: "ftp",
"webmaster@www.google.com", "webmaster", "www.google.com", Authority: "webmaster@www.google.com",
"/", "", "", Userinfo: "webmaster",
Host: "www.google.com",
RawPath: "/",
Path: "/",
}, },
"", "",
}, },
...@@ -71,10 +78,13 @@ var urltests = []URLTest{ ...@@ -71,10 +78,13 @@ var urltests = []URLTest{
URLTest{ URLTest{
"ftp://john%20doe@www.google.com/", "ftp://john%20doe@www.google.com/",
&URL{ &URL{
"ftp://john%20doe@www.google.com/", Raw: "ftp://john%20doe@www.google.com/",
"ftp", "//john%20doe@www.google.com/", Scheme: "ftp",
"john doe@www.google.com", "john doe", "www.google.com", Authority: "john doe@www.google.com",
"/", "", "", Userinfo: "john doe",
Host: "www.google.com",
RawPath: "/",
Path: "/",
}, },
"ftp://john%20doe@www.google.com/", "ftp://john%20doe@www.google.com/",
}, },
...@@ -82,10 +92,13 @@ var urltests = []URLTest{ ...@@ -82,10 +92,13 @@ var urltests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/?q=go+language", "http://www.google.com/?q=go+language",
&URL{ &URL{
"http://www.google.com/?q=go+language", Raw: "http://www.google.com/?q=go+language",
"http", "//www.google.com/?q=go+language", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/", "q=go+language", "", Host: "www.google.com",
RawPath: "/?q=go+language",
Path: "/",
RawQuery: "q=go+language",
}, },
"", "",
}, },
...@@ -93,10 +106,13 @@ var urltests = []URLTest{ ...@@ -93,10 +106,13 @@ var urltests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/?q=go%20language", "http://www.google.com/?q=go%20language",
&URL{ &URL{
"http://www.google.com/?q=go%20language", Raw: "http://www.google.com/?q=go%20language",
"http", "//www.google.com/?q=go%20language", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/", "q=go%20language", "", Host: "www.google.com",
RawPath: "/?q=go%20language",
Path: "/",
RawQuery: "q=go%20language",
}, },
"", "",
}, },
...@@ -104,10 +120,13 @@ var urltests = []URLTest{ ...@@ -104,10 +120,13 @@ var urltests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/a%20b?q=c+d", "http://www.google.com/a%20b?q=c+d",
&URL{ &URL{
"http://www.google.com/a%20b?q=c+d", Raw: "http://www.google.com/a%20b?q=c+d",
"http", "//www.google.com/a%20b?q=c+d", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/a b", "q=c+d", "", Host: "www.google.com",
RawPath: "/a%20b?q=c+d",
Path: "/a b",
RawQuery: "q=c+d",
}, },
"", "",
}, },
...@@ -115,10 +134,10 @@ var urltests = []URLTest{ ...@@ -115,10 +134,10 @@ var urltests = []URLTest{
URLTest{ URLTest{
"http:www.google.com/?q=go+language", "http:www.google.com/?q=go+language",
&URL{ &URL{
"http:www.google.com/?q=go+language", Raw: "http:www.google.com/?q=go+language",
"http", "www.google.com/?q=go+language", Scheme: "http",
"", "", "", RawPath: "www.google.com/?q=go+language",
"www.google.com/?q=go+language", "", "", Path: "www.google.com/?q=go+language",
}, },
"http:www.google.com/%3fq%3dgo%2blanguage", "http:www.google.com/%3fq%3dgo%2blanguage",
}, },
...@@ -126,10 +145,10 @@ var urltests = []URLTest{ ...@@ -126,10 +145,10 @@ var urltests = []URLTest{
URLTest{ URLTest{
"mailto:/webmaster@golang.org", "mailto:/webmaster@golang.org",
&URL{ &URL{
"mailto:/webmaster@golang.org", Raw: "mailto:/webmaster@golang.org",
"mailto", "/webmaster@golang.org", Scheme: "mailto",
"", "", "", RawPath: "/webmaster@golang.org",
"/webmaster@golang.org", "", "", Path: "/webmaster@golang.org",
}, },
"", "",
}, },
...@@ -137,10 +156,10 @@ var urltests = []URLTest{ ...@@ -137,10 +156,10 @@ var urltests = []URLTest{
URLTest{ URLTest{
"mailto:webmaster@golang.org", "mailto:webmaster@golang.org",
&URL{ &URL{
"mailto:webmaster@golang.org", Raw: "mailto:webmaster@golang.org",
"mailto", "webmaster@golang.org", Scheme: "mailto",
"", "", "", RawPath: "webmaster@golang.org",
"webmaster@golang.org", "", "", Path: "webmaster@golang.org",
}, },
"", "",
}, },
...@@ -148,10 +167,10 @@ var urltests = []URLTest{ ...@@ -148,10 +167,10 @@ var urltests = []URLTest{
URLTest{ URLTest{
"/foo?query=http://bad", "/foo?query=http://bad",
&URL{ &URL{
"/foo?query=http://bad", Raw: "/foo?query=http://bad",
"", "/foo?query=http://bad", RawPath: "/foo?query=http://bad",
"", "", "", Path: "/foo",
"/foo", "query=http://bad", "", RawQuery: "query=http://bad",
}, },
"", "",
}, },
...@@ -161,10 +180,13 @@ var urlnofragtests = []URLTest{ ...@@ -161,10 +180,13 @@ var urlnofragtests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/?q=go+language#foo", "http://www.google.com/?q=go+language#foo",
&URL{ &URL{
"http://www.google.com/?q=go+language#foo", Raw: "http://www.google.com/?q=go+language#foo",
"http", "//www.google.com/?q=go+language#foo", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/", "q=go+language#foo", "", Host: "www.google.com",
RawPath: "/?q=go+language#foo",
Path: "/",
RawQuery: "q=go+language#foo",
}, },
"", "",
}, },
...@@ -174,20 +196,28 @@ var urlfragtests = []URLTest{ ...@@ -174,20 +196,28 @@ var urlfragtests = []URLTest{
URLTest{ URLTest{
"http://www.google.com/?q=go+language#foo", "http://www.google.com/?q=go+language#foo",
&URL{ &URL{
"http://www.google.com/?q=go+language", Raw: "http://www.google.com/?q=go+language#foo",
"http", "//www.google.com/?q=go+language", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/", "q=go+language", "foo", Host: "www.google.com",
RawPath: "/?q=go+language#foo",
Path: "/",
RawQuery: "q=go+language",
Fragment: "foo",
}, },
"", "",
}, },
URLTest{ URLTest{
"http://www.google.com/?q=go+language#foo%26bar", "http://www.google.com/?q=go+language#foo%26bar",
&URL{ &URL{
"http://www.google.com/?q=go+language", Raw: "http://www.google.com/?q=go+language#foo%26bar",
"http", "//www.google.com/?q=go+language", Scheme: "http",
"www.google.com", "", "www.google.com", Authority: "www.google.com",
"/", "q=go+language", "foo&bar", Host: "www.google.com",
RawPath: "/?q=go+language#foo%26bar",
Path: "/",
RawQuery: "q=go+language",
Fragment: "foo&bar",
}, },
"", "",
}, },
......
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