Skip to content

Commit 6f8eb25

Browse files
author
Michael Eden
authored
Merge pull request #128 from cyderize/fix/dataframe-payload
Dataframe reads all of payload. Fixes #119
2 parents f2c9e8f + 0eee840 commit 6f8eb25

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

src/client/builder.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ mod async_imports {
5555
#[cfg(feature="async")]
5656
use self::async_imports::*;
5757

58-
// TODO: add extra funcs for future stuff, like auto ping and auto close
59-
60-
6158
/// Build clients with a builder-style API
6259
/// This makes it easy to create and configure a websocket
6360
/// connection:
@@ -662,7 +659,6 @@ impl<'u> ClientBuilder<'u> {
662659
Box::new(future)
663660
}
664661

665-
// TODO: add timeout option for connecting
666662
// TODO: add conveniences like .response_to_pings, .send_close, etc.
667663
/// Asynchronously create an insecure (plain TCP) connection to the client.
668664
///
@@ -974,6 +970,4 @@ mod tests {
974970
assert!(protos.contains(&"electric".to_string()));
975971
assert!(!protos.contains(&"rust-websocket".to_string()));
976972
}
977-
978-
// TODO: a few more
979973
}

src/codec/ws.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ use ws::message::Message as MessageTrait;
2424
use ws::util::header::read_header;
2525
use result::WebSocketError;
2626

27-
// TODO: IMPORTANT: check if frame_size is correct,
28-
// do not call .reserve with the entire size
29-
3027
/// Even though a websocket connection may look perfectly symmetrical
3128
/// in reality there are small differences between clients and servers.
3229
/// This type is passed to the codecs to inform them of what role they are in
@@ -290,8 +287,6 @@ impl<M> Encoder for MessageCodec<M>
290287
}
291288
}
292289

293-
// TODO: add tests to check boundary cases for reading dataframes
294-
295290
#[cfg(test)]
296291
mod tests {
297292
use super::*;

src/dataframe.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Module containing the default implementation of data frames.
2-
use std::io::{Read, Write};
2+
use std::io::{self, Read, Write};
33
use result::{WebSocketResult, WebSocketError};
44
use ws::dataframe::DataFrame as DataFrameable;
55
use ws::util::header::DataFrameHeader;
@@ -61,7 +61,6 @@ impl DataFrame {
6161
if !should_be_masked {
6262
return Err(WebSocketError::DataFrameError("Expected unmasked data frame"));
6363
}
64-
// TODO: move data to avoid copy?
6564
mask::mask_data(mask, &body)
6665
}
6766
None => {
@@ -87,7 +86,10 @@ impl DataFrame {
8786
let header = try!(dfh::read_header(reader));
8887

8988
let mut data: Vec<u8> = Vec::with_capacity(header.len as usize);
90-
reader.take(header.len).read_to_end(&mut data)?;
89+
let read = reader.take(header.len).read_to_end(&mut data)?;
90+
if (read as u64) < header.len {
91+
return Err(io::Error::new(io::ErrorKind::UnexpectedEof, "incomplete payload").into());
92+
}
9193

9294
DataFrame::read_dataframe_body(header, data, should_be_masked)
9395
}
@@ -212,6 +214,25 @@ mod tests {
212214
};
213215
assert_eq!(obtained, expected);
214216
}
217+
218+
#[test]
219+
fn read_incomplete_payloads() {
220+
let mut data = vec![0x8au8, 0x08, 0x19, 0xac, 0xab, 0x8a, 0x52, 0x4e, 0x05, 0x00];
221+
let payload = vec![25, 172, 171, 138, 82, 78, 5, 0];
222+
let short_header = DataFrame::read_dataframe(&mut &data[..1], false);
223+
let short_payload = DataFrame::read_dataframe(&mut &data[..6], false);
224+
let full_payload = DataFrame::read_dataframe(&mut &data[..], false);
225+
data.push(0xff);
226+
let more_payload = DataFrame::read_dataframe(&mut &data[..], false);
227+
228+
match (short_header.unwrap_err(), short_payload.unwrap_err()) {
229+
(WebSocketError::NoDataAvailable, WebSocketError::NoDataAvailable) => (),
230+
_ => assert!(false),
231+
};
232+
assert_eq!(full_payload.unwrap().data, payload);
233+
assert_eq!(more_payload.unwrap().data, payload);
234+
}
235+
215236
#[bench]
216237
fn bench_read_dataframe(b: &mut Bencher) {
217238
let data = b"The quick brown fox jumps over the lazy dog";

src/receiver.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,6 @@ impl ws::Receiver for Receiver {
132132
}
133133
}
134134

135-
// TODO: this is slow, easy fix
136-
let buffer = self.buffer.clone();
137-
self.buffer.clear();
138-
139-
Ok(buffer)
135+
Ok(::std::mem::replace(&mut self.buffer, Vec::new()))
140136
}
141137
}

0 commit comments

Comments
 (0)