/
conn.go
57 lines (54 loc) · 1.16 KB
/
conn.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
package spdy
import (
framing "github.com/kr/spdy/spdyframing"
"io"
"net"
"net/http"
"sync"
)
// Conn represents a SPDY client connection.
// It implements http.RoundTripper for making HTTP requests.
type Conn struct {
Conn net.Conn
s *framing.Session
once sync.Once
}
// RoundTrip implements interface http.RoundTripper.
func (c *Conn) RoundTrip(r *http.Request) (*http.Response, error) {
c.once.Do(func() {
fr := framing.NewFramer(c.Conn, c.Conn)
c.s = framing.Start(fr, false, func(s *framing.Stream) {
// TODO(kr): Make each stream available
// to its associated request.
s.Reset(framing.RefusedStream)
})
})
reqHeader, flag, err := RequestFramingHeader(r)
body := r.Body
r.Body = nil
if err != nil {
return nil, err
}
st, err := c.s.Open(reqHeader, flag)
if err != nil {
return nil, err
}
if body != nil {
go func() {
// TODO(kr): handle errors
_, err := io.Copy(st, body)
if err != nil {
return
}
st.Close()
}()
}
h := st.Header() // waits for SYN_REPLY
resp, err := ReadResponse(h, nil, st, r)
if err != nil {
st.Reset(framing.ProtocolError)
return nil, err
}
resp.Request = r
return resp, nil
}