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
use bytes::BytesMut;
use std::{io, str};
use tokio_codec::{Decoder, Encoder};
#[derive(Debug, Clone)]
pub enum Separator {
Empty,
Byte(u8),
}
impl Default for Separator {
fn default() -> Self {
Separator::Byte(b'\n')
}
}
#[derive(Debug, Default)]
pub struct StreamCodec {
incoming_separator: Separator,
outgoing_separator: Separator,
}
impl StreamCodec {
pub fn stream_incoming() -> Self {
StreamCodec::new(Separator::Empty, Default::default())
}
pub fn new(incoming_separator: Separator, outgoing_separator: Separator) -> Self {
StreamCodec {
incoming_separator,
outgoing_separator,
}
}
}
fn is_whitespace(byte: u8) -> bool {
match byte {
0x0D | 0x0A | 0x20 | 0x09 => true,
_ => false,
}
}
impl Decoder for StreamCodec {
type Item = String;
type Error = io::Error;
fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<Self::Item>> {
if let Separator::Byte(separator) = self.incoming_separator {
if let Some(i) = buf.as_ref().iter().position(|&b| b == separator) {
let line = buf.split_to(i);
buf.split_to(1);
match str::from_utf8(&line.as_ref()) {
Ok(s) => Ok(Some(s.to_string())),
Err(_) => Err(io::Error::new(io::ErrorKind::Other, "invalid UTF-8")),
}
} else {
Ok(None)
}
} else {
let mut depth = 0;
let mut in_str = false;
let mut is_escaped = false;
let mut start_idx = 0;
let mut whitespaces = 0;
for idx in 0..buf.as_ref().len() {
let byte = buf.as_ref()[idx];
if (byte == b'{' || byte == b'[') && !in_str {
if depth == 0 {
start_idx = idx;
}
depth += 1;
} else if (byte == b'}' || byte == b']') && !in_str {
depth -= 1;
} else if byte == b'"' && !is_escaped {
in_str = !in_str;
} else if is_whitespace(byte) {
whitespaces += 1;
}
if byte == b'\\' && !is_escaped && in_str {
is_escaped = true;
} else {
is_escaped = false;
}
if depth == 0 && idx != start_idx && idx - start_idx + 1 > whitespaces {
let bts = buf.split_to(idx + 1);
match String::from_utf8(bts.as_ref().to_vec()) {
Ok(val) => return Ok(Some(val)),
Err(_) => {
return Ok(None);
}
};
}
}
Ok(None)
}
}
}
impl Encoder for StreamCodec {
type Item = String;
type Error = io::Error;
fn encode(&mut self, msg: String, buf: &mut BytesMut) -> io::Result<()> {
let mut payload = msg.into_bytes();
if let Separator::Byte(separator) = self.outgoing_separator {
payload.push(separator);
}
buf.extend_from_slice(&payload);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::StreamCodec;
use bytes::{BufMut, BytesMut};
use tokio_codec::Decoder;
#[test]
fn simple_encode() {
let mut buf = BytesMut::with_capacity(2048);
buf.put_slice(b"{ test: 1 }{ test: 2 }{ test: 3 }");
let mut codec = StreamCodec::stream_incoming();
let request = codec
.decode(&mut buf)
.expect("There should be no error in simple test")
.expect("There should be at least one request in simple test");
assert_eq!(request, "{ test: 1 }");
}
#[test]
fn escape() {
let mut buf = BytesMut::with_capacity(2048);
buf.put_slice(br#"{ test: "\"\\" }{ test: "\ " }{ test: "\}" }[ test: "\]" ]"#);
let mut codec = StreamCodec::stream_incoming();
let request = codec
.decode(&mut buf)
.expect("There should be no error in first escape test")
.expect("There should be a request in first escape test");
assert_eq!(request, r#"{ test: "\"\\" }"#);
let request2 = codec
.decode(&mut buf)
.expect("There should be no error in 2nd escape test")
.expect("There should be a request in 2nd escape test");
assert_eq!(request2, r#"{ test: "\ " }"#);
let request3 = codec
.decode(&mut buf)
.expect("There should be no error in 3rd escape test")
.expect("There should be a request in 3rd escape test");
assert_eq!(request3, r#"{ test: "\}" }"#);
let request4 = codec
.decode(&mut buf)
.expect("There should be no error in 4th escape test")
.expect("There should be a request in 4th escape test");
assert_eq!(request4, r#"[ test: "\]" ]"#);
}
#[test]
fn whitespace() {
let mut buf = BytesMut::with_capacity(2048);
buf.put_slice(b"{ test: 1 }\n\n\n\n{ test: 2 }\n\r{\n test: 3 } ");
let mut codec = StreamCodec::stream_incoming();
let request = codec
.decode(&mut buf)
.expect("There should be no error in first whitespace test")
.expect("There should be a request in first whitespace test");
assert_eq!(request, "{ test: 1 }");
let request2 = codec
.decode(&mut buf)
.expect("There should be no error in first 2nd test")
.expect("There should be aa request in 2nd whitespace test");
assert_eq!(request2, "\n\n\n\n{ test: 2 }");
let request3 = codec
.decode(&mut buf)
.expect("There should be no error in first 3rd test")
.expect("There should be a request in 3rd whitespace test");
assert_eq!(request3, "\n\r{\n test: 3 }");
let request4 = codec
.decode(&mut buf)
.expect("There should be no error in first 4th test");
assert!(
request4.is_none(),
"There should be no 4th request because it contains only whitespaces"
);
}
#[test]
fn fragmented_encode() {
let mut buf = BytesMut::with_capacity(2048);
buf.put_slice(b"{ test: 1 }{ test: 2 }{ tes");
let mut codec = StreamCodec::stream_incoming();
let request = codec
.decode(&mut buf)
.expect("There should be no error in first fragmented test")
.expect("There should be at least one request in first fragmented test");
assert_eq!(request, "{ test: 1 }");
codec
.decode(&mut buf)
.expect("There should be no error in second fragmented test")
.expect("There should be at least one request in second fragmented test");
assert_eq!(String::from_utf8(buf.as_ref().to_vec()).unwrap(), "{ tes");
buf.put_slice(b"t: 3 }");
let request = codec
.decode(&mut buf)
.expect("There should be no error in third fragmented test")
.expect("There should be at least one request in third fragmented test");
assert_eq!(request, "{ test: 3 }");
}
#[test]
fn huge() {
let request = r#"
{
"jsonrpc":"2.0",
"method":"say_hello",
"params": [
42,
0,
{
"from":"0xb60e8dd61c5d32be8058bb8eb970870f07233155",
"gas":"0x2dc6c0",
"data":"0x606060405260003411156010576002565b6001805433600160a060020a0319918216811790925560028054909116909117905561291f806100406000396000f3606060405236156100e55760e060020a600035046304029f2381146100ed5780630a1273621461015f57806317c1dd87146102335780631f9ea25d14610271578063266fa0e91461029357806349593f5314610429578063569aa0d8146104fc57806359a4669f14610673578063647a4d5f14610759578063656104f5146108095780636e9febfe1461082b57806370de8c6e1461090d57806371bde852146109ed5780638f30435d14610ab4578063916dbc1714610da35780639f5a7cd414610eef578063c91540f614610fe6578063eae99e1c146110b5578063fedc2a281461115a575b61122d610002565b61122d6004808035906020019082018035906020019191908080601f01602080910402602001604051908101604052809392919081815260200183838082843750949650509335935050604435915050606435600154600090600160a060020a03908116339091161461233357610002565b61122f6004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437509496505093359350506044359150506064355b60006000600060005086604051808280519060200190808383829060006004602084601f0104600f02600301f1509050019150509081526020016040518091039020600050905042816005016000508560ff1660028110156100025760040201835060010154604060020a90046001604060020a0316116115df576115d6565b6112416004355b604080516001604060020a038316408152606060020a33600160a060020a031602602082015290519081900360340190205b919050565b61122d600435600254600160a060020a0390811633909116146128e357610002565b61125e6004808035906020019082018035906020019191908080601f01602080910402602001604051908101604052809392919081815260200183838082843750949650509335935050505060006000600060006000600060005087604051808280519060200190808383829060006004602084601f0104600f02600301f1509050019150509081526020016040518091039020600050905080600001600050600087600160a060020a0316815260200190815260200160002060005060000160059054906101000a90046001604060020a03169450845080600001600050600087600160a060020a03168152602001908152602001600020600050600001600d9054906101000a90046001604060020a03169350835080600001600050600087600160a060020a0316815260200190815260200160002060005060000160009054906101000a900460ff169250825080600001600050600087600160a060020a0316815260200190815260200160002060005060000160019054906101000a900463ffffffff16915081505092959194509250565b61122d6004808035906020019082018035906020019191908080601f01602080910402602001604051908101604052809392919081815260200183838082843750949650509335935050604435915050606435608435600060006000600060005088604051808280519060200190808383829060006004602084601f0104600f02600301f15090500191505090815260200160405180910390206000509250346000141515611c0e5760405133600160a060020a0316908290349082818181858883f193505050501515611c1a57610002565b6112996004808035906020019082018035906020019191908080601f01602080910402602001604051908101604052809392919081815260200183838082843750949650509335935050604435915050600060006000600060006000600060006000508a604051808280519060200190808383829060006004602084601f0104600f02600301f15090500191505090815260200160405180910390206000509050806001016000508960ff16600281101561000257600160a060020a038a168452828101600101602052604084205463ffffffff1698506002811015610002576040842054606060020a90046001604060020a031697506002811015610002576040842054640100000000900463ffffffff169650600281101561000257604084206001015495506002811015610002576040842054604060020a900463ffffffff169450600281101561000257505060409091205495999498509296509094509260a060020a90046001604060020a0316919050565b61122d6004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437509496505050505050506000600060005082604051808280519060200190808383829060006004602084601f0104600f02600301f15090500191505090815260200160405180910390206000509050348160050160005082600d0160009054906101000a900460ff1660ff16600281101561000257600402830160070180546001608060020a0381169093016001608060020a03199390931692909217909155505b5050565b6112e26004808035906020019082018035906020019191908080601f01602080910003423423094734987103498712093847102938740192387401349857109487501938475"
}
]
}"#;
let mut buf = BytesMut::with_capacity(65536);
buf.put_slice(request.as_bytes());
let mut codec = StreamCodec::stream_incoming();
let parsed_request = codec
.decode(&mut buf)
.expect("There should be no error in huge test")
.expect("There should be at least one request huge test");
assert_eq!(request, parsed_request);
}
#[test]
fn simple_line_codec() {
let mut buf = BytesMut::with_capacity(2048);
buf.put_slice(b"{ test: 1 }\n{ test: 2 }\n{ test: 3 }");
let mut codec = StreamCodec::default();
let request = codec
.decode(&mut buf)
.expect("There should be no error in simple test")
.expect("There should be at least one request in simple test");
let request2 = codec
.decode(&mut buf)
.expect("There should be no error in simple test")
.expect("There should be at least one request in simple test");
assert_eq!(request, "{ test: 1 }");
assert_eq!(request2, "{ test: 2 }");
}
}