mirror of
https://github.com/gin-gonic/gin.git
synced 2026-07-08 19:54:08 +08:00
Compare commits
4 Commits
0a2e4c0d08
...
c936531675
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c936531675 | ||
|
|
5fad976b37 | ||
|
|
93ff771e6d | ||
|
|
ce6939ce40 |
@ -18,15 +18,8 @@ linters:
|
||||
- wastedassign
|
||||
settings:
|
||||
gosec:
|
||||
includes:
|
||||
- G102
|
||||
- G106
|
||||
- G108
|
||||
- G109
|
||||
- G111
|
||||
- G112
|
||||
- G201
|
||||
- G203
|
||||
excludes:
|
||||
- G115
|
||||
perfsprint:
|
||||
int-conversion: true
|
||||
err-error: true
|
||||
|
||||
10
context.go
10
context.go
@ -55,6 +55,14 @@ const ContextRequestKey ContextKeyType = 0
|
||||
// abortIndex represents a typical value used in abort functions.
|
||||
const abortIndex int8 = math.MaxInt8 >> 1
|
||||
|
||||
// safeInt8 converts int to int8 safely, capping at math.MaxInt8
|
||||
func safeInt8(n int) int8 {
|
||||
if n > math.MaxInt8 {
|
||||
return math.MaxInt8
|
||||
}
|
||||
return int8(n)
|
||||
}
|
||||
|
||||
// Context is the most important part of gin. It allows us to pass variables between middleware,
|
||||
// manage the flow, validate the JSON of a request and render a JSON response for example.
|
||||
type Context struct {
|
||||
@ -186,7 +194,7 @@ func (c *Context) FullPath() string {
|
||||
// See example in GitHub.
|
||||
func (c *Context) Next() {
|
||||
c.index++
|
||||
for c.index < int8(len(c.handlers)) {
|
||||
for c.index < safeInt8(len(c.handlers)) {
|
||||
if c.handlers[c.index] != nil {
|
||||
c.handlers[c.index](c)
|
||||
}
|
||||
|
||||
24
docs/doc.md
24
docs/doc.md
@ -9,6 +9,7 @@
|
||||
- [Using GET, POST, PUT, PATCH, DELETE and OPTIONS](#using-get-post-put-patch-delete-and-options)
|
||||
- [Parameters in path](#parameters-in-path)
|
||||
- [Querystring parameters](#querystring-parameters)
|
||||
- [Retrieving the path of a registered handler](#retrieving-the-path-of-a-registered-handler)
|
||||
- [Multipart/Urlencoded Form](#multiparturlencoded-form)
|
||||
- [Another example: query + post form](#another-example-query--post-form)
|
||||
- [Map as querystring or postform parameters](#map-as-querystring-or-postform-parameters)
|
||||
@ -184,6 +185,29 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### Retrieving the path of a registered handler
|
||||
|
||||
Once a handler is registered with the router, you can retrieve its path with the `PathFor` method:
|
||||
|
||||
```go
|
||||
func main() {
|
||||
// Creates a gin router with default middleware:
|
||||
// logger and recovery (crash-free) middleware
|
||||
router := gin.Default()
|
||||
|
||||
getUser := func(c *Context) {
|
||||
// Handle the GET request.
|
||||
}
|
||||
router.GET("/users/:name", getUser)
|
||||
path := router.PathFor(getUser, ":name", "gopher")
|
||||
|
||||
print(path) // Prints /users/gopher
|
||||
|
||||
router.Run()
|
||||
}
|
||||
router := gin.Default()
|
||||
```
|
||||
|
||||
### Multipart/Urlencoded Form
|
||||
|
||||
```go
|
||||
|
||||
112
gin.go
112
gin.go
@ -23,10 +23,12 @@ import (
|
||||
"golang.org/x/net/http2/h2c"
|
||||
)
|
||||
|
||||
const defaultMultipartMemory = 32 << 20 // 32 MB
|
||||
const escapedColon = "\\:"
|
||||
const colon = ":"
|
||||
const backslash = "\\"
|
||||
const (
|
||||
defaultMultipartMemory = 32 << 20 // 32 MB
|
||||
escapedColon = "\\:"
|
||||
colon = ":"
|
||||
backslash = "\\"
|
||||
)
|
||||
|
||||
var (
|
||||
default404Body = []byte("404 page not found")
|
||||
@ -46,8 +48,10 @@ var defaultTrustedCIDRs = []*net.IPNet{
|
||||
},
|
||||
}
|
||||
|
||||
var regSafePrefix = regexp.MustCompile("[^a-zA-Z0-9/-]+")
|
||||
var regRemoveRepeatedChar = regexp.MustCompile("/{2,}")
|
||||
var (
|
||||
regSafePrefix = regexp.MustCompile("[^a-zA-Z0-9/-]+")
|
||||
regRemoveRepeatedChar = regexp.MustCompile("/{2,}")
|
||||
)
|
||||
|
||||
// HandlerFunc defines the handler used by gin middleware as return value.
|
||||
type HandlerFunc func(*Context)
|
||||
@ -94,6 +98,10 @@ const (
|
||||
type Engine struct {
|
||||
RouterGroup
|
||||
|
||||
// routeTreesUpdated ensures that the initialization or update of the route trees
|
||||
// (used for routing HTTP requests) happens only once, even if called multiple times concurrently.
|
||||
routeTreesUpdated sync.Once
|
||||
|
||||
// RedirectTrailingSlash enables automatic redirection if the current route can't be matched but a
|
||||
// handler for the path with (without) the trailing slash exists.
|
||||
// For example if /foo/ is requested but a route only exists for /foo, the
|
||||
@ -384,6 +392,24 @@ func (engine *Engine) Routes() (routes RoutesInfo) {
|
||||
return routes
|
||||
}
|
||||
|
||||
// Routes returns a slice of registered routes, including some useful information, such as:
|
||||
// the http method, path and the handler name.
|
||||
func (engine *Engine) Route(handler HandlerFunc) (route RouteInfo, ok bool) {
|
||||
handlerName := nameOfFunction(handler)
|
||||
routes := RoutesInfo{}
|
||||
for _, tree := range engine.trees {
|
||||
routes = iterate("", tree.method, routes, tree.root)
|
||||
|
||||
for _, route := range routes {
|
||||
if route.Handler == handlerName {
|
||||
return route, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return RouteInfo{}, false
|
||||
}
|
||||
|
||||
func iterate(path, method string, routes RoutesInfo, root *node) RoutesInfo {
|
||||
path += root.path
|
||||
if len(root.handlers) > 0 {
|
||||
@ -537,7 +563,11 @@ func (engine *Engine) Run(addr ...string) (err error) {
|
||||
engine.updateRouteTrees()
|
||||
address := resolveAddress(addr)
|
||||
debugPrint("Listening and serving HTTP on %s\n", address)
|
||||
err = http.ListenAndServe(address, engine.Handler())
|
||||
server := &http.Server{ // #nosec G112
|
||||
Addr: address,
|
||||
Handler: engine.Handler(),
|
||||
}
|
||||
err = server.ListenAndServe()
|
||||
return
|
||||
}
|
||||
|
||||
@ -553,7 +583,11 @@ func (engine *Engine) RunTLS(addr, certFile, keyFile string) (err error) {
|
||||
"Please check https://github.com/gin-gonic/gin/blob/master/docs/doc.md#dont-trust-all-proxies for details.")
|
||||
}
|
||||
|
||||
err = http.ListenAndServeTLS(addr, certFile, keyFile, engine.Handler())
|
||||
server := &http.Server{ // #nosec G112
|
||||
Addr: addr,
|
||||
Handler: engine.Handler(),
|
||||
}
|
||||
err = server.ListenAndServeTLS(certFile, keyFile)
|
||||
return
|
||||
}
|
||||
|
||||
@ -576,7 +610,10 @@ func (engine *Engine) RunUnix(file string) (err error) {
|
||||
defer listener.Close()
|
||||
defer os.Remove(file)
|
||||
|
||||
err = http.Serve(listener, engine.Handler())
|
||||
server := &http.Server{ // #nosec G112
|
||||
Handler: engine.Handler(),
|
||||
}
|
||||
err = server.Serve(listener)
|
||||
return
|
||||
}
|
||||
|
||||
@ -630,12 +667,19 @@ func (engine *Engine) RunListener(listener net.Listener) (err error) {
|
||||
"Please check https://github.com/gin-gonic/gin/blob/master/docs/doc.md#dont-trust-all-proxies for details.")
|
||||
}
|
||||
|
||||
err = http.Serve(listener, engine.Handler())
|
||||
server := &http.Server{ // #nosec G112
|
||||
Handler: engine.Handler(),
|
||||
}
|
||||
err = server.Serve(listener)
|
||||
return
|
||||
}
|
||||
|
||||
// ServeHTTP conforms to the http.Handler interface.
|
||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
engine.routeTreesUpdated.Do(func() {
|
||||
engine.updateRouteTrees()
|
||||
})
|
||||
|
||||
c := engine.pool.Get().(*Context)
|
||||
c.writermem.reset(w)
|
||||
c.Request = req
|
||||
@ -659,6 +703,43 @@ func (engine *Engine) HandleContext(c *Context) {
|
||||
c.handlers = oldHandlers
|
||||
}
|
||||
|
||||
// PathFor returns the path registered for the specified handler function.
|
||||
// Route values are passed pair-wise as key value.
|
||||
func (engine *Engine) PathFor(handler HandlerFunc, values ...interface{}) string {
|
||||
route, ok := engine.Route(handler)
|
||||
|
||||
if !ok || len(route.Path) == 0 || len(values)%2 != 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
url := route.Path
|
||||
params := make(map[string]string)
|
||||
if len(values) > 0 {
|
||||
key := ""
|
||||
for k, v := range values {
|
||||
if k%2 == 0 {
|
||||
key = fmt.Sprint(v)
|
||||
} else {
|
||||
params[key] = fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
urls := strings.Split(url, "/")
|
||||
for _, v := range urls {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
if v[0:1] == ":" {
|
||||
if u, ok := params[v]; ok {
|
||||
delete(params, v)
|
||||
url = strings.Replace(url, v, u, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return url + toQuerystring(params)
|
||||
}
|
||||
|
||||
func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
httpMethod := c.Request.Method
|
||||
rPath := c.Request.URL.Path
|
||||
@ -787,3 +868,14 @@ func redirectRequest(c *Context) {
|
||||
http.Redirect(c.Writer, req, rURL, code)
|
||||
c.writermem.WriteHeaderNow()
|
||||
}
|
||||
|
||||
func toQuerystring(params map[string]string) string {
|
||||
if len(params) == 0 {
|
||||
return ""
|
||||
}
|
||||
u := "?"
|
||||
for k, v := range params {
|
||||
u += k + "=" + v + "&"
|
||||
}
|
||||
return strings.TrimRight(u, "&")
|
||||
}
|
||||
|
||||
118
gin_test.go
118
gin_test.go
@ -825,6 +825,25 @@ func TestPrepareTrustedCIRDsWith(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathFor(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
getIndex := func(c *Context) {}
|
||||
r.GET("/", getIndex)
|
||||
|
||||
users := r.Group("/users")
|
||||
|
||||
postUser := func(c *Context) {}
|
||||
users.POST("", postUser)
|
||||
|
||||
getUser := func(c *Context) {}
|
||||
users.GET("/:name", getUser)
|
||||
|
||||
assert.Equal(t, "/", r.PathFor(getIndex))
|
||||
assert.Equal(t, "/users", r.PathFor(postUser))
|
||||
assert.Equal(t, "/users/gopher", r.PathFor(getUser, ":name", "gopher"))
|
||||
}
|
||||
|
||||
func parseCIDR(cidr string) *net.IPNet {
|
||||
_, parsedCIDR, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
@ -913,3 +932,102 @@ func TestMethodNotAllowedNoRoute(t *testing.T) {
|
||||
assert.NotPanics(t, func() { g.ServeHTTP(resp, req) })
|
||||
assert.Equal(t, http.StatusNotFound, resp.Code)
|
||||
}
|
||||
|
||||
// Test the fix for https://github.com/gin-gonic/gin/pull/4415
|
||||
func TestLiteralColonWithRun(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
router.updateRouteTrees()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
}
|
||||
|
||||
func TestLiteralColonWithDirectServeHTTP(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
}
|
||||
|
||||
func TestLiteralColonWithHandler(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
handler := router.Handler()
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
}
|
||||
|
||||
func TestLiteralColonWithHTTPServer(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"path": "literal_colon"})
|
||||
})
|
||||
|
||||
router.GET("/test/:param", func(c *Context) {
|
||||
c.JSON(http.StatusOK, H{"param": c.Param("param")})
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "literal_colon")
|
||||
|
||||
w2 := httptest.NewRecorder()
|
||||
req2, _ := http.NewRequest(http.MethodGet, "/test/foo", nil)
|
||||
router.ServeHTTP(w2, req2)
|
||||
|
||||
assert.Equal(t, http.StatusOK, w2.Code)
|
||||
assert.Contains(t, w2.Body.String(), "foo")
|
||||
}
|
||||
|
||||
// Test that updateRouteTrees is called only once
|
||||
func TestUpdateRouteTreesCalledOnce(t *testing.T) {
|
||||
SetMode(TestMode)
|
||||
router := New()
|
||||
|
||||
router.GET(`/test\:action`, func(c *Context) {
|
||||
c.String(http.StatusOK, "ok")
|
||||
})
|
||||
|
||||
for range 5 {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest(http.MethodGet, "/test:action", nil)
|
||||
router.ServeHTTP(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "ok", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
13
tree.go
13
tree.go
@ -5,6 +5,7 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net/url"
|
||||
"strings"
|
||||
"unicode"
|
||||
@ -77,14 +78,22 @@ func (n *node) addChild(child *node) {
|
||||
}
|
||||
}
|
||||
|
||||
// safeUint16 converts int to uint16 safely, capping at math.MaxUint16
|
||||
func safeUint16(n int) uint16 {
|
||||
if n > math.MaxUint16 {
|
||||
return math.MaxUint16
|
||||
}
|
||||
return uint16(n)
|
||||
}
|
||||
|
||||
func countParams(path string) uint16 {
|
||||
colons := strings.Count(path, ":")
|
||||
stars := strings.Count(path, "*")
|
||||
return uint16(colons + stars)
|
||||
return safeUint16(colons + stars)
|
||||
}
|
||||
|
||||
func countSections(path string) uint16 {
|
||||
return uint16(strings.Count(path, "/"))
|
||||
return safeUint16(strings.Count(path, "/"))
|
||||
}
|
||||
|
||||
type nodeType uint8
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user