-
Notifications
You must be signed in to change notification settings - Fork 13
/
stream.go
301 lines (277 loc) · 7.14 KB
/
stream.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
// 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 SPDY protocol which is described in
// draft-mbelshe-httpbis-spdy-00.
//
// http://tools.ietf.org/html/draft-mbelshe-httpbis-spdy-00
package spdy
import (
"net/http"
"errors"
"io"
"io/ioutil"
"fmt"
)
/*
** A stream is just a place holder for an id, frame reader and frame writer.
*/
type Stream struct {
Id uint32
input *StreamPipeReader
output *StreamPipeWriter
errors []*Error
local bool // Was this stream created locally?
sendErrors bool
Closed bool
// FIXME: unidirectional
// FIXME: priority
}
func NewStream(id uint32, local bool) (*Stream, *Stream) {
debug("NewStream(%d)", id)
inputR, inputW := StreamPipe(id, local)
outputR, outputW := StreamPipe(id, !local)
stream := &Stream{input: inputR, output: outputW, sendErrors: false, Id: id, local: local}
peer := &Stream{input: outputR, output: inputW, sendErrors: true, Id: id, local: local}
return stream, peer
}
func (s *Stream) ReadFrame() (Frame, error) {
// Inject errors, if any
if len(s.errors) > 0 {
err := s.errors[len(s.errors) - 1]
s.errors = s.errors[:len(s.errors) - 1]
return err.ToFrame(), nil
}
frame, err := s.input.ReadFrame()
if err != nil {
return nil, err
}
if _, isRst := frame.(*RstStreamFrame); isRst {
s.Close()
}
s.debug("Received %#v err=%#v", frame, err)
return frame, nil
}
func (s *Stream) debug(msg string, args ...interface{}) {
debug(fmt.Sprintf("[STREAM %d %p] %s", s.Id, s, msg), args...)
}
func (s *Stream) WriteFrame(frame Frame) error {
s.debug("Passing %#v", frame)
err := s.output.WriteFrame(frame)
if err != nil {
// Send err as an RST_FRAME if possible and if sendErrors=true
if e, sendable := err.(*Error); sendable && s.sendErrors {
// [...] An endpoint MUST NOT send a RST_STREAM in
// response to an RST_STREAM, as doing so would lead to RST_STREAM
// loops [...]
if _, receivedRst := frame.(*RstStreamFrame); !receivedRst {
s.debug("Sending error (%s) as RST_STREAM frame", e)
s.errors = append(s.errors, e)
}
return nil
}
// Otherwise just pass the error
s.debug("Error %s is not sendable. Returning", err)
return err
}
if _, isRst := frame.(*RstStreamFrame); isRst {
s.Close()
}
return nil
}
func (s *Stream) Close() {
if s.Closed {
return
}
s.Closed = true
s.output.Close()
s.input.Close()
}
func (s *Stream) Reply(headers *http.Header, fin bool) error {
if headers == nil {
headers = new(http.Header)
}
var flags ControlFlags
if fin {
flags = ControlFlagFin
}
return s.WriteFrame(&SynReplyFrame{
StreamId: s.Id,
Headers: *headers,
CFHeader: ControlFrameHeader{Flags:flags},
})
}
func (s *Stream) Syn(headers *http.Header, fin bool) error {
if headers == nil {
headers = new(http.Header)
}
var flags ControlFlags
if fin {
flags = ControlFlagFin
}
return s.WriteFrame(&SynStreamFrame{
StreamId: s.Id,
Headers: *headers,
CFHeader: ControlFrameHeader{Flags:flags},
})
}
func (s *Stream) WriteHeadersFrame(headers *http.Header, fin bool) error {
if headers == nil {
headers = &http.Header{}
}
var flags ControlFlags
if fin {
flags = ControlFlagFin
}
return s.WriteFrame(&HeadersFrame{
StreamId: s.Id,
Headers: *headers,
CFHeader: ControlFrameHeader{Flags:flags},
})
}
func (s *Stream) WriteDataFrame(data []byte, fin bool) error {
var flags DataFlags
if fin {
flags = DataFlagFin
}
return s.WriteFrame(&DataFrame{
StreamId: s.Id,
Data: data,
Flags: flags,
})
}
func (s *Stream) CopyFrom(src io.Reader) error {
data := make([]byte, 4096)
for {
n, err := src.Read(data)
if err == io.EOF {
return nil
} else if err != nil {
return err
}
if err := s.WriteDataFrame(data[:n], false); err != nil {
return err
}
}
return nil
}
func (s *Stream) Rst(status StatusCode) error {
return s.WriteFrame(&RstStreamFrame{StreamId: s.Id, Status: status})
}
func (stream *Stream) Serve(handler http.Handler) {
stream.debug("Running handler")
if handler == nil {
stream.Rst(RefusedStream)
return
}
w := &ResponseWriter{Stream: stream}
r, err := stream.ParseHTTPRequest();
if err != nil {
// FIXME: send error
stream.debug("Error parsing http request: %s\n", err)
return
}
handler.ServeHTTP(w, r)
stream.debug("Handler returned. Cleaning up.")
stream.WriteDataFrame(nil, true) // Close the stream in case the handler hasn't
_, err = io.Copy(ioutil.Discard, r.Body) // Drain all remaining input
if err != nil {
stream.debug("Error while draining: %s", err)
}
stream.debug("Done cleaning up")
}
func (s *Stream) ParseHTTPRequest() (*http.Request, error) {
if s.input.NFrames > 0 {
return nil, errors.New("Can't parse HTTP request: first SPDY frame already read")
}
frame, err := s.ReadFrame()
if err != nil {
return nil, err
}
headers := frame.GetHeaders()
method := headers.Get("method")
if method == "" {
method = "GET"
}
s.debug("headers = %#v", *headers)
path := headers.Get("url")
if path == "" {
path = "/"
}
s.debug("path = %s", (*headers)["url"])
bodyReader, bodyWriter := io.Pipe()
go func() {
ExtractData(s, bodyWriter)
bodyWriter.Close()
}()
r, err := http.NewRequest(method, path, bodyReader)
if err != nil {
return nil, err
}
UpdateHeaders(&r.Header, headers)
return r, nil
}
func StreamPipe(id uint32, reply bool) (*StreamPipeReader, *StreamPipeWriter) {
pipeReader, pipeWriter := Pipe(4096) // Buffering is Ok after writing, but not before (for sendErrors)
reader := &StreamPipeReader{PipeReader: pipeReader}
writer := &StreamPipeWriter{PipeWriter: pipeWriter, id: id, reply: reply, Headers: make(http.Header)}
return reader, writer
}
type StreamPipeReader struct {
*PipeReader
}
type StreamPipeWriter struct {
*PipeWriter
reply bool // If true, must start with SYN_REPLY. Otherwise must start with SYN_STREAM
closed bool
id uint32
Headers http.Header
}
func (p *StreamPipeWriter) WriteFrame(frame Frame) error {
if p.closed {
return &Error{StreamClosed, p.id}
}
if id, exists := frame.GetStreamId(); !exists || id != p.id {
return errors.New("Wrong stream ID")
}
// Check for the correct sequence of frames
switch frame.(type) {
// SYN_STREAM is only allowed as the first frame and if reply=false
case *SynStreamFrame: {
if p.NFrames > 0 || p.reply {
return &Error{IllegalSynStream, p.id}
}
}
// SYN_REPLY is only allowed as the first frame and if reply=true
case *SynReplyFrame: {
if p.NFrames > 0 || !p.reply {
return &Error{IllegalSynReply, p.id}
}
}
// Any other frames are forbidden as the first frame
default: {
if p.NFrames == 0 {
return &Error{IllegalFirstFrame, p.id}
}
}
}
if err := p.PipeWriter.WriteFrame(frame); err != nil {
return err
}
/* If FLAG_FIN=true, close the pipe */
if frame.GetFinFlag() {
debug("FIN=1, closing StreamPipe")
p.closed = true
p.PipeWriter.Close()
}
/* On a RST_STREAM, close the pipe */
if _, isRst := frame.(*RstStreamFrame); isRst {
debug("Received RST_STREAM. Closing")
p.closed = true
}
/* Store headers */
if headers := frame.GetHeaders(); headers != nil {
UpdateHeaders(&p.Headers, headers)
}
return nil
}