-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
202 additions
and
60 deletions.
There are no files selected for viewing
Empty file.
1 change: 1 addition & 0 deletions
1
internal/embedded/ch_migrations/20240124130010_add_pageviews_referrer_domain.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE prisme.events_pageviews ADD COLUMN referrer_domain String DEFAULT 'direct'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"golang.org/x/net/idna" | ||
) | ||
|
||
// ReferrerDomain define an HTTP referral. A referral is either direct (empty string) | ||
// or a valid absolute URL from which domain is extracted. | ||
type ReferrerDomain struct { | ||
value string | ||
} | ||
|
||
// ParseReferrerDomain parses the given value as a referrer and returns it. | ||
// An error is returned if the value is not a valid referrer. | ||
func ParseReferrerDomain(value string) (ReferrerDomain, error) { | ||
// Direct source. | ||
if value == "" { | ||
return ReferrerDomain{}, nil | ||
} | ||
|
||
u, err := url.ParseRequestURI(value) | ||
if err != nil { | ||
return ReferrerDomain{}, fmt.Errorf("invalid referrer: %w", err) | ||
} | ||
|
||
source, err := idna.Lookup.ToASCII(u.Hostname()) | ||
if err != nil { | ||
return ReferrerDomain{}, err | ||
} | ||
|
||
return ReferrerDomain{source}, nil | ||
} | ||
|
||
// String implements fmt.Stringer. | ||
func (s ReferrerDomain) String() string { | ||
return s.value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package event | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseReferrerDomain(t *testing.T) { | ||
t.Run("Invalid", func(t *testing.T) { | ||
invalidDomains := []string{ | ||
"mydomain*com", | ||
"123domain!", | ||
"_invalid-domain.com", | ||
"space domain.com", | ||
"my domain .com", | ||
"domain#invalid.com", | ||
"-hyphenstart.com", | ||
"domain_with_underscores-.com", | ||
} | ||
|
||
for _, domain := range invalidDomains { | ||
t.Run(domain, func(t *testing.T) { | ||
referrerDomain, err := ParseReferrerDomain("http://" + domain + "/foo") | ||
require.Error(t, err) | ||
require.Equal(t, ReferrerDomain{}, referrerDomain) | ||
}) | ||
} | ||
}) | ||
|
||
t.Run("Valid", func(t *testing.T) { | ||
validDomains := []string{ | ||
"alphabets123.com", | ||
"my-domain-name.com", | ||
"1234example.net", | ||
"tech-geeks.org", | ||
"secure-site.info", | ||
"bestblogsite.biz", | ||
"creative-web.dev", | ||
"xyz-company.co", | ||
"e-commerce-site.store", | ||
"travel-experts.travel", | ||
"xn--kn8h.to", | ||
} | ||
|
||
for _, domain := range validDomains { | ||
t.Run(domain, func(t *testing.T) { | ||
referrerDomain, err := ParseReferrerDomain("http://" + domain + "/foo") | ||
require.NoError(t, err) | ||
require.NotEqual(t, ReferrerDomain{}, referrerDomain) | ||
require.Equal(t, domain, referrerDomain.String()) | ||
}) | ||
} | ||
|
||
t.Run("🏹.to", func(t *testing.T) { | ||
url := "http://🏹.to/" | ||
referrer, err := ParseReferrerDomain(url) | ||
require.NoError(t, err) | ||
require.NotEqual(t, ReferrerDomain{}, referrer) | ||
require.Equal(t, "xn--kn8h.to", referrer.String()) | ||
}) | ||
|
||
t.Run("Direct", func(t *testing.T) { | ||
referrerDomain, err := ParseReferrerDomain("") | ||
require.NoError(t, err) | ||
require.Equal(t, "", referrerDomain.String()) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters