mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-31 02:27:19 -04:00 
			
		
		
		
	replacer: evaluate header placeholder when replacing
fix issue #1137 Signed-off-by: Tw <tw19881113@gmail.com>
This commit is contained in:
		
							parent
							
								
									b06b3981cf
								
							
						
					
					
						commit
						99a6b2db67
					
				| @ -91,20 +91,13 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla | |||||||
| 			io.Closer | 			io.Closer | ||||||
| 		}{io.TeeReader(r.Body, rb), io.Closer(r.Body)} | 		}{io.TeeReader(r.Body, rb), io.Closer(r.Body)} | ||||||
| 	} | 	} | ||||||
| 	rep := &replacer{ | 	return &replacer{ | ||||||
| 		request:            r, | 		request:            r, | ||||||
| 		requestBody:        rb, | 		requestBody:        rb, | ||||||
| 		responseRecorder:   rr, | 		responseRecorder:   rr, | ||||||
| 		customReplacements: make(map[string]string), | 		customReplacements: make(map[string]string), | ||||||
| 		emptyValue:         emptyValue, | 		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 { | func canLogRequest(r *http.Request) bool { | ||||||
| @ -143,10 +136,6 @@ func (r *replacer) Replace(s string) string { | |||||||
| 
 | 
 | ||||||
| 		// get a replacement | 		// get a replacement | ||||||
| 		placeholder := s[idxStart : idxEnd+1] | 		placeholder := s[idxStart : idxEnd+1] | ||||||
| 		// Header replacements - they are case-insensitive |  | ||||||
| 		if placeholder[1] == '>' { |  | ||||||
| 			placeholder = strings.ToLower(placeholder) |  | ||||||
| 		} |  | ||||||
| 		replacement := r.getSubstitution(placeholder) | 		replacement := r.getSubstitution(placeholder) | ||||||
| 
 | 
 | ||||||
| 		// append prefix + replacement | 		// append prefix + replacement | ||||||
| @ -197,7 +186,18 @@ func (r *replacer) getSubstitution(key string) string { | |||||||
| 		return value | 		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 { | 	switch key { | ||||||
| 	case "{method}": | 	case "{method}": | ||||||
| 		return r.request.Method | 		return r.request.Method | ||||||
|  | |||||||
| @ -45,6 +45,8 @@ func TestReplace(t *testing.T) { | |||||||
| 	request.Header.Set("Custom", "foobarbaz") | 	request.Header.Set("Custom", "foobarbaz") | ||||||
| 	request.Header.Set("ShorterVal", "1") | 	request.Header.Set("ShorterVal", "1") | ||||||
| 	repl := NewReplacer(request, recordRequest, "-") | 	repl := NewReplacer(request, recordRequest, "-") | ||||||
|  | 	// add some headers after creating replacer | ||||||
|  | 	request.Header.Set("CustomAdd", "caddy") | ||||||
| 
 | 
 | ||||||
| 	hostname, err := os.Hostname() | 	hostname, err := os.Hostname() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| @ -60,7 +62,8 @@ func TestReplace(t *testing.T) { | |||||||
| 		{"This request method is {method}.", "This request method is POST."}, | 		{"This request method is {method}.", "This request method is POST."}, | ||||||
| 		{"The response status is {status}.", "The response status is 200."}, | 		{"The response status is {status}.", "The response status is 200."}, | ||||||
| 		{"The Custom header is {>Custom}.", "The Custom header is foobarbaz."}, | 		{"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 cUsToM header is {>cUsToM}...", "The cUsToM header is foobarbaz..."}, | ||||||
| 		{"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."}, | 		{"The Non-Existent header is {>Non-Existent}.", "The Non-Existent header is -."}, | ||||||
| 		{"Bad {host placeholder...", "Bad {host placeholder..."}, | 		{"Bad {host placeholder...", "Bad {host placeholder..."}, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user