...

Source file src/github.com/emicklei/go-restful/v3/filter.go

Documentation: github.com/emicklei/go-restful/v3

     1  package 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.
     8  type 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  	ParameterDocs []*Parameter     // the parameter docs for the route
    13  	Operation     string           // the name of the operation
    14  }
    15  
    16  // ProcessFilter passes the request,response pair through the next of Filters.
    17  // Each filter can decide to proceed to the next Filter or handle the Response itself.
    18  func (f *FilterChain) ProcessFilter(request *Request, response *Response) {
    19  	if f.Index < len(f.Filters) {
    20  		f.Index++
    21  		f.Filters[f.Index-1](request, response, f)
    22  	} else {
    23  		f.Target(request, response)
    24  	}
    25  }
    26  
    27  // FilterFunction definitions must call ProcessFilter on the FilterChain to pass on the control and eventually call the RouteFunction
    28  type FilterFunction func(*Request, *Response, *FilterChain)
    29  
    30  // NoBrowserCacheFilter is a filter function to set HTTP headers that disable browser caching
    31  // See examples/restful-no-cache-filter.go for usage
    32  func NoBrowserCacheFilter(req *Request, resp *Response, chain *FilterChain) {
    33  	resp.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
    34  	resp.Header().Set("Pragma", "no-cache")                                   // HTTP 1.0.
    35  	resp.Header().Set("Expires", "0")                                         // Proxies.
    36  	chain.ProcessFilter(req, resp)
    37  }
    38  

View as plain text