Skip to content

Commit

Permalink
vhost: fix a wrong usage of sort.Reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Dec 26, 2016
1 parent c2c9f68 commit cdcc124
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions src/models/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,15 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) {
if err != nil {
return err
}
log.Info("ProxyName [%s], type http listen for host [%s] location [%s]", p.Name, domain, "")
p.listeners = append(p.listeners, l)
} else {
for _, location := range p.Locations {
l, err := VhostHttpMuxer.Listen(domain, location, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
log.Info("ProxyName [%s], type http listen for host [%s] location [%s]", p.Name, domain, location)
p.listeners = append(p.listeners, l)
}
}
Expand All @@ -165,13 +167,15 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) {
if err != nil {
return err
}
log.Info("ProxyName [%s], type http listen for host [%s] location [%s]", p.Name, p.SubDomain, "")
p.listeners = append(p.listeners, l)
} else {
for _, location := range p.Locations {
l, err := VhostHttpMuxer.Listen(p.SubDomain, location, p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
log.Info("ProxyName [%s], type http listen for host [%s] location [%s]", p.Name, p.SubDomain, location)
p.listeners = append(p.listeners, l)
}
}
Expand All @@ -182,13 +186,15 @@ func (p *ProxyServer) Start(c *conn.Conn) (err error) {
if err != nil {
return err
}
log.Info("ProxyName [%s], type https listen for host [%s]", p.Name, domain)
p.listeners = append(p.listeners, l)
}
if p.SubDomain != "" {
l, err := VhostHttpsMuxer.Listen(p.SubDomain, "", p.HostHeaderRewrite, p.HttpUserName, p.HttpPassWord)
if err != nil {
return err
}
log.Info("ProxyName [%s], type https listen for host [%s]", p.Name, p.SubDomain)
p.listeners = append(p.listeners, l)
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/utils/vhost/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *VhostRouters) Add(domain, location string, l *Listener) {

vrs, found := r.RouterByDomain[domain]
if !found {
vrs = make([]*VhostRouter, 0)
vrs = make([]*VhostRouter, 0, 1)
}

vr := &VhostRouter{
Expand All @@ -39,25 +39,25 @@ func (r *VhostRouters) Add(domain, location string, l *Listener) {
}
vrs = append(vrs, vr)

sort.Reverse(ByLocation(vrs))
sort.Sort(sort.Reverse(ByLocation(vrs)))
r.RouterByDomain[domain] = vrs
}

func (r *VhostRouters) Del(l *Listener) {
func (r *VhostRouters) Del(domain, location string) {
r.mutex.Lock()
defer r.mutex.Unlock()

vrs, found := r.RouterByDomain[l.name]
vrs, found := r.RouterByDomain[domain]
if !found {
return
}

for i, vr := range vrs {
if vr.listener == l {
if vr.location == location {
if len(vrs) > i+1 {
r.RouterByDomain[l.name] = append(vrs[:i], vrs[i+1:]...)
r.RouterByDomain[domain] = append(vrs[:i], vrs[i+1:]...)
} else {
r.RouterByDomain[l.name] = vrs[:i]
r.RouterByDomain[domain] = vrs[:i]
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/vhost/vhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (v *VhostMuxer) Listen(name, location, rewriteHost, userName, passWord stri

l = &Listener{
name: name,
location: location,
rewriteHost: rewriteHost,
userName: userName,
passWord: passWord,
Expand Down Expand Up @@ -153,6 +154,7 @@ func (v *VhostMuxer) handle(c *conn.Conn) {

type Listener struct {
name string
location string
rewriteHost string
userName string
passWord string
Expand Down Expand Up @@ -180,7 +182,7 @@ func (l *Listener) Accept() (*conn.Conn, error) {
}

func (l *Listener) Close() error {
l.mux.registryRouter.Del(l)
l.mux.registryRouter.Del(l.name, l.location)
close(l.accept)
return nil
}
Expand Down

0 comments on commit cdcc124

Please sign in to comment.