diff --git a/reader/icon/finder.go b/reader/icon/finder.go index fe6e86df..c55e4d1c 100644 --- a/reader/icon/finder.go +++ b/reader/icon/finder.go @@ -20,7 +20,7 @@ import ( // FindIcon try to find the website's icon. func FindIcon(websiteURL string) (*model.Icon, error) { - rootURL := url.GetRootURL(websiteURL) + rootURL := url.RootURL(websiteURL) client := http.NewClient(rootURL) response, err := client.Get() if err != nil { @@ -72,9 +72,9 @@ func parseDocument(websiteURL string, data io.Reader) (string, error) { } if iconURL == "" { - iconURL = url.GetRootURL(websiteURL) + "favicon.ico" + iconURL = url.RootURL(websiteURL) + "favicon.ico" } else { - iconURL, _ = url.GetAbsoluteURL(websiteURL, iconURL) + iconURL, _ = url.AbsoluteURL(websiteURL, iconURL) } return iconURL, nil diff --git a/reader/sanitizer/sanitizer.go b/reader/sanitizer/sanitizer.go index ad286c6e..1c1c7c17 100644 --- a/reader/sanitizer/sanitizer.go +++ b/reader/sanitizer/sanitizer.go @@ -88,7 +88,7 @@ func sanitizeAttributes(baseURL, tagName string, attributes []html.Attribute) ([ if tagName == "iframe" && !isValidIframeSource(attribute.Val) { continue } else { - value, err = url.GetAbsoluteURL(baseURL, value) + value, err = url.AbsoluteURL(baseURL, value) if err != nil { continue } diff --git a/reader/subscription/finder.go b/reader/subscription/finder.go index cb6fbf20..506fe1a8 100644 --- a/reader/subscription/finder.go +++ b/reader/subscription/finder.go @@ -79,7 +79,7 @@ func parseDocument(websiteURL string, data io.Reader) (Subscriptions, error) { } if feedURL, exists := s.Attr("href"); exists { - subscription.URL, _ = url.GetAbsoluteURL(websiteURL, feedURL) + subscription.URL, _ = url.AbsoluteURL(websiteURL, feedURL) } if subscription.Title == "" { diff --git a/reader/url/url.go b/reader/url/url.go index eea658bf..e5a7c62d 100644 --- a/reader/url/url.go +++ b/reader/url/url.go @@ -8,8 +8,8 @@ import "net/url" import "fmt" import "strings" -// GetAbsoluteURL converts the input URL as absolute URL if necessary. -func GetAbsoluteURL(baseURL, input string) (string, error) { +// AbsoluteURL converts the input URL as absolute URL if necessary. +func AbsoluteURL(baseURL, input string) (string, error) { if strings.HasPrefix(input, "//") { input = "https://" + input[2:] } @@ -31,13 +31,13 @@ func GetAbsoluteURL(baseURL, input string) (string, error) { return base.ResolveReference(u).String(), nil } -// GetRootURL returns absolute URL without the path. -func GetRootURL(websiteURL string) string { +// RootURL returns absolute URL without the path. +func RootURL(websiteURL string) string { if strings.HasPrefix(websiteURL, "//") { websiteURL = "https://" + websiteURL[2:] } - absoluteURL, err := GetAbsoluteURL(websiteURL, "") + absoluteURL, err := AbsoluteURL(websiteURL, "") if err != nil { return websiteURL } @@ -59,3 +59,13 @@ func IsHTTPS(websiteURL string) bool { return strings.ToLower(parsedURL.Scheme) == "https" } + +// Domain returns only the domain part of the given URL. +func Domain(websiteURL string) string { + parsedURL, err := url.Parse(websiteURL) + if err != nil { + return websiteURL + } + + return parsedURL.Host +} diff --git a/reader/url/url_test.go b/reader/url/url_test.go index 0a970868..13c723ae 100644 --- a/reader/url/url_test.go +++ b/reader/url/url_test.go @@ -5,7 +5,7 @@ import "testing" func TestGetAbsoluteURLWithAbsolutePath(t *testing.T) { expected := `https://example.org/path/file.ext` input := `/path/file.ext` - output, err := GetAbsoluteURL("https://example.org/folder/", input) + output, err := AbsoluteURL("https://example.org/folder/", input) if err != nil { t.Error(err) @@ -19,7 +19,7 @@ func TestGetAbsoluteURLWithAbsolutePath(t *testing.T) { func TestGetAbsoluteURLWithRelativePath(t *testing.T) { expected := `https://example.org/folder/path/file.ext` input := `path/file.ext` - output, err := GetAbsoluteURL("https://example.org/folder/", input) + output, err := AbsoluteURL("https://example.org/folder/", input) if err != nil { t.Error(err) @@ -33,7 +33,7 @@ func TestGetAbsoluteURLWithRelativePath(t *testing.T) { func TestGetAbsoluteURLWithRelativePaths(t *testing.T) { expected := `https://example.org/path/file.ext` input := `path/file.ext` - output, err := GetAbsoluteURL("https://example.org/folder", input) + output, err := AbsoluteURL("https://example.org/folder", input) if err != nil { t.Error(err) @@ -47,7 +47,7 @@ func TestGetAbsoluteURLWithRelativePaths(t *testing.T) { func TestWhenInputIsAlreadyAbsolute(t *testing.T) { expected := `https://example.org/path/file.ext` input := `https://example.org/path/file.ext` - output, err := GetAbsoluteURL("https://example.org/folder/", input) + output, err := AbsoluteURL("https://example.org/folder/", input) if err != nil { t.Error(err) @@ -61,7 +61,7 @@ func TestWhenInputIsAlreadyAbsolute(t *testing.T) { func TestGetAbsoluteURLWithProtocolRelative(t *testing.T) { expected := `https://static.example.org/path/file.ext` input := `//static.example.org/path/file.ext` - output, err := GetAbsoluteURL("https://www.example.org/", input) + output, err := AbsoluteURL("https://www.example.org/", input) if err != nil { t.Error(err) @@ -75,7 +75,7 @@ func TestGetAbsoluteURLWithProtocolRelative(t *testing.T) { func TestGetRootURL(t *testing.T) { expected := `https://example.org/` input := `https://example.org/path/file.ext` - output := GetRootURL(input) + output := RootURL(input) if expected != output { t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected) @@ -85,7 +85,7 @@ func TestGetRootURL(t *testing.T) { func TestGetRootURLWithProtocolRelativePath(t *testing.T) { expected := `https://static.example.org/` input := `//static.example.org/path/file.ext` - output := GetRootURL(input) + output := RootURL(input) if expected != output { t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected) @@ -105,3 +105,13 @@ func TestIsHTTPS(t *testing.T) { t.Error("Unable to recognize malformed URL") } } + +func TestGetDomain(t *testing.T) { + expected := `static.example.org` + input := `http://static.example.org/` + output := Domain(input) + + if expected != output { + t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected) + } +} diff --git a/server/template/html/entry.html b/server/template/html/entry.html index e832b27b..7c190d3b 100644 --- a/server/template/html/entry.html +++ b/server/template/html/entry.html @@ -55,7 +55,7 @@ {{ else if hasPrefix .MimeType "image/" }}