From 4023390d386341576281e5929cfff919c251f2d8 Mon Sep 17 00:00:00 2001 From: Icelk Date: Tue, 30 Aug 2022 09:22:38 +0200 Subject: [PATCH] Make handshake dependencies optional. --- Cargo.toml | 13 +++++++------ src/error.rs | 12 ++++++++++++ src/lib.rs | 12 ++++++++++-- tests/no_send_after_close.rs | 2 ++ tests/receive_after_init_close.rs | 2 ++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a631fe..8802ae0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,8 @@ include = ["benches/**/*", "src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"] all-features = true [features] -default = [] +default = ["handshake"] +handshake = ["base64", "http", "httparse", "sha1", "url"] native-tls = ["native-tls-crate"] native-tls-vendored = ["native-tls", "native-tls-crate/vendored"] 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"] [dependencies] -base64 = "0.13.0" +base64 = { version = "0.13.0", optional = true } byteorder = "1.3.2" bytes = "1.0" -http = "0.2" -httparse = "1.3.4" +http = { version = "0.2", optional = true } +httparse = { version = "1.3.4", optional = true } log = "0.4.8" rand = "0.8.0" -sha1 = "0.10" +sha1 = { version = "0.10", optional = true } thiserror = "1.0.23" -url = "2.1.0" +url = { version = "2.1.0", optional = true } utf-8 = "0.7.5" [dependencies.native-tls-crate] diff --git a/src/error.rs b/src/error.rs index c025080..3e15c7c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,6 +3,7 @@ use std::{io, result, str, string}; use crate::protocol::{frame::coding::Data, Message}; +#[cfg(feature = "handshake")] use http::{header::HeaderName, Response}; use thiserror::Error; @@ -63,9 +64,11 @@ pub enum Error { Url(#[from] UrlError), /// HTTP error. #[error("HTTP error: {}", .0.status())] + #[cfg(feature = "handshake")] Http(Response>), /// HTTP format error. #[error("HTTP format error: {0}")] + #[cfg(feature = "handshake")] HttpFormat(#[from] http::Error), } @@ -81,36 +84,42 @@ impl From for Error { } } +#[cfg(feature = "handshake")] impl From for Error { fn from(err: http::header::InvalidHeaderValue) -> Self { Error::HttpFormat(err.into()) } } +#[cfg(feature = "handshake")] impl From for Error { fn from(err: http::header::InvalidHeaderName) -> Self { Error::HttpFormat(err.into()) } } +#[cfg(feature = "handshake")] impl From for Error { fn from(_: http::header::ToStrError) -> Self { Error::Utf8 } } +#[cfg(feature = "handshake")] impl From for Error { fn from(err: http::uri::InvalidUri) -> Self { Error::HttpFormat(err.into()) } } +#[cfg(feature = "handshake")] impl From for Error { fn from(err: http::status::InvalidStatusCode) -> Self { Error::HttpFormat(err.into()) } } +#[cfg(feature = "handshake")] impl From for Error { fn from(err: httparse::Error) -> Self { match err { @@ -138,6 +147,7 @@ pub enum CapacityError { } /// Indicates the specific type/cause of a protocol error. +#[allow(missing_copy_implementations)] #[derive(Error, Debug, PartialEq, Eq, Clone)] pub enum ProtocolError { /// Use of the wrong HTTP method (the WebSocket protocol requires the GET method be used). @@ -169,12 +179,14 @@ pub enum ProtocolError { CustomResponseSuccessful, /// 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}")] + #[cfg(feature = "handshake")] InvalidHeader(HeaderName), /// No more data while still performing handshake. #[error("Handshake not finished")] HandshakeIncomplete, /// Wrapper around a [`httparse::Error`] value. #[error("httparse error: {0}")] + #[cfg(feature = "handshake")] HttparseError(#[from] httparse::Error), /// Not allowed to send after having sent a closing frame. #[error("Sending after closing is not allowed")] diff --git a/src/lib.rs b/src/lib.rs index c2f29c2..ec2f828 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,13 +12,17 @@ unused_import_braces )] +#[cfg(feature = "handshake")] pub use http; pub mod buffer; +#[cfg(feature = "handshake")] pub mod client; pub mod error; +#[cfg(feature = "handshake")] pub mod handshake; pub mod protocol; +#[cfg(feature = "handshake")] mod server; pub mod stream; #[cfg(any(feature = "native-tls", feature = "__rustls-tls"))] @@ -29,10 +33,14 @@ const READ_BUFFER_CHUNK_SIZE: usize = 4096; type ReadBuffer = buffer::ReadBuffer; pub use crate::{ - client::{client, connect}, error::{Error, Result}, - handshake::{client::ClientHandshake, server::ServerHandshake, HandshakeError}, 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}, }; diff --git a/tests/no_send_after_close.rs b/tests/no_send_after_close.rs index a9dcab2..c19b1da 100644 --- a/tests/no_send_after_close.rs +++ b/tests/no_send_after_close.rs @@ -8,10 +8,12 @@ use std::{ time::Duration, }; +#[cfg(feature = "handshake")] use tungstenite::{accept, connect, error::ProtocolError, Error, Message}; use url::Url; #[test] +#[cfg(feature = "handshake")] fn test_no_send_after_close() { env_logger::init(); diff --git a/tests/receive_after_init_close.rs b/tests/receive_after_init_close.rs index 87f8dda..4a8e3a4 100644 --- a/tests/receive_after_init_close.rs +++ b/tests/receive_after_init_close.rs @@ -8,10 +8,12 @@ use std::{ time::Duration, }; +#[cfg(feature = "handshake")] use tungstenite::{accept, connect, Error, Message}; use url::Url; #[test] +#[cfg(feature = "handshake")] fn test_receive_after_init_close() { env_logger::init();