make websocket errors more verbose

This commit is contained in:
Toshit Chawda 2024-09-06 22:24:56 -07:00
parent 0768cb9502
commit d6c095fe7b
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
2 changed files with 37 additions and 29 deletions

View file

@ -71,15 +71,15 @@ pub enum EpoxyError {
#[error("HTTP ToStr: {0:?} ({0})")] #[error("HTTP ToStr: {0:?} ({0})")]
ToStr(#[from] http::header::ToStrError), ToStr(#[from] http::header::ToStrError),
#[cfg(feature = "full")] #[cfg(feature = "full")]
#[error("Fastwebsockets: {0:?} ({0})")]
FastWebSockets(#[from] fastwebsockets::WebSocketError),
#[cfg(feature = "full")]
#[error("Pemfile: {0:?} ({0})")] #[error("Pemfile: {0:?} ({0})")]
Pemfile(std::io::Error), Pemfile(std::io::Error),
#[cfg(feature = "full")] #[cfg(feature = "full")]
#[error("Webpki: {0:?} ({0})")] #[error("Webpki: {0:?} ({0})")]
Webpki(#[from] webpki::Error), Webpki(#[from] webpki::Error),
#[error("Wisp WebSocket failed to connect")]
WebSocketConnectFailed,
#[error("Custom wisp transport: {0}")] #[error("Custom wisp transport: {0}")]
WispTransport(String), WispTransport(String),
#[error("Invalid Wisp transport")] #[error("Invalid Wisp transport")]
@ -89,6 +89,22 @@ pub enum EpoxyError {
#[error("Wisp transport already closed")] #[error("Wisp transport already closed")]
WispTransportClosed, WispTransportClosed,
#[cfg(feature = "full")]
#[error("Fastwebsockets: {0:?} ({0})")]
FastWebSockets(#[from] fastwebsockets::WebSocketError),
#[cfg(feature = "full")]
#[error("Invalid websocket response status code: {0} != {1}")]
WsInvalidStatusCode(u16, u16),
#[cfg(feature = "full")]
#[error("Invalid websocket upgrade header: {0:?} != \"websocket\"")]
WsInvalidUpgradeHeader(String),
#[cfg(feature = "full")]
#[error("Invalid websocket connection header: {0:?} != \"Upgrade\"")]
WsInvalidConnectionHeader(String),
#[cfg(feature = "full")]
#[error("Invalid websocket payload, only String/ArrayBuffer accepted")]
WsInvalidPayload,
#[error("Invalid URL scheme")] #[error("Invalid URL scheme")]
InvalidUrlScheme, InvalidUrlScheme,
#[error("No URL host found")] #[error("No URL host found")]
@ -99,22 +115,8 @@ pub enum EpoxyError {
InvalidRequestBody, InvalidRequestBody,
#[error("Invalid request")] #[error("Invalid request")]
InvalidRequest, InvalidRequest,
#[error("Invalid websocket response status code")]
WsInvalidStatusCode,
#[error("Invalid websocket upgrade header")]
WsInvalidUpgradeHeader,
#[error("Invalid websocket connection header")]
WsInvalidConnectionHeader,
#[error("Invalid websocket payload")]
WsInvalidPayload,
#[error("Invalid payload")] #[error("Invalid payload")]
InvalidPayload, InvalidPayload,
#[error("Invalid certificate store")]
InvalidCertStore,
#[error("WebSocket failed to connect")]
WebSocketConnectFailed,
#[error("Failed to construct response headers object")] #[error("Failed to construct response headers object")]
ResponseHeadersFromEntriesFailed, ResponseHeadersFromEntriesFailed,
#[error("Failed to construct response object")] #[error("Failed to construct response object")]
@ -185,7 +187,6 @@ enum EpoxyCompression {
Gzip, Gzip,
} }
// ugly hack. switch to serde-wasm-bindgen or a knockoff // ugly hack. switch to serde-wasm-bindgen or a knockoff
cfg_if! { cfg_if! {
if #[cfg(feature = "full")] { if #[cfg(feature = "full")] {

View file

@ -205,27 +205,34 @@ impl EpoxyWebSocket {
// https://github.com/snapview/tungstenite-rs/blob/314feea3055a93e585882fb769854a912a7e6dae/src/handshake/client.rs#L189 // https://github.com/snapview/tungstenite-rs/blob/314feea3055a93e585882fb769854a912a7e6dae/src/handshake/client.rs#L189
fn verify(response: &Response<Incoming>) -> Result<(), EpoxyError> { fn verify(response: &Response<Incoming>) -> Result<(), EpoxyError> {
if response.status() != StatusCode::SWITCHING_PROTOCOLS { if response.status() != StatusCode::SWITCHING_PROTOCOLS {
return Err(EpoxyError::WsInvalidStatusCode); return Err(EpoxyError::WsInvalidStatusCode(
response.status().as_u16(),
StatusCode::SWITCHING_PROTOCOLS.as_u16(),
));
} }
let headers = response.headers(); let headers = response.headers();
if !headers let upgrade_header = headers
.get(UPGRADE) .get(UPGRADE)
.and_then(|h| h.to_str().ok()) .and_then(|h| h.to_str().ok())
.map(|h| h.eq_ignore_ascii_case("websocket")) .unwrap_or_default();
.unwrap_or(false)
{ if !upgrade_header.eq_ignore_ascii_case("websocket") {
return Err(EpoxyError::WsInvalidUpgradeHeader); return Err(EpoxyError::WsInvalidUpgradeHeader(
upgrade_header.to_string(),
));
} }
if !headers let connection_header = headers
.get(CONNECTION) .get(CONNECTION)
.and_then(|h| h.to_str().ok()) .and_then(|h| h.to_str().ok())
.map(|h| h.eq_ignore_ascii_case("Upgrade")) .unwrap_or_default();
.unwrap_or(false)
{ if !connection_header.eq_ignore_ascii_case("Upgrade") {
return Err(EpoxyError::WsInvalidConnectionHeader); return Err(EpoxyError::WsInvalidConnectionHeader(
connection_header.to_string(),
));
} }
Ok(()) Ok(())