Skip to content

Commit

Permalink
Content type fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
arran4 committed Nov 12, 2023
1 parent 705829d commit 4811ee8
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions faviconProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ func FaviconProxyHandler(w http.ResponseWriter, r *http.Request) {
}

// Find the favicon URL from the root page content
faviconURL, err := findFaviconURL(rootPageContent, urlParam)
faviconURL, fileType, err := findFaviconURL(rootPageContent, up)
if err != nil {
http.Error(w, fmt.Sprintf("Error finding favicon URL: %s", err), http.StatusInternalServerError)
return
}

if fileType == "" {
fileType = "image/x-icon"
}
// Proxy the favicon request
faviconContent, err := downloadUrl(faviconURL)
if err != nil {
Expand All @@ -79,7 +82,7 @@ func FaviconProxyHandler(w http.ResponseWriter, r *http.Request) {
cacheFavicon(urlParam, faviconContent)

// Serve the favicon content
w.Header().Set("Content-Type", "image/x-icon")
w.Header().Set("Content-Type", fileType)
_, _ = w.Write(faviconContent)
}

Expand All @@ -95,38 +98,34 @@ func fetchURL(urlParam string) ([]byte, error) {
return io.ReadAll(io.LimitReader(resp.Body, 1*1024*1024+1))
}

func findFaviconURL(pageContent []byte, baseURL string) (string, error) {
func findFaviconURL(pageContent []byte, baseURL *url.URL) (string, string, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(string(pageContent)))
if err != nil {
return "", err
return "", "", err
}

var faviconURL string
var faviconPath string
var fileType string

// Find the favicon URL from the meta tags
doc.Find("link[rel='icon'], link[rel='shortcut icon']").Each(func(i int, s *goquery.Selection) {
doc.Find("link[rel='icon'], link[rel='shortcut icon'], link[rel='alternate icon']").Each(func(i int, s *goquery.Selection) {
if href, exists := s.Attr("href"); exists {
faviconURL = href
faviconPath = href
fileType, _ = s.Attr("type")
return
}
})

if faviconURL == "" {
faviconURL = baseURL + "favicon.ico"
if faviconPath == "" {
faviconPath = "/favicon.ico"
}

// Convert relative URLs to absolute URLs
u, err := url.Parse(faviconURL)
if err != nil {
return "", err
}
base, err := url.Parse(baseURL)
p, err := baseURL.Parse(faviconPath)
if err != nil {
return "", err
return "", "", err
}
faviconURL = base.ResolveReference(u).String()

return faviconURL, nil
return p.String(), fileType, nil
}

func downloadUrl(url string) ([]byte, error) {
Expand Down

0 comments on commit 4811ee8

Please sign in to comment.