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

Adding support for unescaped request uri #2

Merged
merged 3 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion urlutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,23 @@ func ParseWithScheme(u string) (*URL, error) {
// prepend default scheme if absent to increase parsing capabilities
u = PreprendDefaultScheme(u)

var origReqURI string
U, err := url.Parse(u)
if err != nil {
return nil, err
// try to reparse without the request path
// attempt to find the forward slash at the beginning of the path
schemeDoubleForwardSlash := strings.Index(u, "//") + 2
forwardSlashPosition := schemeDoubleForwardSlash + strings.Index(u[schemeDoubleForwardSlash:], "/")
if forwardSlashPosition > 0 {
origReqURI = u[forwardSlashPosition:]
u = u[:forwardSlashPosition]
U, err = url.Parse(u)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}

// attempts to infer port
Expand Down Expand Up @@ -84,6 +98,9 @@ func ParseWithScheme(u string) (*URL, error) {
if ruri := U.RequestURI(); ruri != "/" {
requri = ruri
}
if origReqURI != "" && origReqURI != "/" {
requri = origReqURI
}

return &URL{
Scheme: scheme,
Expand Down
5 changes: 5 additions & 0 deletions urlutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ func TestParse(t *testing.T) {
require.Nil(t, err, "could not parse url")
U.Port = "15000"
require.Equal(t, "https://a.b.c.d:15000", U.String(), "port not replaced")

// replacing port
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")
}