mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 02:27:19 -04:00 
			
		
		
		
	On matched header rules, replacer is used to replace any placeholders
defined in header rules iex. X-Backend {hostname} where {hostname} will
be replaced by the hostname key present in the replacer
hostname key added to replacer. The value is determined by the output of
`os.Hostname()`
		
	
			
		
			
				
	
	
		
			52 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Package headers provides middleware that appends headers to
 | |
| // requests based on a set of configuration rules that define
 | |
| // which routes receive which headers.
 | |
| package headers
 | |
| 
 | |
| import (
 | |
| 	"net/http"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/mholt/caddy/middleware"
 | |
| )
 | |
| 
 | |
| // Headers is middleware that adds headers to the responses
 | |
| // for requests matching a certain path.
 | |
| type Headers struct {
 | |
| 	Next  middleware.Handler
 | |
| 	Rules []Rule
 | |
| }
 | |
| 
 | |
| // ServeHTTP implements the middleware.Handler interface and serves requests,
 | |
| // setting headers on the response according to the configured rules.
 | |
| func (h Headers) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
 | |
| 	replacer := middleware.NewReplacer(r, nil, "")
 | |
| 	for _, rule := range h.Rules {
 | |
| 		if middleware.Path(r.URL.Path).Matches(rule.Path) {
 | |
| 			for _, header := range rule.Headers {
 | |
| 				if strings.HasPrefix(header.Name, "-") {
 | |
| 					w.Header().Del(strings.TrimLeft(header.Name, "-"))
 | |
| 				} else {
 | |
| 					w.Header().Set(header.Name, replacer.Replace(header.Value))
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 	return h.Next.ServeHTTP(w, r)
 | |
| }
 | |
| 
 | |
| type (
 | |
| 	// Rule groups a slice of HTTP headers by a URL pattern.
 | |
| 	// TODO: use http.Header type instead?
 | |
| 	Rule struct {
 | |
| 		Path    string
 | |
| 		Headers []Header
 | |
| 	}
 | |
| 
 | |
| 	// Header represents a single HTTP header, simply a name and value.
 | |
| 	Header struct {
 | |
| 		Name  string
 | |
| 		Value string
 | |
| 	}
 | |
| )
 |