From 726d04aca750c5e3b491da2d771ea2bf0d8e8930 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 20 Feb 2021 14:22:40 +0100 Subject: [PATCH] web: fix return status for error page with localization --- database/src/lib.rs | 10 ++++++++++ src/web/mod.rs | 12 ++++++++---- src/web/vks_web.rs | 2 ++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/database/src/lib.rs b/database/src/lib.rs index 455e8da..0b71c00 100644 --- a/database/src/lib.rs +++ b/database/src/lib.rs @@ -66,6 +66,16 @@ pub enum Query { Invalid(), } +impl Query { + pub fn is_invalid(&self) -> bool { + match self { + Query::Invalid() => true, + Query::InvalidShort() => true, + _ => false, + } + } +} + impl FromStr for Query { type Err = anyhow::Error; diff --git a/src/web/mod.rs b/src/web/mod.rs index 8f9d08e..381fc07 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -281,6 +281,10 @@ pub fn key_to_response_plain( i18n: I18n, query: Query, ) -> MyResponse { + if query.is_invalid() { + return MyResponse::bad_request_plain(describe_query_error(&i18n, &query)); + } + let fp = if let Some(fp) = db.lookup_primary_fingerprint(&query) { fp } else { @@ -979,13 +983,13 @@ pub mod tests { fn search_invalid() { let (_tmpdir, client) = client().unwrap(); check_response(&client, "/search?q=0x1234abcd", - Status::BadRequest, "not supported, sorry!"); + Status::BadRequest, "not supported"); check_response(&client, "/search?q=1234abcd", - Status::BadRequest, "not supported, sorry!"); + Status::BadRequest, "not supported"); check_response(&client, "/pks/lookup?op=get&search=0x1234abcd", - Status::BadRequest, "not supported, sorry!"); + Status::BadRequest, "not supported"); check_response(&client, "/pks/lookup?op=get&search=1234abcd", - Status::BadRequest, "not supported, sorry!"); + Status::BadRequest, "not supported"); } diff --git a/src/web/vks_web.rs b/src/web/vks_web.rs index 6815c7f..6581536 100644 --- a/src/web/vks_web.rs +++ b/src/web/vks_web.rs @@ -243,6 +243,8 @@ fn key_to_response( ) -> MyResponse { let fp = if let Some(fp) = db.lookup_primary_fingerprint(&query) { fp + } else if query.is_invalid() { + return MyResponse::bad_request("index", anyhow!(describe_query_error(&i18n, &query))); } else { return MyResponse::not_found(None, describe_query_error(&i18n, &query)); };