finder.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package icon // import "miniflux.app/reader/icon"
  5. import (
  6. "encoding/base64"
  7. "fmt"
  8. "io"
  9. "strings"
  10. "miniflux.app/config"
  11. "miniflux.app/crypto"
  12. "miniflux.app/http/client"
  13. "miniflux.app/logger"
  14. "miniflux.app/model"
  15. "miniflux.app/url"
  16. "github.com/PuerkitoBio/goquery"
  17. )
  18. // FindIcon try to find the website's icon.
  19. func FindIcon(websiteURL string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
  20. rootURL := url.RootURL(websiteURL)
  21. clt := client.NewClientWithConfig(rootURL, config.Opts)
  22. clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
  23. if fetchViaProxy {
  24. clt.WithProxy()
  25. }
  26. response, err := clt.Get()
  27. if err != nil {
  28. return nil, fmt.Errorf("unable to download website index page: %v", err)
  29. }
  30. if response.HasServerFailure() {
  31. return nil, fmt.Errorf("unable to download website index page: status=%d", response.StatusCode)
  32. }
  33. iconURL, err := parseDocument(rootURL, response.Body)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if strings.HasPrefix(iconURL, "data:") {
  38. return parseImageDataURL(iconURL)
  39. }
  40. logger.Debug("[FindIcon] Fetching icon => %s", iconURL)
  41. icon, err := downloadIcon(iconURL, fetchViaProxy, allowSelfSignedCertificates)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return icon, nil
  46. }
  47. func parseDocument(websiteURL string, data io.Reader) (string, error) {
  48. queries := []string{
  49. "link[rel='shortcut icon']",
  50. "link[rel='Shortcut Icon']",
  51. "link[rel='icon shortcut']",
  52. "link[rel='icon']",
  53. }
  54. doc, err := goquery.NewDocumentFromReader(data)
  55. if err != nil {
  56. return "", fmt.Errorf("unable to read document: %v", err)
  57. }
  58. var iconURL string
  59. for _, query := range queries {
  60. doc.Find(query).Each(func(i int, s *goquery.Selection) {
  61. if href, exists := s.Attr("href"); exists {
  62. iconURL = strings.TrimSpace(href)
  63. }
  64. })
  65. if iconURL != "" {
  66. break
  67. }
  68. }
  69. if iconURL == "" {
  70. iconURL = url.RootURL(websiteURL) + "favicon.ico"
  71. } else {
  72. iconURL, _ = url.AbsoluteURL(websiteURL, iconURL)
  73. }
  74. return iconURL, nil
  75. }
  76. func downloadIcon(iconURL string, fetchViaProxy, allowSelfSignedCertificates bool) (*model.Icon, error) {
  77. clt := client.NewClientWithConfig(iconURL, config.Opts)
  78. clt.AllowSelfSignedCertificates = allowSelfSignedCertificates
  79. if fetchViaProxy {
  80. clt.WithProxy()
  81. }
  82. response, err := clt.Get()
  83. if err != nil {
  84. return nil, fmt.Errorf("unable to download iconURL: %v", err)
  85. }
  86. if response.HasServerFailure() {
  87. return nil, fmt.Errorf("unable to download icon: status=%d", response.StatusCode)
  88. }
  89. body, err := io.ReadAll(response.Body)
  90. if err != nil {
  91. return nil, fmt.Errorf("unable to read downloaded icon: %v", err)
  92. }
  93. if len(body) == 0 {
  94. return nil, fmt.Errorf("downloaded icon is empty, iconURL=%s", iconURL)
  95. }
  96. icon := &model.Icon{
  97. Hash: crypto.HashFromBytes(body),
  98. MimeType: response.ContentType,
  99. Content: body,
  100. }
  101. return icon, nil
  102. }
  103. func parseImageDataURL(value string) (*model.Icon, error) {
  104. colon := strings.Index(value, ":")
  105. semicolon := strings.Index(value, ";")
  106. comma := strings.Index(value, ",")
  107. if colon <= 0 || semicolon <= 0 || comma <= 0 {
  108. return nil, fmt.Errorf(`icon: invalid data url "%s"`, value)
  109. }
  110. mimeType := value[colon+1 : semicolon]
  111. encoding := value[semicolon+1 : comma]
  112. data := value[comma+1:]
  113. if encoding != "base64" {
  114. return nil, fmt.Errorf(`icon: unsupported data url encoding "%s"`, value)
  115. }
  116. if !strings.HasPrefix(mimeType, "image/") {
  117. return nil, fmt.Errorf(`icon: invalid mime type "%s"`, mimeType)
  118. }
  119. blob, err := base64.StdEncoding.DecodeString(data)
  120. if err != nil {
  121. return nil, fmt.Errorf(`icon: invalid data "%s" (%v)`, value, err)
  122. }
  123. if len(blob) == 0 {
  124. return nil, fmt.Errorf(`icon: empty data "%s"`, value)
  125. }
  126. icon := &model.Icon{
  127. Hash: crypto.HashFromBytes(blob),
  128. Content: blob,
  129. MimeType: mimeType,
  130. }
  131. return icon, nil
  132. }