This repository has been archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
connection.go
870 lines (710 loc) · 19.1 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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
package spdy
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"net/url"
"runtime/debug"
"strconv"
"strings"
)
var Log = func(fmt string, args ...interface{}) {}
// data in connections are only accessible on the connection dispatch thread
type Connection struct {
// general connection info
socket net.Conn
version int
handler http.Handler
remoteAddr net.Addr
tls *tls.ConnectionState
rxWindow int
// tx thread channels
sendControl chan frame
sendWindowUpdate chan frame
sendData [maxPriorities]chan frame
dataSent chan error
// dispatch thread channels
onStartRequest chan *stream // do not use directly, use startRequest instead
onRequestStarted chan error
// For requests this happens when response.Body.Close is called. For
// replies this happens when the handler function returns.
onStreamFinished chan *stream
// stream info
streams map[int]*stream
lastStreamOpened int
nextStreamId int
goAway bool
onGoAway chan bool
nextPingId uint32
}
// nextTxFrame gets the next frame to be written to the socket in prioritized
// order. If it has to block it will flush the output buffer first.
func (c *Connection) nextTxFrame(buf *bufio.Writer) (frame, chan error) {
// try a non-blocking receive in priority order
select {
case f := <-c.sendControl:
return f, nil
case f := <-c.sendWindowUpdate:
return f, nil
default:
}
for _, ch := range c.sendData {
select {
case f := <-ch:
return f, c.dataSent
default:
}
}
buf.Flush()
// do a blocking receive on all the send channels
select {
case f := <-c.sendControl:
return f, nil
case f := <-c.sendWindowUpdate:
return f, nil
case f := <-c.sendData[0]:
return f, c.dataSent
case f := <-c.sendData[1]:
return f, c.dataSent
case f := <-c.sendData[2]:
return f, c.dataSent
case f := <-c.sendData[3]:
return f, c.dataSent
case f := <-c.sendData[4]:
return f, c.dataSent
case f := <-c.sendData[5]:
return f, c.dataSent
case f := <-c.sendData[6]:
return f, c.dataSent
case f := <-c.sendData[7]:
return f, c.dataSent
}
panic("unreachable")
}
// txPump runs the connection transmit loop which receives frames from the
// session tx threads and writes them out to the underlying socket. The frames
// are prioritized by receiving from a number of send channels which are
// polled from highest priority to lowest before blocking on them all.
func (c *Connection) txPump() {
buf := bufio.NewWriter(c.socket)
zip := compressor{}
for {
f, finish := c.nextTxFrame(buf)
if f == nil {
break
}
err := f.WriteFrame(c.socket, &zip)
if finish != nil {
finish <- err
}
}
}
// rxPump runs the connection receive loop for both client and server
// connections. It finds the message boundaries and sends each one over to the
// connection thread.
func (c *Connection) rxPump(dispatch chan []byte, dispatched chan error, rxError chan error) {
buf := new(buffer)
for {
d, err := buf.Get(c.socket, 8)
if err != nil {
rxError <- err
return
}
length := int(fromBig32(d[4:])&0xFFFFFF) + 8
d, err = buf.Get(c.socket, length)
// If the buffer overflows we will not get an error instead
// len(d) < 8 + length. We try and continue anyways, and the
// disptach thread can decide whether we need to throw a
// session error and disconnect or just send a stream error.
if err != nil {
rxError <- err
return
}
dispatch <- d
err = <-dispatched
if err != nil {
rxError <- err
return
}
buf.Flush(len(d))
length -= len(d)
// If we couldn't buffer all of the message above, consume the
// rest of the data in the message and dump the data on the
// floor.
for length > 0 {
d, err := buf.Get(c.socket, length)
if err != nil {
rxError <- err
return
}
buf.Flush(len(d))
length -= len(d)
}
}
}
// run runs the main connection thread which is responsible for dispatching
// messages to the streams and managing the list of streams.
func (c *Connection) Run() {
unzip := decompressor{}
if t, ok := c.socket.(*tls.Conn); ok {
if err := t.Handshake(); err != nil {
return
}
c.tls = new(tls.ConnectionState)
*c.tls = t.ConnectionState()
}
dispatch := make(chan []byte)
dispatched := make(chan error)
rxError := make(chan error)
go c.txPump()
go c.rxPump(dispatch, dispatched, rxError)
for {
select {
case s := <-c.onStartRequest:
err := c.handleStartRequest(s)
c.onRequestStarted <- err
case s := <-c.onStreamFinished:
// Handle the race where we sent/received a reset
// before we handled this message.
if c.streams[s.streamId] != s {
break
}
if !s.isRecipient && !s.rxFinished {
c.sendReset(s.streamId, rstCancel)
}
c.finishStream(s, errCancel(s.streamId))
case d := <-dispatch:
err := c.handleFrame(d, &unzip)
if err == nil {
dispatched <- nil
break
}
serr, ok := err.(streamError)
// Session error, we are going to abort the
// connection. Send the error to the rx thread, it
// will then send it back in rxError.
if !ok {
dispatched <- err
break
}
// Stream error, abort the stream
sid := serr.StreamId()
c.sendReset(sid, serr.resetCode())
if s := c.streams[sid]; s != nil {
c.finishStream(s, err)
}
dispatched <- nil
case err := <-rxError:
// Session error, have to abort the whole connection
c.goAway = true
close(c.onGoAway)
for _, s := range c.streams {
c.finishStream(s, err)
}
// close the control channel to ensure that the tx
// thread shuts down
close(c.sendControl)
c.socket.Close()
return
}
}
}
/* finishStream removes a completed stream.
*
* It then shuts down the stream setting txError and rxError so the stream
* rx/tx threads can see the error (if they are still running).
*
* It also removes the stream from the stream list so any further frames
* concerning this stream force a rstInvalidStream.
*
* Finally it recursively shuts down associated streams.
*
* Close conditions:
* 1. Error sent
* 2. Error received
*
* 3. If requestor, when we close the response Body (rx closed). If the
* request hasn't finished then we send a cancel RST_STREAM.
*
* 4. If recipient, when we finish the response. This is on the completion of
* the handler callback. The request may not have been completely read. In
* this case we do not send a RST_STREAM as the request may have been serviced
* without reading the full request (eg if we errored with a HTTP error status
* code - in this case the stream succeeded).
*/
func (c *Connection) finishStream(s *stream, err error) {
// We use rxError != nil, etc to figure out if we have finished
if err == nil {
panic("")
}
delete(c.streams, s.streamId)
// Disconnect child streams
for _, a := range s.children {
// Reset the parent pointer so the child doesn't try and
// remove itself from the parent
a.parent = nil
c.finishStream(a, err)
}
s.rxLock.Lock()
s.rxError = err
s.rxCond.Broadcast()
s.rxLock.Unlock()
s.txLock.Lock()
s.txError = err
s.txCond.Broadcast()
s.txLock.Unlock()
close(s.errorChannel)
// Remove ourself from our parent
if s.parent != nil {
p := s.parent
for i, s2 := range p.children {
if s2 == s {
p.children = append(p.children[:i], p.children[i+1:]...)
break
}
}
}
if c.goAway && len(c.streams) == 0 {
c.socket.Close()
}
}
func (c *Connection) sendReset(streamId int, reason int) {
c.sendControl <- &rstStreamFrame{
Version: c.version,
StreamId: streamId,
Reason: reason,
}
}
/* handleStartRequest sends the SYN_STREAM and registers streams where we are
* the initiator */
func (c *Connection) handleStartRequest(s *stream) error {
s.streamId = c.nextStreamId
c.nextStreamId += 2
assocId := 0
if s.parent != nil {
assocId = s.parent.streamId
}
if uint(s.streamId) > maxStreamId || c.goAway {
return errGoAway
}
u := s.request.URL
// Fixup the url if we need to set the scheme and prefer the host in
// the request
if u.Scheme == "" || u.Host != s.request.Host {
u = new(url.URL)
*u = *s.request.URL
u.Host = s.request.Host
if u.Scheme == "" {
u.Scheme = "https"
}
}
// note we always use the control channel to ensure that the
// SYN_STREAM packets are sent out in the order in which the stream
// ids were allocated
c.sendControl <- &synStreamFrame{
Version: c.version,
StreamId: s.streamId,
AssociatedStreamId: assocId,
Finished: s.txFinished,
Unidirectional: s.rxFinished,
Header: s.request.Header,
Priority: s.txPriority,
URL: u,
Proto: s.request.Proto,
Method: s.request.Method,
}
// unidirectional and immediate finish messages never
// get added to the streams table and will shortly be gc'd
if s.txFinished && s.rxFinished {
return nil
}
c.streams[s.streamId] = s
if s.parent != nil {
s.parent.children = append(s.parent.children, s)
}
return nil
}
func handlerFinish(s *streamTx) {
if err := recover(); err != nil {
var buf bytes.Buffer
fmt.Fprintf(&buf, "spdy: panic serving %d: %v\n", s.streamId, err)
buf.Write(debug.Stack())
log.Print(buf.String())
}
s.close()
s.connection.onStreamFinished <- (*stream)(s)
}
func handlerThread(h http.Handler, s *streamTx, req *http.Request) {
defer handlerFinish(s)
h.ServeHTTP(s, req)
}
func (c *Connection) handleSynStream(d []byte, unzip *decompressor) error {
f, err := parseSynStream(d, unzip)
if err != nil {
return err
}
Log("rx SYN_STREAM %+v\n", f)
// The remote has reopened an already opened stream. We kill both.
// Check this first as if any other check fails and this would've also
// failed sending out the reset will invalidate the existing stream.
if s2 := c.streams[f.StreamId]; s2 != nil {
return errStreamInUse(f.StreamId)
}
if f.Version != c.version {
return errStreamVersion{f.StreamId, f.Version}
}
// The remote tried to open a stream of the wrong type (eg its a
// client and tried to open a server stream).
if (f.StreamId & 1) == (c.nextStreamId & 1) {
return errStreamProtocol(f.StreamId)
}
// Stream Ids must monotonically increase
if f.StreamId <= c.lastStreamOpened {
return errStreamProtocol(f.StreamId)
}
c.lastStreamOpened = f.StreamId
// The handler is either the connection global one or the associated
// stream one.
handler := c.handler
var parent *stream
if f.AssociatedStreamId > 0 {
// You are only allowed to open associated streams to streams
// that you are the recipient.
if (f.AssociatedStreamId & 1) != (c.nextStreamId & 1) {
return errStreamProtocol(f.StreamId)
}
parent = c.streams[f.AssociatedStreamId]
// The remote tried to open a stream associated with a closed
// stream. We kill this new stream.
if parent == nil {
return errInvalidAssociatedStream{f.StreamId, f.AssociatedStreamId}
}
handler = parent.childHandler
}
if handler == nil {
return errRefusedStream(f.StreamId)
}
// The SYN_STREAM passed all of our tests, so go ahead and create the
// stream, hook it up and start a request handler thread.
r := &http.Request{
Method: f.Method,
URL: f.URL,
Proto: f.Proto,
ProtoMajor: f.ProtoMajor,
ProtoMinor: f.ProtoMinor,
Header: f.Header,
Host: f.URL.Host,
RemoteAddr: c.remoteAddr.String(),
RequestURI: f.URL.Path,
TLS: c.tls,
}
if cl, err := strconv.ParseInt(f.Header.Get("Content-Length"), 10, 64); err != nil {
r.ContentLength = cl
}
extra := &RequestExtra{
Unidirectional: f.Finished,
Priority: f.Priority,
AssociatedHandler: nil,
}
s := c.newStream(r, f.Unidirectional, extra)
s.streamId = f.StreamId
s.isRecipient = true
s.request.Body = (*streamRx)(s)
// Messages that have both their rx and tx pipes already closed don't
// need to be added to the streams table.
if !(s.txFinished && s.rxFinished) {
c.streams[f.StreamId] = s
if parent != nil {
parent.children = append(parent.children, s)
s.parent = parent
}
}
go handlerThread(handler, (*streamTx)(s), r)
return nil
}
func (c *Connection) handleSynReply(d []byte, unzip *decompressor) error {
f, err := parseSynReply(d, unzip)
if err != nil {
return err
}
Log("rx SYN_REPLY %+v\n", f)
s := c.streams[f.StreamId]
if s == nil {
return errInvalidStream(f.StreamId)
}
if f.Version != c.version {
return errStreamVersion{f.StreamId, f.Version}
}
if s.rxResponse != nil {
return errStreamInUse(f.StreamId)
}
if s.rxFinished {
return errStreamAlreadyClosed(f.StreamId)
}
r := &http.Response{
Status: f.Status,
Proto: f.Proto,
ProtoMajor: f.ProtoMajor,
ProtoMinor: f.ProtoMinor,
Header: f.Header,
Body: (*streamRx)(s),
Request: s.request,
}
split := strings.SplitN(f.Status, " ", 2)
if r.StatusCode, err = strconv.Atoi(split[0]); err != nil {
return errStreamProtocol(f.StreamId)
}
if cl, err := strconv.ParseInt(f.Header.Get("Content-Length"), 10, 64); err == nil {
r.ContentLength = cl
}
s.rxLock.Lock()
s.rxResponse = r
s.rxFinished = f.Finished
s.rxCond.Broadcast()
s.rxLock.Unlock()
return nil
}
func (c *Connection) handleHeaders(d []byte, unzip *decompressor) error {
f, err := parseHeaders(d, unzip)
if err != nil {
return err
}
Log("rx HEADERS %+v\n", f)
s := c.streams[f.StreamId]
if s == nil {
return errInvalidStream(f.StreamId)
}
if f.Version != c.version {
return errStreamVersion{f.StreamId, f.Version}
}
if s.rxFinished {
return errStreamAlreadyClosed(f.StreamId)
}
if f.Finished {
s.rxLock.Lock()
s.rxFinished = f.Finished
s.rxLock.Unlock()
}
return nil
}
func (c *Connection) handleRstStream(d []byte) error {
f, err := parseRstStream(d)
if err != nil {
return err
}
Log("rx RST_STREAM %+v\n", f)
s := c.streams[f.StreamId]
if s == nil {
// ignore resets for closed streams
return nil
}
err = errStreamProtocol(f.StreamId)
switch f.Reason {
case rstInvalidStream:
err = errInvalidStream(f.StreamId)
case rstRefusedStream:
err = errRefusedStream(f.StreamId)
case rstUnsupportedVersion:
err = errStreamVersion{f.StreamId, c.version}
case rstCancel:
err = errCancel(f.StreamId)
case rstFlowControlError:
err = errStreamFlowControl(f.StreamId)
case rstStreamInUse:
err = errStreamInUse(f.StreamId)
case rstStreamAlreadyClosed:
err = errStreamAlreadyClosed(f.StreamId)
}
// Don't return an error and handle the error locally since we don't
// want to send a RST_STREAM
c.finishStream(s, err)
return nil
}
func (c *Connection) handleSettings(d []byte) error {
f, err := parseSettings(d)
if err != nil {
return err
}
Log("rx SETTINGS %+v\n", f)
if f.Version != c.version {
return errSessionVersion(f.Version)
}
if !f.HaveWindow {
return nil
}
change := f.Window - c.rxWindow
c.rxWindow = f.Window
for _, s := range c.streams {
s.txLock.Lock()
s.txWindow += change
s.txCond.Broadcast()
s.txLock.Unlock()
}
return nil
}
func (c *Connection) handleWindowUpdate(d []byte) error {
f, err := parseWindowUpdate(d)
if err != nil {
return err
}
Log("rx WINDOW_UPDATE %+v\n", f)
s := c.streams[f.StreamId]
if s == nil {
return errInvalidStream(f.StreamId)
}
if f.Version != c.version {
return errStreamVersion{f.StreamId, f.Version}
}
s.txLock.Lock()
s.txWindow += f.WindowDelta
s.txCond.Broadcast()
s.txLock.Unlock()
return nil
}
func (c *Connection) handlePing(d []byte) error {
f, err := parsePing(d)
if err != nil {
return err
}
Log("rx PING %+v\n", f)
if f.Version != c.version {
return errSessionVersion(f.Version)
}
// Ignore loopback pings
if (f.Id & 1) != (c.nextPingId & 1) {
c.sendControl <- &pingFrame{
Version: c.version,
Id: f.Id,
}
}
return nil
}
func (c *Connection) handleGoAway(d []byte) error {
f, err := parseGoAway(d)
if err != nil {
return err
}
Log("rx GO_AWAY %+v\n", f)
if f.Version != c.version {
return errSessionVersion(f.Version)
}
// This is so we don't start any streams after this point, and
// finishStream will detect once we've finished all the active streams
// and shut down the socket.
c.goAway = true
close(c.onGoAway)
for id, s := range c.streams {
err := errSessionProtocol
switch f.Reason {
case rstSuccess:
err = errGoAway
case rstUnsupportedVersion:
err = errSessionVersion(c.version)
case rstFlowControlError:
err = errSessionFlowControl
}
// Reset all streams that we started which are after the last
// accepted stream
if id > f.LastStreamId && (id&1) == (c.nextStreamId&1) {
c.finishStream(s, err)
}
}
return nil
}
func (c *Connection) handleData(d []byte) error {
f, err := parseData(d)
if err != nil {
return err
}
Log("rx DATA &{StreamId:%d Finished:%v Data:len %d}\n", f.StreamId, f.Finished, len(f.Data))
s := c.streams[f.StreamId]
if s == nil {
return errInvalidStream(f.StreamId)
}
if s.rxFinished {
return errStreamAlreadyClosed(f.StreamId)
}
// The rx pump thread could not give us the entire message due to it
// being too large.
if length := int(fromBig32(d[4:]) & 0xFFFFFF); length != len(f.Data) {
return errStreamFlowControl(f.StreamId)
}
s.rxLock.Lock()
s.rxBuffer.Write(f.Data)
s.rxFinished = f.Finished
s.rxCond.Broadcast()
s.rxLock.Unlock()
return nil
}
func (c *Connection) handleFrame(d []byte, unzip *decompressor) error {
code := fromBig32(d[0:])
if code&0x80000000 == 0 {
return c.handleData(d)
}
if length := int(fromBig32(d[4:]) & 0xFFFFFF); length+8 != len(d) {
return errSessionFlowControl
}
switch code & 0x8000FFFF {
case synStreamCode:
return c.handleSynStream(d, unzip)
case synReplyCode:
return c.handleSynReply(d, unzip)
case rstStreamCode:
return c.handleRstStream(d)
case settingsCode:
return c.handleSettings(d)
case pingCode:
return c.handlePing(d)
case windowUpdateCode:
return c.handleWindowUpdate(d)
case headersCode:
return c.handleHeaders(d, unzip)
case goAwayCode:
return c.handleGoAway(d)
}
// Messages with unknown type are ignored.
return nil
}
// NewConnection creates a SPDY client or server connection around sock.
//
// sock should be the underlying socket already connected. Typically this is a
// TLS connection which has already gone through next protocol negotiation,
// but any socket will work.
//
// Handler is used to provide the callback for any content pushed from the
// server. If it is nil then pushed streams are refused.
//
// The connection won't be started until you run Connection.Run()
func NewConnection(sock net.Conn, handler http.Handler, version int, server bool) *Connection {
c := &Connection{
socket: sock,
version: version,
handler: handler,
remoteAddr: sock.RemoteAddr(),
rxWindow: defaultWindow,
sendControl: make(chan frame, 100),
sendWindowUpdate: make(chan frame, 100),
dataSent: make(chan error),
onStartRequest: make(chan *stream),
onRequestStarted: make(chan error),
onStreamFinished: make(chan *stream),
streams: make(map[int]*stream),
lastStreamOpened: 0,
onGoAway: make(chan bool),
}
for i := 0; i < len(c.sendData); i++ {
c.sendData[i] = make(chan frame)
}
if server {
c.nextStreamId = 2
c.nextPingId = 0
} else {
c.nextStreamId = 1
c.nextPingId = 1
}
return c
}