Skip to content

Commit

Permalink
chore(volo-http): refactor websocket (#503)
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Li <[email protected]>
  • Loading branch information
yukiiiteru authored Sep 26, 2024
1 parent 3e51290 commit 2134818
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 498 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ webpki-roots = "0.26"
tokio-rustls = "0.25"
native-tls = "0.2"
tokio-native-tls = "0.3"
tokio-tungstenite = "0.23"

tungstenite = "0.24"
tokio-tungstenite = "0.24"

[profile.release]
opt-level = 3
Expand Down
4 changes: 3 additions & 1 deletion volo-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ tokio-util = { workspace = true, features = ["io"] }
tracing.workspace = true

# =====optional=====

# server optional
matchit = { workspace = true, optional = true }

# protocol optional
tungstenite = { workspace = true, optional = true }
tokio-tungstenite = { workspace = true, optional = true }

# tls optional
Expand Down Expand Up @@ -95,7 +97,7 @@ full = ["client", "server", "rustls", "cookie", "query", "form", "json", "tls",
client = ["hyper/client", "hyper/http1"] # client core
server = ["hyper/server", "hyper/http1", "dep:matchit"] # server core

ws = ["dep:tokio-tungstenite"]
ws = ["dep:tungstenite", "dep:tokio-tungstenite"]

tls = ["rustls"]
__tls = []
Expand Down
66 changes: 0 additions & 66 deletions volo-http/src/error/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,69 +99,3 @@ pub fn body_collection_error() -> ExtractBodyError {
pub fn invalid_content_type() -> ExtractBodyError {
ExtractBodyError::Generic(GenericRejectionError::InvalidContentType)
}

/// Rejection used for [`WebSocketUpgrade`](crate::server::utils::ws::WebSocketUpgrade).
#[derive(Debug)]
#[non_exhaustive]
pub enum WebSocketUpgradeRejectionError {
/// The request method must be `GET`
MethodNotGet,
/// The HTTP version is not supported
InvalidHttpVersion,
/// The `Connection` header is invalid
InvalidConnectionHeader,
/// The `Upgrade` header is invalid
InvalidUpgradeHeader,
/// The `Sec-WebSocket-Version` header is invalid
InvalidWebSocketVersionHeader,
/// The `Sec-WebSocket-Key` header is missing
WebSocketKeyHeaderMissing,
/// The connection is not upgradable
ConnectionNotUpgradable,
}

impl WebSocketUpgradeRejectionError {
/// Convert the [`WebSocketUpgradeRejectionError`] to the corresponding [`StatusCode`]
fn to_status_code(&self) -> StatusCode {
match self {
Self::MethodNotGet => StatusCode::METHOD_NOT_ALLOWED,
Self::InvalidHttpVersion => StatusCode::HTTP_VERSION_NOT_SUPPORTED,
Self::InvalidConnectionHeader => StatusCode::BAD_REQUEST,
Self::InvalidUpgradeHeader => StatusCode::BAD_REQUEST,
Self::InvalidWebSocketVersionHeader => StatusCode::BAD_REQUEST,
Self::WebSocketKeyHeaderMissing => StatusCode::BAD_REQUEST,
Self::ConnectionNotUpgradable => StatusCode::UPGRADE_REQUIRED,
}
}
}

impl Error for WebSocketUpgradeRejectionError {}

impl fmt::Display for WebSocketUpgradeRejectionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MethodNotGet => write!(f, "Request method must be 'GET'"),
Self::InvalidHttpVersion => {
write!(f, "Http version not support, only support HTTP 1.1 for now")
}
Self::InvalidConnectionHeader => {
write!(f, "Connection header did not include 'upgrade'")
}
Self::InvalidUpgradeHeader => write!(f, "`Upgrade` header did not include 'websocket'"),
Self::InvalidWebSocketVersionHeader => {
write!(f, "`Sec-WebSocket-Version` header did not include '13'")
}
Self::WebSocketKeyHeaderMissing => write!(f, "`Sec-WebSocket-Key` header missing"),
Self::ConnectionNotUpgradable => write!(
f,
"WebSocket request couldn't be upgraded since no upgrade state was present"
),
}
}
}

impl IntoResponse for WebSocketUpgradeRejectionError {
fn into_response(self) -> ServerResponse {
self.to_status_code().into_response()
}
}
2 changes: 0 additions & 2 deletions volo-http/src/server/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ pub use file_response::FileResponse;
pub use serve_dir::ServeDir;
#[cfg(feature = "ws")]
pub mod ws;
#[cfg(feature = "ws")]
pub use self::ws::{Config as WebSocketConfig, Message, WebSocket, WebSocketUpgrade};
Loading

0 comments on commit 2134818

Please sign in to comment.