Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(net/ghttp): Server Domain if is empty str, bind handler pattern will add @ which is not expect #4100 #4101

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions net/ghttp/ghttp_server_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func (s *Server) Domain(domains string) *Domain {
// BindHandler binds the handler for the specified pattern.
func (d *Domain) BindHandler(pattern string, handler interface{}) {
for domain := range d.domains {
d.server.BindHandler(pattern+"@"+domain, handler)
d.server.BindHandler(patternBindDomain(pattern, domain), handler)
}
}

func (d *Domain) doBindHandler(ctx context.Context, in doBindHandlerInput) {
for domain := range d.domains {
d.server.doBindHandler(ctx, doBindHandlerInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Pattern: patternBindDomain(in.Pattern, domain),
FuncInfo: in.FuncInfo,
Middleware: in.Middleware,
Source: in.Source,
Expand All @@ -51,15 +51,15 @@ func (d *Domain) doBindHandler(ctx context.Context, in doBindHandlerInput) {
// BindObject binds the object for the specified pattern.
func (d *Domain) BindObject(pattern string, obj interface{}, methods ...string) {
for domain := range d.domains {
d.server.BindObject(pattern+"@"+domain, obj, methods...)
d.server.BindObject(patternBindDomain(pattern, domain), obj, methods...)
}
}

func (d *Domain) doBindObject(ctx context.Context, in doBindObjectInput) {
for domain := range d.domains {
d.server.doBindObject(ctx, doBindObjectInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Pattern: patternBindDomain(in.Pattern, domain),
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Expand All @@ -71,15 +71,15 @@ func (d *Domain) doBindObject(ctx context.Context, in doBindObjectInput) {
// BindObjectMethod binds the method for the specified pattern.
func (d *Domain) BindObjectMethod(pattern string, obj interface{}, method string) {
for domain := range d.domains {
d.server.BindObjectMethod(pattern+"@"+domain, obj, method)
d.server.BindObjectMethod(patternBindDomain(pattern, domain), obj, method)
}
}

func (d *Domain) doBindObjectMethod(ctx context.Context, in doBindObjectMethodInput) {
for domain := range d.domains {
d.server.doBindObjectMethod(ctx, doBindObjectMethodInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Pattern: patternBindDomain(in.Pattern, domain),
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Expand All @@ -91,15 +91,15 @@ func (d *Domain) doBindObjectMethod(ctx context.Context, in doBindObjectMethodIn
// BindObjectRest binds the RESTful API for the specified pattern.
func (d *Domain) BindObjectRest(pattern string, obj interface{}) {
for domain := range d.domains {
d.server.BindObjectRest(pattern+"@"+domain, obj)
d.server.BindObjectRest(patternBindDomain(pattern, domain), obj)
}
}

func (d *Domain) doBindObjectRest(ctx context.Context, in doBindObjectInput) {
for domain := range d.domains {
d.server.doBindObjectRest(ctx, doBindObjectInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Pattern: patternBindDomain(in.Pattern, domain),
Object: in.Object,
Method: in.Method,
Middleware: in.Middleware,
Expand All @@ -111,15 +111,15 @@ func (d *Domain) doBindObjectRest(ctx context.Context, in doBindObjectInput) {
// BindHookHandler binds the hook handler for the specified pattern.
func (d *Domain) BindHookHandler(pattern string, hook HookName, handler HandlerFunc) {
for domain := range d.domains {
d.server.BindHookHandler(pattern+"@"+domain, hook, handler)
d.server.BindHookHandler(patternBindDomain(pattern, domain), hook, handler)
}
}

func (d *Domain) doBindHookHandler(ctx context.Context, in doBindHookHandlerInput) {
for domain := range d.domains {
d.server.doBindHookHandler(ctx, doBindHookHandlerInput{
Prefix: in.Prefix,
Pattern: in.Pattern + "@" + domain,
Pattern: patternBindDomain(in.Pattern, domain),
HookName: in.HookName,
Handler: in.Handler,
Source: in.Source,
Expand All @@ -130,7 +130,7 @@ func (d *Domain) doBindHookHandler(ctx context.Context, in doBindHookHandlerInpu
// BindHookHandlerByMap binds the hook handler for the specified pattern.
func (d *Domain) BindHookHandlerByMap(pattern string, hookMap map[HookName]HandlerFunc) {
for domain := range d.domains {
d.server.BindHookHandlerByMap(pattern+"@"+domain, hookMap)
d.server.BindHookHandlerByMap(patternBindDomain(pattern, domain), hookMap)
}
}

Expand All @@ -151,18 +151,25 @@ func (d *Domain) BindStatusHandlerByMap(handlerMap map[int]HandlerFunc) {
// BindMiddleware binds the middleware for the specified pattern.
func (d *Domain) BindMiddleware(pattern string, handlers ...HandlerFunc) {
for domain := range d.domains {
d.server.BindMiddleware(pattern+"@"+domain, handlers...)
d.server.BindMiddleware(patternBindDomain(pattern, domain), handlers...)
}
}

// BindMiddlewareDefault binds the default middleware for the specified pattern.
func (d *Domain) BindMiddlewareDefault(handlers ...HandlerFunc) {
for domain := range d.domains {
d.server.BindMiddleware(defaultMiddlewarePattern+"@"+domain, handlers...)
d.server.BindMiddleware(patternBindDomain(defaultMiddlewarePattern, domain), handlers...)
}
}

// Use adds middleware to the domain.
func (d *Domain) Use(handlers ...HandlerFunc) {
d.BindMiddlewareDefault(handlers...)
}

func patternBindDomain(pattern, domain string) string {
if domain != "" {
return pattern + "@" + domain
}
return pattern
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might have something to do with the regular expression at

array, _ := gregex.MatchString(`(.*?)%([A-Z]+):(.+)@(.+)`, k)
The regular expression might better be (.*?)%([A-Z]+):(.+?)@(.+).
You also need commit associated unit testing case for this update.

}
Loading