-
Notifications
You must be signed in to change notification settings - Fork 1
/
connection.go
248 lines (221 loc) · 6.58 KB
/
connection.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
package minhq
import (
"bytes"
"errors"
"io"
"io/ioutil"
"github.com/ekr/minq"
"github.com/martinthomson/minhq/hc"
"github.com/martinthomson/minhq/mw"
)
// HTTPError is one of the QUIC/HTTP error codes defined.
type HTTPError uint16
// ErrHttp* are the standard defined error codes.
const (
ErrHttpStopping = HTTPError(0x0)
ErrHttpNoError = HTTPError(0x1)
ErrHttpPushRefused = HTTPError(0x2)
ErrHttpInternalError = HTTPError(0x3)
ErrHttpPushAlreadyInCache = HTTPError(0x4)
ErrHttpRequestCancelled = HTTPError(0x5)
ErrHttpDecompressionFailed = HTTPError(0x6)
ErrHttpUnknownStreamType = HTTPError(0xd)
)
func (e HTTPError) String() string {
switch e {
case ErrHttpStopping:
return "STOPPING"
case ErrHttpNoError:
return "NO_ERROR"
case ErrHttpPushRefused:
return "PUSH_REFUSED"
case ErrHttpInternalError:
return "INTERNAL_ERROR"
case ErrHttpPushAlreadyInCache:
return "PUSH_ALREADY_IN_CACHE"
case ErrHttpRequestCancelled:
return "REQUEST_CANCELLED"
case ErrHttpDecompressionFailed:
return "HTTP_HPACK_DECOMPRESSION_FAILED"
case ErrHttpUnknownStreamType:
return "HTTP_UNKNOWN_STREAM_TYPE"
default:
return "Too lazy to do this right now"
}
}
type unidirectionalStreamType byte
const (
unidirectionalStreamControl = unidirectionalStreamType(0x43)
unidirectionalStreamPush = unidirectionalStreamType(0x50)
unidirectionalStreamQpackEncoder = unidirectionalStreamType(0x48)
unidirectionalStreamQpackDecoder = unidirectionalStreamType(0x68)
)
func (ut unidirectionalStreamType) String() string {
switch ut {
case unidirectionalStreamControl:
return "Control"
case unidirectionalStreamPush:
return "Push"
case unidirectionalStreamQpackEncoder:
return "QPACK Encoder"
case unidirectionalStreamQpackDecoder:
return "QPACK Decoder"
}
return "Unknown"
}
// These errors are commonly reported error codes.
var (
ErrWtf = HTTPError(3)
ErrQuicWtf = minq.ErrorCode(0xa) // TODO use app error code
ErrExtraData = errors.New("Extra data at the end of a frame")
ErrNonZeroFlags = errors.New("Frame flags were non-zero")
ErrInvalidFrame = errors.New("Invalid frame type for context")
)
// Config contains connection-level configuration options, such as the intended
// capacity of the header table.
type Config struct {
DecoderTableCapacity hc.TableCapacity
ConcurrentDecoders uint16
MaxConcurrentPushes uint64
// TrackConnections determines whether a server creates a channel for new connections.
// If true, new connections will be written to the Server.Connections channel.
TrackConnections bool
// InformationalResponses causes channels to be created on client requests and push
// promises that emit informational responses. Setting this to false causes
// informational responses to be discarded.
InformationalResponses bool
}
// connectionHandler is used by subclasses of connection to deal with frames that only they handle.
type connectionHandler interface {
HandleFrame(FrameType, FrameReader) error
HandleUnidirectionalStream(unidirectionalStreamType, *recvStream) error
}
// connection is an abstract wrapper around mw.Connection (a wrapper around
// minq.Connection in turn).
type connection struct {
config *Config
mw.Connection
decoder *hc.QpackDecoder
encoder *hc.QpackEncoder
controlStream *sendStream
// ready is closed when the connection is truly ready to send
// requests or responses. Read from it before sending anything that
// depends on settings.
ready chan struct{}
}
// connect ensures that the connection is ready to go. It spawns a few goroutines
// to handle the control streams.
func (c *connection) connect(handler connectionHandler) error {
<-c.Connected
c.controlStream = newSendStream(c.CreateSendStream())
err := c.sendSettings()
if err != nil {
return err
}
encoderStream := c.CreateSendStream()
_, err = encoderStream.Write([]byte{byte(unidirectionalStreamQpackEncoder)})
if err != nil {
return err
}
c.encoder = hc.NewQpackEncoder(encoderStream, 0, 0)
decoderStream := c.CreateSendStream()
_, err = decoderStream.Write([]byte{byte(unidirectionalStreamQpackDecoder)})
if err != nil {
return err
}
c.decoder = hc.NewQpackDecoder(decoderStream, c.config.DecoderTableCapacity)
// Asynchronously wait for incoming streams and then spawn handlers for each.
// ready is used to signal that we have received settings from the other side.
go c.serviceUnidirectionalStreams(handler, c.ready)
return nil
}
// FatalError is a helper that passes on HTTP errors to the underlying connection.
func (c *connection) FatalError(e HTTPError) error {
return c.Error(uint16(e), "")
}
func (c *connection) handlePriority(r io.Reader) error {
// TODO implement something useful
_, err := io.Copy(ioutil.Discard, r)
if err != nil {
c.FatalError(ErrWtf)
return err
}
return nil
}
func (c *connection) sendSettings() error {
err := c.controlStream.WriteByte(byte(unidirectionalStreamControl))
if err != nil {
return err
}
sw := settingsWriter{c.config}
var buf bytes.Buffer
n, err := sw.WriteTo(&buf)
if err != nil {
return err
}
if n != int64(buf.Len()) {
return ErrStreamBlocked
}
_, err = c.controlStream.WriteFrame(frameSettings, buf.Bytes())
return err
}
// This spits out a SETTINGS frame and then sits there reading the control
// stream until it encounters an error.
func (c *connection) serviceControlStream(controlStream *recvStream,
handler connectionHandler, ready chan<- struct{}) error {
t, r, err := controlStream.ReadFrame()
if err != nil {
return err
}
if t != frameSettings {
return err
}
sr := settingsReader{c}
err = sr.readSettings(r)
if err != nil {
return err
}
close(ready)
for {
t, r, err = controlStream.ReadFrame()
if err != nil {
return err
}
switch t {
case framePriority:
err = c.handlePriority(r)
default:
err = handler.HandleFrame(t, r)
}
if err != nil {
return err
}
}
}
func (c *connection) serviceUnidirectionalStreams(handler connectionHandler,
ready chan<- struct{}) {
for s := range c.Connection.RemoteRecvStreams {
go func(s *recvStream) {
b, err := s.ReadByte()
if err != nil {
c.FatalError(ErrWtf)
return
}
t := unidirectionalStreamType(b)
switch t {
case unidirectionalStreamControl:
c.serviceControlStream(s, handler, ready)
case unidirectionalStreamQpackDecoder:
err = c.encoder.ServiceAcknowledgments(s)
case unidirectionalStreamQpackEncoder:
err = c.decoder.ReadTableUpdates(s)
c.decoder.Close()
default:
err = handler.HandleUnidirectionalStream(t, s)
}
if err != nil {
c.FatalError(ErrWtf)
}
}(newRecvStream(s))
}
}