Skip to content

Commit

Permalink
Finished docs and updated tests, removed warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
illegalprime committed May 28, 2017
1 parent 8764318 commit 2307070
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 187 deletions.
12 changes: 6 additions & 6 deletions autobahn/client-results.json
Expand Up @@ -1723,7 +1723,7 @@
"reportfile": "rust_websocket_case_3_2.json"
},
"3.3": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2,
"remoteCloseCode": null,
Expand Down Expand Up @@ -2773,28 +2773,28 @@
"reportfile": "rust_websocket_case_6_3_2.json"
},
"6.4.1": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2002,
"remoteCloseCode": null,
"reportfile": "rust_websocket_case_6_4_1.json"
},
"6.4.2": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2002,
"remoteCloseCode": null,
"reportfile": "rust_websocket_case_6_4_2.json"
},
"6.4.3": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2003,
"remoteCloseCode": null,
"reportfile": "rust_websocket_case_6_4_3.json"
},
"6.4.4": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2002,
"remoteCloseCode": null,
Expand Down Expand Up @@ -3634,4 +3634,4 @@
"reportfile": "rust_websocket_case_9_8_6.json"
}
}
}
}
10 changes: 5 additions & 5 deletions autobahn/server-results.json
Expand Up @@ -2773,28 +2773,28 @@
"reportfile": "rust_websocket_case_6_3_2.json"
},
"6.4.1": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2002,
"remoteCloseCode": null,
"reportfile": "rust_websocket_case_6_4_1.json"
},
"6.4.2": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2001,
"remoteCloseCode": null,
"reportfile": "rust_websocket_case_6_4_2.json"
},
"6.4.3": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2003,
"remoteCloseCode": null,
"reportfile": "rust_websocket_case_6_4_3.json"
},
"6.4.4": {
"behavior": "NON-STRICT",
"behavior": "OK",
"behaviorClose": "OK",
"duration": 2003,
"remoteCloseCode": null,
Expand Down Expand Up @@ -3634,4 +3634,4 @@
"reportfile": "rust_websocket_case_9_8_6.json"
}
}
}
}
3 changes: 2 additions & 1 deletion scripts/autobahn-client.sh
Expand Up @@ -16,8 +16,9 @@ wstest -m fuzzingserver -s 'autobahn/fuzzingserver.json' & \
sleep 10

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

if [[ $DIFF ]]; then
Expand Down
3 changes: 2 additions & 1 deletion scripts/autobahn-server.sh
Expand Up @@ -13,8 +13,9 @@ function cleanup() {
trap cleanup TERM EXIT

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

if [[ $DIFF ]]; then
Expand Down
50 changes: 31 additions & 19 deletions src/client/builder.rs
@@ -1,26 +1,32 @@
//! Everything you need to create a client connection to a websocket.

use std::borrow::Cow;
use std::net::TcpStream;
use std::net::ToSocketAddrs;
pub use url::{Url, ParseError};
use url::Position;
use hyper::version::HttpVersion;
use hyper::http::h1::Incoming;
use hyper::http::RawStatus;
use hyper::status::StatusCode;
use hyper::buffer::BufReader;
use hyper::method::Method;
use hyper::uri::RequestUri;
use hyper::http::h1::parse_response;
use hyper::header::{Headers, Header, HeaderFormat, Host, Connection, ConnectionOption, Upgrade,
Protocol, ProtocolName};
use unicase::UniCase;
use header::extensions::Extension;
use header::{WebSocketAccept, WebSocketKey, WebSocketVersion, WebSocketProtocol,
WebSocketExtensions, Origin};
use result::{WSUrlErrorKind, WebSocketResult, WebSocketError};
use stream::{self, Stream};
use header::{WebSocketKey, WebSocketVersion, WebSocketProtocol, WebSocketExtensions, Origin};
use hyper::header::{Headers, Header, HeaderFormat};
use hyper::version::HttpVersion;

#[cfg(any(feature="sync", feature="async"))]
mod common_imports {
pub use std::net::TcpStream;
pub use std::net::ToSocketAddrs;
pub use url::Position;
pub use hyper::http::h1::Incoming;
pub use hyper::http::RawStatus;
pub use hyper::status::StatusCode;
pub use hyper::buffer::BufReader;
pub use hyper::method::Method;
pub use hyper::uri::RequestUri;
pub use hyper::http::h1::parse_response;
pub use hyper::header::{Host, Connection, ConnectionOption, Upgrade, Protocol, ProtocolName};
pub use unicase::UniCase;
pub use header::WebSocketAccept;
pub use result::{WSUrlErrorKind, WebSocketResult, WebSocketError};
pub use stream::{self, Stream};
}
#[cfg(any(feature="sync", feature="async"))]
use self::common_imports::*;

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

#[cfg(any(feature="sync-ssl", feature="async-ssl"))]
use native_tls::{TlsStream, TlsConnector};
use native_tls::TlsConnector;
#[cfg(feature="sync-ssl")]
use native_tls::TlsStream;

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

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

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

Expand Down Expand Up @@ -890,6 +900,7 @@ impl<'u> ClientBuilder<'u> {
Ok(())
}

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

#[cfg(feature="sync")]
fn establish_tcp(&mut self, secure: Option<bool>) -> WebSocketResult<TcpStream> {
Ok(TcpStream::connect(self.extract_host_port(secure)?)?)
}
Expand Down
16 changes: 9 additions & 7 deletions src/codec/ws.rs
Expand Up @@ -364,7 +364,7 @@ mod tests {
fn message_codec_client_send_receive() {
let mut core = Core::new().unwrap();
let mut input = Vec::new();
Message::text("50 schmeckels").serialize(&mut input, false);
Message::text("50 schmeckels").serialize(&mut input, false).unwrap();

let f = ReadWritePair(Cursor::new(input), Cursor::new(vec![]))
.framed(MessageCodec::new(Context::Client))
Expand All @@ -375,15 +375,17 @@ mod tests {
s
})
.and_then(|s| s.send(Message::text("ethan bradberry")))
.map(|s| {
let stream = s.into_parts().inner;
.and_then(|s| {
let mut stream = s.into_parts().inner;
stream.1.set_position(0);
println!("buffer: {:?}", stream.1);
ReadWritePair(stream.1, stream.0)
.framed(MessageCodec::default(Context::Client))
.framed(MessageCodec::default(Context::Server))
.into_future()
.map_err(|e| e.0)
.map(|(message, _)| {
assert_eq!(message, Some(Message::text("ethan bradberry").into()))
});
})
});

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

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

Expand Down
40 changes: 19 additions & 21 deletions src/lib.rs
Expand Up @@ -5,33 +5,25 @@

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

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

/// A collection of handy synchronous-only parts of the `server` module.
pub mod server {
pub use server::sync::*;
pub use server::upgrade::sync::Upgrade;
Expand All @@ -114,13 +108,15 @@ pub mod sync {
}
pub use server::sync::Server;

/// A collection of handy synchronous-only parts of the `client` module.
pub mod client {
pub use client::sync::*;
pub use client::builder::ClientBuilder;
}
pub use client::sync::Client;
}

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

/// A collection of handy asynchronous-only parts of the `server` module.
pub mod server {
pub use server::async::*;
pub use server::upgrade::async::Upgrade;
Expand All @@ -140,6 +137,7 @@ pub mod async {
}
pub use server::async::Server;

/// A collection of handy asynchronous-only parts of the `client` module.
pub mod client {
pub use client::async::*;
pub use client::builder::ClientBuilder;
Expand Down

0 comments on commit 2307070

Please sign in to comment.