From 3ef4cd4a8f2b2a72f20c84949fed2c8ea9be5e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E4=B9=94=E4=BC=A6?= Date: Tue, 14 Aug 2018 16:15:22 +0800 Subject: [PATCH] We removed the content before the symbol '@' in the url --- domainutil/util.go | 9 ++++++++- domainutil/util_test.go | 29 ++++++++++++++++------------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/domainutil/util.go b/domainutil/util.go index 6c376f0..e374036 100644 --- a/domainutil/util.go +++ b/domainutil/util.go @@ -2,6 +2,7 @@ package domainutil import ( "strings" + "golang.org/x/net/idna" ) @@ -83,13 +84,19 @@ func stripURLParts(url string) string { url = url[index+3:] } + // Now, if the url looks like this: username:password@www.example.com/path?query=? + // we remove the content before the '@' symbol + if index := strings.Index(url, "@"); index > -1 { + url = url[index+1:] + } + //Strip path (and query with it) if index := strings.Index(url, "/"); index > -1 { url = url[:index] } else if index := strings.Index(url, "?"); index > -1 { //Strip query if path is not found url = url[:index] } - + //Convert domain to unicode if strings.Index(url, "xn--") != -1 { var err error diff --git a/domainutil/util_test.go b/domainutil/util_test.go index 612e2b3..97ed13f 100644 --- a/domainutil/util_test.go +++ b/domainutil/util_test.go @@ -168,19 +168,22 @@ func BenchmarkDomain(b *testing.B) { func TestStripURLParts(t *testing.T) { //Test cases cases := map[string]string{ - "http://google.com": "google.com", - "http://google.com/ding?true": "google.com", - "google.com/?ding=false": "google.com", - "google.com?ding=false": "google.com", - "nonexist.***": "nonexist.***", - "google.com": "google.com", - "google.co.uk": "google.co.uk", - "gama.google.com": "gama.google.com", - "gama.google.co.uk": "gama.google.co.uk", - "beta.gama.google.co.uk": "beta.gama.google.co.uk", - "https://beta.gama.google.co.uk": "beta.gama.google.co.uk", - "xn--n3h.example": "☃.example", - "xn--äää": "", + "http://google.com": "google.com", + "http://google.com/ding?true": "google.com", + "google.com/?ding=false": "google.com", + "google.com?ding=false": "google.com", + "nonexist.***": "nonexist.***", + "google.com": "google.com", + "google.co.uk": "google.co.uk", + "gama.google.com": "gama.google.com", + "gama.google.co.uk": "gama.google.co.uk", + "beta.gama.google.co.uk": "beta.gama.google.co.uk", + "https://beta.gama.google.co.uk": "beta.gama.google.co.uk", + "xn--n3h.example": "☃.example", + "xn--äää": "", + "http://admin:adminpw@google.com": "google.com", + "admin:adminpw@gama.google.com": "gama.google.com", + "https://admin:adminpw@gama.google.com/path?key=value": "gama.google.com", } //Test each domain, some should fail (expected)