mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	Changes regarding issue 180
The get parameters are now forwarded when redirected. Added some tests to validate this behavior.
This commit is contained in:
		
							parent
							
								
									d3c229375c
								
							
						
					
					
						commit
						8a2d0890a2
					
				@ -6,7 +6,8 @@ import (
 | 
				
			|||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"html"
 | 
						"html"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"strings"
 | 
						"net/url"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/mholt/caddy/middleware"
 | 
						"github.com/mholt/caddy/middleware"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -22,7 +23,21 @@ func (rd Redirect) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
 | 
				
			|||||||
	for _, rule := range rd.Rules {
 | 
						for _, rule := range rd.Rules {
 | 
				
			||||||
		if rule.From == "/" {
 | 
							if rule.From == "/" {
 | 
				
			||||||
			// Catchall redirect preserves path (TODO: Standardize/formalize this behavior)
 | 
								// Catchall redirect preserves path (TODO: Standardize/formalize this behavior)
 | 
				
			||||||
			newPath := strings.TrimSuffix(rule.To, "/") + r.URL.Path
 | 
								toURL, err := url.ParseRequestURI(rule.To)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return 500, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								newPath := toURL.Host + toURL.Path + r.URL.Path
 | 
				
			||||||
 | 
								rmSlashs := regexp.MustCompile("//+")
 | 
				
			||||||
 | 
								newPath = rmSlashs.ReplaceAllString(newPath, "/")
 | 
				
			||||||
 | 
								newPath = toURL.Scheme + "://" + newPath
 | 
				
			||||||
 | 
								parameters := toURL.Query()
 | 
				
			||||||
 | 
								for k, v := range r.URL.Query() {
 | 
				
			||||||
 | 
									parameters.Set(k, v[0])
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								if len(parameters) > 0 {
 | 
				
			||||||
 | 
									newPath = newPath + "?" + parameters.Encode()
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			if rule.Meta {
 | 
								if rule.Meta {
 | 
				
			||||||
				fmt.Fprintf(w, metaRedir, html.EscapeString(newPath))
 | 
									fmt.Fprintf(w, metaRedir, html.EscapeString(newPath))
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
 | 
				
			|||||||
@ -39,6 +39,43 @@ func TestMetaRedirect(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestParametersRedirect(t *testing.T) {
 | 
				
			||||||
 | 
						re := Redirect{
 | 
				
			||||||
 | 
							Rules: []Rule{
 | 
				
			||||||
 | 
								{From: "/", Meta: false, To: "http://example.com/"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req, err := http.NewRequest("GET", "/a?b=c", nil)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Test: Could not create HTTP request: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rec := httptest.NewRecorder()
 | 
				
			||||||
 | 
						re.ServeHTTP(rec, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if "http://example.com/a?b=c" != rec.Header().Get("Location") {
 | 
				
			||||||
 | 
							t.Fatalf("Test: expected location header %q but was %q", "http://example.com/a?b=c", rec.Header().Get("Location"))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						re = Redirect{
 | 
				
			||||||
 | 
							Rules: []Rule{
 | 
				
			||||||
 | 
								{From: "/", Meta: false, To: "http://example.com/a?b=c"},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						req, err = http.NewRequest("GET", "/d?e=f", nil)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatalf("Test: Could not create HTTP request: %v", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						re.ServeHTTP(rec, req)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if "http://example.com/a/d?b=c&e=f" != rec.Header().Get("Location") {
 | 
				
			||||||
 | 
							t.Fatalf("Test: expected location header %q but was %q", "http://example.com/a/d?b=c&e=f", rec.Header().Get("Location"))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestRedirect(t *testing.T) {
 | 
					func TestRedirect(t *testing.T) {
 | 
				
			||||||
	for i, test := range []struct {
 | 
						for i, test := range []struct {
 | 
				
			||||||
		from             string
 | 
							from             string
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user