forked from SlyMarbo/spdy
/
client.go
50 lines (41 loc) · 1.29 KB
/
client.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
// 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 (
"errors"
"net"
"net/http"
"github.com/SlyMarbo/spdy/common"
"github.com/SlyMarbo/spdy/spdy2"
"github.com/SlyMarbo/spdy/spdy3"
)
// init modifies http.DefaultClient to use a spdy.Transport, enabling
// support for SPDY in functions like http.Get.
func init() {
http.DefaultClient = NewClient(false)
}
// NewClientConn is used to create a SPDY connection, using the given
// net.Conn for the underlying connection, and the given Receiver to
// receive server pushes.
func NewClientConn(conn net.Conn, push common.Receiver, version, subversion int) (common.Conn, error) {
if conn == nil {
return nil, errors.New("Error: Connection initialised with nil net.conn.")
}
switch version {
case 3:
out := spdy3.NewConn(conn, nil, subversion)
out.PushReceiver = push
return out, nil
case 2:
out := spdy2.NewConn(conn, nil)
out.PushReceiver = push
return out, nil
default:
return nil, errors.New("Error: Unrecognised SPDY version.")
}
}
// NewClient creates an http.Client that supports SPDY.
func NewClient(insecureSkipVerify bool) *http.Client {
return &http.Client{Transport: NewTransport(insecureSkipVerify)}
}