forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
160 lines (138 loc) · 2.94 KB
/
decoder.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
package payload
import (
"bufio"
"encoding/base64"
"io"
"io/ioutil"
"github.com/googollee/go-socket.io/engineio/frame"
"github.com/googollee/go-socket.io/engineio/packet"
)
type byteReader interface {
ReadByte() (byte, error)
io.Reader
}
type readerFeeder interface {
getReader() (io.Reader, bool, error)
putReader(error) error
}
type decoder struct {
b64Reader io.Reader
limitReader io.LimitedReader
rawReader byteReader
feeder readerFeeder
ft frame.Type
pt packet.Type
supportBinary bool
}
func (d *decoder) NextReader() (frame.Type, packet.Type, io.ReadCloser, error) {
if d.rawReader == nil {
r, supportBinary, err := d.feeder.getReader()
if err != nil {
return 0, 0, nil, err
}
br, ok := r.(byteReader)
if !ok {
br = bufio.NewReader(r)
}
if err := d.setNextReader(br, supportBinary); err != nil {
return 0, 0, nil, d.sendError(err)
}
}
return d.ft, d.pt, d, nil
}
func (d *decoder) Read(p []byte) (int, error) {
if d.b64Reader != nil {
return d.b64Reader.Read(p)
}
return d.limitReader.Read(p)
}
func (d *decoder) Close() error {
if _, err := io.Copy(ioutil.Discard, d); err != nil {
return d.sendError(err)
}
err := d.setNextReader(d.rawReader, d.supportBinary)
if err != nil {
if err != io.EOF {
return d.sendError(err)
}
d.rawReader = nil
d.limitReader.R = nil
d.limitReader.N = 0
d.b64Reader = nil
err = d.sendError(nil)
}
return err
}
func (d *decoder) setNextReader(r byteReader, supportBinary bool) error {
var read func(byteReader) (frame.Type, packet.Type, int64, error)
if supportBinary {
read = d.binaryRead
} else {
read = d.textRead
}
ft, pt, l, err := read(r)
if err != nil {
return err
}
d.ft = ft
d.pt = pt
d.rawReader = r
d.limitReader.R = r
d.limitReader.N = l
d.supportBinary = supportBinary
if !supportBinary && ft == frame.Binary {
d.b64Reader = base64.NewDecoder(base64.StdEncoding, &d.limitReader)
} else {
d.b64Reader = nil
}
return nil
}
func (d *decoder) sendError(err error) error {
if e := d.feeder.putReader(err); e != nil {
return e
}
return err
}
func (d *decoder) textRead(r byteReader) (frame.Type, packet.Type, int64, error) {
l, err := readTextLen(r)
if err != nil {
return 0, 0, 0, err
}
ft := frame.String
b, err := r.ReadByte()
if err != nil {
return 0, 0, 0, err
}
l--
if b == 'b' {
ft = frame.Binary
b, err = r.ReadByte()
if err != nil {
return 0, 0, 0, err
}
l--
}
pt := packet.ByteToPacketType(b, frame.String)
return ft, pt, l, nil
}
func (d *decoder) binaryRead(r byteReader) (frame.Type, packet.Type, int64, error) {
b, err := r.ReadByte()
if err != nil {
return 0, 0, 0, err
}
if b > 1 {
return 0, 0, 0, errInvalidPayload
}
ft := frame.ByteToFrameType(b)
l, err := readBinaryLen(r)
if err != nil {
return 0, 0, 0, err
}
b, err = r.ReadByte()
if err != nil {
return 0, 0, 0, err
}
pt := packet.ByteToPacketType(b, ft)
l--
return ft, pt, l, nil
}