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
[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]

View File

@ -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<Option<String>>),
/// HTTP format error.
#[error("HTTP format error: {0}")]
#[cfg(feature = "handshake")]
HttpFormat(#[from] http::Error),
}
@ -81,36 +84,42 @@ impl From<string::FromUtf8Error> for Error {
}
}
#[cfg(feature = "handshake")]
impl From<http::header::InvalidHeaderValue> for Error {
fn from(err: http::header::InvalidHeaderValue) -> Self {
Error::HttpFormat(err.into())
}
}
#[cfg(feature = "handshake")]
impl From<http::header::InvalidHeaderName> for Error {
fn from(err: http::header::InvalidHeaderName) -> Self {
Error::HttpFormat(err.into())
}
}
#[cfg(feature = "handshake")]
impl From<http::header::ToStrError> for Error {
fn from(_: http::header::ToStrError) -> Self {
Error::Utf8
}
}
#[cfg(feature = "handshake")]
impl From<http::uri::InvalidUri> for Error {
fn from(err: http::uri::InvalidUri) -> Self {
Error::HttpFormat(err.into())
}
}
#[cfg(feature = "handshake")]
impl From<http::status::InvalidStatusCode> for Error {
fn from(err: http::status::InvalidStatusCode) -> Self {
Error::HttpFormat(err.into())
}
}
#[cfg(feature = "handshake")]
impl From<httparse::Error> 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")]

View File

@ -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<READ_BUFFER_CHUNK_SIZE>;
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},
};

View File

@ -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();

View File

@ -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();