From 7cde24a8e3f0c696a01e58fca8c3bd4a9aa0402a Mon Sep 17 00:00:00 2001 From: Ngalim Siregar Date: Wed, 3 Jul 2019 23:11:53 +0700 Subject: [PATCH 1/2] Refactor redirect request in gin.go --- binding/form.go | 2 +- gin.go | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/binding/form.go b/binding/form.go index 9e9fc3dedf..b93c34cf42 100644 --- a/binding/form.go +++ b/binding/form.go @@ -8,7 +8,7 @@ import ( "net/http" ) -const defaultMemory = 32 * 1024 * 1024 +const defaultMemory = 32 << 20 type formBinding struct{} type formPostBinding struct{} diff --git a/gin.go b/gin.go index 220f0401e3..d01b869ef6 100644 --- a/gin.go +++ b/gin.go @@ -455,9 +455,7 @@ func redirectTrailingSlash(c *Context) { if length := len(p); length > 1 && p[length-1] == '/' { req.URL.Path = p[:length-1] } - debugPrint("redirecting request %d: %s --> %s", code, p, req.URL.String()) - http.Redirect(c.Writer, req, req.URL.String(), code) - c.writermem.WriteHeaderNow() + redirectRequest(c, code) } func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool { @@ -470,10 +468,16 @@ func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool { code = http.StatusTemporaryRedirect } req.URL.Path = string(fixedPath) - debugPrint("redirecting request %d: %s --> %s", code, rPath, req.URL.String()) - http.Redirect(c.Writer, req, req.URL.String(), code) - c.writermem.WriteHeaderNow() + redirectRequest(c, code) return true } return false } + +func redirectRequest(c *Context, code int) { + rPath := c.Request.URL.Path + rURL := c.Request.URL.String() + debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL) + http.Redirect(c.Writer, c.Request, rURL, code) + c.writermem.WriteHeaderNow() +} From 6ec633dbe6ee87bc3c8d38809b6f738f36d2bc6c Mon Sep 17 00:00:00 2001 From: Ngalim Siregar Date: Thu, 4 Jul 2019 08:06:04 +0700 Subject: [PATCH 2/2] Update http status code --- gin.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/gin.go b/gin.go index d01b869ef6..e27b1579b0 100644 --- a/gin.go +++ b/gin.go @@ -446,16 +446,11 @@ func redirectTrailingSlash(c *Context) { if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." { p = prefix + "/" + req.URL.Path } - code := http.StatusMovedPermanently // Permanent redirect, request with GET method - if req.Method != "GET" { - code = http.StatusTemporaryRedirect - } - req.URL.Path = p + "/" if length := len(p); length > 1 && p[length-1] == '/' { req.URL.Path = p[:length-1] } - redirectRequest(c, code) + redirectRequest(c) } func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool { @@ -463,21 +458,23 @@ func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool { rPath := req.URL.Path if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok { - code := http.StatusMovedPermanently // Permanent redirect, request with GET method - if req.Method != "GET" { - code = http.StatusTemporaryRedirect - } req.URL.Path = string(fixedPath) - redirectRequest(c, code) + redirectRequest(c) return true } return false } -func redirectRequest(c *Context, code int) { - rPath := c.Request.URL.Path - rURL := c.Request.URL.String() +func redirectRequest(c *Context) { + req := c.Request + rPath := req.URL.Path + rURL := req.URL.String() + + code := http.StatusMovedPermanently // Permanent redirect, request with GET method + if req.Method != "GET" { + code = http.StatusTemporaryRedirect + } debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL) - http.Redirect(c.Writer, c.Request, rURL, code) + http.Redirect(c.Writer, req, rURL, code) c.writermem.WriteHeaderNow() }