-
Notifications
You must be signed in to change notification settings - Fork 4
/
transport.go
146 lines (117 loc) · 3.32 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
package http2
import (
"crypto/tls"
"fmt"
. "github.com/Jxck/color"
. "github.com/Jxck/http2/frame"
. "github.com/Jxck/logger"
"net/http"
"strconv"
)
// Transport implements http.RoundTriper
// with RoundTrip(request) response
type Transport struct {
Conn *Conn
CertPath string
KeyPath string
}
// connect tcp connection with host
func (transport *Transport) Connect(url *URL) (err error) {
address := url.Host + ":" + url.Port
// loading key pair
cert, err := tls.LoadX509KeyPair(transport.CertPath, transport.KeyPath)
if err != nil {
return err
}
// setting TLS config
config := tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
NextProtos: []string{VERSION},
}
conn, err := tls.Dial("tcp", address, &config)
if err != nil {
return err
}
// check connection state
state := conn.ConnectionState()
Info("%v %v", Yellow("handshake"), state.HandshakeComplete)
Info("%v %v", Yellow("protocol"), state.NegotiatedProtocol)
Conn := NewConn(conn)
// send Magic Octet
err = Conn.WriteMagic()
if err != nil {
return err
}
go Conn.WriteLoop()
// send default settings to id 0
settingsFrame := NewSettingsFrame(UNSET, 0, DefaultSettings)
Conn.WriteChan <- settingsFrame
transport.Conn = Conn
go Conn.ReadLoop()
return
}
// http.RoundTriper implementation
func (transport *Transport) RoundTrip(req *http.Request) (res *http.Response, err error) {
// add headers
req.Header.Add("accept", "*/*")
req.Header.Add("x-http2-version", VERSION)
if req.ContentLength != 0 {
req.Header.Add("content-length", fmt.Sprintf("%d", req.ContentLength))
}
Notice("\n%s", White(util.RequestString(req)))
url, err := NewURL(req.URL.String()) // err
if err != nil {
Error("%v", err)
return nil, err
}
req = util.UpgradeRequest(req, url)
// establish tcp connection and handshake
err = transport.Connect(url)
if err != nil {
Error("%v", err)
return nil, err
}
callback, response := TransportCallBack(req)
transport.Conn.CallBack = callback
// create stream
stream := transport.Conn.NewStream(<-NextClientStreamID)
transport.Conn.Streams[stream.ID] = stream
// send request header via HEADERS Frame
var flags Flag = END_STREAM + END_HEADERS
headerBlockFragment := stream.EncodeHeader(req.Header)
Trace("encoded header block %v", headerBlockFragment)
frame := NewHeadersFrame(flags, stream.ID, nil, headerBlockFragment, nil)
frame.Headers = req.Header
stream.Write(frame) // TODO: err
res = <-response
stream.Close()
Notice("\n%s", White(util.ResponseString(res)))
// TODO: send GOAWAY
// stream.Write(NewGoAwayFrame(0, stream.ID, NO_ERROR, nil))
return res, nil
}
func TransportCallBack(req *http.Request) (CallBack, chan *http.Response) {
response := make(chan *http.Response)
return func(stream *Stream) {
body := stream.Bucket.Body
headers := stream.Bucket.Headers
status, _ := strconv.Atoi(headers.Get(":status")) // err
headers.Del(":status")
res := &http.Response{
Status: fmt.Sprintf("%d %s", status, http.StatusText(status)),
StatusCode: status,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: headers,
Body: body,
ContentLength: int64(body.Len()),
// TransferEncoding []string
// Close bool
// Trailer Header
Request: req,
}
response <- res
}, response
}