mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Rewrite: Use middleware.Replacer for simple rule.
This commit is contained in:
		
							parent
							
								
									0b51369932
								
							
						
					
					
						commit
						016344bae7
					
				@ -52,7 +52,11 @@ func (s SimpleRule) Rewrite(r *http.Request) bool {
 | 
				
			|||||||
		// take note of this rewrite for internal use by fastcgi
 | 
							// take note of this rewrite for internal use by fastcgi
 | 
				
			||||||
		// all we need is the URI, not full URL
 | 
							// all we need is the URI, not full URL
 | 
				
			||||||
		r.Header.Set(headerFieldName, r.URL.RequestURI())
 | 
							r.Header.Set(headerFieldName, r.URL.RequestURI())
 | 
				
			||||||
		r.URL.Path = s.To
 | 
					
 | 
				
			||||||
 | 
							// replace variables
 | 
				
			||||||
 | 
							to := path.Clean(middleware.NewReplacer(r, nil, "").Replace(s.To))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							r.URL.Path = to
 | 
				
			||||||
		return true
 | 
							return true
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return false
 | 
						return false
 | 
				
			||||||
 | 
				
			|||||||
@ -17,6 +17,7 @@ func TestRewrite(t *testing.T) {
 | 
				
			|||||||
		Rules: []Rule{
 | 
							Rules: []Rule{
 | 
				
			||||||
			NewSimpleRule("/from", "/to"),
 | 
								NewSimpleRule("/from", "/to"),
 | 
				
			||||||
			NewSimpleRule("/a", "/b"),
 | 
								NewSimpleRule("/a", "/b"),
 | 
				
			||||||
 | 
								NewSimpleRule("/b", "/b{uri}"),
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -50,6 +51,7 @@ func TestRewrite(t *testing.T) {
 | 
				
			|||||||
	}{
 | 
						}{
 | 
				
			||||||
		{"/from", "/to"},
 | 
							{"/from", "/to"},
 | 
				
			||||||
		{"/a", "/b"},
 | 
							{"/a", "/b"},
 | 
				
			||||||
 | 
							{"/b", "/b/b"},
 | 
				
			||||||
		{"/aa", "/aa"},
 | 
							{"/aa", "/aa"},
 | 
				
			||||||
		{"/", "/"},
 | 
							{"/", "/"},
 | 
				
			||||||
		{"/a?foo=bar", "/b?foo=bar"},
 | 
							{"/a?foo=bar", "/b?foo=bar"},
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user