undo URL percent-encoding for SQLite connection strings

This commit is contained in:
George Kaplan 2020-04-16 21:49:05 -04:00 committed by Ryan Leckey
parent 45744e8033
commit 7df6d4dbcf
2 changed files with 17 additions and 7 deletions

View file

@ -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> {

View file

@ -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('/');