diff options
author | Adam Mathes <adam@trenchant.org> | 2018-04-26 20:28:25 -0700 |
---|---|---|
committer | Adam Mathes <adam@trenchant.org> | 2018-04-26 20:28:25 -0700 |
commit | d2cd2e0a261b641ae4435cdf82fed10c8a345a54 (patch) | |
tree | 091b4ac20928d38cdc4b46fb776e3d8bf61525f8 | |
parent | 5d063b0f8f7dee735cc5eeb8e21c37a9f453bacd (diff) | |
download | neko-d2cd2e0a261b641ae4435cdf82fed10c8a345a54.tar.gz neko-d2cd2e0a261b641ae4435cdf82fed10c8a345a54.tar.bz2 neko-d2cd2e0a261b641ae4435cdf82fed10c8a345a54.zip |
img proxy
-rw-r--r-- | web/web.go | 33 |
1 files changed, 24 insertions, 9 deletions
@@ -14,6 +14,7 @@ import ( "net/http" "path" "strconv" + "time" ) func indexHandler(w http.ResponseWriter, r *http.Request) { @@ -131,24 +132,38 @@ func categoryHandler(w http.ResponseWriter, r *http.Request) { } func imageProxyHandler(w http.ResponseWriter, r *http.Request) { - // caching...??? - imgURL := r.URL.String() - fmt.Printf("proxying: %s\n", imgURL) decodedURL, err := base64.URLEncoding.DecodeString(imgURL) - fmt.Printf("decoded: %s\n", decodedURL) + // pseudo-caching + if r.Header.Get("If-None-Match") == string(decodedURL) { + w.WriteHeader(http.StatusNotModified) + return + } + + if r.Header.Get("Etag") == string(decodedURL) { + w.WriteHeader(http.StatusNotModified) + return + } + + // set headers + w.Header().Set("ETag", string(decodedURL)) + w.Header().Set("Cache-Control", "public") + w.Header().Set("Expires", time.Now().Add(48*time.Hour).Format(time.RFC1123)) + + // grab the img resp, err := http.Get(string(decodedURL)) if err != nil { - http.Error(w, "sorry", 404) + http.Error(w, "filed to proxy image", 404) return } - bts, _ := ioutil.ReadAll(resp.Body) - - etag := crypto.HashFromBytes(body) + bts, err := ioutil.ReadAll(resp.Body) + if err != nil { + http.Error(w, "failed to read proxy image", 404) + return + } - w.Header().Set("Content-Type", "application/json") w.Write(bts) return } |