Added support for max-age=0 and no-cache.
This commit is contained in:
parent
2c178c85e6
commit
d8aa42f7ec
13
server.go
13
server.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/hectane/go-asyncserver"
|
"github.com/hectane/go-asyncserver"
|
||||||
"github.com/nathan-osman/go-aptproxy/cache"
|
"github.com/nathan-osman/go-aptproxy/cache"
|
||||||
|
"github.com/pquerna/cachecontrol/cacheobject"
|
||||||
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -32,6 +33,16 @@ func rewrite(rawurl string) string {
|
|||||||
return rawurl
|
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) {
|
func (s *Server) writeHeaders(w http.ResponseWriter, e *cache.Entry) {
|
||||||
if e.ContentType != "" {
|
if e.ContentType != "" {
|
||||||
w.Header().Set("Content-Type", 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)
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r, err := s.cache.GetReader(rewrite(req.RequestURI), false)
|
r, err := s.cache.GetReader(rewrite(req.RequestURI), shouldForce(req))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
log.Println("[ERR]", err)
|
log.Println("[ERR]", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user