Debian Codegraph @

Reader.go

raw symbols SCIP
1 /*
2 * Copyright (c) 2014 by Farsight Security, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package framestream
18
19 import (
20 "bufio"
21 "encoding/binary"
22 "io"
23 "io/ioutil"
24 "time"
25 )
26
27 type ReaderOptions struct {
28 // The ContentTypes accepted by the Reader. May be left unset for no
29 // content negotiation. If the corresponding Writer offers a disjoint
30 // set of ContentTypes, NewReader() will return ErrContentTypeMismatch.
31 ContentTypes [][]byte
32 // If Bidirectional is true, the underlying io.Reader must be an
33 // io.ReadWriter, and the Reader will engage in a bidirectional
34 // handshake with its peer to establish content type and communicate
35 // shutdown.
36 Bidirectional bool
37 // Timeout gives the timeout for reading the initial handshake messages
38 // from the peer and writing response messages if Bidirectional. It is
39 // only effective for underlying Readers satisfying net.Conn.
40 Timeout time.Duration
41 }
42
43 // Reader reads data frames from an underlying io.Reader using the Frame
44 // Streams framing protocol.
45 type Reader struct {
46 contentType []byte
47 bidirectional bool
48 r *bufio.Reader
49 w *bufio.Writer
50 stopped bool
51 }
52
53 // NewReader creates a Frame Streams Reader reading from the given io.Reader
54 // with the given ReaderOptions.
55 func NewReader(r io.Reader, opt *ReaderOptions) (*Reader, error) {
56 if opt == nil {
57 opt = &ReaderOptions{}
58 }
59 tr := timeoutReader(r, opt)
60 reader := &Reader{
61 bidirectional: opt.Bidirectional,
62 r: bufio.NewReader(tr),
63 w: nil,
64 }
65
66 if len(opt.ContentTypes) > 0 {
67 reader.contentType = opt.ContentTypes[0]
68 }
69
70 var cf ControlFrame
71 if opt.Bidirectional {
72 w, ok := tr.(io.Writer)
73 if !ok {
74 return nil, ErrType
75 }
76 reader.w = bufio.NewWriter(w)
77
78 // Read the ready control frame.
79 err := cf.DecodeTypeEscape(reader.r, CONTROL_READY)
80 if err != nil {
81 return nil, err
82 }
83
84 // Check content type.
85 if t, ok := cf.ChooseContentType(opt.ContentTypes); ok {
86 reader.contentType = t
87 } else {
88 return nil, ErrContentTypeMismatch
89 }
90
91 // Send the accept control frame.
92 accept := ControlAccept
93 accept.SetContentType(reader.contentType)
94 err = accept.EncodeFlush(reader.w)
95 if err != nil {
96 return nil, err
97 }
98 }
99
100 // Read the start control frame.
101 err := cf.DecodeTypeEscape(reader.r, CONTROL_START)
102 if err != nil {
103 return nil, err
104 }
105
106 // Disable the read timeout to prevent killing idle connections.
107 disableReadTimeout(tr)
108
109 // Check content type.
110 if !cf.MatchContentType(reader.contentType) {
111 return nil, ErrContentTypeMismatch
112 }
113
114 return reader, nil
115 }
116
117 // ReadFrame reads a data frame into the supplied buffer, returning its length.
118 // If the frame is longer than the supplied buffer, Read returns
119 // ErrDataFrameTooLarge and discards the frame. Subsequent calls to Read()
120 // after this error may succeed.
121 func (r *Reader) ReadFrame(b []byte) (length int, err error) {
122 if r.stopped {
123 return 0, EOF
124 }
125
126 for length == 0 {
127 length, err = r.readFrame(b)
128 if err != nil {
129 return
130 }
131 }
132
133 return
134 }
135
136 // ContentType returns the content type negotiated with the Writer.
137 func (r *Reader) ContentType() []byte {
138 return r.contentType
139 }
140
141 func (r *Reader) readFrame(b []byte) (int, error) {
142 // Read the frame length.
143 var frameLen uint32
144 err := binary.Read(r.r, binary.BigEndian, &frameLen)
145 if err != nil {
146 return 0, err
147 }
148
149 if frameLen > uint32(len(b)) {
150 io.CopyN(ioutil.Discard, r.r, int64(frameLen))
151 return 0, ErrDataFrameTooLarge
152 }
153
154 if frameLen == 0 {
155 // This is a control frame.
156 var cf ControlFrame
157 err = cf.Decode(r.r)
158 if err != nil {
159 return 0, err
160 }
161 if cf.ControlType == CONTROL_STOP {
162 r.stopped = true
163 if r.bidirectional {
164 ff := &ControlFrame{ControlType: CONTROL_FINISH}
165 err = ff.EncodeFlush(r.w)
166 if err != nil {
167 return 0, err
168 }
169 }
170 return 0, EOF
171 }
172 return 0, err
173 }
174
175 return io.ReadFull(r.r, b[0:frameLen])
176 }