mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Merge pull request #1145 from tw4452852/header_placeholder
replacer: evaluate header placeholder when replacing
This commit is contained in:
		
						commit
						8620581f95
					
				@ -91,20 +91,13 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla
 | 
			
		||||
			io.Closer
 | 
			
		||||
		}{io.TeeReader(r.Body, rb), io.Closer(r.Body)}
 | 
			
		||||
	}
 | 
			
		||||
	rep := &replacer{
 | 
			
		||||
	return &replacer{
 | 
			
		||||
		request:            r,
 | 
			
		||||
		requestBody:        rb,
 | 
			
		||||
		responseRecorder:   rr,
 | 
			
		||||
		customReplacements: make(map[string]string),
 | 
			
		||||
		emptyValue:         emptyValue,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Header placeholders (case-insensitive)
 | 
			
		||||
	for header, values := range r.Header {
 | 
			
		||||
		rep.customReplacements["{>"+strings.ToLower(header)+"}"] = strings.Join(values, ",")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return rep
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func canLogRequest(r *http.Request) bool {
 | 
			
		||||
@ -143,10 +136,6 @@ func (r *replacer) Replace(s string) string {
 | 
			
		||||
 | 
			
		||||
		// get a replacement
 | 
			
		||||
		placeholder := s[idxStart : idxEnd+1]
 | 
			
		||||
		// Header replacements - they are case-insensitive
 | 
			
		||||
		if placeholder[1] == '>' {
 | 
			
		||||
			placeholder = strings.ToLower(placeholder)
 | 
			
		||||
		}
 | 
			
		||||
		replacement := r.getSubstitution(placeholder)
 | 
			
		||||
 | 
			
		||||
		// append prefix + replacement
 | 
			
		||||
@ -197,7 +186,18 @@ func (r *replacer) getSubstitution(key string) string {
 | 
			
		||||
		return value
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// search default replacements then
 | 
			
		||||
	// search request headers then
 | 
			
		||||
	if key[1] == '>' {
 | 
			
		||||
		want := key[2 : len(key)-1]
 | 
			
		||||
		for key, values := range r.request.Header {
 | 
			
		||||
			// Header placeholders (case-insensitive)
 | 
			
		||||
			if strings.EqualFold(key, want) {
 | 
			
		||||
				return strings.Join(values, ",")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// search default replacements in the end
 | 
			
		||||
	switch key {
 | 
			
		||||
	case "{method}":
 | 
			
		||||
		return r.request.Method
 | 
			
		||||
 | 
			
		||||
@ -45,6 +45,8 @@ func TestReplace(t *testing.T) {
 | 
			
		||||
	request.Header.Set("Custom", "foobarbaz")
 | 
			
		||||
	request.Header.Set("ShorterVal", "1")
 | 
			
		||||
	repl := NewReplacer(request, recordRequest, "-")
 | 
			
		||||
	// add some headers after creating replacer
 | 
			
		||||
	request.Header.Set("CustomAdd", "caddy")
 | 
			
		||||
 | 
			
		||||
	hostname, err := os.Hostname()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
@ -60,7 +62,8 @@ func TestReplace(t *testing.T) {
 | 
			
		||||
		{"This request method is {method}.", "This request method is POST."},
 | 
			
		||||
		{"The response status is {status}.", "The response status is 200."},
 | 
			
		||||
		{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."},
 | 
			
		||||
		{"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\nCustom: foobarbaz\\r\\nShorterval: 1\\r\\n\\r\\n."},
 | 
			
		||||
		{"The CustomAdd header is {>CustomAdd}.", "The CustomAdd header is caddy."},
 | 
			
		||||
		{"The request is {request}.", "The request is POST / HTTP/1.1\\r\\nHost: localhost\\r\\nCustom: foobarbaz\\r\\nCustomadd: caddy\\r\\nShorterval: 1\\r\\n\\r\\n."},
 | 
			
		||||
		{"The cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."},
 | 
			
		||||
		{"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."},
 | 
			
		||||
		{"Bad {host placeholder...", "Bad {host placeholder..."},
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user