Deprecate missing format inference on RPC protocol (#4729)

Co-authored-by: Tobie Morgan Hitchcock <tobie@surrealdb.com>
This commit is contained in:
Raphael Darley 2024-09-10 21:20:14 +01:00 committed by GitHub
parent bd031ccbe8
commit 8f30ee08cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 17 additions and 6 deletions

View file

@ -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.

View file

@ -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"))]

View file

@ -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),

View file

@ -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())),
},
}
}

View file

@ -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<Arc<RpcState>>,
headers: HeaderMap,
) -> Result<impl IntoResponse, impl IntoResponse> {
// 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,
};