From 0a0952e6edb2f4a8c7883b661ef6d4bb4eb14405 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 24 Aug 2020 11:37:03 +0530 Subject: parse host header and return full url --- src/db.rs | 2 +- src/service.rs | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/db.rs b/src/db.rs index 299e07c..c67e39e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -11,7 +11,7 @@ pub fn open_connection

(p: P) -> Result where P: AsRef + fmt::Display, { - info!("Opened connection to databse"); + info!("Opened connection to database"); Ok(Connection::open_with_flags( &p, OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, diff --git a/src/service.rs b/src/service.rs index 4477ee8..35471ea 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,6 +1,6 @@ // extern use anyhow::{Context, Result}; -use hyper::header::CONTENT_TYPE; +use hyper::header::{HeaderValue, CONTENT_TYPE}; use hyper::{Body, Method, Request, Response, StatusCode}; use log::{error, info, trace}; use multer::Multipart; @@ -11,12 +11,18 @@ use url::form_urlencoded; // std use std::collections::HashMap; -fn respond_with_shortlink>(shortlink: S) -> Response { +fn get_host(req: &Request) -> Option { + let host = req.headers().get("host").map(|h| h.clone())?; + return Some(host); +} + +fn respond_with_shortlink>(shortlink: S, host: &[u8]) -> Response { + let url = [host, b"/", shortlink.as_ref()].concat(); info!("Successfully generated shortlink"); Response::builder() .status(StatusCode::OK) .header("content-type", "text/html") - .body(Body::from(shortlink.as_ref().to_string())) + .body(Body::from(url)) .unwrap() } @@ -51,11 +57,13 @@ fn get_link>(url: S, conn: &Connection) -> Result> } async fn process_multipart( - body: Body, + req: Request, boundary: String, conn: &mut Connection, ) -> Result> { - let mut m = Multipart::new(body, boundary); + let _h = get_host(&req); + let host = _h.as_ref().map(|h| h.as_bytes()).unwrap_or(b""); + let mut m = Multipart::new(req.into_body(), boundary); if let Some(field) = m.next_field().await? { if field.name() == Some("shorten") { trace!("Recieved valid multipart request"); @@ -65,7 +73,7 @@ async fn process_multipart( .with_context(|| format!("Expected field name"))?; let shortlink = shorten(content, conn)?; - return Ok(respond_with_shortlink(shortlink)); + return Ok(respond_with_shortlink(shortlink, host)); } } trace!("Unprocessable multipart request!"); @@ -73,6 +81,8 @@ async fn process_multipart( } async fn process_form(req: Request, conn: &mut Connection) -> Result> { + let _h = get_host(&req); + let host = _h.as_ref().map(|h| h.as_bytes()).unwrap_or(b""); let b = hyper::body::to_bytes(req) .await .with_context(|| format!("Failed to stream request body!"))?; @@ -84,7 +94,7 @@ async fn process_form(req: Request, conn: &mut Connection) -> Result, mut conn: Connection) -> Resul } trace!("Attempting to parse multipart request"); - return process_multipart(req.into_body(), boundary.unwrap(), &mut conn).await; + return process_multipart(req, boundary.unwrap(), &mut conn).await; } &Method::GET => { trace!("GET: {}", req.uri()); -- cgit v1.2.3