mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-13 09:18:15 +08:00
Compare commits
3 Commits
e300aae1e7
...
441a9d532f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
441a9d532f | ||
|
|
b2b489dbf4 | ||
|
|
ce6939ce40 |
27
context.go
27
context.go
@ -978,14 +978,27 @@ func (c *Context) ClientIP() string {
|
||||
}
|
||||
}
|
||||
|
||||
// It also checks if the remoteIP is a trusted proxy or not.
|
||||
// In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks
|
||||
// defined by Engine.SetTrustedProxies()
|
||||
remoteIP := net.ParseIP(c.RemoteIP())
|
||||
if remoteIP == nil {
|
||||
return ""
|
||||
var (
|
||||
trusted bool
|
||||
remoteIP net.IP
|
||||
)
|
||||
// If gin is listening a unix socket, always trust it.
|
||||
localAddr, ok := c.Request.Context().Value(http.LocalAddrContextKey).(net.Addr)
|
||||
if ok && strings.HasPrefix(localAddr.Network(), "unix") {
|
||||
trusted = true
|
||||
}
|
||||
|
||||
// Fallback
|
||||
if !trusted {
|
||||
// It also checks if the remoteIP is a trusted proxy or not.
|
||||
// In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks
|
||||
// defined by Engine.SetTrustedProxies()
|
||||
remoteIP = net.ParseIP(c.RemoteIP())
|
||||
if remoteIP == nil {
|
||||
return ""
|
||||
}
|
||||
trusted = c.engine.isTrustedProxy(remoteIP)
|
||||
}
|
||||
trusted := c.engine.isTrustedProxy(remoteIP)
|
||||
|
||||
if trusted && c.engine.ForwardedByClientIP && c.engine.RemoteIPHeaders != nil {
|
||||
for _, headerName := range c.engine.RemoteIPHeaders {
|
||||
|
||||
@ -1915,6 +1915,16 @@ func TestContextClientIP(t *testing.T) {
|
||||
c.engine.trustedCIDRs, _ = c.engine.prepareTrustedCIDRs()
|
||||
resetContextForClientIPTests(c)
|
||||
|
||||
// unix address
|
||||
addr := &net.UnixAddr{Net: "unix", Name: "@"}
|
||||
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), http.LocalAddrContextKey, addr))
|
||||
c.Request.RemoteAddr = addr.String()
|
||||
assert.Equal(t, "20.20.20.20", c.ClientIP())
|
||||
|
||||
// reset
|
||||
c.Request = c.Request.WithContext(context.Background())
|
||||
resetContextForClientIPTests(c)
|
||||
|
||||
// Legacy tests (validating that the defaults don't break the
|
||||
// (insecure!) old behaviour)
|
||||
assert.Equal(t, "20.20.20.20", c.ClientIP())
|
||||
|
||||
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
|
||||
|
||||
66
gin.go
66
gin.go
@ -394,6 +394,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 {
|
||||
@ -687,6 +705,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
|
||||
@ -830,3 +885,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, "&")
|
||||
}
|
||||
|
||||
19
gin_test.go
19
gin_test.go
@ -897,6 +897,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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user