-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsession.go
279 lines (253 loc) · 6.84 KB
/
session.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
// Copyright 2011 The Go Authors. 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 implements the SPDY protocol which is described in
// draft-mbelshe-httpbis-spdy-00.
//
// http://tools.ietf.org/html/draft-mbelshe-httpbis-spdy-00
package spdy
import (
"errors"
"fmt"
"net/http"
)
/*
* type Session
*
* A high-level representation of a SPDY connection
* <<
* connection: A transport-level connection between two endpoints.
* session: A synonym for a connection.
* >>
* (http://tools.ietf.org/html/draft-mbelshe-httpbis-spdy-00#section-1.2)
*
*/
type Session struct {
Server bool // Are we the server? (necessary for stream ID numbering)
lastStreamIdOut uint32 // Last (and highest-numbered) stream ID we allocated
lastStreamIdIn uint32 // Last (and highest-numbered) stream ID we received
streams map[uint32]*Stream
handler http.Handler
closed bool
outputR *PipeReader
outputW *PipeWriter
}
func NewSession(handler http.Handler, server bool) *Session {
outputR, outputW := Pipe(4096)
session := &Session{
Server: server,
streams: make(map[uint32]*Stream),
handler: handler,
outputR: outputR,
outputW: outputW,
}
if session.handler == nil {
session.outputW.WriteFrame(&GoAwayFrame{})
}
return session
}
func (session *Session) Close() {
session.closed = true
for id := range session.streams {
session.CloseStream(id)
}
}
func (session *Session) Closed() bool {
return session.closed
}
/*
* Compute the ID which should be used to open the next stream
*
* Per http://tools.ietf.org/html/draft-mbelshe-httpbis-spdy-00#section-2.3.2
* <<
* If the server is initiating the stream,
* the Stream-ID must be even. If the client is initiating the stream,
* the Stream-ID must be odd. 0 is not a valid Stream-ID. Stream-IDs
* from each side of the connection must increase monotonically as new
* streams are created. E.g. Stream 2 may be created after stream 3,
* but stream 7 must not be created after stream 9. Stream IDs do not
* wrap: when a client or server cannot create a new stream id without
* exceeding a 31 bit value, it MUST NOT create a new stream.
* >>
*/
func (session *Session) nextIdOut() (uint32, error) {
if session.lastStreamIdOut == 0 {
if session.Server {
return 2, nil
} else {
return 1, nil
}
}
if session.lastStreamIdOut >= 0xffffffff - 1 {
return 0, errors.New("Can't allocate new streams: uint32 overflow")
}
return session.lastStreamIdOut + 2, nil
}
func (session *Session) nextIdIn() (uint32, error) {
if session.lastStreamIdIn == 0 {
if session.Server {
return 1, nil
} else {
return 2, nil
}
}
if session.lastStreamIdIn + 2 > 0xffffffff {
return 0, errors.New("Can't allocate new streams: uint32 overflow")
}
return session.lastStreamIdIn + 2, nil
}
/*
** InitiateStream() initiates a new local stream. It does not send SYN_STREAM or
** any other frame. That is the responsibility of the caller.
*/
func (session *Session) InitiateStream() (*Stream, error) {
newId, err := session.nextIdOut()
if err != nil {
return nil, err
}
if stream, err := session.newStream(newId, true); err != nil {
return nil, err
} else {
return stream, nil
}
return nil, nil
}
/*
* Create a new stream and register it at `id` in `session`
*
* If `id` is invalid or already registered, the call will fail.
*/
func (session *Session) newStream(id uint32, local bool) (*Stream, error) {
/* If the ID is valid, register the stream. Otherwise, send a protocol error */
if !session.streamIdIsValid(id, local) {
return nil, &Error{InvalidStreamId, id}
}
stream, streamPeer := NewStream(id, local)
session.streams[id] = streamPeer
if local {
session.lastStreamIdOut = id
} else {
session.lastStreamIdIn = id
}
/* Copy stream output to session output */
go func() {
err := Copy(session.outputW, streamPeer)
/* Close the stream if there's an error (inluding EOF) */
if err != nil {
session.CloseStream(id)
} else {
if streamPeer.Closed {
session.CloseStream(id)
}
}
}()
return stream, nil
}
func (session *Session) streamIdIsValid(id uint32, local bool) bool {
if id == 0 {
return false
}
/* Is this ID valid? */
if local {
if !session.isLocalId(id) {
return false
}
if nextId, err := session.nextIdOut(); err == nil {
if id < nextId {
return false
}
}
} else {
if session.isLocalId(id) {
return false
}
if nextId, err := session.nextIdIn(); err == nil {
if id < nextId {
return false
}
}
}
return true
}
func (session *Session) CloseStream(id uint32) error {
stream, exists := session.streams[id]
if !exists {
return errors.New(fmt.Sprintf("No such stream: %v", id))
}
stream.Close()
delete(session.streams, id)
return nil
}
/*
** Return the number of open streams
*/
func (session *Session) NStreams() int {
return len(session.streams)
}
func (session *Session) ReadFrame() (Frame, error) {
return session.outputR.ReadFrame()
}
func (session *Session) WriteFrame(frame Frame) error {
debug("Received frame: %#v", frame)
/* Is this frame stream-specific? */
if streamId, exists := frame.GetStreamId(); exists {
/* SYN_STREAM frame: create the stream */
if _, ok := frame.(*SynStreamFrame); ok {
if stream, err := session.newStream(streamId, false); err != nil {
if e, sendable := err.(*Error); sendable {
if err := session.outputW.WriteFrame(e.ToFrame()); err != nil {
return err
}
return nil
} else {
return err
}
} else {
go stream.Serve(session.handler)
}
}
streamPeer, exists := session.streams[streamId]
if !exists {
session.outputW.WriteFrame(&RstStreamFrame{StreamId: streamId, Status: ProtocolError})
return nil
}
err := streamPeer.WriteFrame(frame)
if err != nil {
debug("Error while passing frame to stream: %s. Closing stream.", err)
session.CloseStream(streamId)
return err
} else if streamPeer.Closed {
debug("Stream %d is fully closed. De-registering", streamId)
}
/* Is this frame session-wide? */
} else {
switch frame.(type) {
case *SettingsFrame: debug("SETTINGS\n")
case *NoopFrame: debug("NOOP\n")
case *PingFrame: session.outputW.WriteFrame(frame)
case *GoAwayFrame: debug("GOAWAY\n")
default: debug("Unknown frame type!")
}
}
return nil
}
func (session *Session) Serve(peer ReadWriter) error {
defer session.Close()
if err := Splice(session, peer, true); err != nil {
return err
}
return nil
}
/*
* Return true if it's legal for `id` to be locally created
* (eg. even-numbered if we're the server, odd-numbered if we're the client)
*/
func (session *Session) isLocalId(id uint32) bool {
if id == 0 {
return false
}
if session.Server {
return (id%2 == 0) /* Return true if id is even */
}
return (id%2 != 0) /* Return true if id is odd */
}