Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Preserve API origin path prefixes

Manav Rathid6b9880f6671
diff --git a/rust/crates/core/src/http.rs b/rust/crates/core/src/http.rs
index 312401fb50..3910daffed 100644
--- a/rust/crates/core/src/http.rs
+++ b/rust/crates/core/src/http.rs
@@ -18,13 +18,15 @@ use gloo_timers::future::sleep;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
+use reqwest::Method;
use reqwest::header::{HeaderName, HeaderValue};
-use reqwest::{Method, Url};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use zeroize::ZeroizeOnDrop;
+use crate::urls::api_url;
+
const CLIENT_PACKAGE: HeaderName = HeaderName::from_static("x-client-package");
const CLIENT_VERSION: HeaderName = HeaderName::from_static("x-client-version");
const AUTH_TOKEN: HeaderName = HeaderName::from_static("x-auth-token");
@@ -340,14 +342,10 @@ impl Api {
}
fn request(&self, method: Method, path: &str) -> RequestBuilder {
- let mut builder = match Url::parse(&self.origin) {
- Ok(mut url) => {
- url.set_path(path);
- self.http.client.request(method, url)
- }
- // reqwest hits the same parse failure, and reports it at send time.
- Err(_) => self.http.client.request(method, self.origin.as_str()),
- };
+ let mut builder = self
+ .http
+ .client
+ .request(method, api_url(&self.origin, path));
if let Some(client_package) = &self.client_package {
builder = builder.header(CLIENT_PACKAGE, client_package);
}
@@ -971,6 +969,24 @@ mod tests {
mock.assert_async().await;
}
+ #[tokio::test]
+ async fn origin_path_prefix_is_preserved() {
+ let mut server = Server::new_async().await;
+ let mock = server
+ .mock("GET", "/ente/ping")
+ .with_body(r#"{"message":"pong","id":"abc"}"#)
+ .create_async()
+ .await;
+
+ let api = Api::new(
+ Http::new().unwrap(),
+ ApiConfig::new(format!("{}/ente", server.url())),
+ );
+ api.ping().await.unwrap();
+
+ mock.assert_async().await;
+ }
+
#[tokio::test]
async fn retry_retries_retryable_failures() {
let mut server = Server::new_async().await;
diff --git a/rust/crates/core/src/urls.rs b/rust/crates/core/src/urls.rs
index e584c05717..b2bfbe02c6 100644
--- a/rust/crates/core/src/urls.rs
+++ b/rust/crates/core/src/urls.rs
@@ -3,12 +3,25 @@
/// Production API origin.
pub const PRODUCTION_API_ORIGIN: &str = "https://api.ente.com";
+/// Append a trusted endpoint path to the configured API origin.
+///
+/// The configured value is expected to be an origin and endpoint paths are
+/// trusted. Any path prefix is retained as a best-effort convenience for
+/// self-hosted deployments.
+pub(crate) fn api_url(origin: &str, path: &str) -> String {
+ format!(
+ "{}/{}",
+ origin.trim_end_matches('/'),
+ path.trim_start_matches('/')
+ )
+}
+
/// Generate the download URL for a file.
pub fn file_download_url(api_origin: &str, file_id: i64) -> String {
if api_origin == PRODUCTION_API_ORIGIN {
format!("https://files.ente.com/?fileID={}", file_id)
} else {
- format!("{}/files/download/{}", api_origin, file_id)
+ api_url(api_origin, &format!("files/download/{file_id}"))
}
}
@@ -27,4 +40,10 @@ mod tests {
let url = file_download_url("https://my-server.example.com", 99);
assert_eq!(url, "https://my-server.example.com/files/download/99");
}
+
+ #[test]
+ fn test_custom_server_path_prefix() {
+ let url = file_download_url("https://my-server.example.com/ente/", 99);
+ assert_eq!(url, "https://my-server.example.com/ente/files/download/99");
+ }
}