Skip to content

Commit f6f04a1

Browse files
committed
refactor: extract func to getting hostname from host
1 parent 511de40 commit f6f04a1

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/app/main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package app
33
import (
44
"../param"
55
"../serverErrHandler"
6+
"../util"
67
"../vhost"
78
"crypto/tls"
89
"errors"
910
"net"
1011
"net/http"
1112
"os"
12-
"strings"
1313
"sync"
1414
)
1515

@@ -211,11 +211,7 @@ func NewApp(params []*param.Param) *App {
211211
l.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
212212
var serveVHost *vhost.VHost
213213

214-
hostname := r.Host
215-
colonIndex := strings.LastIndexByte(hostname, ':')
216-
if colonIndex >= 0 {
217-
hostname = hostname[:colonIndex]
218-
}
214+
hostname := util.ExtractHostname(r.Host)
219215

220216
for _, vh := range l.vhosts {
221217
if vh.MatchHostname(hostname) {

src/util/extractHostname.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package util
2+
3+
import "strings"
4+
5+
func ExtractHostname(host string) string {
6+
colonIndex := strings.LastIndexByte(host, ':')
7+
if colonIndex >= 0 {
8+
return host[:colonIndex]
9+
}
10+
return host
11+
}

src/util/extractHostname_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package util
2+
3+
import "testing"
4+
5+
func TestExtractHostname(t *testing.T) {
6+
var host, hostname string
7+
8+
host = "example.com"
9+
hostname = ExtractHostname(host)
10+
if hostname != "example.com" {
11+
t.Error(hostname)
12+
}
13+
14+
host = "example.com:8080"
15+
hostname = ExtractHostname(host)
16+
if hostname != "example.com" {
17+
t.Error(hostname)
18+
}
19+
}

0 commit comments

Comments
 (0)