diff --git a/Cargo.lock b/Cargo.lock index 79d1baf..5a59a1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2444,6 +2444,7 @@ dependencies = [ "shared", "thiserror", "tokio", + "urlencoding", ] [[package]] @@ -3037,6 +3038,12 @@ dependencies = [ "percent-encoding 2.3.0", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "utf-8" version = "0.7.6" diff --git a/notmuch/src/lib.rs b/notmuch/src/lib.rs index 07d6f91..f746267 100644 --- a/notmuch/src/lib.rs +++ b/notmuch/src/lib.rs @@ -454,6 +454,8 @@ pub enum NotmuchError { SerdeJson(#[from] serde_json::Error), #[error("failed to parse bytes as str")] Utf8Error(#[from] std::str::Utf8Error), + #[error("failed to parse bytes as String")] + StringUtf8Error(#[from] std::string::FromUtf8Error), #[error("failed to parse str as int")] ParseIntError(#[from] std::num::ParseIntError), } diff --git a/server/Cargo.toml b/server/Cargo.toml index ba1f885..09c74dd 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -16,6 +16,7 @@ serde = { version = "1.0.147", features = ["derive"] } log = "0.4.17" tokio = "1.26.0" glog = "0.1.0" +urlencoding = "2.1.3" [dependencies.rocket_contrib] version = "0.4.11" diff --git a/server/src/main.rs b/server/src/main.rs index 9f655d4..214a809 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -4,7 +4,7 @@ extern crate rocket; use std::{error::Error, io::Cursor, str::FromStr}; use glog::Flags; -use notmuch::{Notmuch, NotmuchError, SearchSummary, ThreadSet}; +use notmuch::{Notmuch, NotmuchError, ThreadSet}; use rocket::{ http::{ContentType, Header}, request::Request, @@ -40,13 +40,14 @@ async fn search( ) -> Result, Debug> { let page = page.unwrap_or(0); let results_per_page = results_per_page.unwrap_or(10); + let query = urlencoding::decode(query).map_err(NotmuchError::from)?; info!(" search '{query}'"); let res = shared::SearchResult { - summary: nm.search(query, page * results_per_page, results_per_page)?, + summary: nm.search(&query, page * results_per_page, results_per_page)?, query: query.to_string(), page, results_per_page, - total: nm.count(query)?, + total: nm.count(&query)?, }; Ok(Json(res)) } @@ -56,13 +57,15 @@ async fn show_pretty( nm: &State, query: &str, ) -> Result, Debug> { - let res = nm.show(query)?; + let query = urlencoding::decode(query).map_err(NotmuchError::from)?; + let res = nm.show(&query)?; Ok(Json(res)) } #[get("/show/")] async fn show(nm: &State, query: &str) -> Result, Debug> { - let res = nm.show(query)?; + let query = urlencoding::decode(query).map_err(NotmuchError::from)?; + let res = nm.show(&query)?; Ok(Json(res)) }