blob: c23bfb591ad77be8d1ac53b1a5e4073f50d4751d [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001package restful
2
3// Copyright 2013 Ernest Micklei. All rights reserved.
4// Use of this source code is governed by a license
5// that can be found in the LICENSE file.
6
7// FilterChain is a request scoped object to process one or more filters before calling the target RouteFunction.
8type FilterChain struct {
9 Filters []FilterFunction // ordered list of FilterFunction
10 Index int // index into filters that is currently in progress
11 Target RouteFunction // function to call after passing all filters
12}
13
14// ProcessFilter passes the request,response pair through the next of Filters.
15// Each filter can decide to proceed to the next Filter or handle the Response itself.
16func (f *FilterChain) ProcessFilter(request *Request, response *Response) {
17 if f.Index < len(f.Filters) {
18 f.Index++
19 f.Filters[f.Index-1](request, response, f)
20 } else {
21 f.Target(request, response)
22 }
23}
24
25// FilterFunction definitions must call ProcessFilter on the FilterChain to pass on the control and eventually call the RouteFunction
26type FilterFunction func(*Request, *Response, *FilterChain)
27
28// NoBrowserCacheFilter is a filter function to set HTTP headers that disable browser caching
29// See examples/restful-no-cache-filter.go for usage
30func NoBrowserCacheFilter(req *Request, resp *Response, chain *FilterChain) {
31 resp.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
32 resp.Header().Set("Pragma", "no-cache") // HTTP 1.0.
33 resp.Header().Set("Expires", "0") // Proxies.
34 chain.ProcessFilter(req, resp)
35}