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

undo URL percent-encoding for SQLite connection strings #259

Merged
merged 1 commit into from
Apr 18, 2020
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
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