Skip to content

Commit 2307070

Browse files
committed
Finished docs and updated tests, removed warnings.
1 parent 8764318 commit 2307070

File tree

12 files changed

+277
-187
lines changed

12 files changed

+277
-187
lines changed

autobahn/client-results.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@
17231723
"reportfile": "rust_websocket_case_3_2.json"
17241724
},
17251725
"3.3": {
1726-
"behavior": "NON-STRICT",
1726+
"behavior": "OK",
17271727
"behaviorClose": "OK",
17281728
"duration": 2,
17291729
"remoteCloseCode": null,
@@ -2773,28 +2773,28 @@
27732773
"reportfile": "rust_websocket_case_6_3_2.json"
27742774
},
27752775
"6.4.1": {
2776-
"behavior": "NON-STRICT",
2776+
"behavior": "OK",
27772777
"behaviorClose": "OK",
27782778
"duration": 2002,
27792779
"remoteCloseCode": null,
27802780
"reportfile": "rust_websocket_case_6_4_1.json"
27812781
},
27822782
"6.4.2": {
2783-
"behavior": "NON-STRICT",
2783+
"behavior": "OK",
27842784
"behaviorClose": "OK",
27852785
"duration": 2002,
27862786
"remoteCloseCode": null,
27872787
"reportfile": "rust_websocket_case_6_4_2.json"
27882788
},
27892789
"6.4.3": {
2790-
"behavior": "NON-STRICT",
2790+
"behavior": "OK",
27912791
"behaviorClose": "OK",
27922792
"duration": 2003,
27932793
"remoteCloseCode": null,
27942794
"reportfile": "rust_websocket_case_6_4_3.json"
27952795
},
27962796
"6.4.4": {
2797-
"behavior": "NON-STRICT",
2797+
"behavior": "OK",
27982798
"behaviorClose": "OK",
27992799
"duration": 2002,
28002800
"remoteCloseCode": null,
@@ -3634,4 +3634,4 @@
36343634
"reportfile": "rust_websocket_case_9_8_6.json"
36353635
}
36363636
}
3637-
}
3637+
}

autobahn/server-results.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,28 +2773,28 @@
27732773
"reportfile": "rust_websocket_case_6_3_2.json"
27742774
},
27752775
"6.4.1": {
2776-
"behavior": "NON-STRICT",
2776+
"behavior": "OK",
27772777
"behaviorClose": "OK",
27782778
"duration": 2002,
27792779
"remoteCloseCode": null,
27802780
"reportfile": "rust_websocket_case_6_4_1.json"
27812781
},
27822782
"6.4.2": {
2783-
"behavior": "NON-STRICT",
2783+
"behavior": "OK",
27842784
"behaviorClose": "OK",
27852785
"duration": 2001,
27862786
"remoteCloseCode": null,
27872787
"reportfile": "rust_websocket_case_6_4_2.json"
27882788
},
27892789
"6.4.3": {
2790-
"behavior": "NON-STRICT",
2790+
"behavior": "OK",
27912791
"behaviorClose": "OK",
27922792
"duration": 2003,
27932793
"remoteCloseCode": null,
27942794
"reportfile": "rust_websocket_case_6_4_3.json"
27952795
},
27962796
"6.4.4": {
2797-
"behavior": "NON-STRICT",
2797+
"behavior": "OK",
27982798
"behaviorClose": "OK",
27992799
"duration": 2003,
28002800
"remoteCloseCode": null,
@@ -3634,4 +3634,4 @@
36343634
"reportfile": "rust_websocket_case_9_8_6.json"
36353635
}
36363636
}
3637-
}
3637+
}

scripts/autobahn-client.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ wstest -m fuzzingserver -s 'autobahn/fuzzingserver.json' & \
1616
sleep 10
1717

1818
function test_diff() {
19+
SAVED_RESULTS=$(sed 's/NON-STRICT/OK/g' autobahn/client-results.json)
1920
DIFF=$(diff \
20-
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/client-results.json') \
21+
<(jq -S 'del(."rust-websocket" | .. | .duration?)' "$SAVED_RESULTS") \
2122
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/client/index.json') )
2223

2324
if [[ $DIFF ]]; then

scripts/autobahn-server.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ function cleanup() {
1313
trap cleanup TERM EXIT
1414

1515
function test_diff() {
16+
SAVED_RESULTS=$(sed 's/NON-STRICT/OK/g' autobahn/server-results.json)
1617
DIFF=$(diff \
17-
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/server-results.json') \
18+
<(jq -S 'del(."rust-websocket" | .. | .duration?)' "$SAVED_RESULTS") \
1819
<(jq -S 'del(."rust-websocket" | .. | .duration?)' 'autobahn/server/index.json') )
1920

2021
if [[ $DIFF ]]; then

src/client/builder.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
//! Everything you need to create a client connection to a websocket.
22
33
use std::borrow::Cow;
4-
use std::net::TcpStream;
5-
use std::net::ToSocketAddrs;
64
pub use url::{Url, ParseError};
7-
use url::Position;
8-
use hyper::version::HttpVersion;
9-
use hyper::http::h1::Incoming;
10-
use hyper::http::RawStatus;
11-
use hyper::status::StatusCode;
12-
use hyper::buffer::BufReader;
13-
use hyper::method::Method;
14-
use hyper::uri::RequestUri;
15-
use hyper::http::h1::parse_response;
16-
use hyper::header::{Headers, Header, HeaderFormat, Host, Connection, ConnectionOption, Upgrade,
17-
Protocol, ProtocolName};
18-
use unicase::UniCase;
195
use header::extensions::Extension;
20-
use header::{WebSocketAccept, WebSocketKey, WebSocketVersion, WebSocketProtocol,
21-
WebSocketExtensions, Origin};
22-
use result::{WSUrlErrorKind, WebSocketResult, WebSocketError};
23-
use stream::{self, Stream};
6+
use header::{WebSocketKey, WebSocketVersion, WebSocketProtocol, WebSocketExtensions, Origin};
7+
use hyper::header::{Headers, Header, HeaderFormat};
8+
use hyper::version::HttpVersion;
9+
10+
#[cfg(any(feature="sync", feature="async"))]
11+
mod common_imports {
12+
pub use std::net::TcpStream;
13+
pub use std::net::ToSocketAddrs;
14+
pub use url::Position;
15+
pub use hyper::http::h1::Incoming;
16+
pub use hyper::http::RawStatus;
17+
pub use hyper::status::StatusCode;
18+
pub use hyper::buffer::BufReader;
19+
pub use hyper::method::Method;
20+
pub use hyper::uri::RequestUri;
21+
pub use hyper::http::h1::parse_response;
22+
pub use hyper::header::{Host, Connection, ConnectionOption, Upgrade, Protocol, ProtocolName};
23+
pub use unicase::UniCase;
24+
pub use header::WebSocketAccept;
25+
pub use result::{WSUrlErrorKind, WebSocketResult, WebSocketError};
26+
pub use stream::{self, Stream};
27+
}
28+
#[cfg(any(feature="sync", feature="async"))]
29+
use self::common_imports::*;
2430

2531
#[cfg(feature="sync")]
2632
use super::sync::Client;
@@ -29,7 +35,9 @@ use super::sync::Client;
2935
use stream::sync::NetworkStream;
3036

3137
#[cfg(any(feature="sync-ssl", feature="async-ssl"))]
32-
use native_tls::{TlsStream, TlsConnector};
38+
use native_tls::TlsConnector;
39+
#[cfg(feature="sync-ssl")]
40+
use native_tls::TlsStream;
3341

3442
#[cfg(feature="async")]
3543
mod async_imports {
@@ -819,6 +827,7 @@ impl<'u> ClientBuilder<'u> {
819827
Ok(async::TcpStream::connect(&address, handle))
820828
}
821829

830+
#[cfg(any(feature="sync", feature="async"))]
822831
fn build_request(&mut self) -> String {
823832
// enter host if available (unix sockets don't have hosts)
824833
if let Some(host) = self.url.host_str() {
@@ -855,6 +864,7 @@ impl<'u> ClientBuilder<'u> {
855864
resource
856865
}
857866

867+
#[cfg(any(feature="sync", feature="async"))]
858868
fn validate(&self, response: &Incoming<RawStatus>) -> WebSocketResult<()> {
859869
let status = StatusCode::from_u16(response.subject.0);
860870

@@ -890,6 +900,7 @@ impl<'u> ClientBuilder<'u> {
890900
Ok(())
891901
}
892902

903+
#[cfg(any(feature="sync", feature="async"))]
893904
fn extract_host_port(&self, secure: Option<bool>) -> WebSocketResult<(&str, u16)> {
894905
let port = match (self.url.port(), secure) {
895906
(Some(port), _) => port,
@@ -906,6 +917,7 @@ impl<'u> ClientBuilder<'u> {
906917
Ok((host, port))
907918
}
908919

920+
#[cfg(feature="sync")]
909921
fn establish_tcp(&mut self, secure: Option<bool>) -> WebSocketResult<TcpStream> {
910922
Ok(TcpStream::connect(self.extract_host_port(secure)?)?)
911923
}

src/codec/ws.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ mod tests {
364364
fn message_codec_client_send_receive() {
365365
let mut core = Core::new().unwrap();
366366
let mut input = Vec::new();
367-
Message::text("50 schmeckels").serialize(&mut input, false);
367+
Message::text("50 schmeckels").serialize(&mut input, false).unwrap();
368368

369369
let f = ReadWritePair(Cursor::new(input), Cursor::new(vec![]))
370370
.framed(MessageCodec::new(Context::Client))
@@ -375,15 +375,17 @@ mod tests {
375375
s
376376
})
377377
.and_then(|s| s.send(Message::text("ethan bradberry")))
378-
.map(|s| {
379-
let stream = s.into_parts().inner;
378+
.and_then(|s| {
379+
let mut stream = s.into_parts().inner;
380+
stream.1.set_position(0);
381+
println!("buffer: {:?}", stream.1);
380382
ReadWritePair(stream.1, stream.0)
381-
.framed(MessageCodec::default(Context::Client))
383+
.framed(MessageCodec::default(Context::Server))
382384
.into_future()
383385
.map_err(|e| e.0)
384386
.map(|(message, _)| {
385387
assert_eq!(message, Some(Message::text("ethan bradberry").into()))
386-
});
388+
})
387389
});
388390

389391
core.run(f).unwrap();
@@ -393,7 +395,7 @@ mod tests {
393395
fn message_codec_server_send_receive() {
394396
let mut core = Core::new().unwrap();
395397
let mut input = Vec::new();
396-
Message::text("50 schmeckels").serialize(&mut input, true);
398+
Message::text("50 schmeckels").serialize(&mut input, true).unwrap();
397399

398400
let f = ReadWritePair(Cursor::new(input.as_slice()), Cursor::new(vec![]))
399401
.framed(MessageCodec::new(Context::Server))
@@ -406,7 +408,7 @@ mod tests {
406408
.and_then(|s| s.send(Message::text("ethan bradberry")))
407409
.map(|s| {
408410
let mut written = vec![];
409-
Message::text("ethan bradberry").serialize(&mut written, false);
411+
Message::text("ethan bradberry").serialize(&mut written, false).unwrap();
410412
assert_eq!(written, s.into_parts().inner.1.into_inner());
411413
});
412414

src/lib.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,25 @@
55

66
//! Rust-WebSocket is a WebSocket (RFC6455) library written in Rust.
77
//!
8-
//! # Clients
9-
//! WebSocket clients make use of the `Client` object, which features two connection
10-
//! functions: `connect()` and `connect_ssl_context()`. See the `Client` struct
11-
//! documentation for more information. These both return a client-side `Request`
12-
//! object which is sent to the server with the `send()` method. The `Request` can
13-
//! be altered, typically using `Request.headers.set()` to add additional headers
14-
//! or change existing ones before calling `send()`.
8+
//! # Synchronous and Asynchronous
9+
//! This crate has both async and sync implementations of websockets, you are free to
10+
//! choose which one you would like to use by switching on the `async` or `sync` features
11+
//! for this crate. By default both are switched on since they do not conflict with each
12+
//! other.
13+
//!
14+
//! You'll find many modules with `::sync` and `::async` submodules that separate these
15+
//! behaviours. Since it get's tedious to add these on when appropriate a top-level
16+
//! convenience module called `websocket::sync` and `websocket::async` has been added that
17+
//! groups all the sync and async stuff, respectively.
1518
//!
16-
//! Calling `send()` on a `Request` will obtain a `Response`, which can be checked
17-
//! with the `validate()` method, which will return `Ok(())` if the response is a
18-
//! valid one. Data frames and messages can then be sent by obtaining a `Client`
19-
//! object with `begin()`.
19+
//! # Clients
20+
//! To make a client use the `ClientBuilder` struct, this builder has methods
21+
//! for creating both synchronous and asynchronous clients.
2022
//!
2123
//! # Servers
2224
//! WebSocket servers act similarly to the `TcpListener`, and listen for connections.
2325
//! See the `Server` struct documentation for more information. The `bind()` and
2426
//! `bind_secure()` functions will bind the server to the given `SocketAddr`.
25-
//! `Server` implements Iterator and can be used to iterate over incoming `Request`
26-
//! items.
27-
//!
28-
//! Requests can be validated using `validate()`, and other parts of the request may
29-
//! be examined (e.g. the Host header and/or the Origin header). A call to `accept()`
30-
//! or `fail()` will generate a `Response` which either accepts the connection, or
31-
//! denies it respectively.
32-
//!
33-
//! A `Response` can then be altered if necessary, and is sent with the 'send()`
34-
//! method, returning a `Client` ready to send and receive data frames or messages.
3527
//!
3628
//! # Extending Rust-WebSocket
3729
//! The `ws` module contains the traits and functions used by Rust-WebSockt at a lower
@@ -95,6 +87,7 @@ pub mod client;
9587
pub mod server;
9688
pub mod stream;
9789

90+
/// A collection of handy synchronous-only parts of the crate.
9891
#[cfg(feature="sync")]
9992
pub mod sync {
10093
pub use sender;
@@ -106,6 +99,7 @@ pub mod sync {
10699
pub use stream::sync::Stream;
107100
pub use stream::sync as stream;
108101

102+
/// A collection of handy synchronous-only parts of the `server` module.
109103
pub mod server {
110104
pub use server::sync::*;
111105
pub use server::upgrade::sync::Upgrade;
@@ -114,13 +108,15 @@ pub mod sync {
114108
}
115109
pub use server::sync::Server;
116110

111+
/// A collection of handy synchronous-only parts of the `client` module.
117112
pub mod client {
118113
pub use client::sync::*;
119114
pub use client::builder::ClientBuilder;
120115
}
121116
pub use client::sync::Client;
122117
}
123118

119+
/// A collection of handy asynchronous-only parts of the crate.
124120
#[cfg(feature="async")]
125121
pub mod async {
126122
pub use codec;
@@ -132,6 +128,7 @@ pub mod async {
132128
pub use stream::async::Stream;
133129
pub use stream::async as stream;
134130

131+
/// A collection of handy asynchronous-only parts of the `server` module.
135132
pub mod server {
136133
pub use server::async::*;
137134
pub use server::upgrade::async::Upgrade;
@@ -140,6 +137,7 @@ pub mod async {
140137
}
141138
pub use server::async::Server;
142139

140+
/// A collection of handy asynchronous-only parts of the `client` module.
143141
pub mod client {
144142
pub use client::async::*;
145143
pub use client::builder::ClientBuilder;

0 commit comments

Comments
 (0)