Skip to content

Commit

Permalink
Don't send the fragment/hash/# part of a URL to the server
Browse files Browse the repository at this point in the history
Fixes #748
  • Loading branch information
erikdubbelboer committed Feb 28, 2020
1 parent aa96a47 commit 76b74e3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
15 changes: 15 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import (
"github.com/valyala/bytebufferpool"
)

// Don't send the fragment/hash/# part of a URL to the server.
func TestFragmentInURIRequest(t *testing.T) {
var req Request
req.SetRequestURI("https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#events")

var b bytes.Buffer
req.WriteTo(&b)
got := b.String()
expected := "GET /ee/user/project/integrations/webhooks.html HTTP/1.1\r\nHost: docs.gitlab.com\r\n\r\n"

if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}

func TestRequestCopyTo(t *testing.T) {
t.Parallel()

Expand Down
11 changes: 6 additions & 5 deletions uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,6 @@ func (u *URI) RequestURI() []byte {
dst = append(dst, '?')
dst = append(dst, u.queryString...)
}
if len(u.hash) > 0 {
dst = append(dst, '#')
dst = append(dst, u.hash...)
}
u.requestURI = dst
return u.requestURI
}
Expand Down Expand Up @@ -519,7 +515,12 @@ func (u *URI) FullURI() []byte {
// AppendBytes appends full uri to dst and returns the extended dst.
func (u *URI) AppendBytes(dst []byte) []byte {
dst = u.appendSchemeHost(dst)
return append(dst, u.RequestURI()...)
dst = append(dst, u.RequestURI()...)
if len(u.hash) > 0 {
dst = append(dst, '#')
dst = append(dst, u.hash...)
}
return dst
}

func (u *URI) appendSchemeHost(dst []byte) []byte {
Expand Down
19 changes: 11 additions & 8 deletions uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,18 @@ func testURIFullURI(t *testing.T, scheme, host, path, hash string, args *Args, e
}

func TestURIParseNilHost(t *testing.T) {
testURIParseScheme(t, "http://google.com/foo?bar#baz", "http", "google.com", "/foo?bar#baz")
testURIParseScheme(t, "HTtP://google.com/", "http", "google.com", "/")
testURIParseScheme(t, "://google.com/xyz", "http", "google.com", "/xyz")
testURIParseScheme(t, "//google.com/foobar", "http", "google.com", "/foobar")
testURIParseScheme(t, "fTP://aaa.com", "ftp", "aaa.com", "/")
testURIParseScheme(t, "httPS://aaa.com", "https", "aaa.com", "/")
testURIParseScheme(t, "http://google.com/foo?bar#baz", "http", "google.com", "/foo?bar", "baz")
testURIParseScheme(t, "HTtP://google.com/", "http", "google.com", "/", "")
testURIParseScheme(t, "://google.com/xyz", "http", "google.com", "/xyz", "")
testURIParseScheme(t, "//google.com/foobar", "http", "google.com", "/foobar", "")
testURIParseScheme(t, "fTP://aaa.com", "ftp", "aaa.com", "/", "")
testURIParseScheme(t, "httPS://aaa.com", "https", "aaa.com", "/", "")

// missing slash after hostname
testURIParseScheme(t, "http://foobar.com?baz=111", "http", "foobar.com", "/?baz=111")
testURIParseScheme(t, "http://foobar.com?baz=111", "http", "foobar.com", "/?baz=111", "")
}

func testURIParseScheme(t *testing.T, uri, expectedScheme, expectedHost, expectedRequestURI string) {
func testURIParseScheme(t *testing.T, uri, expectedScheme, expectedHost, expectedRequestURI, expectedHash string) {
var u URI
u.Parse(nil, []byte(uri))
if string(u.Scheme()) != expectedScheme {
Expand All @@ -297,6 +297,9 @@ func testURIParseScheme(t *testing.T, uri, expectedScheme, expectedHost, expecte
if string(u.RequestURI()) != expectedRequestURI {
t.Fatalf("Unexepcted requestURI %q. Expecting %q for uri %q", u.RequestURI(), expectedRequestURI, uri)
}
if string(u.hash) != expectedHash {
t.Fatalf("Unexepcted hash %q. Expecting %q for uri %q", u.hash, expectedHash, uri)
}
}

func TestURIParse(t *testing.T) {
Expand Down

0 comments on commit 76b74e3

Please sign in to comment.