forked from SlyMarbo/spdy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_stream.go
296 lines (250 loc) · 5.96 KB
/
request_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
// Copyright 2013 Jamie Hall. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package spdy3
import (
"errors"
"fmt"
"net/http"
"sync"
"github.com/SlyMarbo/spdy/common"
"github.com/SlyMarbo/spdy/spdy3/frames"
)
// RequestStream is a structure that implements
// the Stream and ResponseWriter interfaces. This
// is used for responding to client requests.
type RequestStream struct {
sync.Mutex
Request *http.Request
Receiver common.Receiver
recvMutex sync.Mutex
shutdownOnce sync.Once
conn *Conn
streamID common.StreamID
flow *flowControl
state *common.StreamState
output chan<- common.Frame
header http.Header
headerChan chan func()
responseCode int
stop <-chan bool
finished chan struct{}
}
func NewRequestStream(conn *Conn, streamID common.StreamID, output chan<- common.Frame) *RequestStream {
out := new(RequestStream)
out.conn = conn
out.streamID = streamID
out.output = output
out.stop = conn.stop
out.state = new(common.StreamState)
out.state.CloseHere()
out.header = make(http.Header)
out.finished = make(chan struct{})
out.headerChan = make(chan func(), 5)
go out.processFrames()
return out
}
/***********************
* http.ResponseWriter *
***********************/
func (s *RequestStream) Header() http.Header {
return s.header
}
// Write is one method with which request data is sent.
func (s *RequestStream) Write(inputData []byte) (int, error) {
if s.closed() || s.state.ClosedHere() {
return 0, errors.New("Error: Stream already closed.")
}
// Copy the data locally to avoid any pointer issues.
data := make([]byte, len(inputData))
copy(data, inputData)
// Send any new headers.
s.writeHeader()
// Chunk the response if necessary.
// Data is sent to the flow control to
// ensure that the protocol is followed.
written := 0
for len(data) > common.MAX_DATA_SIZE {
n, err := s.flow.Write(data[:common.MAX_DATA_SIZE])
if err != nil {
return written, err
}
written += n
data = data[common.MAX_DATA_SIZE:]
}
if len(data) > 0 {
n, err := s.flow.Write(data)
written += n
if err != nil {
return written, err
}
}
return written, nil
}
// WriteHeader is used to set the HTTP status code.
func (s *RequestStream) WriteHeader(int) {
s.writeHeader()
}
/*****************
* io.Closer *
*****************/
// Close is used to stop the stream safely.
func (s *RequestStream) Close() error {
defer common.Recover()
s.Lock()
s.shutdownOnce.Do(s.shutdown)
s.Unlock()
return nil
}
func (s *RequestStream) shutdown() {
s.writeHeader()
if s.state != nil {
if s.state.OpenThere() {
// Send the RST_STREAM.
rst := new(frames.RST_STREAM)
rst.StreamID = s.streamID
rst.Status = common.RST_STREAM_CANCEL
s.output <- rst
}
s.state.Close()
}
if s.flow != nil {
s.flow.Close()
}
select {
case <-s.finished:
default:
close(s.finished)
}
select {
case <-s.headerChan:
default:
close(s.headerChan)
}
s.conn.requestStreamLimit.Close()
s.output = nil
s.Request = nil
s.Receiver = nil
s.header = nil
s.stop = nil
s.conn.streamsLock.Lock()
delete(s.conn.streams, s.streamID)
s.conn.streamsLock.Unlock()
}
/**********
* Stream *
**********/
func (s *RequestStream) Conn() common.Conn {
return s.conn
}
func (s *RequestStream) ReceiveFrame(frame common.Frame) error {
s.recvMutex.Lock()
defer s.recvMutex.Unlock()
if frame == nil {
return errors.New("Nil frame received.")
}
// Process the frame depending on its type.
switch frame := frame.(type) {
case *frames.DATA:
// Extract the data.
data := frame.Data
if data == nil {
data = []byte{}
}
// Give to the client.
s.flow.Receive(frame.Data)
s.headerChan <- func() {
s.Receiver.ReceiveData(s.Request, data, frame.Flags.FIN())
if frame.Flags.FIN() {
s.state.CloseThere()
s.Close()
}
}
case *frames.SYN_REPLY:
s.headerChan <- func() {
s.Receiver.ReceiveHeader(s.Request, frame.Header)
if frame.Flags.FIN() {
s.state.CloseThere()
s.Close()
}
}
case *frames.HEADERS:
s.headerChan <- func() {
s.Receiver.ReceiveHeader(s.Request, frame.Header)
if frame.Flags.FIN() {
s.state.CloseThere()
s.Close()
}
}
case *frames.WINDOW_UPDATE:
err := s.flow.UpdateWindow(frame.DeltaWindowSize)
if err != nil {
reply := new(frames.RST_STREAM)
reply.StreamID = s.streamID
reply.Status = common.RST_STREAM_FLOW_CONTROL_ERROR
s.output <- reply
}
default:
return errors.New(fmt.Sprintf("Received unknown frame of type %T.", frame))
}
return nil
}
func (s *RequestStream) CloseNotify() <-chan bool {
return s.stop
}
// run is the main control path of
// the stream. Data is recieved,
// processed, and then the stream
// is cleaned up and closed.
func (s *RequestStream) Run() error {
// Receive and process inbound frames.
<-s.finished
// Make sure any queued data has been sent.
if s.flow.Paused() {
return errors.New(fmt.Sprintf("Error: Stream %d has been closed with data still buffered.\n", s.streamID))
}
// Clean up state.
s.state.CloseHere()
return nil
}
func (s *RequestStream) State() *common.StreamState {
return s.state
}
func (s *RequestStream) StreamID() common.StreamID {
return s.streamID
}
func (s *RequestStream) closed() bool {
if s.conn == nil || s.state == nil || s.Receiver == nil {
return true
}
select {
case _ = <-s.stop:
return true
default:
return false
}
}
// writeHeader is used to flush HTTP headers.
func (s *RequestStream) writeHeader() {
if len(s.header) == 0 {
return
}
// Create the HEADERS frame.
header := new(frames.HEADERS)
header.StreamID = s.streamID
header.Header = make(http.Header)
// Clear the headers that have been sent.
for name, values := range s.header {
for _, value := range values {
header.Header.Add(name, value)
}
s.header.Del(name)
}
s.output <- header
}
func (s *RequestStream) processFrames() {
defer common.Recover()
for f := range s.headerChan {
f()
}
}