woodpecker/server/middleware/header.go

32 lines
1,022 B
Go
Raw Normal View History

2014-09-29 01:36:24 +00:00
package middleware
import (
"net/http"
"time"
"github.com/zenazn/goji/web"
)
// SetHeaders is a middleware function that applies
// default headers and caching rules to each request.
func SetHeaders(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("X-Frame-Options", "DENY")
w.Header().Add("X-Content-Type-Options", "nosniff")
w.Header().Add("X-XSS-Protection", "1; mode=block")
w.Header().Add("Cache-Control", "no-cache")
w.Header().Add("Cache-Control", "no-store")
w.Header().Add("Cache-Control", "max-age=0")
w.Header().Add("Cache-Control", "must-revalidate")
w.Header().Add("Cache-Control", "value")
w.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
w.Header().Set("Expires", "Thu, 01 Jan 1970 00:00:00 GMT")
2015-02-09 18:59:39 +00:00
if r.TLS != nil {
2015-02-09 18:53:52 +00:00
w.Header().Add("Strict-Transport-Security", "max-age=31536000")
}
2014-09-29 01:36:24 +00:00
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}