🏡 index : ~doyle/bin.git

author Jordan Johnson-Doyle <jordan@doyle.la> 2019-02-12 1:08:38.0 +00:00:00
committer Jordan Johnson-Doyle <jordan@doyle.la> 2019-02-12 1:08:38.0 +00:00:00
commit
b876bb9c84cc6823d7a615b76c2ce7d39385c08b [patch]
tree
55e0bf42ec090bce14273725cf7e9f382c4553f2
parent
ae33163f9adfd788382d069395c46536e9643bb7
download
b876bb9c84cc6823d7a615b76c2ce7d39385c08b.tar.gz

Return correct URL from the API, not one pointing to localhost



Diff

 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 = "<input>")]
fn submit_raw(input: Data) -> std::io::Result<String> {
fn submit_raw(input: Data, host: HostHeader) -> std::io::Result<String> {
    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<Html, String>,
}

/// 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<IsPlaintextRequest, ()> {
        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("/<key>")]
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<IsPlaintextRequest, ()> {
        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<String>);

impl<'a, 'r> FromRequest<'a, 'r> for HostHeader {
    type Error = ();

    fn from_request(request: &'a Request<'r>) -> Outcome<HostHeader, ()> {
        Outcome::Success(HostHeader(request.headers().get_one("Host").map(|h| h.to_string())))
    }
}