-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReader.go
176 lines (154 loc) · 4.33 KB
/
Reader.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
/*
* Copyright (c) 2014 by Farsight Security, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package framestream
import (
"bufio"
"encoding/binary"
"io"
"io/ioutil"
"time"
)
type ReaderOptions struct {
// The ContentTypes accepted by the Reader. May be left unset for no
// content negotiation. If the corresponding Writer offers a disjoint
// set of ContentTypes, NewReader() will return ErrContentTypeMismatch.
ContentTypes [][]byte
// If Bidirectional is true, the underlying io.Reader must be an
// io.ReadWriter, and the Reader will engage in a bidirectional
// handshake with its peer to establish content type and communicate
// shutdown.
Bidirectional bool
// Timeout gives the timeout for reading the initial handshake messages
// from the peer and writing response messages if Bidirectional. It is
// only effective for underlying Readers satisfying net.Conn.
Timeout time.Duration
}
// Reader reads data frames from an underlying io.Reader using the Frame
// Streams framing protocol.
type Reader struct {
contentType []byte
bidirectional bool
r *bufio.Reader
w *bufio.Writer
stopped bool
}
// NewReader creates a Frame Streams Reader reading from the given io.Reader
// with the given ReaderOptions.
func NewReader(r io.Reader, opt *ReaderOptions) (*Reader, error) {
if opt == nil {
opt = &ReaderOptions{}
}
tr := timeoutReader(r, opt)
reader := &Reader{
bidirectional: opt.Bidirectional,
r: bufio.NewReader(tr),
w: nil,
}
if len(opt.ContentTypes) > 0 {
reader.contentType = opt.ContentTypes[0]
}
var cf ControlFrame
if opt.Bidirectional {
w, ok := tr.(io.Writer)
if !ok {
return nil, ErrType
}
reader.w = bufio.NewWriter(w)
// Read the ready control frame.
err := cf.DecodeTypeEscape(reader.r, CONTROL_READY)
if err != nil {
return nil, err
}
// Check content type.
if t, ok := cf.ChooseContentType(opt.ContentTypes); ok {
reader.contentType = t
} else {
return nil, ErrContentTypeMismatch
}
// Send the accept control frame.
accept := ControlAccept
accept.SetContentType(reader.contentType)
err = accept.EncodeFlush(reader.w)
if err != nil {
return nil, err
}
}
// Read the start control frame.
err := cf.DecodeTypeEscape(reader.r, CONTROL_START)
if err != nil {
return nil, err
}
// Disable the read timeout to prevent killing idle connections.
disableReadTimeout(tr)
// Check content type.
if !cf.MatchContentType(reader.contentType) {
return nil, ErrContentTypeMismatch
}
return reader, nil
}
// ReadFrame reads a data frame into the supplied buffer, returning its length.
// If the frame is longer than the supplied buffer, Read returns
// ErrDataFrameTooLarge and discards the frame. Subsequent calls to Read()
// after this error may succeed.
func (r *Reader) ReadFrame(b []byte) (length int, err error) {
if r.stopped {
return 0, EOF
}
for length == 0 {
length, err = r.readFrame(b)
if err != nil {
return
}
}
return
}
// ContentType returns the content type negotiated with the Writer.
func (r *Reader) ContentType() []byte {
return r.contentType
}
func (r *Reader) readFrame(b []byte) (int, error) {
// Read the frame length.
var frameLen uint32
err := binary.Read(r.r, binary.BigEndian, &frameLen)
if err != nil {
return 0, err
}
if frameLen > uint32(len(b)) {
io.CopyN(ioutil.Discard, r.r, int64(frameLen))
return 0, ErrDataFrameTooLarge
}
if frameLen == 0 {
// This is a control frame.
var cf ControlFrame
err = cf.Decode(r.r)
if err != nil {
return 0, err
}
if cf.ControlType == CONTROL_STOP {
r.stopped = true
if r.bidirectional {
ff := &ControlFrame{ControlType: CONTROL_FINISH}
err = ff.EncodeFlush(r.w)
if err != nil {
return 0, err
}
}
return 0, EOF
}
return 0, err
}
return io.ReadFull(r.r, b[0:frameLen])
}