From b876bb9c84cc6823d7a615b76c2ce7d39385c08b Mon Sep 17 00:00:00 2001 From: Jordan Johnson-Doyle Date: Tue, 12 Feb 2019 01:08:38 +0000 Subject: [PATCH] Return correct URL from the API, not one pointing to localhost --- src/main.rs | 38 ++++++++++++++++++-------------------- src/params.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index f8b285e..d1dba56 100644 --- a/src/main.rs +++ a/src/main.rs @@ -11,15 +11,17 @@ mod io; mod highlight; +mod params; use io::{store_paste, get_paste}; use highlight::highlight; +use params::{IsPlaintextRequest, HostHeader}; use askama::Template; use askama_escape::{MarkupDisplay, Html}; -use rocket::{Request, Data}; -use rocket::request::{Form, FromRequest, Outcome}; +use rocket::Data; +use rocket::request::Form; use rocket::response::Redirect; use rocket::response::content::Content; use rocket::http::ContentType; @@ -48,10 +50,16 @@ } #[put("/", data = "")] -fn submit_raw(input: Data) -> std::io::Result { +fn submit_raw(input: Data, host: HostHeader) -> std::io::Result { let mut data = String::new(); input.open().take(1024 * 1000).read_to_string(&mut data)?; - Ok(format!("https://{}/{}", "localhost:8000", store_paste(data))) + + let paste = store_paste(data); + + match host.0 { + Some(host) => Ok(format!("https://{}/{}", host, paste)), + None => Ok(paste) + } } @@ -59,28 +67,6 @@ #[template(path = "paste.html")] struct Render { content: MarkupDisplay, -} - -/// Holds a value that determines whether or not this request wanted a plaintext response. -/// -/// We assume anything with the text/plain Accept or Content-Type headers want plaintext, -/// and also anything calling us from the console or that we can't identify. -struct IsPlaintextRequest(bool); -impl<'a, 'r> FromRequest<'a, 'r> for IsPlaintextRequest { - type Error = (); - - fn from_request(request: &'a Request<'r>) -> Outcome { - if let Some(format) = request.format() { - if format.is_plain() { - return Outcome::Success(IsPlaintextRequest(true)); - } - } - - match request.headers().get_one("User-Agent").and_then(|u| u.splitn(2, '/').next()) { - None | Some("Wget") | Some("curl") | Some("HTTPie") => Outcome::Success(IsPlaintextRequest(true)), - _ => Outcome::Success(IsPlaintextRequest(false)) - } - } } #[get("/")] diff --git a/src/params.rs b/src/params.rs new file mode 100644 index 0000000..2e08228 100644 --- /dev/null +++ a/src/params.rs @@ -1,0 +1,35 @@ +use rocket::Request; +use rocket::request::{FromRequest, Outcome}; + +/// Holds a value that determines whether or not this request wanted a plaintext response. +/// +/// We assume anything with the text/plain Accept or Content-Type headers want plaintext, +/// and also anything calling us from the console or that we can't identify. +pub struct IsPlaintextRequest(pub bool); + +impl<'a, 'r> FromRequest<'a, 'r> for IsPlaintextRequest { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> Outcome { + if let Some(format) = request.format() { + if format.is_plain() { + return Outcome::Success(IsPlaintextRequest(true)); + } + } + + match request.headers().get_one("User-Agent").and_then(|u| u.splitn(2, '/').next()) { + None | Some("Wget") | Some("curl") | Some("HTTPie") => Outcome::Success(IsPlaintextRequest(true)), + _ => Outcome::Success(IsPlaintextRequest(false)) + } + } +} + +pub struct HostHeader(pub Option); + +impl<'a, 'r> FromRequest<'a, 'r> for HostHeader { + type Error = (); + + fn from_request(request: &'a Request<'r>) -> Outcome { + Outcome::Success(HostHeader(request.headers().get_one("Host").map(|h| h.to_string()))) + } +}-- rgit 0.1.3