use std::fmt;
#[cfg(feature = "server")]
pub(crate) mod client_request;
#[cfg(feature = "client")]
pub(crate) mod server_response;
#[cfg(feature = "client")]
pub type Response = http::Response<()>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
MissingHeader(&'static str),
UpgradeNotWebSocket,
ConnectionNotUpgrade,
UnsupportedWebSocketVersion,
Parsing(httparse::Error),
DidNotSwitchProtocols(u16),
WrongWebSocketAccept,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::MissingHeader(header) => {
f.write_str("missing required header: ")?;
f.write_str(header)
}
Error::UpgradeNotWebSocket => f.write_str("upgrade header value was not websocket"),
Error::ConnectionNotUpgrade => f.write_str("connection header value was not upgrade"),
Error::UnsupportedWebSocketVersion => f.write_str("unsupported WebSocket version"),
Error::Parsing(e) => e.fmt(f),
Error::DidNotSwitchProtocols(status) => {
f.write_str("expected HTTP 101 Switching Protocols, got status code ")?;
f.write_fmt(format_args!("{status}"))
}
Error::WrongWebSocketAccept => f.write_str("mismatching Sec-WebSocket-Accept header"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::MissingHeader(_)
| Error::UpgradeNotWebSocket
| Error::ConnectionNotUpgrade
| Error::UnsupportedWebSocketVersion
| Error::DidNotSwitchProtocols(_)
| Error::WrongWebSocketAccept => None,
Error::Parsing(e) => Some(e),
}
}
}
impl From<httparse::Error> for Error {
fn from(err: httparse::Error) -> Self {
Self::Parsing(err)
}
}