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(-)
@@ -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>,
}
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>")]
@@ -1,0 +1,35 @@
use rocket::Request;
use rocket::request::{FromRequest, Outcome};
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())))
}
}