Skip to content

Commit

Permalink
undo URL percent-encoding for SQLite connection strings
Browse files Browse the repository at this point in the history
  • Loading branch information
g-s-k authored and mehcode committed Apr 18, 2020
1 parent 45744e8 commit 7df6d4d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 1 addition & 7 deletions sqlx-core/src/sqlite/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,9 @@ unsafe impl Send for SqliteConnectionHandle {}
async fn establish(url: Result<Url, url::ParseError>) -> crate::Result<SqliteConnection> {
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<SqliteConnectionHandle> {
Expand Down
16 changes: 16 additions & 0 deletions sqlx-core/src/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str> {
// 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('/');

Expand Down

0 comments on commit 7df6d4d

Please sign in to comment.