diff --git a/gen_router.go b/gen_router.go index 3dcf9e6b..0f66dde3 100644 --- a/gen_router.go +++ b/gen_router.go @@ -539,6 +539,32 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) { return w.Writer.Write(b) } +type WriterIntercept struct { + w http.ResponseWriter + code int +} + +func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept { + return &WriterIntercept{w:w,code:200} +} + +func (writ *WriterIntercept) Header() http.Header { + return writ.w.Header() +} + +func (writ *WriterIntercept) Write(pieces []byte) (int, error) { + return writ.w.Write(pieces) +} + +func (writ *WriterIntercept) WriteHeader(code int) { + writ.w.WriteHeader(code) + writ.code = code +} + +func (writ *WriterIntercept) GetCode() int { + return writ.code +} + type GenRouter struct { UploadHandler func(http.ResponseWriter, *http.Request) extraRoutes map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError @@ -555,9 +581,12 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) { return &GenRouter{ UploadHandler: func(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) - w.Header().Set("Vary", "Accept-Encoding") - http.StripPrefix("/uploads/",uploads).ServeHTTP(w,req) + writ := NewWriterIntercept(w) + http.StripPrefix("/uploads/",uploads).ServeHTTP(writ,req) + if writ.GetCode() == 200 { + w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) + w.Header().Set("Vary", "Accept-Encoding") + } }, extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError), requestLogger: log.New(f, "", log.LstdFlags), diff --git a/router_gen/main.go b/router_gen/main.go index 72ebb6b8..25b2da88 100644 --- a/router_gen/main.go +++ b/router_gen/main.go @@ -310,6 +310,32 @@ func (w gzipResponseWriter) Write(b []byte) (int, error) { return w.Writer.Write(b) } +type WriterIntercept struct { + w http.ResponseWriter + code int +} + +func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept { + return &WriterIntercept{w:w,code:200} +} + +func (writ *WriterIntercept) Header() http.Header { + return writ.w.Header() +} + +func (writ *WriterIntercept) Write(pieces []byte) (int, error) { + return writ.w.Write(pieces) +} + +func (writ *WriterIntercept) WriteHeader(code int) { + writ.w.WriteHeader(code) + writ.code = code +} + +func (writ *WriterIntercept) GetCode() int { + return writ.code +} + type GenRouter struct { UploadHandler func(http.ResponseWriter, *http.Request) extraRoutes map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError @@ -326,9 +352,12 @@ func NewGenRouter(uploads http.Handler) (*GenRouter, error) { return &GenRouter{ UploadHandler: func(w http.ResponseWriter, req *http.Request) { - w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) - w.Header().Set("Vary", "Accept-Encoding") - http.StripPrefix("/uploads/",uploads).ServeHTTP(w,req) + writ := NewWriterIntercept(w) + http.StripPrefix("/uploads/",uploads).ServeHTTP(writ,req) + if writ.GetCode() == 200 { + w.Header().Set("Cache-Control", "max-age=" + strconv.Itoa(int(common.Day))) + w.Header().Set("Vary", "Accept-Encoding") + } }, extraRoutes: make(map[string]func(http.ResponseWriter, *http.Request, common.User) common.RouteError), requestLogger: log.New(f, "", log.LstdFlags),