From 0ca9592c6740d8583814a9e9624352f120525ec2 Mon Sep 17 00:00:00 2001 From: George Kaplan Date: Thu, 16 Apr 2020 21:49:05 -0400 Subject: [PATCH] undo URL percent-encoding for SQLite connection strings --- sqlx-core/src/sqlite/connection.rs | 8 +------- sqlx-core/src/url.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/sqlx-core/src/sqlite/connection.rs b/sqlx-core/src/sqlite/connection.rs index 9c502cf04b..2205fb68ea 100644 --- a/sqlx-core/src/sqlite/connection.rs +++ b/sqlx-core/src/sqlite/connection.rs @@ -48,15 +48,9 @@ unsafe impl Send for SqliteConnectionHandle {} async fn establish(url: Result) -> crate::Result { let mut worker = Worker::new(); - let url = url?; - let url = url - .as_str() - .trim_start_matches("sqlite:") - .trim_start_matches("//"); - // By default, we connect to an in-memory database. // TODO: Handle the error when there are internal NULs in the database URL - let filename = CString::new(url).unwrap(); + let filename = CString::new(url?.path_decoded().to_string()).unwrap(); let handle = worker .run(move || -> crate::Result { diff --git a/sqlx-core/src/url.rs b/sqlx-core/src/url.rs index 86d45c0bc9..fc6383c69b 100644 --- a/sqlx-core/src/url.rs +++ b/sqlx-core/src/url.rs @@ -78,6 +78,22 @@ impl Url { } } + /// Undo URL percent-encoding and return [authority]path[query] + /// + /// Mostly a hack to fix special-character handling for SQLite as its connection string is a + /// file path and not _really_ a URL + pub fn path_decoded(&self) -> Cow { + // omit scheme (e.g. `sqlite://`, `mysql://`) + let url_str = &self.0.as_str()[self.0.scheme().len()..] + .trim_start_matches(':') + .trim_start_matches("//"); + + // decode + percent_encoding::percent_decode_str(url_str) + .decode_utf8() + .expect("percent-encoded path contained non-UTF-8 bytes") + } + pub fn database(&self) -> Option<&str> { let database = self.0.path().trim_start_matches('/');