aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-08-23 08:33:21 +0100
committerAkshay <[email protected]>2020-08-23 08:33:21 +0100
commit21f1d776762035012f02be39c96978b0719e0613 (patch)
tree24fd73624f305281c391825875dcaf941e716090 /src
parentf36a8fb2eee46cffa87213e070136e5f28194a16 (diff)
initial multipart handling
Diffstat (limited to 'src')
-rw-r--r--src/main.rs72
1 files changed, 50 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs
index cb76d5b..769ed03 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,8 @@
1use anyhow::{Context, Result}; 1use anyhow::{Context, Result};
2use hyper::header::CONTENT_TYPE;
2use hyper::service::{make_service_fn, service_fn}; 3use hyper::service::{make_service_fn, service_fn};
3use hyper::{Body, Method, Request, Response, Server, StatusCode}; 4use hyper::{Body, Method, Request, Response, Server, StatusCode};
5use multer::Multipart;
4use nanoid::nanoid; 6use nanoid::nanoid;
5use rusqlite::{params, Connection, OpenFlags, NO_PARAMS}; 7use rusqlite::{params, Connection, OpenFlags, NO_PARAMS};
6use url::form_urlencoded; 8use url::form_urlencoded;
@@ -34,34 +36,62 @@ fn get_link<S: AsRef<str>>(url: S, conn: &mut Connection) -> Result<Option<Strin
34 } 36 }
35} 37}
36 38
39async fn process_multipart(body: Body, boundary: String) -> Result<Response<Body>> {
40 let mut m = Multipart::new(body, boundary);
41 if let Some(field) = m.next_field().await? {
42 let content = field
43 .text()
44 .await
45 .with_context(|| format!("Expected field name"))?;
46 eprintln!("{}", content);
47 }
48 Ok(Response::builder()
49 .status(StatusCode::OK)
50 .body(Body::empty())?)
51}
52
37async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> { 53async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> {
38 let mut conn = init_db("./urls.db_3").unwrap(); 54 let mut conn = init_db("./urls.db_3").unwrap();
39 eprintln!("{:?}", req);
40 55
41 match req.method() { 56 match req.method() {
42 &Method::POST => { 57 &Method::POST => {
43 let b = hyper::body::to_bytes(req) 58 let boundary = req
44 .await 59 .headers()
45 .with_context(|| format!("Failed to stream request body!"))?; 60 .get(CONTENT_TYPE)
61 .and_then(|ct| ct.to_str().ok())
62 .and_then(|ct| multer::parse_boundary(ct).ok());
46 63
47 let params = form_urlencoded::parse(b.as_ref()) 64 // Send `BAD_REQUEST` status if the content-type is not multipart/form-data.
48 .into_owned() 65 if boundary.is_none() {
49 .collect::<HashMap<String, String>>(); 66 let b = hyper::body::to_bytes(req)
67 .await
68 .with_context(|| format!("Failed to stream request body!"))?;
50 69
51 if let Some(n) = params.get("shorten") { 70 let params = form_urlencoded::parse(b.as_ref())
52 let shortlink = shorten(n, &mut conn)?; 71 .into_owned()
53 eprintln!("{}", shortlink); 72 .collect::<HashMap<String, String>>();
54 let res = Response::builder() 73
55 .status(StatusCode::OK) 74 if let Some(n) = params.get("shorten") {
56 .header("content-type", "text/html") 75 let shortlink = shorten(n, &mut conn)?;
57 .body(Body::from(shortlink))?; 76 eprintln!("{}", shortlink);
58 Ok(res) 77 let res = Response::builder()
78 .status(StatusCode::OK)
79 .header("content-type", "text/html")
80 .body(Body::from(shortlink))?;
81 return Ok(res);
82 } else {
83 eprintln!("hello world");
84 let res = Response::builder()
85 .status(StatusCode::UNPROCESSABLE_ENTITY)
86 .body(Body::empty())?;
87 return Ok(res);
88 }
89 }
90
91 if let Ok(res) = process_multipart(req.into_body(), boundary.unwrap()).await {
92 return Ok(res);
59 } else { 93 } else {
60 eprintln!("hello world"); 94 panic!("nani");
61 let res = Response::builder()
62 .status(StatusCode::UNPROCESSABLE_ENTITY)
63 .body(Body::empty())?;
64 Ok(res)
65 } 95 }
66 } 96 }
67 &Method::GET => { 97 &Method::GET => {
@@ -113,9 +143,7 @@ async fn main() -> Result<()> {
113 make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); 143 make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) });
114 144
115 let server = Server::bind(&addr).serve(service); 145 let server = Server::bind(&addr).serve(service);
116
117 println!("Listening on http://{}", addr); 146 println!("Listening on http://{}", addr);
118
119 server.await.unwrap(); 147 server.await.unwrap();
120 Ok(()) 148 Ok(())
121} 149}