From 64fb835e6f157d4b103084d7b6bb4d13440aac06 Mon Sep 17 00:00:00 2001 From: Alexander Nyquist Date: Sat, 2 Aug 2014 17:06:09 +0200 Subject: [PATCH] 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.