From 8f30ee08cf73180150ad730058128a7b1083f78c Mon Sep 17 00:00:00 2001 From: Raphael Darley Date: Tue, 10 Sep 2024 21:20:14 +0100 Subject: [PATCH] Deprecate missing format inference on RPC protocol (#4729) Co-authored-by: Tobie Morgan Hitchcock --- sdk/src/api/engine/remote/ws/mod.rs | 1 + sdk/src/api/engine/remote/ws/native.rs | 4 ++++ src/err/mod.rs | 3 +-- src/net/headers/mod.rs | 2 +- src/net/rpc.rs | 13 ++++++++++--- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/sdk/src/api/engine/remote/ws/mod.rs b/sdk/src/api/engine/remote/ws/mod.rs index 43c83948..5382e4aa 100644 --- a/sdk/src/api/engine/remote/ws/mod.rs +++ b/sdk/src/api/engine/remote/ws/mod.rs @@ -24,6 +24,7 @@ use uuid::Uuid; pub(crate) const PATH: &str = "rpc"; const PING_INTERVAL: Duration = Duration::from_secs(5); const REVISION_HEADER: &str = "revision"; +const BINCODE_HEADER: &str = "bincode"; enum RequestEffect { /// Completing this request sets a variable to a give value. diff --git a/sdk/src/api/engine/remote/ws/native.rs b/sdk/src/api/engine/remote/ws/native.rs index 6629bd39..f83485aa 100644 --- a/sdk/src/api/engine/remote/ws/native.rs +++ b/sdk/src/api/engine/remote/ws/native.rs @@ -80,6 +80,10 @@ pub(crate) async fn connect( request .headers_mut() .insert(SEC_WEBSOCKET_PROTOCOL, HeaderValue::from_static(super::REVISION_HEADER)); + } else { + request + .headers_mut() + .insert(SEC_WEBSOCKET_PROTOCOL, HeaderValue::from_static(super::BINCODE_HEADER)); } #[cfg(any(feature = "native-tls", feature = "rustls"))] diff --git a/src/err/mod.rs b/src/err/mod.rs index 94f18968..670f4718 100644 --- a/src/err/mod.rs +++ b/src/err/mod.rs @@ -2,7 +2,6 @@ use crate::cli::abstraction::auth::Error as SurrealAuthError; use axum::response::{IntoResponse, Response}; use axum::Error as AxumError; use axum::Json; -use axum_extra::typed_header::TypedHeaderRejection; use base64::DecodeError as Base64Error; use http::{HeaderName, StatusCode}; use reqwest::Error as ReqwestError; @@ -38,7 +37,7 @@ pub enum Error { OperationUnsupported, #[error("There was a problem parsing the header {0}: {1}")] - InvalidHeader(HeaderName, TypedHeaderRejection), + InvalidHeader(HeaderName, String), #[error("There was a problem with the database: {0}")] Db(#[from] SurrealError), diff --git a/src/net/headers/mod.rs b/src/net/headers/mod.rs index 1cd7f846..9ca82eb2 100644 --- a/src/net/headers/mod.rs +++ b/src/net/headers/mod.rs @@ -58,7 +58,7 @@ where Ok(TypedHeader(val)) => Ok(Some(val.to_string())), Err(e) => match e.reason() { TypedHeaderRejectionReason::Missing => Ok(None), - _ => Err(Error::InvalidHeader(H::name().to_owned(), e)), + _ => Err(Error::InvalidHeader(H::name().to_owned(), e.to_string())), }, } } diff --git a/src/net/rpc.rs b/src/net/rpc.rs index baf5c560..cdc4b48e 100644 --- a/src/net/rpc.rs +++ b/src/net/rpc.rs @@ -22,8 +22,8 @@ use axum::{ use axum_extra::headers::Header; use axum_extra::TypedHeader; use bytes::Bytes; +use http::header::SEC_WEBSOCKET_PROTOCOL; use http::HeaderMap; -use http::HeaderValue; use surrealdb::dbs::Session; use surrealdb::kvs::Datastore; use surrealdb::rpc::format::Format; @@ -54,6 +54,13 @@ async fn get_handler( State(rpc_state): State>, headers: HeaderMap, ) -> Result { + // Check that a valid header has been specified + if headers.get(SEC_WEBSOCKET_PROTOCOL).is_none() { + warn!("A connection was made without a specified protocol."); + warn!("Automatic inference of the protocol format is deprecated in SurrealDB 2.0 and will be removed in SurrealDB 3.0."); + warn!("Please upgrade any client to ensure that the connection format is specified."); + } + // Check if there is a connection id header specified let id = match headers.get(SurrealId::name()) { // Use the specific SurrealDB id header when provided @@ -119,9 +126,9 @@ async fn handle_socket( id: Uuid, ) { // Check if there is a WebSocket protocol specified - let format = match ws.protocol().map(HeaderValue::to_str) { + let format = match ws.protocol().and_then(|h| h.to_str().ok()) { // Any selected protocol will always be a valie value - Some(protocol) => protocol.unwrap().into(), + Some(protocol) => protocol.into(), // No protocol format was specified _ => Format::None, };