forked from SlyMarbo/spdy
/
transport.go
332 lines (281 loc) · 8.6 KB
/
transport.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2013 Jamie Hall. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package spdy
import (
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"sync"
"time"
"github.com/SlyMarbo/spdy/common"
)
// A Transport is an HTTP/SPDY http.RoundTripper.
type Transport struct {
m sync.Mutex
// Proxy specifies a function to return a proxy for a given
// Request. If the function returns a non-nil error, the
// request is aborted with the provided error.
// If Proxy is nil or returns a nil *URL, no proxy is used.
Proxy func(*http.Request) (*url.URL, error)
// Dial specifies the dial function for creating TCP
// connections.
// If Dial is nil, net.Dial is used.
Dial func(network, addr string) (net.Conn, error) // TODO: use
// TLSClientConfig specifies the TLS configuration to use with
// tls.Client. If nil, the default configuration is used.
TLSClientConfig *tls.Config
// DisableKeepAlives, if true, prevents re-use of TCP connections
// between different HTTP requests.
DisableKeepAlives bool
// DisableCompression, if true, prevents the Transport from
// requesting compression with an "Accept-Encoding: gzip"
// request header when the Request contains no existing
// Accept-Encoding value. If the Transport requests gzip on
// its own and gets a gzipped response, it's transparently
// decoded in the Response.Body. However, if the user
// explicitly requested gzip it is not automatically
// uncompressed.
DisableCompression bool
// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int
// ResponseHeaderTimeout, if non-zero, specifies the amount of
// time to wait for a server's response headers after fully
// writing the request (including its body, if any). This
// time does not include the time to read the response body.
ResponseHeaderTimeout time.Duration
spdyConns map[string]common.Conn // SPDY connections mapped to host:port.
tcpConns map[string]chan net.Conn // Non-SPDY connections mapped to host:port.
connLimit map[string]chan struct{} // Used to enforce the TCP conn limit.
// Priority is used to determine the request priority of SPDY
// requests. If nil, spdy.DefaultPriority is used.
Priority func(*url.URL) common.Priority
// Receiver is used to receive the server's response. If left
// nil, the default Receiver will parse and create a normal
// Response.
Receiver common.Receiver
// PushReceiver is used to receive server pushes. If left nil,
// pushes will be refused. The provided Request will be that
// sent with the server push. See Receiver for more detail on
// its methods.
PushReceiver common.Receiver
}
// NewTransport gives a simple initialised Transport.
func NewTransport(insecureSkipVerify bool) *Transport {
return &Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecureSkipVerify,
NextProtos: npn(),
},
}
}
// dial makes the connection to an endpoint.
func (t *Transport) dial(u *url.URL) (conn net.Conn, err error) {
if t.TLSClientConfig == nil {
t.TLSClientConfig = &tls.Config{
NextProtos: npn(),
}
} else if t.TLSClientConfig.NextProtos == nil {
t.TLSClientConfig.NextProtos = npn()
}
// Wait for a connection slot to become available.
<-t.connLimit[u.Host]
switch u.Scheme {
case "http":
conn, err = net.Dial("tcp", u.Host)
case "https":
conn, err = tls.Dial("tcp", u.Host, t.TLSClientConfig)
default:
err = errors.New(fmt.Sprintf("Error: URL has invalid scheme %q.", u.Scheme))
}
if err != nil {
// The connection never happened, which frees up a slot.
t.connLimit[u.Host] <- struct{}{}
}
return conn, err
}
// doHTTP is used to process an HTTP(S) request, using the TCP connection pool.
func (t *Transport) doHTTP(conn net.Conn, req *http.Request) (*http.Response, error) {
debug.Printf("Requesting %q over HTTP.\n", req.URL.String())
// Create the HTTP ClientConn, which handles the
// HTTP details.
httpConn := httputil.NewClientConn(conn, nil)
res, err := httpConn.Do(req)
if err != nil {
return nil, err
}
if !res.Close {
t.tcpConns[req.URL.Host] <- conn
} else {
// This connection is closing, so another can be used.
t.connLimit[req.URL.Host] <- struct{}{}
err = httpConn.Close()
if err != nil {
return nil, err
}
}
return res, nil
}
// RoundTrip handles the actual request; ensuring a connection is
// made, determining which protocol to use, and performing the
// request.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
u := req.URL
// Make sure the URL host contains the port.
if !strings.Contains(u.Host, ":") {
switch u.Scheme {
case "http":
u.Host += ":80"
case "https":
u.Host += ":443"
}
}
conn, tcpConn, err := t.process(req)
if err != nil {
return nil, err
}
if tcpConn != nil {
return t.doHTTP(tcpConn, req)
}
// The connection has now been established.
debug.Printf("Requesting %q over SPDY.\n", u.String())
// Determine the request priority.
var priority common.Priority
if t.Priority != nil {
priority = t.Priority(req.URL)
} else {
priority = common.DefaultPriority(req.URL)
}
res, err := conn.RequestResponse(req, t.Receiver, priority)
if conn.Closed() {
t.connLimit[u.Host] <- struct{}{}
}
if err != nil {
return nil, err
}
return res, nil
}
func (t *Transport) process(req *http.Request) (common.Conn, net.Conn, error) {
t.m.Lock()
defer t.m.Unlock()
u := req.URL
// Initialise structures if necessary.
if t.spdyConns == nil {
t.spdyConns = make(map[string]common.Conn)
}
if t.tcpConns == nil {
t.tcpConns = make(map[string]chan net.Conn)
}
if t.connLimit == nil {
t.connLimit = make(map[string]chan struct{})
}
if t.MaxIdleConnsPerHost == 0 {
t.MaxIdleConnsPerHost = http.DefaultMaxIdleConnsPerHost
}
if _, ok := t.connLimit[u.Host]; !ok {
limitChan := make(chan struct{}, t.MaxIdleConnsPerHost)
t.connLimit[u.Host] = limitChan
for i := 0; i < t.MaxIdleConnsPerHost; i++ {
limitChan <- struct{}{}
}
}
// Check the non-SPDY connection pool.
if connChan, ok := t.tcpConns[u.Host]; ok {
select {
case tcpConn := <-connChan:
// Use a connection from the pool.
return nil, tcpConn, nil
default:
}
} else {
t.tcpConns[u.Host] = make(chan net.Conn, t.MaxIdleConnsPerHost)
}
// Check the SPDY connection pool.
conn, ok := t.spdyConns[u.Host]
if !ok || u.Scheme == "http" || (conn != nil && conn.Closed()) {
tcpConn, err := t.dial(req.URL)
if err != nil {
return nil, nil, err
}
if tlsConn, ok := tcpConn.(*tls.Conn); !ok {
// Handle HTTP requests.
return nil, tcpConn, nil
} else {
// Handle HTTPS/SPDY requests.
state := tlsConn.ConnectionState()
// Complete handshake if necessary.
if !state.HandshakeComplete {
err = tlsConn.Handshake()
if err != nil {
return nil, nil, err
}
}
// Verify hostname, unless requested not to.
if !t.TLSClientConfig.InsecureSkipVerify {
err = tlsConn.VerifyHostname(req.URL.Host)
if err != nil {
// Also try verifying the hostname with/without a port number.
i := strings.Index(req.URL.Host, ":")
err = tlsConn.VerifyHostname(req.URL.Host[:i])
if err != nil {
return nil, nil, err
}
}
}
// If a protocol could not be negotiated, assume HTTPS.
if !state.NegotiatedProtocolIsMutual {
return nil, tcpConn, nil
}
// Scan the list of supported NPN strings.
supported := false
for _, proto := range npn() {
if state.NegotiatedProtocol == proto {
supported = true
break
}
}
// Ensure the negotiated protocol is supported.
if !supported && state.NegotiatedProtocol != "" {
msg := fmt.Sprintf("Error: Unsupported negotiated protocol %q.", state.NegotiatedProtocol)
return nil, nil, errors.New(msg)
}
// Handle the protocol.
switch state.NegotiatedProtocol {
case "http/1.1", "":
return nil, tcpConn, nil
case "spdy/3.1":
newConn, err := NewClientConn(tlsConn, t.PushReceiver, 3, 1)
if err != nil {
return nil, nil, err
}
go newConn.Run()
t.spdyConns[u.Host] = newConn
conn = newConn
case "spdy/3":
newConn, err := NewClientConn(tlsConn, t.PushReceiver, 3, 0)
if err != nil {
return nil, nil, err
}
go newConn.Run()
t.spdyConns[u.Host] = newConn
conn = newConn
case "spdy/2":
newConn, err := NewClientConn(tlsConn, t.PushReceiver, 2, 0)
if err != nil {
return nil, nil, err
}
go newConn.Run()
t.spdyConns[u.Host] = newConn
conn = newConn
}
}
}
return conn, nil, nil
}