From d8aa42f7ecf5d8b0019d118a39dc261fbb6d795d Mon Sep 17 00:00:00 2001 From: Nathan Osman Date: Thu, 5 May 2016 22:45:33 -0700 Subject: [PATCH] Added support for max-age=0 and no-cache. --- server.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 1947cb3..26bc363 100644 --- a/server.go +++ b/server.go @@ -3,6 +3,7 @@ package main import ( "github.com/hectane/go-asyncserver" "github.com/nathan-osman/go-aptproxy/cache" + "github.com/pquerna/cachecontrol/cacheobject" "io" "log" @@ -32,6 +33,16 @@ func rewrite(rawurl string) string { return rawurl } +func shouldForce(req *http.Request) bool { + d, err := cacheobject.ParseRequestCacheControl(req.Header.Get("Cache-Control")) + if err != nil { + if d.MaxAge == 0 || d.NoCache { + return true + } + } + return false +} + func (s *Server) writeHeaders(w http.ResponseWriter, e *cache.Entry) { if e.ContentType != "" { w.Header().Set("Content-Type", e.ContentType) @@ -56,7 +67,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } - r, err := s.cache.GetReader(rewrite(req.RequestURI), false) + r, err := s.cache.GetReader(rewrite(req.RequestURI), shouldForce(req)) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Println("[ERR]", err)