Make handshake dependencies optional.

This commit is contained in:
Icelk 2022-08-30 09:22:38 +02:00
parent 1978a1b5ff
commit 4023390d38
5 changed files with 33 additions and 8 deletions

View File

@ -18,7 +18,8 @@ include = ["benches/**/*", "src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"]
all-features = true all-features = true
[features] [features]
default = [] default = ["handshake"]
handshake = ["base64", "http", "httparse", "sha1", "url"]
native-tls = ["native-tls-crate"] native-tls = ["native-tls-crate"]
native-tls-vendored = ["native-tls", "native-tls-crate/vendored"] native-tls-vendored = ["native-tls", "native-tls-crate/vendored"]
rustls-tls-native-roots = ["__rustls-tls", "rustls-native-certs"] rustls-tls-native-roots = ["__rustls-tls", "rustls-native-certs"]
@ -26,16 +27,16 @@ rustls-tls-webpki-roots = ["__rustls-tls", "webpki-roots"]
__rustls-tls = ["rustls", "webpki"] __rustls-tls = ["rustls", "webpki"]
[dependencies] [dependencies]
base64 = "0.13.0" base64 = { version = "0.13.0", optional = true }
byteorder = "1.3.2" byteorder = "1.3.2"
bytes = "1.0" bytes = "1.0"
http = "0.2" http = { version = "0.2", optional = true }
httparse = "1.3.4" httparse = { version = "1.3.4", optional = true }
log = "0.4.8" log = "0.4.8"
rand = "0.8.0" rand = "0.8.0"
sha1 = "0.10" sha1 = { version = "0.10", optional = true }
thiserror = "1.0.23" thiserror = "1.0.23"
url = "2.1.0" url = { version = "2.1.0", optional = true }
utf-8 = "0.7.5" utf-8 = "0.7.5"
[dependencies.native-tls-crate] [dependencies.native-tls-crate]

View File

@ -3,6 +3,7 @@
use std::{io, result, str, string}; use std::{io, result, str, string};
use crate::protocol::{frame::coding::Data, Message}; use crate::protocol::{frame::coding::Data, Message};
#[cfg(feature = "handshake")]
use http::{header::HeaderName, Response}; use http::{header::HeaderName, Response};
use thiserror::Error; use thiserror::Error;
@ -63,9 +64,11 @@ pub enum Error {
Url(#[from] UrlError), Url(#[from] UrlError),
/// HTTP error. /// HTTP error.
#[error("HTTP error: {}", .0.status())] #[error("HTTP error: {}", .0.status())]
#[cfg(feature = "handshake")]
Http(Response<Option<String>>), Http(Response<Option<String>>),
/// HTTP format error. /// HTTP format error.
#[error("HTTP format error: {0}")] #[error("HTTP format error: {0}")]
#[cfg(feature = "handshake")]
HttpFormat(#[from] http::Error), HttpFormat(#[from] http::Error),
} }
@ -81,36 +84,42 @@ impl From<string::FromUtf8Error> for Error {
} }
} }
#[cfg(feature = "handshake")]
impl From<http::header::InvalidHeaderValue> for Error { impl From<http::header::InvalidHeaderValue> for Error {
fn from(err: http::header::InvalidHeaderValue) -> Self { fn from(err: http::header::InvalidHeaderValue) -> Self {
Error::HttpFormat(err.into()) Error::HttpFormat(err.into())
} }
} }
#[cfg(feature = "handshake")]
impl From<http::header::InvalidHeaderName> for Error { impl From<http::header::InvalidHeaderName> for Error {
fn from(err: http::header::InvalidHeaderName) -> Self { fn from(err: http::header::InvalidHeaderName) -> Self {
Error::HttpFormat(err.into()) Error::HttpFormat(err.into())
} }
} }
#[cfg(feature = "handshake")]
impl From<http::header::ToStrError> for Error { impl From<http::header::ToStrError> for Error {
fn from(_: http::header::ToStrError) -> Self { fn from(_: http::header::ToStrError) -> Self {
Error::Utf8 Error::Utf8
} }
} }
#[cfg(feature = "handshake")]
impl From<http::uri::InvalidUri> for Error { impl From<http::uri::InvalidUri> for Error {
fn from(err: http::uri::InvalidUri) -> Self { fn from(err: http::uri::InvalidUri) -> Self {
Error::HttpFormat(err.into()) Error::HttpFormat(err.into())
} }
} }
#[cfg(feature = "handshake")]
impl From<http::status::InvalidStatusCode> for Error { impl From<http::status::InvalidStatusCode> for Error {
fn from(err: http::status::InvalidStatusCode) -> Self { fn from(err: http::status::InvalidStatusCode) -> Self {
Error::HttpFormat(err.into()) Error::HttpFormat(err.into())
} }
} }
#[cfg(feature = "handshake")]
impl From<httparse::Error> for Error { impl From<httparse::Error> for Error {
fn from(err: httparse::Error) -> Self { fn from(err: httparse::Error) -> Self {
match err { match err {
@ -138,6 +147,7 @@ pub enum CapacityError {
} }
/// Indicates the specific type/cause of a protocol error. /// Indicates the specific type/cause of a protocol error.
#[allow(missing_copy_implementations)]
#[derive(Error, Debug, PartialEq, Eq, Clone)] #[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum ProtocolError { pub enum ProtocolError {
/// Use of the wrong HTTP method (the WebSocket protocol requires the GET method be used). /// Use of the wrong HTTP method (the WebSocket protocol requires the GET method be used).
@ -169,12 +179,14 @@ pub enum ProtocolError {
CustomResponseSuccessful, CustomResponseSuccessful,
/// Invalid header is passed. Or the header is missing in the request. Or not present at all. Check the request that you pass. /// Invalid header is passed. Or the header is missing in the request. Or not present at all. Check the request that you pass.
#[error("Missing, duplicated or incorrect header {0}")] #[error("Missing, duplicated or incorrect header {0}")]
#[cfg(feature = "handshake")]
InvalidHeader(HeaderName), InvalidHeader(HeaderName),
/// No more data while still performing handshake. /// No more data while still performing handshake.
#[error("Handshake not finished")] #[error("Handshake not finished")]
HandshakeIncomplete, HandshakeIncomplete,
/// Wrapper around a [`httparse::Error`] value. /// Wrapper around a [`httparse::Error`] value.
#[error("httparse error: {0}")] #[error("httparse error: {0}")]
#[cfg(feature = "handshake")]
HttparseError(#[from] httparse::Error), HttparseError(#[from] httparse::Error),
/// Not allowed to send after having sent a closing frame. /// Not allowed to send after having sent a closing frame.
#[error("Sending after closing is not allowed")] #[error("Sending after closing is not allowed")]

View File

@ -12,13 +12,17 @@
unused_import_braces unused_import_braces
)] )]
#[cfg(feature = "handshake")]
pub use http; pub use http;
pub mod buffer; pub mod buffer;
#[cfg(feature = "handshake")]
pub mod client; pub mod client;
pub mod error; pub mod error;
#[cfg(feature = "handshake")]
pub mod handshake; pub mod handshake;
pub mod protocol; pub mod protocol;
#[cfg(feature = "handshake")]
mod server; mod server;
pub mod stream; pub mod stream;
#[cfg(any(feature = "native-tls", feature = "__rustls-tls"))] #[cfg(any(feature = "native-tls", feature = "__rustls-tls"))]
@ -29,10 +33,14 @@ const READ_BUFFER_CHUNK_SIZE: usize = 4096;
type ReadBuffer = buffer::ReadBuffer<READ_BUFFER_CHUNK_SIZE>; type ReadBuffer = buffer::ReadBuffer<READ_BUFFER_CHUNK_SIZE>;
pub use crate::{ pub use crate::{
client::{client, connect},
error::{Error, Result}, error::{Error, Result},
handshake::{client::ClientHandshake, server::ServerHandshake, HandshakeError},
protocol::{Message, WebSocket}, protocol::{Message, WebSocket},
};
#[cfg(feature = "handshake")]
pub use crate::{
client::{client, connect},
handshake::{client::ClientHandshake, server::ServerHandshake, HandshakeError},
server::{accept, accept_hdr, accept_hdr_with_config, accept_with_config}, server::{accept, accept_hdr, accept_hdr_with_config, accept_with_config},
}; };

View File

@ -8,10 +8,12 @@ use std::{
time::Duration, time::Duration,
}; };
#[cfg(feature = "handshake")]
use tungstenite::{accept, connect, error::ProtocolError, Error, Message}; use tungstenite::{accept, connect, error::ProtocolError, Error, Message};
use url::Url; use url::Url;
#[test] #[test]
#[cfg(feature = "handshake")]
fn test_no_send_after_close() { fn test_no_send_after_close() {
env_logger::init(); env_logger::init();

View File

@ -8,10 +8,12 @@ use std::{
time::Duration, time::Duration,
}; };
#[cfg(feature = "handshake")]
use tungstenite::{accept, connect, Error, Message}; use tungstenite::{accept, connect, Error, Message};
use url::Url; use url::Url;
#[test] #[test]
#[cfg(feature = "handshake")]
fn test_receive_after_init_close() { fn test_receive_after_init_close() {
env_logger::init(); env_logger::init();