diff --git a/fasttld.go b/fasttld.go index 034f2ed..08201d7 100644 --- a/fasttld.go +++ b/fasttld.go @@ -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 diff --git a/fasttld_test.go b/fasttld_test.go index ec7b844..546a4be 100644 --- a/fasttld_test.go +++ b/fasttld_test.go @@ -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"}, diff --git a/strings.go b/strings.go index 7e83e4a..4908dd3 100644 --- a/strings.go +++ b/strings.go @@ -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 {