github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/go-chi/chi/middleware/nocache.go (about)

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