Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hrana URL strings: don't append pipeline/cursor routes blindly #919

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions libsql/src/hrana/connection.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::hrana::cursor::Cursor;
use crate::hrana::pipeline::{BatchStreamReq, StreamRequest, StreamResponse};
use crate::hrana::proto::{Batch, BatchResult, Stmt};
use crate::hrana::stream::HranaStream;
use crate::hrana::stream::{parse_hrana_urls, HranaStream};
use crate::hrana::{HranaError, HttpSend, Result, Statement};
use crate::util::coerce_url_scheme;
use std::sync::atomic::{AtomicBool, AtomicI64, AtomicU64, Ordering};
Expand Down Expand Up @@ -32,9 +32,8 @@ where
{
pub fn new(url: String, token: String, inner: T) -> Self {
// The `libsql://` protocol is an alias for `https://`.
let base_url = coerce_url_scheme(&url);
let pipeline_url = Arc::from(format!("{base_url}/v3/pipeline"));
let cursor_url = Arc::from(format!("{base_url}/v3/cursor"));
let base_url = coerce_url_scheme(url);
let (pipeline_url, cursor_url) = parse_hrana_urls(&base_url);
HttpConnection(Arc::new(InnerClient {
inner,
pipeline_url,
Expand Down
17 changes: 16 additions & 1 deletion libsql/src/hrana/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ where
let body = stream_to_bytes(body).await?;
let mut response: ServerMsg = serde_json::from_slice(&body)?;
if let Some(base_url) = response.base_url.take() {
self.pipeline_url = Arc::from(base_url);
let (pipeline_url, cursor_url) = parse_hrana_urls(&base_url);
self.pipeline_url = pipeline_url;
self.cursor_url = cursor_url;
}
match response.baton.take() {
None => {
Expand Down Expand Up @@ -379,6 +381,19 @@ where
}
}

pub(super) fn parse_hrana_urls(url: &str) -> (Arc<str>, Arc<str>) {
let (mut base_url, query) = match url.rfind('?') {
Some(i) => url.split_at(i),
None => (url, ""),
};
if base_url.ends_with('/') {
base_url = &base_url[0..(base_url.len() - 1)];
}
let pipeline_url = Arc::from(format!("{base_url}/v3/pipeline{query}"));
let cursor_url = Arc::from(format!("{base_url}/v3/cursor{query}"));
(pipeline_url, cursor_url)
}

async fn stream_to_bytes<S>(mut stream: S) -> Result<Bytes>
where
S: Stream<Item = std::io::Result<Bytes>> + Unpin,
Expand Down
19 changes: 16 additions & 3 deletions libsql/src/local/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,16 @@ impl Database {
auth_token: String,
encryption_key: Option<bytes::Bytes>,
) -> Result<Database> {
Self::open_http_sync_internal(connector, db_path, endpoint, auth_token, None, false, encryption_key).await
Self::open_http_sync_internal(
connector,
db_path,
endpoint,
auth_token,
None,
false,
encryption_key,
)
.await
}

#[cfg(feature = "replication")]
Expand All @@ -73,7 +82,7 @@ impl Database {

let mut db = Database::open(&db_path, OpenFlags::default())?;

let endpoint = coerce_url_scheme(&endpoint);
let endpoint = coerce_url_scheme(endpoint);
let remote = crate::replication::client::Client::new(
connector,
endpoint.as_str().try_into().unwrap(),
Expand All @@ -98,7 +107,11 @@ impl Database {
}

#[cfg(feature = "replication")]
pub async fn open_local_sync(db_path: impl Into<String>, flags: OpenFlags, encryption_key: Option<bytes::Bytes>) -> Result<Database> {
pub async fn open_local_sync(
db_path: impl Into<String>,
flags: OpenFlags,
encryption_key: Option<bytes::Bytes>,
) -> Result<Database> {
use std::path::PathBuf;

let db_path = db_path.into();
Expand Down
2 changes: 1 addition & 1 deletion libsql/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cfg_replication_or_remote! {
}

cfg_replication_or_remote_or_hrana! {
pub(crate) fn coerce_url_scheme(url: &str) -> String {
pub(crate) fn coerce_url_scheme(url: String) -> String {
let mut url = url.replace("libsql://", "https://");

if !url.contains("://") {
Expand Down
Loading