forked from SlyMarbo/spdy
/
transport.go
371 lines (322 loc) · 9.61 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// 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 (
"bytes"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
// 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]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) Priority
// Receiver is used to receive the server's response. If left
// nil, the default Receiver will parse and create a normal
// Response.
Receiver 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 Receiver
}
// dial makes the connection to an endpoint.
func (t *Transport) dial(u *url.URL) (net.Conn, 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":
return net.Dial("tcp", u.Host)
case "https":
return tls.Dial("tcp", u.Host, t.TLSClientConfig)
default:
return nil, errors.New(fmt.Sprintf("Error: URL has invalid scheme %q.", u.Scheme))
}
}
// 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"
}
}
t.m.Lock()
// Initialise structures if necessary.
if t.spdyConns == nil {
t.spdyConns = make(map[string]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:
t.m.Unlock()
// Use a connection from the pool.
return t.doHTTP(tcpConn, req)
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" {
tcpConn, err := t.dial(req.URL)
if err != nil {
t.m.Unlock()
return nil, err
}
// Handle HTTPS/SPDY requests.
if tlsConn, ok := tcpConn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
// Complete handshake if necessary.
if !state.HandshakeComplete {
err = tlsConn.Handshake()
if err != nil {
t.m.Unlock()
return 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 {
t.m.Unlock()
return nil, err
}
}
}
// If a protocol could not be negotiated, assume HTTPS.
if !state.NegotiatedProtocolIsMutual {
t.m.Unlock()
return t.doHTTP(tcpConn, req)
}
// 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 {
msg := fmt.Sprintf("Error: Unsupported negotiated protocol %q.", state.NegotiatedProtocol)
t.m.Unlock()
return nil, errors.New(msg)
}
// Handle the protocol.
switch state.NegotiatedProtocol {
case "http/1.1", "":
t.m.Unlock()
return t.doHTTP(tcpConn, req)
case "spdy/3.1":
newConn, err := NewClientConn(tlsConn, t.PushReceiver, 3.1)
if err != nil {
return nil, err
}
go newConn.Run()
t.spdyConns[u.Host] = newConn
conn = newConn
case "spdy/3":
newConn, err := NewClientConn(tlsConn, t.PushReceiver, 3)
if err != nil {
return nil, err
}
go newConn.Run()
t.spdyConns[u.Host] = newConn
conn = newConn
case "spdy/2":
newConn, err := NewClientConn(tlsConn, t.PushReceiver, 2)
if err != nil {
return nil, err
}
go newConn.Run()
t.spdyConns[u.Host] = newConn
conn = newConn
}
} else {
// Handle HTTP requests.
t.m.Unlock()
return t.doHTTP(tcpConn, req)
}
}
t.m.Unlock()
// The connection has now been established.
debug.Printf("Requesting %q over SPDY.\n", u.String())
// Determine the request priority.
priority := Priority(0)
if t.Priority != nil {
priority = t.Priority(req.URL)
} else {
priority = DefaultPriority(req.URL)
}
return conn.RequestResponse(req, t.Receiver, priority)
}
// response is used in handling responses; storing
// the data as it's received, and producing an
// http.Response once complete.
//
// response may be given a Receiver to enable live
// handling of the response data. This is provided
// by setting spdy.Transport.Receiver.
type response struct {
StatusCode int
Header http.Header
Data *bytes.Buffer
Request *http.Request
Receiver Receiver
}
func (r *response) ReceiveData(req *http.Request, data []byte, finished bool) {
r.Data.Write(data)
if r.Receiver != nil {
r.Receiver.ReceiveData(req, data, finished)
}
}
var statusRegex = regexp.MustCompile(`\A\s*(?P<code>\d+)`)
func (r *response) ReceiveHeader(req *http.Request, header http.Header) {
if r.Header == nil {
r.Header = make(http.Header)
}
updateHeader(r.Header, header)
if status := r.Header.Get(":status"); status != "" && statusRegex.MatchString(status) {
if matches := statusRegex.FindAllStringSubmatch(status, -1); matches != nil {
s, err := strconv.Atoi(matches[0][1])
if err == nil {
r.StatusCode = s
}
}
}
if r.Receiver != nil {
r.Receiver.ReceiveHeader(req, header)
}
}
func (r *response) ReceiveRequest(req *http.Request) bool {
if r.Receiver != nil {
return r.Receiver.ReceiveRequest(req)
}
return false
}
func (r *response) Response() *http.Response {
if r.Data == nil {
r.Data = new(bytes.Buffer)
}
out := new(http.Response)
out.Status = fmt.Sprintf("%d %s", r.StatusCode, http.StatusText(r.StatusCode))
out.StatusCode = r.StatusCode
out.Proto = "HTTP/1.1"
out.ProtoMajor = 1
out.ProtoMinor = 1
out.Header = r.Header
out.Body = &readCloser{r.Data}
out.ContentLength = int64(r.Data.Len())
out.TransferEncoding = nil
out.Close = true
out.Trailer = make(http.Header)
out.Request = r.Request
return out
}