Skip to content

Commit

Permalink
valid url len
Browse files Browse the repository at this point in the history
  • Loading branch information
ndjuric-bit committed Apr 23, 2024
1 parent 56035bb commit 181f164
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 10 additions & 3 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ var (
func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err error) {

trimmedUrl := strings.TrimSpace(fullUrl)

if len(trimmedUrl) == 0 {
return "", "", ErrInvalidUrl
}
//remove everything behind ? or #
if index := strings.Index(trimmedUrl, "?"); index > 0 {
trimmedUrl = trimmedUrl[0:index]
Expand All @@ -35,9 +37,14 @@ func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err er
if trimmedUrl[len(trimmedUrl)-1:] != "/" {
trimmedUrl = trimmedUrl + "/"
}
trimmedUrl = strings.Replace(trimmedUrl, "http://", "https://", 1)

if len(trimmedUrl) < 9 {
if strings.Index(trimmedUrl, "http") == -1 {
trimmedUrl = "https://" + trimmedUrl
} else {
trimmedUrl = strings.Replace(trimmedUrl, "http://", "https://", 1)
}

if len(trimmedUrl) < 11 {
return "", "", ErrInvalidUrl
}
host := trimmedUrl[8:]
Expand Down
5 changes: 5 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ func TestTrimUrlForScylla(t *testing.T) {
wantUrl: "https://www.gala.de/amp/lifestyle/film-tv-musik/joko-winterscheidt--er-holt-sich-seine-show-zurueck-24032084.html&amp_tf=Von %1$s&aoh=17095534826758&csi=1&referrer=https://www.google.com&ampshare=https://www.gala.de/lifestyle/film-tv-musik/joko-winterscheidt-siegt-gegen-klaas-und-holt-sich-seine-show-zurueck-24032084.html/",
wantHost: "www.gala.de",
},
{
fullUrl: "www.gala.de",
wantUrl: "https://www.gala.de/",
wantHost: "www.gala.de",
},
}
for _, tt := range tests {
t.Run(tt.fullUrl, func(_ *testing.T) {
Expand Down

0 comments on commit 181f164

Please sign in to comment.