Skip to content

Commit e98a8fe

Browse files
committed
feat: upgrade goVirtualHost to support prefix hostname match
1 parent 09857c3 commit e98a8fe

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,13 @@ server [options]
256256
If request host name does not match any virtual host,
257257
server will try to use first virtual host that has no hostname,
258258
otherwise use the first virtual host.
259+
260+
If hostname option starts with ".",
261+
treat it as a suffix,
262+
to match all levels of sub domains,
263+
e.g. ".example.com".
264+
265+
If hostname option ends with ".",
266+
treat it as a prefix,
267+
to match all levels of suffix domains.
259268
```

README.zh-CN.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,13 @@ server [选项]
253253
如果请求的主机名不匹配任何虚拟主机,
254254
服务器尝试使用第一个没有指定主机名的虚拟主机,
255255
如果失败则使用第一个虚拟主机。
256+
257+
如果主机名选项以“.”开头,
258+
则将其当作后缀,
259+
匹配所有层级的子域名,
260+
例如“.example.com”。
261+
262+
如果主机名以“.”结尾,
263+
则将其当作前缀,
264+
匹配所有域名后缀。
256265
```

src/goVirtualHost/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ func extractHostName(host string) string {
1414
maxIndex := hostLen - 1
1515
closeIndex := strings.IndexByte(host, ']')
1616
if closeIndex == maxIndex {
17-
return host
17+
return host[1:closeIndex]
1818
}
1919
if closeIndex > 1 && closeIndex < maxIndex && host[closeIndex+1] == ':' {
20-
return host[:closeIndex+1]
20+
return host[1:closeIndex]
2121
}
2222
}
2323

src/goVirtualHost/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ func TestExtractHostname(t *testing.T) {
1919

2020
host = "[fe80::1]"
2121
hostname = extractHostName(host)
22-
if hostname != "[fe80::1]" {
22+
if hostname != "fe80::1" {
2323
t.Error(hostname)
2424
}
2525

2626
host = "[fe80::1]:8080"
2727
hostname = extractHostName(host)
28-
if hostname != "[fe80::1]" {
28+
if hostname != "fe80::1" {
2929
t.Error(hostname)
3030
}
3131
}

src/goVirtualHost/vhost.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ func (v *vhost) matchHostName(name string) bool {
2222
if hostname == reqHostName {
2323
return true
2424
}
25-
if len(hostname) > 0 && hostname[0] == '.' && strings.HasSuffix(reqHostName, hostname) {
26-
return true
25+
if len(hostname) > 1 {
26+
if hostname[0] == '.' && strings.HasSuffix(reqHostName, hostname) {
27+
return true
28+
} else if hostname[len(hostname)-1] == '.' && strings.HasPrefix(reqHostName, hostname) {
29+
return true
30+
}
2731
}
2832
}
2933
return false

src/goVirtualHost/vhost_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package goVirtualHost
2+
3+
import "testing"
4+
5+
func TestMatchHostName(t *testing.T) {
6+
7+
var vh *vhost
8+
9+
vh = newVhost(nil, []string{"www.example.com"}, nil)
10+
if !vh.matchHostName("www.example.com") {
11+
t.Error()
12+
}
13+
if vh.matchHostName("example.com") {
14+
t.Error()
15+
}
16+
17+
vh = newVhost(nil, []string{".example.com"}, nil)
18+
if !vh.matchHostName("www.example.com") {
19+
t.Error()
20+
}
21+
if vh.matchHostName("example.com") {
22+
t.Error()
23+
}
24+
25+
vh = newVhost(nil, []string{".example.com", "example.com"}, nil)
26+
if !vh.matchHostName("www.example.com") {
27+
t.Error()
28+
}
29+
if !vh.matchHostName("example.com") {
30+
t.Error()
31+
}
32+
33+
vh = newVhost(nil, []string{"example."}, nil)
34+
if !vh.matchHostName("example.com") {
35+
t.Error()
36+
}
37+
if !vh.matchHostName("example.net") {
38+
t.Error()
39+
}
40+
}

0 commit comments

Comments
 (0)