From 99d59ca3be5ce319e43671f63449b36344bb3f84 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 23 Aug 2020 17:38:49 +0530 Subject: switch to smol rt, fix form urlencoded requests --- Cargo.lock | 261 +++++++++++++++++++++++++++++++++++++++++++++--------------- Cargo.toml | 6 +- src/main.rs | 75 +++++++++-------- 3 files changed, 242 insertions(+), 100 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1c73a0e..9759d76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,10 +16,60 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" [[package]] -name = "arc-swap" -version = "0.4.7" +name = "async-channel" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43de69555a39d52918e2bc33a408d3c0a86c829b212d898f4ca25d21a6387478" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" +checksum = "90f47c78ea98277cb1f5e6f60ba4fc762f5eafe9f6511bc2f7dfd8b75c225650" +dependencies = [ + "async-io", + "futures-lite", + "multitask", + "parking 1.0.6", + "scoped-tls", + "waker-fn", +] + +[[package]] +name = "async-io" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae22a338d28c75b53702b66f77979062cb29675db376d99e451af4fa79dedb3" +dependencies = [ + "cfg-if", + "concurrent-queue", + "futures-lite", + "libc", + "once_cell", + "parking 2.0.0", + "polling", + "socket2", + "vec-arena", + "wepoll-sys-stjepang", + "winapi 0.3.9", +] + +[[package]] +name = "async-task" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17772156ef2829aadc587461c7753af20b7e8db1529bc66855add962a3b35d3" + +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" [[package]] name = "autocfg" @@ -39,12 +89,37 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blocking" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea5800d29218fea137b0880387e5948694a23c93fcdde157006966693a865c7c" +dependencies = [ + "async-channel", + "atomic-waker", + "futures-lite", + "once_cell", + "waker-fn", +] + [[package]] name = "bytes" version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" +[[package]] +name = "cache-padded" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" + +[[package]] +name = "cc" +version = "1.0.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66120af515773fb005778dc07c261bd201ec8ce50bd6e7144c927753fe013381" + [[package]] name = "cfg-if" version = "0.1.10" @@ -60,6 +135,15 @@ dependencies = [ "bitflags", ] +[[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + [[package]] name = "derive_more" version = "0.99.9" @@ -71,6 +155,12 @@ dependencies = [ "syn", ] +[[package]] +name = "easy-parallel" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd4afd79212583ff429b913ad6605242ed7eec277e950b1438f300748f948f4" + [[package]] name = "encoding_rs" version = "0.8.23" @@ -80,6 +170,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "event-listener" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cd41440ae7e4734bbd42302f63eaba892afc93a3912dad84006247f0dedb0e" + [[package]] name = "fallible-iterator" version = "0.2.0" @@ -92,6 +188,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" +[[package]] +name = "fastrand" +version = "1.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bd3bdaaf0a72155260a1c098989b60db1cbb22d6a628e64f16237aa4da93cc7" + [[package]] name = "fnv" version = "1.0.7" @@ -168,6 +270,21 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" +[[package]] +name = "futures-lite" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97999970129b808f0ccba93211201d431fcc12d7e1ffae03a61b5cedd1a7ced2" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking 2.0.0", + "pin-project-lite", + "waker-fn", +] + [[package]] name = "futures-macro" version = "0.3.5" @@ -248,12 +365,11 @@ name = "hedge" version = "0.1.0" dependencies = [ "anyhow", - "futures", "hyper", "multer", "nanoid", "rusqlite", - "tokio", + "smol", "url", ] @@ -440,35 +556,12 @@ dependencies = [ "kernel32-sys", "libc", "log", - "miow 0.2.1", + "miow", "net2", "slab", "winapi 0.2.8", ] -[[package]] -name = "mio-named-pipes" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0840c1c50fd55e521b247f949c241c9997709f23bd7f023b9762cd561e935656" -dependencies = [ - "log", - "mio", - "miow 0.3.5", - "winapi 0.3.9", -] - -[[package]] -name = "mio-uds" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" -dependencies = [ - "iovec", - "libc", - "mio", -] - [[package]] name = "miow" version = "0.2.1" @@ -481,16 +574,6 @@ dependencies = [ "ws2_32-sys", ] -[[package]] -name = "miow" -version = "0.3.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e" -dependencies = [ - "socket2", - "winapi 0.3.9", -] - [[package]] name = "multer" version = "1.2.2" @@ -510,6 +593,17 @@ dependencies = [ "twoway", ] +[[package]] +name = "multitask" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c09c35271e7dcdb5f709779111f2c8e8ab8e06c1b587c1c6a9e179d865aaa5b4" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", +] + [[package]] name = "nanoid" version = "0.3.0" @@ -546,6 +640,18 @@ version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad" +[[package]] +name = "parking" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb300f271742d4a2a66c01b6b2fa0c83dfebd2e0bf11addb879a3547b4ed87c" + +[[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -590,6 +696,18 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" +[[package]] +name = "polling" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e09dffb745feffca5be3dea51c02b7b368c4597ab0219a82acaf9799ab3e0d1" +dependencies = [ + "cfg-if", + "libc", + "wepoll-sys-stjepang", + "winapi 0.3.9", +] + [[package]] name = "proc-macro-hack" version = "0.5.18" @@ -775,14 +893,10 @@ dependencies = [ ] [[package]] -name = "signal-hook-registry" -version = "1.2.1" +name = "scoped-tls" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e12110bc539e657a646068aaf5eb5b63af9d0c1f7b29c97113fad80e15f035" -dependencies = [ - "arc-swap", - "libc", -] +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" [[package]] name = "slab" @@ -796,6 +910,23 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" +[[package]] +name = "smol" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67583f4ccc13bbb105a0752058d8ad66c47753d85445952809bcaca891954f83" +dependencies = [ + "async-channel", + "async-executor", + "async-io", + "blocking", + "cfg-if", + "easy-parallel", + "futures-lite", + "num_cpus", + "tokio", +] + [[package]] name = "socket2" version = "0.3.12" @@ -855,28 +986,11 @@ dependencies = [ "futures-core", "iovec", "lazy_static", - "libc", "memchr", "mio", - "mio-named-pipes", - "mio-uds", "num_cpus", "pin-project-lite", - "signal-hook-registry", "slab", - "tokio-macros", - "winapi 0.3.9", -] - -[[package]] -name = "tokio-macros" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" -dependencies = [ - "proc-macro2", - "quote", - "syn", ] [[package]] @@ -982,6 +1096,18 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" +[[package]] +name = "vec-arena" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cb18268690309760d59ee1a9b21132c126ba384f374c59a94db4bc03adeb561" + +[[package]] +name = "waker-fn" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9571542c2ce85ce642e6b58b3364da2fb53526360dfb7c211add4f5c23105ff7" + [[package]] name = "want" version = "0.3.0" @@ -992,6 +1118,15 @@ dependencies = [ "try-lock", ] +[[package]] +name = "wepoll-sys-stjepang" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fd319e971980166b53e17b1026812ad66c6b54063be879eb182342b55284694" +dependencies = [ + "cc", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index b180b0d..ca60681 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,11 +8,13 @@ edition = "2018" [dependencies] hyper = "0.13" -tokio = { version = "0.2", features = ["full"] } rusqlite = "0.24.0" nanoid = "0.3.0" url = "2.1.1" anyhow = "1.0" -futures = "0.3.5" multer = "1.2" + +[dependencies.smol] +version = "0.3.3" +features = [ "tokio02" ] diff --git a/src/main.rs b/src/main.rs index 769ed03..86af042 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,18 @@ use url::form_urlencoded; use std::collections::HashMap; use std::path::Path; +fn respond_with_shortlink>(shortlink: S) -> Response { + Response::builder() + .status(StatusCode::OK) + .header("content-type", "text/html") + .body(Body::from(shortlink.as_ref().to_string())) + .unwrap() +} + +fn respond_with_status(s: StatusCode) -> Response { + Response::builder().status(s).body(Body::empty()).unwrap() +} + fn shorten>(url: S, conn: &mut Connection) -> Result { let mut stmt = conn.prepare("select * from urls where link = ?1")?; let mut rows = stmt.query(params![url.as_ref().to_string()])?; @@ -36,14 +48,22 @@ fn get_link>(url: S, conn: &mut Connection) -> Result Result> { +async fn process_multipart( + body: Body, + boundary: String, + conn: &mut Connection, +) -> Result> { let mut m = Multipart::new(body, boundary); if let Some(field) = m.next_field().await? { - let content = field - .text() - .await - .with_context(|| format!("Expected field name"))?; - eprintln!("{}", content); + if field.name() == Some("shorten") { + let content = field + .text() + .await + .with_context(|| format!("Expected field name"))?; + + let shortlink = shorten(content, conn)?; + return Ok(respond_with_shortlink(shortlink)); + } } Ok(Response::builder() .status(StatusCode::OK) @@ -61,7 +81,6 @@ async fn shortner_service(req: Request) -> Result> { .and_then(|ct| ct.to_str().ok()) .and_then(|ct| multer::parse_boundary(ct).ok()); - // Send `BAD_REQUEST` status if the content-type is not multipart/form-data. if boundary.is_none() { let b = hyper::body::to_bytes(req) .await @@ -72,27 +91,14 @@ async fn shortner_service(req: Request) -> Result> { .collect::>(); if let Some(n) = params.get("shorten") { - let shortlink = shorten(n, &mut conn)?; - eprintln!("{}", shortlink); - let res = Response::builder() - .status(StatusCode::OK) - .header("content-type", "text/html") - .body(Body::from(shortlink))?; - return Ok(res); + let s = shorten(n, &mut conn)?; + return Ok(respond_with_shortlink(s)); } else { - eprintln!("hello world"); - let res = Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(Body::empty())?; - return Ok(res); + return Ok(respond_with_status(StatusCode::UNPROCESSABLE_ENTITY)); } } - if let Ok(res) = process_multipart(req.into_body(), boundary.unwrap()).await { - return Ok(res); - } else { - panic!("nani"); - } + return process_multipart(req.into_body(), boundary.unwrap(), &mut conn).await; } &Method::GET => { let shortlink = req.uri().path().to_string(); @@ -135,15 +141,14 @@ fn init_db>(p: P) -> Result { return Ok(conn); } -#[tokio::main] -async fn main() -> Result<()> { - let addr = ([127, 0, 0, 1], 3000).into(); - - let service = - make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); - - let server = Server::bind(&addr).serve(service); - println!("Listening on http://{}", addr); - server.await.unwrap(); - Ok(()) +fn main() -> Result<()> { + smol::run(async { + let addr = ([127, 0, 0, 1], 3000).into(); + let service = + make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(shortner_service)) }); + let server = Server::bind(&addr).serve(service); + println!("Listening on http://{}", addr); + server.await.unwrap(); + Ok(()) + }) } -- cgit v1.2.3