Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from projectdiscovery/url-fragment
Browse files Browse the repository at this point in the history
added fragment
  • Loading branch information
Mzack9999 authored Jun 3, 2022
2 parents ac626c1 + 7c0de38 commit f4c60e5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions urlutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type URL struct {
Host string
Port string
RequestURI string
Fragment string
}

func (u URL) String() string {
Expand All @@ -40,6 +41,12 @@ func (u URL) String() string {
fullURL.WriteString(":" + u.Port)
}
fullURL.WriteString(u.RequestURI)
if u.Fragment != "" {
if u.RequestURI == "" {
fullURL.WriteString("/")
}
fullURL.WriteString("#" + u.Fragment)
}
return fullURL.String()
}

Expand Down Expand Up @@ -102,13 +109,17 @@ func ParseWithScheme(u string) (*URL, error) {
requri = origReqURI
}

// fragment
fragment := U.Fragment

return &URL{
Scheme: scheme,
Host: host,
Username: username,
Password: password,
Port: port,
RequestURI: requri,
Fragment: fragment,
}, nil
}

Expand Down
8 changes: 8 additions & 0 deletions urlutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ func TestParse(t *testing.T) {
U, err = Parse("https://a.b.c.d//d")
require.Nil(t, err, "could not parse url")
require.Equal(t, "https://a.b.c.d:443//d", U.String(), "unexpected url")

// fragmented url
U, err = Parse("http://127.0.0.1/#a")
require.Nil(t, err, "could not parse url")
require.Equal(t, "http", U.Scheme, "different scheme")
require.Equal(t, "127.0.0.1", U.Host, "different host")
require.Equal(t, "a", U.Fragment, "different fragment")
require.Equal(t, "http://127.0.0.1:80/#a", U.String(), "different full url")
}

0 comments on commit f4c60e5

Please sign in to comment.