From 593de4e91369b0d001bb0327274b20a73895389f Mon Sep 17 00:00:00 2001 From: Alexander Nyquist Date: Tue, 29 Jul 2014 00:48:02 +0200 Subject: [PATCH 1/4] Added support for redirects --- README.md | 12 +++++++++++- context.go | 5 +++++ render/render.go | 16 +++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6fa52d0..15868de 100644 --- a/README.md +++ b/README.md @@ -228,7 +228,7 @@ func main() { } ``` -#### XML, and JSON rendering +#### XML and JSON rendering ```go func main() { @@ -297,6 +297,16 @@ func main() { } ``` +#### Redirects + +Issuing a HTTP redirect is easy: + +```r.GET("/test", func(c *gin.Context) { + c.Redirect("http://www.google.com/", 302) +}) + +Both internal and external locations are supported. +``` #### Custom Middlewares diff --git a/context.go b/context.go index 17ba45c..65a3c5c 100644 --- a/context.go +++ b/context.go @@ -247,6 +247,11 @@ func (c *Context) String(code int, format string, values ...interface{}) { c.Render(code, render.Plain, format, values) } +// Returns a 302 redirect to the specific location. +func (c *Context) Redirect(location string, code int) { + c.Render(302, render.Redirect, location, code) +} + // Writes some data into the body stream and updates the HTTP code. func (c *Context) Data(code int, contentType string, data []byte) { if len(contentType) > 0 { diff --git a/render/render.go b/render/render.go index 2915ddc..b034dae 100644 --- a/render/render.go +++ b/render/render.go @@ -22,6 +22,9 @@ type ( // Plain text plainRender struct{} + // Redirects + redirectRender struct{} + // form binding HTMLRender struct { Template *template.Template @@ -29,9 +32,10 @@ type ( ) var ( - JSON = jsonRender{} - XML = xmlRender{} - Plain = plainRender{} + JSON = jsonRender{} + XML = xmlRender{} + Plain = plainRender{} + Redirect = redirectRender{} ) func writeHeader(w http.ResponseWriter, code int, contentType string) { @@ -47,6 +51,12 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) return encoder.Encode(data[0]) } +func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { + w.Header().Set("Location", data[0].(string)) + w.WriteHeader(data[1].(int)) + return nil +} + func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { writeHeader(w, code, "application/xml") encoder := xml.NewEncoder(w) From 2c4460d7cc42f8e6f915f36ff80f5cb6937c57a7 Mon Sep 17 00:00:00 2001 From: Alexander Nyquist Date: Tue, 29 Jul 2014 00:51:34 +0200 Subject: [PATCH 2/4] Fixed status code when redirecting --- context.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 65a3c5c..ccad913 100644 --- a/context.go +++ b/context.go @@ -247,9 +247,9 @@ func (c *Context) String(code int, format string, values ...interface{}) { c.Render(code, render.Plain, format, values) } -// Returns a 302 redirect to the specific location. +// Returns a HTTP redirect to the specific location. func (c *Context) Redirect(location string, code int) { - c.Render(302, render.Redirect, location, code) + c.Render(code, render.Redirect, location, code) } // Writes some data into the body stream and updates the HTTP code. From e350ae7c7ea10e92a5c51a886ccd957b1a12afb6 Mon Sep 17 00:00:00 2001 From: Alexander Nyquist Date: Tue, 29 Jul 2014 00:53:56 +0200 Subject: [PATCH 3/4] Removed redundancy when redirecting --- context.go | 2 +- render/render.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index ccad913..069f126 100644 --- a/context.go +++ b/context.go @@ -249,7 +249,7 @@ func (c *Context) String(code int, format string, values ...interface{}) { // Returns a HTTP redirect to the specific location. func (c *Context) Redirect(location string, code int) { - c.Render(code, render.Redirect, location, code) + c.Render(code, render.Redirect, location) } // Writes some data into the body stream and updates the HTTP code. diff --git a/render/render.go b/render/render.go index b034dae..293bbf9 100644 --- a/render/render.go +++ b/render/render.go @@ -53,7 +53,7 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) func (_ redirectRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { w.Header().Set("Location", data[0].(string)) - w.WriteHeader(data[1].(int)) + w.WriteHeader(code) return nil } From 64fb835e6f157d4b103084d7b6bb4d13440aac06 Mon Sep 17 00:00:00 2001 From: Alexander Nyquist Date: Sat, 2 Aug 2014 17:06:09 +0200 Subject: [PATCH 4/4] Only accepting 3xx status codes when redirecting. Swapped location and code arguments for Redirect signature --- README.md | 2 +- context.go | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 15868de..7b9196d 100644 --- a/README.md +++ b/README.md @@ -302,7 +302,7 @@ func main() { Issuing a HTTP redirect is easy: ```r.GET("/test", func(c *gin.Context) { - c.Redirect("http://www.google.com/", 302) + c.Redirect(301, "http://www.google.com/") }) Both internal and external locations are supported. diff --git a/context.go b/context.go index 069f126..8fed41d 100644 --- a/context.go +++ b/context.go @@ -248,8 +248,12 @@ func (c *Context) String(code int, format string, values ...interface{}) { } // Returns a HTTP redirect to the specific location. -func (c *Context) Redirect(location string, code int) { - c.Render(code, render.Redirect, location) +func (c *Context) Redirect(code int, location string) { + if code >= 300 && code <= 308 { + c.Render(code, render.Redirect, location) + } else { + panic(fmt.Sprintf("Cannot send a redirect with status code %d", code)) + } } // Writes some data into the body stream and updates the HTTP code.