Skip to content
Myr

Refactor redirect request in gin.go (#1970)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Refactor redirect request in gin.go * Update http status code

authored by Ngalim Siregar · committed by thinkerou · parent 3c8e29b · Browse files · 2 files+17 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+17 added−16 removed

  • other:+17 added−16 removed

2 files+17 added−16 removed

modified:binding/form.go+1 added-1 removed
6 unmodified lines
7import (8 "net/http"9)1011const defaultMemory = 32 * 1024 * 102411const defaultMemory = 32 << 201213type formBinding struct{}14type formPostBinding struct{}15type formMultipartBinding struct{}
48 unmodified lines
modified:gin.go+16 added-15 removed
455 unmodified lines
456 p := req.URL.Path457 if prefix := path.Clean(c.Request.Header.Get("X-Forwarded-Prefix")); prefix != "." {458 p = prefix + "/" + req.URL.Path459 }460 code := http.StatusMovedPermanently // Permanent redirect, request with GET method461 if req.Method != "GET" {462 code = http.StatusTemporaryRedirect463 }464460 req.URL.Path = p + "/"461 if length := len(p); length > 1 && p[length-1] == '/' {462 req.URL.Path = p[:length-1]463 }469 debugPrint("redirecting request %d: %s --> %s", code, p, req.URL.String())470 http.Redirect(c.Writer, req, req.URL.String(), code)471 c.writermem.WriteHeaderNow()464 redirectRequest(c)465}466467func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {468 req := c.Request469 rPath := req.URL.Path470471 if fixedPath, ok := root.findCaseInsensitivePath(cleanPath(rPath), trailingSlash); ok {479 code := http.StatusMovedPermanently // Permanent redirect, request with GET method480 if req.Method != "GET" {481 code = http.StatusTemporaryRedirect482 }472 req.URL.Path = string(fixedPath)484 debugPrint("redirecting request %d: %s --> %s", code, rPath, req.URL.String())485 http.Redirect(c.Writer, req, req.URL.String(), code)486 c.writermem.WriteHeaderNow()473 redirectRequest(c)474 return true475 }476 return false477}478479func redirectRequest(c *Context) {480 req := c.Request481 rPath := req.URL.Path482 rURL := req.URL.String()483484 code := http.StatusMovedPermanently // Permanent redirect, request with GET method485 if req.Method != "GET" {486 code = http.StatusTemporaryRedirect487 }488 debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)489 http.Redirect(c.Writer, req, rURL, code)490 c.writermem.WriteHeaderNow()491}