From 34172d8cd07c1e431ae37a24a0ab1124efbfab50 Mon Sep 17 00:00:00 2001 From: hansongyu <359859461@qq.com> Date: Sat, 7 May 2022 16:08:45 +0800 Subject: [PATCH 1/3] Update routergroup.go --- routergroup.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/routergroup.go b/routergroup.go index 3fba3a91..bcd82cdb 100644 --- a/routergroup.go +++ b/routergroup.go @@ -89,6 +89,20 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl return group.returnObj() } +func (group *RouterGroup) unhandle(httpMethod, relativePath string) IRoutes { + absolutePath := group.calculateAbsolutePath(relativePath) + group.engine.delRoute(httpMethod, absolutePath) + return group.returnObj() +} + +// REMOVE is the way to delete route +func (group *RouterGroup) REMOVE(httpMethod,relativePath string) IRoutes { + if matched := regEnLetter.MatchString(httpMethod); !matched { + panic("http method " + httpMethod + " is not valid") + } + return group.unhandle(httpMethod, relativePath) +} + // Handle registers a new request handle and middleware with the given path and method. // The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. // See the example code in GitHub. From ee7c6d03b756039451f6776f5adf2b2cd6bc9637 Mon Sep 17 00:00:00 2001 From: hansongyu <359859461@qq.com> Date: Sat, 7 May 2022 16:10:24 +0800 Subject: [PATCH 2/3] Update gin.go --- gin.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/gin.go b/gin.go index c2e95f29..20e80432 100644 --- a/gin.go +++ b/gin.go @@ -337,6 +337,82 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { } } + +func (engine *Engine) delRoute(method, path string) { + assert1(path[0] == '/', "path must begin with '/'") + assert1(method != "", "HTTP method can not be empty") + + root := engine.trees.get(method) + if root == nil { + return + } + target ,parent ,ok ,myself:= root.findNode(path) + if !ok{ + return + } + if !myself { + parent.delChildNode(target) + }else { + root.delChildNode(target) + } + + // Update maxParams + if paramsCount := countParams(path); paramsCount == engine.maxParams { + //TODO:find new maxParams + } + + if sectionsCount := countSections(path); sectionsCount == engine.maxSections { + //TODO:find new maxParams + } +} + +func (n *node) findNode(path string) (target *node,parent *node,found bool,myself bool){ + if n.fullPath == path { + return n,nil,true,true + } + for _,child := range n.children { + if t,p,ok,m := child.findNode(path); ok { + if m { + return t,n,ok,false + } + return t,p,ok,false + }else { + continue + } + } + return nil,nil,false,false +} + +func (n *node) delChildNode(target *node){ + switch len(target.children) { + case 0: + for i, max := 0, len(n.indices); i < max; i++ { + if target.path[0] == n.indices[i] { + n.indices = n.indices[:i] + n.indices[i+1:] + n.children = append(n.children[:i], n.children[i+1:]...) + //TODO:update other params of n + break + } + } + case 1: + for i, max := 0, len(n.indices); i < max; i++ { + if target.path[0] == n.indices[i] { + n.indices = n.indices[:i] + string(target.children[0].path[0]) + n.indices[i+1:] + n.children[i] = target.children[0] + //TODO:update other params of n + break + } + } + default: + target.handlers = nil + //TODO:update other params of target + } + if len(n.children) == 1 { + //TODO:combine n and n.children[0] + } + +} + // Routes returns a slice of registered routes, including some useful information, such as: // the http method, path and the handler name. func (engine *Engine) Routes() (routes RoutesInfo) { From bb8194b526c66fd73ec4f3ccf8903e0d84fd1ea0 Mon Sep 17 00:00:00 2001 From: hansongyu <359859461@qq.com> Date: Mon, 11 Jul 2022 16:11:59 +0800 Subject: [PATCH 3/3] Update gin.go finish TODO --- gin.go | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/gin.go b/gin.go index 20e80432..e55d6ce5 100644 --- a/gin.go +++ b/gin.go @@ -1,4 +1,4 @@ -// Copyright 2014 Manu Martinez-Almeida. All rights reserved. +// Copyright 2014 Manu Martinez-Almeida. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. @@ -147,6 +147,9 @@ type Engine struct { // UseH2C enable h2c support. UseH2C bool + // ContextWithFallback enable fallback Context.Deadline(), Context.Done(), Context.Err() and Context.Value() when Context.Request.Context() is not nil. + ContextWithFallback bool + delims render.Delims secureJSONPrefix string HTMLRender render.HTMLRender @@ -195,7 +198,7 @@ func New() *Engine { trees: make(methodTrees, 0, 9), delims: render.Delims{Left: "{{", Right: "}}"}, secureJSONPrefix: "while(1);", - trustedProxies: []string{"0.0.0.0/0"}, + trustedProxies: []string{"0.0.0.0/0", "::/0"}, trustedCIDRs: defaultTrustedCIDRs, } engine.RouterGroup.engine = engine @@ -207,6 +210,7 @@ func New() *Engine { // Default returns an Engine instance with the Logger and Recovery middleware already attached. func Default() *Engine { + println("hello hsy") debugPrintWARNINGDefault() engine := New() engine.Use(Logger(), Recovery()) @@ -337,7 +341,6 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) { } } - func (engine *Engine) delRoute(method, path string) { assert1(path[0] == '/', "path must begin with '/'") assert1(method != "", "HTTP method can not be empty") @@ -355,15 +358,6 @@ func (engine *Engine) delRoute(method, path string) { }else { root.delChildNode(target) } - - // Update maxParams - if paramsCount := countParams(path); paramsCount == engine.maxParams { - //TODO:find new maxParams - } - - if sectionsCount := countSections(path); sectionsCount == engine.maxSections { - //TODO:find new maxParams - } } func (n *node) findNode(path string) (target *node,parent *node,found bool,myself bool){ @@ -390,7 +384,7 @@ func (n *node) delChildNode(target *node){ if target.path[0] == n.indices[i] { n.indices = n.indices[:i] + n.indices[i+1:] n.children = append(n.children[:i], n.children[i+1:]...) - //TODO:update other params of n + n.priority -- break } } @@ -399,20 +393,16 @@ func (n *node) delChildNode(target *node){ if target.path[0] == n.indices[i] { n.indices = n.indices[:i] + string(target.children[0].path[0]) + n.indices[i+1:] n.children[i] = target.children[0] - //TODO:update other params of n + n.priority -- break } } default: target.handlers = nil - //TODO:update other params of target } - if len(n.children) == 1 { - //TODO:combine n and n.children[0] - } - } + // Routes returns a slice of registered routes, including some useful information, such as: // the http method, path and the handler name. func (engine *Engine) Routes() (routes RoutesInfo) {