1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-06 17:41:00 +00:00

Proxify image enclosures

This commit is contained in:
Frédéric Guillot 2017-12-01 22:29:18 -08:00
parent 1a90c059e7
commit fb2a73c91e
9 changed files with 56 additions and 30 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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 == "" {

View file

@ -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
}

View file

@ -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)
}
}