...

Source file src/github.com/go-chi/chi/middleware/nocache.go

Documentation: github.com/go-chi/chi/middleware

     1  package middleware
     2  
     3  // Ported from Goji's middleware, source:
     4  // https://github.com/zenazn/goji/tree/master/web/middleware
     5  
     6  import (
     7  	"net/http"
     8  	"time"
     9  )
    10  
    11  // Unix epoch time
    12  var epoch = time.Unix(0, 0).Format(time.RFC1123)
    13  
    14  // Taken from https://github.com/mytrile/nocache
    15  var noCacheHeaders = map[string]string{
    16  	"Expires":         epoch,
    17  	"Cache-Control":   "no-cache, no-store, no-transform, must-revalidate, private, max-age=0",
    18  	"Pragma":          "no-cache",
    19  	"X-Accel-Expires": "0",
    20  }
    21  
    22  var etagHeaders = []string{
    23  	"ETag",
    24  	"If-Modified-Since",
    25  	"If-Match",
    26  	"If-None-Match",
    27  	"If-Range",
    28  	"If-Unmodified-Since",
    29  }
    30  
    31  // NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent
    32  // a router (or subrouter) from being cached by an upstream proxy and/or client.
    33  //
    34  // As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:
    35  //      Expires: Thu, 01 Jan 1970 00:00:00 UTC
    36  //      Cache-Control: no-cache, private, max-age=0
    37  //      X-Accel-Expires: 0
    38  //      Pragma: no-cache (for HTTP/1.0 proxies/clients)
    39  func NoCache(h http.Handler) http.Handler {
    40  	fn := func(w http.ResponseWriter, r *http.Request) {
    41  
    42  		// Delete any ETag headers that may have been set
    43  		for _, v := range etagHeaders {
    44  			if r.Header.Get(v) != "" {
    45  				r.Header.Del(v)
    46  			}
    47  		}
    48  
    49  		// Set our NoCache headers
    50  		for k, v := range noCacheHeaders {
    51  			w.Header().Set(k, v)
    52  		}
    53  
    54  		h.ServeHTTP(w, r)
    55  	}
    56  
    57  	return http.HandlerFunc(fn)
    58  }
    59  

View as plain text