diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 72 |
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 @@ | |||
1 | use anyhow::{Context, Result}; | 1 | use anyhow::{Context, Result}; |
2 | use hyper::header::CONTENT_TYPE; | ||
2 | use hyper::service::{make_service_fn, service_fn}; | 3 | use hyper::service::{make_service_fn, service_fn}; |
3 | use hyper::{Body, Method, Request, Response, Server, StatusCode}; | 4 | use hyper::{Body, Method, Request, Response, Server, StatusCode}; |
5 | use multer::Multipart; | ||
4 | use nanoid::nanoid; | 6 | use nanoid::nanoid; |
5 | use rusqlite::{params, Connection, OpenFlags, NO_PARAMS}; | 7 | use rusqlite::{params, Connection, OpenFlags, NO_PARAMS}; |
6 | use url::form_urlencoded; | 8 | use 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 | ||
39 | async 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 | |||
37 | async fn shortner_service(req: Request<Body>) -> Result<Response<Body>> { | 53 | async 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 | } |