Merge pull request #50 from kristofferpeterhansel/master

Fix parsing of fragments
This commit is contained in:
Aliaksandr Valialkin
2016-02-09 20:03:25 +02:00
2 changed files with 29 additions and 10 deletions
+28 -10
View File
@@ -203,23 +203,41 @@ func (x *URI) parse(host, uri []byte, h *RequestHeader) {
lowercaseBytes(x.host)
b := uri
n := bytes.IndexByte(b, '?')
if n < 0 {
queryIndex := bytes.IndexByte(b, '?')
fragmentIndex := bytes.IndexByte(b, '#')
// Ignore query in fragment part
if fragmentIndex >= 0 && queryIndex > fragmentIndex {
queryIndex = -1
}
if queryIndex < 0 && fragmentIndex < 0 {
x.pathOriginal = append(x.pathOriginal, b...)
x.path = normalizePath(x.path, b)
return
}
x.pathOriginal = append(x.pathOriginal, b[:n]...)
x.path = normalizePath(x.path, x.pathOriginal)
b = b[n+1:]
n = bytes.IndexByte(b, '#')
if n >= 0 {
x.hash = append(x.hash, b[n+1:]...)
b = b[:n]
if queryIndex >= 0 {
// Path is everything up to the start of the query
x.pathOriginal = append(x.pathOriginal, b[:queryIndex]...)
x.path = normalizePath(x.path, x.pathOriginal)
if fragmentIndex < 0 {
x.queryString = append(x.queryString, b[queryIndex+1:]...)
} else {
x.queryString = append(x.queryString, b[queryIndex+1:fragmentIndex]...)
}
}
x.queryString = append(x.queryString, b...)
if fragmentIndex >= 0 {
if queryIndex < 0 {
// Path is up to the start of fragment. Unless a query was before it
x.pathOriginal = append(x.pathOriginal, b[:fragmentIndex]...)
x.path = normalizePath(x.path, x.pathOriginal)
}
x.hash = append(x.hash, b[fragmentIndex+1:]...)
b = b[:fragmentIndex]
}
}
func normalizePath(dst, src []byte) []byte {
+1
View File
@@ -52,6 +52,7 @@ func TestURIUpdate(t *testing.T) {
testURIUpdate(t, "http://xx/a/b/c/d", "../qwe/p?zx=34", "http://xx/a/b/qwe/p?zx=34")
testURIUpdate(t, "https://qqq/aaa.html?foo=bar", "?baz=434&aaa", "https://qqq/aaa.html?baz=434&aaa")
testURIUpdate(t, "http://foo.bar/baz", "~a/%20b=c,тест?йцу=ке", "http://foo.bar/~a/%20b=c,%D1%82%D0%B5%D1%81%D1%82?йцу=ке")
testURIUpdate(t, "http://foo.bar/baz", "/qwe#fragment", "http://foo.bar/qwe#fragment")
}
func testURIUpdate(t *testing.T, base, update, result string) {