websocket/
lib.rs

1#![warn(missing_docs)]
2#![cfg_attr(all(test, feature = "nightly"), feature(test))]
3#![allow(
4	clippy::write_with_newline,
5	clippy::type_complexity,
6	clippy::match_ref_pats,
7	clippy::needless_doctest_main
8)]
9#![deny(unused_mut)]
10
11//! Rust-WebSocket is a WebSocket (RFC6455) library written in Rust.
12//!
13//! # Synchronous and Asynchronous
14//! This crate has both async and sync implementations of websockets, you are free to
15//! choose which one you would like to use by switching on the `async` or `sync` features
16//! for this crate. By default both are switched on since they do not conflict with each
17//! other.
18//!
19//! You'll find many modules with `::sync` and `::async` submodules that separate these
20//! behaviours. Since it gets tedious to add these on when appropriate, a top-level
21//! convenience module called `websocket::sync` and `websocket::async` has been added that
22//! groups all the sync and async stuff, respectively.
23//!
24//! # Clients
25//! To make a client use the `ClientBuilder` struct, this builder has methods
26//! for creating both synchronous and asynchronous clients.
27//!
28//! # Servers
29//! WebSocket servers act similarly to the `TcpListener`, and listen for connections.
30//! See the `Server` struct documentation for more information. The `bind()` and
31//! `bind_secure()` functions will bind the server to the given `SocketAddr`.
32//!
33//! # Extending Rust-WebSocket
34//! The `ws` module contains the traits and functions used by Rust-WebSocket at a lower
35//! level. Their usage is explained in the module documentation.
36#[cfg(feature = "async")]
37extern crate bytes;
38#[cfg(feature = "async")]
39pub extern crate futures;
40extern crate hyper;
41#[cfg(any(feature = "sync-ssl", feature = "async-ssl"))]
42pub extern crate native_tls;
43#[cfg(test)]
44extern crate tokio;
45#[cfg(feature = "async")]
46extern crate tokio_codec;
47#[cfg(feature = "async")]
48extern crate tokio_io;
49#[cfg(feature = "async")]
50extern crate tokio_reactor;
51#[cfg(feature = "async")]
52extern crate tokio_tcp;
53#[cfg(feature = "async-ssl")]
54extern crate tokio_tls;
55extern crate unicase;
56pub extern crate url;
57
58pub extern crate websocket_base;
59
60#[cfg(all(feature = "nightly", test))]
61extern crate test;
62
63macro_rules! upsert_header {
64	($headers:expr; $header:ty;  { Some($pat:pat) => $some_match:expr,None => $default:expr }) => {{
65		if $headers.has::<$header>() {
66			if let Some($pat) = $headers.get_mut::<$header>() {
67				$some_match
68			}
69		} else {
70			$headers.set($default);
71		}
72	}};
73}
74
75pub use websocket_base::dataframe;
76pub mod header;
77pub use websocket_base::message;
78pub mod result;
79pub use websocket_base::ws;
80
81#[cfg(feature = "async")]
82pub mod codec;
83
84#[cfg(feature = "sync")]
85pub mod receiver;
86#[cfg(feature = "sync")]
87pub mod sender;
88
89pub mod client;
90pub mod server;
91pub use websocket_base::stream;
92
93/// A collection of handy synchronous-only parts of the crate.
94#[cfg(feature = "sync")]
95pub mod sync {
96	pub use crate::sender;
97	pub use crate::sender::Writer;
98
99	pub use crate::receiver;
100	pub use crate::receiver::Reader;
101
102	pub use crate::stream::sync as stream;
103	pub use crate::stream::sync::Stream;
104
105	/// A collection of handy synchronous-only parts of the `server` module.
106	pub mod server {
107		pub use crate::server::sync::*;
108		pub use crate::server::upgrade::sync as upgrade;
109		pub use crate::server::upgrade::sync::IntoWs;
110		pub use crate::server::upgrade::sync::Upgrade;
111	}
112	pub use crate::server::sync::Server;
113
114	/// A collection of handy synchronous-only parts of the `client` module.
115	pub mod client {
116		pub use crate::client::builder::ClientBuilder;
117		pub use crate::client::sync::*;
118	}
119	pub use crate::client::sync::Client;
120}
121
122/// A collection of handy asynchronous-only parts of the crate.
123#[cfg(feature = "async")]
124pub mod r#async {
125	pub use crate::codec;
126	pub use crate::codec::http::HttpClientCodec;
127	pub use crate::codec::http::HttpServerCodec;
128	pub use crate::codec::ws::Context as MsgCodecCtx;
129	pub use crate::codec::ws::MessageCodec;
130
131	pub use crate::stream::r#async as stream;
132	pub use crate::stream::r#async::Stream;
133
134	/// A collection of handy asynchronous-only parts of the `server` module.
135	pub mod server {
136		pub use crate::server::r#async::*;
137		pub use crate::server::upgrade::r#async as upgrade;
138		pub use crate::server::upgrade::r#async::IntoWs;
139		pub use crate::server::upgrade::r#async::Upgrade;
140	}
141	pub use crate::server::r#async::Server;
142
143	/// A collection of handy asynchronous-only parts of the `client` module.
144	pub mod client {
145		pub use crate::client::builder::ClientBuilder;
146		pub use crate::client::r#async::*;
147	}
148	pub use crate::client::r#async::Client;
149
150	pub use crate::result::r#async::WebSocketFuture;
151
152	pub use futures;
153	pub use tokio_reactor::Handle;
154	pub use tokio_tcp::TcpListener;
155	pub use tokio_tcp::TcpStream;
156}
157
158pub use self::client::builder::ClientBuilder;
159pub use self::message::CloseData;
160pub use self::message::Message;
161pub use self::message::OwnedMessage;
162
163pub use self::result::WebSocketError;
164pub use self::result::WebSocketResult;