From 9d77cc67976ac714a77b0134d57d0bb21d38056d Mon Sep 17 00:00:00 2001 From: Kai Michaelis Date: Tue, 15 Jan 2019 18:30:35 +0100 Subject: [PATCH] return correct mime type for by-* routes --- nginx.conf | 4 ---- src/web/mod.rs | 65 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/nginx.conf b/nginx.conf index e18c95a..1212cb4 100644 --- a/nginx.conf +++ b/nginx.conf @@ -40,10 +40,6 @@ http { proxy_pass http://127.0.0.1:8080; } - location = /keys { - proxy_pass http://127.0.0.1:8080; - } - location ^~ /vks/ { proxy_pass http://127.0.0.1:8080; } diff --git a/src/web/mod.rs b/src/web/mod.rs index 1e8ff72..e902462 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -3,7 +3,7 @@ use rocket::{State, Outcome}; use rocket::http::Status; use rocket::request::{self, Request, FromRequest}; use rocket::response::status::Custom; -use rocket::response::NamedFile; +use rocket::response::{Response, NamedFile}; use rocket::fairing::AdHoc; use rocket_contrib::templates::Template; @@ -96,9 +96,11 @@ impl<'a, 'r> FromRequest<'a, 'r> for queries::Hkp { } } -fn process_key(bytes: &[u8]) -> result::Result> { +fn key_to_response<'a,'b>(bytes: &'a[u8]) -> Response<'b> { use std::io::Write; use sequoia_openpgp::armor::{Writer, Kind}; + use std::io::Cursor; + use rocket::http::{Status, ContentType}; let key = || -> Result { let mut buffer = Vec::default(); @@ -111,56 +113,91 @@ fn process_key(bytes: &[u8]) -> result::Result> { }(); match key { - Ok(s) => Ok(s), + Ok(s) => + Response::build() + .status(Status::Ok) + .header(ContentType::new("application", "pgp-keys")) + .sized_body(Cursor::new(s)) + .finalize(), Err(_) => - Err(Custom(Status::InternalServerError, - "Failed to ASCII armor key".to_string())), + Response::build() + .status(Status::InternalServerError) + .header(ContentType::Plain) + .sized_body(Cursor::new("Failed to ASCII armor key")) + .finalize(), } } #[get("/by-fpr/")] fn by_fpr(db: rocket::State, fpr: String) - -> result::Result> + -> Response { + use std::io::Cursor; + use rocket::http::{Status, ContentType}; + let maybe_key = match Fingerprint::from_str(&fpr) { Ok(ref fpr) => db.by_fpr(fpr), Err(_) => None, }; match maybe_key { - Some(ref bytes) => process_key(bytes), - None => Ok("No such key :-(".to_string()), + Some(ref bytes) => key_to_response(bytes), + None => + Response::build() + .status(Status::NotFound) + .header(ContentType::Plain) + .sized_body(Cursor::new("No such key :-(")) + .finalize(), } } #[get("/by-email/")] fn by_email(db: rocket::State, email: String) - -> result::Result> + -> Response { + use std::io::Cursor; + use rocket::http::{Status, ContentType}; + let maybe_key = match Email::from_str(&email) { Ok(ref email) => db.by_email(email), Err(_) => None, }; match maybe_key { - Some(ref bytes) => process_key(bytes), - None => Ok("No such key :-(".to_string()), + Some(ref bytes) => key_to_response(bytes), + None => + Response::build() + .status(Status::NotFound) + .header(ContentType::Plain) + .sized_body(Cursor::new("No such key :-(")) + .finalize(), } } #[get("/by-kid/")] fn by_kid(db: rocket::State, kid: String) - -> result::Result> + -> Response { + use std::io::Cursor; + use rocket::http::{Status, ContentType}; + let maybe_key = match KeyID::from_str(&kid) { Ok(ref key) => db.by_kid(key), Err(_) => None, }; match maybe_key { - Some(ref bytes) => process_key(bytes), - None => Ok("No such key :-(".to_string()), + Some(ref bytes) => { + key_to_response(bytes) + } + None => + Response::build() + .status(Status::NotFound) + .header(ContentType::Plain) + .sized_body(Cursor::new("No such key :-(")) + .finalize(), } + } #[get("/vks/verify/")]