mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-10-30 10:12:45 -04:00 
			
		
		
		
	Support for placeholders in fastcgi env vars.
This commit is contained in:
		
							parent
							
								
									2e84fe4504
								
							
						
					
					
						commit
						b1cd0bfeff
					
				| @ -254,9 +254,11 @@ func (h Handler) buildEnv(r *http.Request, rule Rule, fpath string) (map[string] | ||||
| 		env["HTTPS"] = "on" | ||||
| 	} | ||||
| 
 | ||||
| 	replacer := httpserver.NewReplacer(r, nil, "") | ||||
| 	// Add env variables from config | ||||
| 	for _, envVar := range rule.EnvVars { | ||||
| 		env[envVar[0]] = envVar[1] | ||||
| 		// replace request placeholders in environment variables | ||||
| 		env[envVar[0]] = replacer.Replace(envVar[1]) | ||||
| 	} | ||||
| 
 | ||||
| 	// Add all HTTP headers to env variables | ||||
|  | ||||
| @ -123,7 +123,8 @@ func TestBuildEnv(t *testing.T) { | ||||
| 		t.Error("Unexpected error:", err.Error()) | ||||
| 	} | ||||
| 
 | ||||
| 	r := http.Request{ | ||||
| 	var newReq = func() *http.Request { | ||||
| 		return &http.Request{ | ||||
| 			Method:     "GET", | ||||
| 			URL:        url, | ||||
| 			Proto:      "HTTP/1.1", | ||||
| @ -133,10 +134,12 @@ func TestBuildEnv(t *testing.T) { | ||||
| 			RemoteAddr: "[2b02:1810:4f2d:9400:70ab:f822:be8a:9093]:51688", | ||||
| 			RequestURI: "/fgci_test.php", | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	fpath := "/fgci_test.php" | ||||
| 
 | ||||
| 	var envExpected = map[string]string{ | ||||
| 	var newEnv = func() map[string]string { | ||||
| 		return map[string]string{ | ||||
| 			"REMOTE_ADDR":     "2b02:1810:4f2d:9400:70ab:f822:be8a:9093", | ||||
| 			"REMOTE_PORT":     "51688", | ||||
| 			"SERVER_PROTOCOL": "HTTP/1.1", | ||||
| @ -144,17 +147,53 @@ func TestBuildEnv(t *testing.T) { | ||||
| 			"REQUEST_METHOD":  "GET", | ||||
| 			"HTTP_HOST":       "localhost:2015", | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	// request | ||||
| 	var r *http.Request | ||||
| 
 | ||||
| 	// expected environment variables | ||||
| 	var envExpected map[string]string | ||||
| 
 | ||||
| 	// 1. Test for full canonical IPv6 address | ||||
| 	testBuildEnv(&r, rule, fpath, envExpected) | ||||
| 	r = newReq() | ||||
| 	testBuildEnv(r, rule, fpath, envExpected) | ||||
| 
 | ||||
| 	// 2. Test for shorthand notation of IPv6 address | ||||
| 	r = newReq() | ||||
| 	r.RemoteAddr = "[::1]:51688" | ||||
| 	envExpected = newEnv() | ||||
| 	envExpected["REMOTE_ADDR"] = "::1" | ||||
| 	testBuildEnv(&r, rule, fpath, envExpected) | ||||
| 	testBuildEnv(r, rule, fpath, envExpected) | ||||
| 
 | ||||
| 	// 3. Test for IPv4 address | ||||
| 	r = newReq() | ||||
| 	r.RemoteAddr = "192.168.0.10:51688" | ||||
| 	envExpected = newEnv() | ||||
| 	envExpected["REMOTE_ADDR"] = "192.168.0.10" | ||||
| 	testBuildEnv(&r, rule, fpath, envExpected) | ||||
| 	testBuildEnv(r, rule, fpath, envExpected) | ||||
| 
 | ||||
| 	// 4. Test for environment variable | ||||
| 	r = newReq() | ||||
| 	rule.EnvVars = [][2]string{ | ||||
| 		{"HTTP_HOST", "localhost:2016"}, | ||||
| 		{"REQUEST_METHOD", "POST"}, | ||||
| 	} | ||||
| 	envExpected = newEnv() | ||||
| 	envExpected["HTTP_HOST"] = "localhost:2016" | ||||
| 	envExpected["REQUEST_METHOD"] = "POST" | ||||
| 	testBuildEnv(r, rule, fpath, envExpected) | ||||
| 
 | ||||
| 	// 5. Test for environment variable placeholders | ||||
| 	r = newReq() | ||||
| 	rule.EnvVars = [][2]string{ | ||||
| 		{"HTTP_HOST", "{host}"}, | ||||
| 		{"CUSTOM_URI", "custom_uri{uri}"}, | ||||
| 		{"CUSTOM_QUERY", "custom=true&{query}"}, | ||||
| 	} | ||||
| 	envExpected = newEnv() | ||||
| 	envExpected["HTTP_HOST"] = "localhost:2015" | ||||
| 	envExpected["CUSTOM_URI"] = "custom_uri/fgci_test.php?test=blabla" | ||||
| 	envExpected["CUSTOM_QUERY"] = "custom=true&test=blabla" | ||||
| 	testBuildEnv(r, rule, fpath, envExpected) | ||||
| } | ||||
|  | ||||
| @ -127,6 +127,11 @@ func NewReplacer(r *http.Request, rr *ResponseRecorder, emptyValue string) Repla | ||||
| // Replace performs a replacement of values on s and returns | ||||
| // the string with the replaced values. | ||||
| func (r *replacer) Replace(s string) string { | ||||
| 	// Do not attempt replacements if no placeholder is found. | ||||
| 	if !strings.ContainsAny(s, "{}") { | ||||
| 		return s | ||||
| 	} | ||||
| 
 | ||||
| 	// Make response placeholders now | ||||
| 	if r.responseRecorder != nil { | ||||
| 		r.replacements["{status}"] = strconv.Itoa(r.responseRecorder.status) | ||||
|  | ||||
| @ -32,7 +32,7 @@ func TestNewReplacer(t *testing.T) { | ||||
| 		if got, want := v.replacements["{status}"], ""; got != want { | ||||
| 			t.Errorf("Expected status to NOT be set before Replace() is called; was: %s", got) | ||||
| 		} | ||||
| 		rep.Replace("foobar") | ||||
| 		rep.Replace("{foobar}") | ||||
| 		if got, want := v.replacements["{status}"], "200"; got != want { | ||||
| 			t.Errorf("Expected status to be %s, was: %s", want, got) | ||||
| 		} | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user