Skip to content

Commit

Permalink
bugfix: handle square brackets in Path
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotwutingfeng committed Jun 2, 2022
1 parent 12481ab commit 6fdbd96
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fasttld.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func (f *FastTLD) Extract(e URLParams) *ExtractResult {

// Check for IPv6 address
var netlocIsIPv6 bool
openingSquareBracketIdx := strings.IndexByte(netloc, '[')
openingSquareBracketIdx := indexFirstByteBefore(netloc, '[', endOfHostWithPortDelimitersSet)
if openingSquareBracketIdx > 0 {
// Erroneous opening square bracket
return &urlParts
}
closingSquareBracketIdx := strings.IndexByte(netloc, ']')
closingSquareBracketIdx := indexFirstByteBefore(netloc, ']', endOfHostWithPortDelimitersSet)
if openingSquareBracketIdx == 0 {
if !(closingSquareBracketIdx > 0 && isIPv6(netloc[1:closingSquareBracketIdx])) {
// Have opening square bracket but invalid IPv6 => Domain is invalid
Expand Down
1 change: 1 addition & 0 deletions fasttld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ var domainOnlySingleTLDTests = []extractTest{
}
var pathTests = []extractTest{
{urlParams: URLParams{URL: "http://www.example.com/this:that"}, expected: &ExtractResult{Scheme: "http://", SubDomain: "www", Domain: "example", Suffix: "com", RegisteredDomain: "example.com", Path: "/this:that"}, description: "Colon in Path"},
{urlParams: URLParams{URL: "http://example.com/oid/[order_id]"}, expected: &ExtractResult{Scheme: "http://", Domain: "example", Suffix: "com", RegisteredDomain: "example.com", Path: "/oid/[order_id]"}, description: "Square brackets in Path"},
}
var wildcardTests = []extractTest{
{urlParams: URLParams{URL: "https://asdf.wwe.ck"},
Expand Down
9 changes: 9 additions & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ func makeNewReplacerParams(toBeReplaced string, toReplaceWith string) []string {
return params
}

// indexFirstByteBefore returns the index of the first instance of byte b
// before any byte in notAfterCharsSet, otherwise -1
func indexFirstByteBefore(s string, b byte, notAfterCharsSet asciiSet) int {
if firstNotAfterCharIdx := indexAnyASCII(s, notAfterCharsSet); firstNotAfterCharIdx != -1 {
return strings.IndexByte(s[0:firstNotAfterCharIdx], b)
}
return strings.IndexByte(s, b)
}

// indexLastByteBefore returns the index of the last instance of byte b
// before any byte in notAfterCharsSet, otherwise -1
func indexLastByteBefore(s string, b byte, notAfterCharsSet asciiSet) int {
Expand Down

0 comments on commit 6fdbd96

Please sign in to comment.