-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttpProxy.go
166 lines (142 loc) · 3.02 KB
/
httpProxy.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package vproxy
import (
"io"
"net/http"
"strings"
)
var hopHeaders = []string{
//"Connection",
"Proxy-Connection",
//"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Te",
"Trailer",
"Transfer-Encoding",
//"Upgrade",
}
type proxyHTTP struct {
*Proxy
}
func (T *proxyHTTP) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
outreq := new(http.Request)
*outreq = *req
// 客户端关闭后,同时代理服务器的请求也取消。
if closeNotifier, ok := rw.(http.CloseNotifier); ok {
if requestCancel, ok := T.Proxy.Tr.(requestCanceler); ok {
reqDone := make(chan struct{})
defer close(reqDone)
clientGone := closeNotifier.CloseNotify()
outreq.Body = struct {
io.Reader
io.Closer
}{
Reader: &runOnFirstRead{
Reader: outreq.Body,
fn: func() {
go func() {
select {
case <-clientGone:
requestCancel.CancelRequest(outreq)
case <-reqDone:
}
}()
},
},
Closer: outreq.Body,
}
}
}
// 处理请求
completionURL(outreq)
outreq.RequestURI = ""
outreq.Header = make(http.Header)
filterHeaders(req.Header)
copyHeaders(outreq.Header, req.Header)
resp, err := T.Proxy.Tr.RoundTrip(outreq)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
T.Proxy.logf(Error, err.Error())
// 502 服务器作为网关或者代理时,为了完成请求访问下一个服务器,但该服务器返回了非法的应答。
http.Error(rw, err.Error(), http.StatusBadGateway)
return
}
T.Proxy.logf(Response, "响应:\r\n%v", resp)
wh := rw.Header()
clearHeaders(wh)
filterHeaders(resp.Header)
copyHeaders(wh, resp.Header)
if len(resp.Trailer) > 0 {
var trailerKeys []string
for k := range resp.Trailer {
trailerKeys = append(trailerKeys, k)
}
rw.Header().Add("Trailer", strings.Join(trailerKeys, ", "))
}
rw.WriteHeader(resp.StatusCode)
if len(resp.Trailer) > 0 {
if fl, ok := rw.(http.Flusher); ok {
fl.Flush()
}
}
var bufSize int = defaultDataBufioSize
if T.Proxy.DataBufioSize != 0 {
bufSize = T.Proxy.DataBufioSize
}
copyDate(rw, resp.Body, bufSize)
}
type requestCanceler interface {
CancelRequest(*http.Request)
}
func completionURL(req *http.Request) {
//有两种方式:
//GET / HTTP/1.1
//Host:www.google.com
//
//GET http://www.google.com/ HTTP/1.1
//Host:www.google.com
//
if req.URL.Host == "" {
req.URL.Host = req.Host
}
if req.URL.Scheme == "" {
if req.TLS != nil {
req.URL.Scheme = "https"
} else {
req.URL.Scheme = "http"
}
}
}
func clearHeaders(headers http.Header) {
for key := range headers {
headers.Del(key)
}
}
func copyHeaders(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func filterHeaders(h http.Header) {
for _, hh := range hopHeaders {
h.Del(hh)
}
}
type runOnFirstRead struct {
io.Reader
fn func()
}
func (c *runOnFirstRead) Read(bs []byte) (int, error) {
if c.fn != nil {
c.fn()
c.fn = nil
}
if c.Reader == nil {
return 0, io.EOF
}
return c.Reader.Read(bs)
}