diff --git a/url.go b/url.go index 2aa53c2..48a4c85 100644 --- a/url.go +++ b/url.go @@ -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] @@ -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:] diff --git a/url_test.go b/url_test.go index f044dfd..eded025 100644 --- a/url_test.go +++ b/url_test.go @@ -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&_tf=Von %1$s&aoh=17095534826758&csi=1&referrer=https://www.google.com&share=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) {