This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
host.go
61 lines (46 loc) · 1.17 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package httptest
import (
"io"
"net/http"
"strings"
)
// ---------------------------------------------------------------------------
type hostsMgr struct {
hostToPortals map[string]string
}
func (p *hostsMgr) initHostsMgr() {
p.hostToPortals = make(map[string]string)
}
func (p *hostsMgr) hostOf(url string) (host string, url2 string, ok bool) {
var istart int
// http://host/xxx or https://host/xxx
if strings.HasPrefix(url[4:], "://") {
istart = 7
} else if strings.HasPrefix(url[4:], "s://") {
istart = 8
} else {
return
}
n := strings.IndexByte(url[istart:], '/')
if n < 1 {
return
}
host = url[istart : istart+n]
portal, ok := p.hostToPortals[host]
if ok {
url2 = url[:istart] + portal + url[istart+n:]
}
return
}
func (p *hostsMgr) newRequest(method, url string, body io.Reader) (req *http.Request, err error) {
if host, url2, ok := p.hostOf(url); ok {
req, err = http.NewRequest(method, url2, body)
req.Host = host
return
}
return http.NewRequest(method, url, body)
}
func (p *hostsMgr) SetHost(host string, portal string) {
p.hostToPortals[host] = portal
}
// ---------------------------------------------------------------------------