2017-11-20 06:10:04 +01:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 06:51:50 +02:00
|
|
|
package icon // import "miniflux.app/reader/icon"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
2017-12-23 04:01:39 +01:00
|
|
|
"encoding/base64"
|
2017-11-20 06:10:04 +01:00
|
|
|
"fmt"
|
2017-11-21 02:12:37 +01:00
|
|
|
"io"
|
2017-12-23 04:01:39 +01:00
|
|
|
"strings"
|
2017-11-21 02:12:37 +01:00
|
|
|
|
2022-03-26 06:05:19 +01:00
|
|
|
stdlib_url "net/url"
|
|
|
|
|
2020-09-27 23:29:48 +02:00
|
|
|
"miniflux.app/config"
|
2018-08-25 06:51:50 +02:00
|
|
|
"miniflux.app/crypto"
|
|
|
|
"miniflux.app/http/client"
|
|
|
|
"miniflux.app/logger"
|
|
|
|
"miniflux.app/model"
|
|
|
|
"miniflux.app/url"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindIcon try to find the website's icon.
|
2022-01-09 00:09:12 +01:00
|
|
|
func FindIcon(websiteURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
|
2017-12-02 07:29:18 +01:00
|
|
|
rootURL := url.RootURL(websiteURL)
|
2022-01-09 00:09:12 +01:00
|
|
|
logger.Debug("[FindIcon] Trying to find an icon: rootURL=%q websiteURL=%q userAgent=%q", rootURL, websiteURL, userAgent)
|
|
|
|
|
2020-09-27 23:29:48 +02:00
|
|
|
clt := client.NewClientWithConfig(rootURL, config.Opts)
|
2022-01-09 00:09:12 +01:00
|
|
|
clt.WithUserAgent(userAgent)
|
2021-02-21 22:42:49 +01:00
|
|
|
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
|
2022-01-09 00:09:12 +01:00
|
|
|
|
2020-09-10 08:28:54 +02:00
|
|
|
if fetchViaProxy {
|
|
|
|
clt.WithProxy()
|
|
|
|
}
|
2021-02-21 22:42:49 +01:00
|
|
|
|
2018-04-28 19:51:07 +02:00
|
|
|
response, err := clt.Get()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2022-01-09 00:09:12 +01:00
|
|
|
return nil, fmt.Errorf("icon: unable to download website index page: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
2022-01-09 00:09:12 +01:00
|
|
|
return nil, fmt.Errorf("icon: unable to download website index page: status=%d", response.StatusCode)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
iconURL, err := parseDocument(rootURL, response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-23 04:01:39 +01:00
|
|
|
if strings.HasPrefix(iconURL, "data:") {
|
|
|
|
return parseImageDataURL(iconURL)
|
|
|
|
}
|
|
|
|
|
2017-12-16 03:55:57 +01:00
|
|
|
logger.Debug("[FindIcon] Fetching icon => %s", iconURL)
|
2022-01-09 00:09:12 +01:00
|
|
|
icon, err := downloadIcon(iconURL, userAgent, fetchViaProxy, allowSelfSignedCertificates)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDocument(websiteURL string, data io.Reader) (string, error) {
|
|
|
|
queries := []string{
|
|
|
|
"link[rel='shortcut icon']",
|
|
|
|
"link[rel='Shortcut Icon']",
|
|
|
|
"link[rel='icon shortcut']",
|
|
|
|
"link[rel='icon']",
|
|
|
|
}
|
|
|
|
|
|
|
|
doc, err := goquery.NewDocumentFromReader(data)
|
|
|
|
if err != nil {
|
2022-01-09 00:09:12 +01:00
|
|
|
return "", fmt.Errorf("icon: unable to read document: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var iconURL string
|
|
|
|
for _, query := range queries {
|
|
|
|
doc.Find(query).Each(func(i int, s *goquery.Selection) {
|
|
|
|
if href, exists := s.Attr("href"); exists {
|
2020-11-07 02:11:52 +01:00
|
|
|
iconURL = strings.TrimSpace(href)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if iconURL != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if iconURL == "" {
|
2017-12-02 07:29:18 +01:00
|
|
|
iconURL = url.RootURL(websiteURL) + "favicon.ico"
|
2017-11-20 06:10:04 +01:00
|
|
|
} else {
|
2017-12-02 07:29:18 +01:00
|
|
|
iconURL, _ = url.AbsoluteURL(websiteURL, iconURL)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return iconURL, nil
|
|
|
|
}
|
|
|
|
|
2022-01-09 00:09:12 +01:00
|
|
|
func downloadIcon(iconURL, userAgent string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
|
2020-09-27 23:29:48 +02:00
|
|
|
clt := client.NewClientWithConfig(iconURL, config.Opts)
|
2022-01-09 00:09:12 +01:00
|
|
|
clt.WithUserAgent(userAgent)
|
2021-02-21 22:42:49 +01:00
|
|
|
clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
|
2020-09-10 08:28:54 +02:00
|
|
|
if fetchViaProxy {
|
|
|
|
clt.WithProxy()
|
|
|
|
}
|
2022-01-09 00:09:12 +01:00
|
|
|
|
2018-04-28 19:51:07 +02:00
|
|
|
response, err := clt.Get()
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2022-01-09 00:09:12 +01:00
|
|
|
return nil, fmt.Errorf("icon: unable to download iconURL: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if response.HasServerFailure() {
|
2022-01-09 00:09:12 +01:00
|
|
|
return nil, fmt.Errorf("icon: unable to download icon: status=%d", response.StatusCode)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 06:19:03 +01:00
|
|
|
body, err := io.ReadAll(response.Body)
|
2017-11-20 06:10:04 +01:00
|
|
|
if err != nil {
|
2022-01-09 00:09:12 +01:00
|
|
|
return nil, fmt.Errorf("icon: unable to read downloaded icon: %v", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(body) == 0 {
|
2022-01-09 00:09:12 +01:00
|
|
|
return nil, fmt.Errorf("icon: downloaded icon is empty, iconURL=%s", iconURL)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
icon := &model.Icon{
|
2018-01-03 04:15:08 +01:00
|
|
|
Hash: crypto.HashFromBytes(body),
|
2017-11-20 06:10:04 +01:00
|
|
|
MimeType: response.ContentType,
|
|
|
|
Content: body,
|
|
|
|
}
|
|
|
|
|
|
|
|
return icon, nil
|
|
|
|
}
|
2017-12-23 04:01:39 +01:00
|
|
|
|
2022-03-26 06:05:19 +01:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs#syntax
|
|
|
|
// data:[<mediatype>][;base64],<data>
|
2017-12-23 04:01:39 +01:00
|
|
|
func parseImageDataURL(value string) (*model.Icon, error) {
|
2022-03-26 06:05:19 +01:00
|
|
|
var mediaType string
|
|
|
|
var encoding string
|
|
|
|
|
|
|
|
if !strings.HasPrefix(value, "data:") {
|
|
|
|
return nil, fmt.Errorf(`icon: invalid data URL (missing data:) %q`, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
value = value[5:]
|
2017-12-23 04:01:39 +01:00
|
|
|
|
2022-03-26 06:05:19 +01:00
|
|
|
comma := strings.Index(value, ",")
|
|
|
|
if comma < 0 {
|
|
|
|
return nil, fmt.Errorf(`icon: invalid data URL (no comma) %q`, value)
|
2017-12-23 04:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data := value[comma+1:]
|
2022-03-26 06:05:19 +01:00
|
|
|
semicolon := strings.Index(value[0:comma], ";")
|
2017-12-23 04:01:39 +01:00
|
|
|
|
2022-03-26 06:05:19 +01:00
|
|
|
if semicolon > 0 {
|
|
|
|
mediaType = value[0:semicolon]
|
|
|
|
encoding = value[semicolon+1 : comma]
|
|
|
|
} else {
|
|
|
|
mediaType = value[0:comma]
|
2017-12-23 04:01:39 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 06:05:19 +01:00
|
|
|
if !strings.HasPrefix(mediaType, "image/") {
|
|
|
|
return nil, fmt.Errorf(`icon: invalid media type %q`, mediaType)
|
2017-12-23 04:01:39 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 06:05:19 +01:00
|
|
|
var blob []byte
|
|
|
|
switch encoding {
|
|
|
|
case "base64":
|
|
|
|
var err error
|
|
|
|
blob, err = base64.StdEncoding.DecodeString(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`icon: invalid data %q (%v)`, value, err)
|
|
|
|
}
|
|
|
|
case "":
|
|
|
|
decodedData, err := stdlib_url.QueryUnescape(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`icon: unable to decode data URL %q`, value)
|
|
|
|
}
|
|
|
|
blob = []byte(decodedData)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf(`icon: unsupported data URL encoding %q`, value)
|
2017-12-23 04:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(blob) == 0 {
|
2022-03-26 06:05:19 +01:00
|
|
|
return nil, fmt.Errorf(`icon: empty data URL %q`, value)
|
2017-12-23 04:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
icon := &model.Icon{
|
2018-01-03 04:15:08 +01:00
|
|
|
Hash: crypto.HashFromBytes(blob),
|
2017-12-23 04:01:39 +01:00
|
|
|
Content: blob,
|
2022-03-26 06:05:19 +01:00
|
|
|
MimeType: mediaType,
|
2017-12-23 04:01:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return icon, nil
|
|
|
|
}
|