Преглед на файлове

Send a transparent pixel if no favicon was found

This can be changed with the `?error=true` parameter.  Sending a
transparent pixel should look better and make caching possible.
Lucas Stadler преди 9 години
родител
ревизия
d0e902ee48
променени са 1 файла, в които са добавени 32 реда и са изтрити 6 реда
  1. 32 6
      go/favicon/favicon.go

+ 32 - 6
go/favicon/favicon.go

@ -16,6 +16,19 @@ import (
16 16
	"github.com/golang/groupcache/lru"
17 17
)
18 18
19
var transparentPixelPNG = []byte{
20
	0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
21
	0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
22
	0x01, 0x03, 0x00, 0x00, 0x00, 0x25, 0xdb, 0x56, 0xca, 0x00, 0x00, 0x00,
23
	0x03, 0x50, 0x4c, 0x54, 0x45, 0x00, 0x00, 0x00, 0xa7, 0x7a, 0x3d, 0xda,
24
	0x00, 0x00, 0x00, 0x01, 0x74, 0x52, 0x4e, 0x53, 0x00, 0x40, 0xe6, 0xd8,
25
	0x66, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63,
26
	0x60, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0xe2, 0x21, 0xbc, 0x33, 0x00,
27
	0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
28
}
29
30
const transparentPixelMD5 = "71a50dbba44c78128b221b7df7bb51f1"
31
19 32
var port = flag.Int("p", 8080, "port [8080]")
20 33
var cacheSize = flag.Int("s", 10000, "cache size [10000]")
21 34
var debug = flag.Bool("debug", false, "Print out debug info")
@ -56,18 +69,31 @@ func HandleProxy(w http.ResponseWriter, r *http.Request) {
56 69
	favicon, err := GetFaviconCached(url)
57 70
	if err != nil {
58 71
		fmt.Printf("Error: '%s': %s\n", url, err)
59
		w.WriteHeader(http.StatusNotFound)
60
		w.Write([]byte(fmt.Sprint(err)))
72
		if r.URL.Query().Get("error") != "" {
73
			w.WriteHeader(http.StatusNotFound)
74
			w.Write([]byte(fmt.Sprint(err)))
75
		} else {
76
			sendBytes(w, r, transparentPixelPNG, transparentPixelMD5)
77
		}
61 78
		return
62 79
	}
63 80
64 81
	image, hash, err := GetImageCached(favicon)
65 82
	if err != nil {
66 83
		fmt.Printf("Error: '%s': %s\n", url, err)
67
		w.WriteHeader(http.StatusNotFound)
68
		w.Write([]byte(fmt.Sprint(err)))
84
		if r.URL.Query().Get("error") != "" {
85
			w.WriteHeader(http.StatusNotFound)
86
			w.Write([]byte(fmt.Sprint(err)))
87
		} else {
88
			sendBytes(w, r, transparentPixelPNG, transparentPixelMD5)
89
		}
69 90
		return
70 91
	}
92
93
	sendBytes(w, r, image, hash)
94
}
95
96
func sendBytes(w http.ResponseWriter, r *http.Request, data []byte, hash string) {
71 97
	w.Header().Set("ETag", hash)
72 98
73 99
	ifNoneMatch := r.Header.Get("If-None-Match")
@ -76,8 +102,8 @@ func HandleProxy(w http.ResponseWriter, r *http.Request) {
76 102
		return
77 103
	}
78 104
79
	w.Header().Set("Content-Length", fmt.Sprintf("%d", len(image)))
80
	w.Write(image)
105
	w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
106
	w.Write(data)
81 107
}
82 108
83 109
func GetImageCached(u string) ([]byte, string, error) {