Version the zrpc protocol using a X-ZRPC-VERSION header

This commit is contained in:
Antonio Scandurra
2021-09-23 15:35:35 +02:00
parent a1ca507498
commit 511cbaa2bd
3 changed files with 36 additions and 20 deletions
+4 -1
View File
@@ -894,8 +894,11 @@ pub fn add_routes(app: &mut tide::Server<Arc<AppState>>, rpc: &Arc<Peer>) {
let connection_upgrade = header_contains_ignore_case(&request, CONNECTION, "upgrade"); let connection_upgrade = header_contains_ignore_case(&request, CONNECTION, "upgrade");
let upgrade_to_websocket = header_contains_ignore_case(&request, UPGRADE, "websocket"); let upgrade_to_websocket = header_contains_ignore_case(&request, UPGRADE, "websocket");
let upgrade_requested = connection_upgrade && upgrade_to_websocket; let upgrade_requested = connection_upgrade && upgrade_to_websocket;
let client_protocol_version: Option<u32> = request
.header("X-ZRPC-VERSION")
.and_then(|v| v.as_str().parse().ok());
if !upgrade_requested { if !upgrade_requested || client_protocol_version != Some(zrpc::VERSION) {
return Ok(Response::new(StatusCode::UpgradeRequired)); return Ok(Response::new(StatusCode::UpgradeRequired));
} }
+30 -19
View File
@@ -55,6 +55,8 @@ pub struct Client {
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum EstablishConnectionError { pub enum EstablishConnectionError {
#[error("upgrade required")]
UpgradeRequired,
#[error("unauthorized")] #[error("unauthorized")]
Unauthorized, Unauthorized,
#[error("{0}")] #[error("{0}")]
@@ -68,8 +70,10 @@ pub enum EstablishConnectionError {
impl From<WebsocketError> for EstablishConnectionError { impl From<WebsocketError> for EstablishConnectionError {
fn from(error: WebsocketError) -> Self { fn from(error: WebsocketError) -> Self {
if let WebsocketError::Http(response) = &error { if let WebsocketError::Http(response) = &error {
if response.status() == StatusCode::UNAUTHORIZED { match response.status() {
return EstablishConnectionError::Unauthorized; StatusCode::UNAUTHORIZED => return EstablishConnectionError::Unauthorized,
StatusCode::UPGRADE_REQUIRED => return EstablishConnectionError::UpgradeRequired,
_ => {}
} }
} }
EstablishConnectionError::Other(error.into()) EstablishConnectionError::Other(error.into())
@@ -85,6 +89,7 @@ impl EstablishConnectionError {
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum Status { pub enum Status {
SignedOut, SignedOut,
UpgradeRequired,
Authenticating, Authenticating,
Connecting, Connecting,
ConnectionError, ConnectionError,
@@ -227,7 +232,7 @@ impl Client {
} }
})); }));
} }
Status::SignedOut => { Status::SignedOut | Status::UpgradeRequired => {
state._maintain_connection.take(); state._maintain_connection.take();
} }
_ => {} _ => {}
@@ -346,6 +351,7 @@ impl Client {
| Status::Reconnecting { .. } | Status::Reconnecting { .. }
| Status::Authenticating | Status::Authenticating
| Status::Reauthenticating => return Ok(()), | Status::Reauthenticating => return Ok(()),
Status::UpgradeRequired => return Err(EstablishConnectionError::UpgradeRequired)?,
}; };
if was_disconnected { if was_disconnected {
@@ -388,22 +394,25 @@ impl Client {
self.set_connection(conn, cx).await; self.set_connection(conn, cx).await;
Ok(()) Ok(())
} }
Err(err) => { Err(EstablishConnectionError::Unauthorized) => {
if matches!(err, EstablishConnectionError::Unauthorized) { self.state.write().credentials.take();
self.state.write().credentials.take(); if used_keychain {
if used_keychain { cx.platform().delete_credentials(&ZED_SERVER_URL).log_err();
cx.platform().delete_credentials(&ZED_SERVER_URL).log_err(); self.set_status(Status::SignedOut, cx);
self.set_status(Status::SignedOut, cx); self.authenticate_and_connect(cx).await
self.authenticate_and_connect(cx).await
} else {
self.set_status(Status::ConnectionError, cx);
Err(err)?
}
} else { } else {
self.set_status(Status::ConnectionError, cx); self.set_status(Status::ConnectionError, cx);
Err(err)? Err(EstablishConnectionError::Unauthorized)?
} }
} }
Err(EstablishConnectionError::UpgradeRequired) => {
self.set_status(Status::UpgradeRequired, cx);
Err(EstablishConnectionError::UpgradeRequired)?
}
Err(error) => {
self.set_status(Status::ConnectionError, cx);
Err(error)?
}
} }
} }
@@ -489,10 +498,12 @@ impl Client {
credentials: &Credentials, credentials: &Credentials,
cx: &AsyncAppContext, cx: &AsyncAppContext,
) -> Task<Result<Connection, EstablishConnectionError>> { ) -> Task<Result<Connection, EstablishConnectionError>> {
let request = Request::builder().header( let request = Request::builder()
"Authorization", .header(
format!("{} {}", credentials.user_id, credentials.access_token), "Authorization",
); format!("{} {}", credentials.user_id, credentials.access_token),
)
.header("X-ZRPC-VERSION", zrpc::VERSION);
cx.background().spawn(async move { cx.background().spawn(async move {
if let Some(host) = ZED_SERVER_URL.strip_prefix("https://") { if let Some(host) = ZED_SERVER_URL.strip_prefix("https://") {
let stream = smol::net::TcpStream::connect(host).await?; let stream = smol::net::TcpStream::connect(host).await?;
+2
View File
@@ -4,3 +4,5 @@ mod peer;
pub mod proto; pub mod proto;
pub use conn::Connection; pub use conn::Connection;
pub use peer::*; pub use peer::*;
pub const VERSION: u32 = 0;