Skip to content

Commit 2ed87ea

Browse files
committed
feat(app): try match unknown hostname to first no hostname vhost
1 parent 6b27e7d commit 2ed87ea

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ server [options]
136136
,,
137137
To specify multiple virtual hosts with options, split these hosts' options by this sign.
138138
Options above can be specified for each virtual host.
139+
140+
If multiple virtual hosts share same IP and ports,
141+
use --hostname to identify them according to the request.
142+
If request host name does not match any virtual host,
143+
server will try to use first virtual host that has no hostname,
144+
otherwise use the first virtual host.
139145
```
140146

141147
## Examples

src/app/listen.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import (
88
)
99

1010
type ListenItem struct {
11-
proto string
12-
addr string
13-
useTLS bool
14-
hostnames []string
15-
certs []tls.Certificate
16-
handler http.Handler
17-
listener net.Listener
18-
server *http.Server
19-
vhosts []*vhost.VHost
11+
proto string
12+
addr string
13+
useTLS bool
14+
hostnames []string
15+
certs []tls.Certificate
16+
handler http.Handler
17+
listener net.Listener
18+
server *http.Server
19+
vhosts []*vhost.VHost
20+
defaultVHost *vhost.VHost
2021
}
2122

2223
type Listens []*ListenItem

src/app/main.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,28 @@ func NewApp(params []*param.Param) *App {
186186
}
187187
}
188188

189-
// handler for each listen item
189+
// init default vhost for each listen item
190190
for _, l := range app.listens {
191-
item := l
191+
for _, vh := range l.vhosts {
192+
if len(vh.Hostnames) == 0 {
193+
l.defaultVHost = vh
194+
break
195+
}
196+
}
197+
198+
if l.defaultVHost == nil {
199+
l.defaultVHost = l.vhosts[0]
200+
}
201+
}
192202

193-
if len(item.vhosts) == 1 {
194-
item.handler = item.vhosts[0].Mux
203+
// handler for each listen item
204+
for _, l := range app.listens {
205+
if len(l.vhosts) == 1 {
206+
l.handler = l.defaultVHost.Mux
195207
continue
196208
}
197209

198-
item.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
210+
l.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
199211
var serveVHost *vhost.VHost
200212

201213
hostname := r.Host
@@ -204,14 +216,15 @@ func NewApp(params []*param.Param) *App {
204216
hostname = hostname[:colonIndex]
205217
}
206218

207-
for _, vh := range item.vhosts {
219+
for _, vh := range l.vhosts {
208220
if vh.MatchHostname(hostname) {
209221
serveVHost = vh
210222
break
211223
}
212224
}
225+
213226
if serveVHost == nil {
214-
serveVHost = item.vhosts[0]
227+
serveVHost = l.defaultVHost
215228
}
216229

217230
serveVHost.Mux.ServeHTTP(w, r)

src/vhost/main.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@ func NewVHost(p *param.Param) *VHost {
6060
mux := serveMux.NewServeMux(p, logger, errHandler)
6161

6262
// hostnames
63-
hostnames := p.Hostnames
64-
for i, length := 0, len(hostnames); i < length; i++ {
65-
hostnames[i] = strings.ToLower(hostnames[i])
66-
}
67-
if len(hostnames) == 0 {
68-
hostnames = append(hostnames, "")
63+
hostnames := make([]string, 0, len(p.Hostnames))
64+
for _, hostname := range p.Hostnames {
65+
hostnames = append(hostnames, strings.ToLower(hostname))
6966
}
7067

7168
// determine can use TLS

0 commit comments

Comments
 (0)