From 0e6163dcd9c5a8f42e16503d6071cf474bc0bb5d Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Sun, 17 May 2020 12:14:29 -0700 Subject: [PATCH] Url parsing --- Cargo.lock | 123 +++++++++++++++++++--------------------------------- Cargo.toml | 2 +- src/main.rs | 13 +++--- 3 files changed, 52 insertions(+), 86 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c717a29..ecddd5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,17 +6,8 @@ version = "1.0.0" dependencies = [ "async-std", "async-tls", - "env_logger", "rustls", -] - -[[package]] -name = "aho-corasick" -version = "0.7.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada" -dependencies = [ - "memchr", + "url", ] [[package]] @@ -66,17 +57,6 @@ dependencies = [ "webpki-roots", ] -[[package]] -name = "atty" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" -dependencies = [ - "hermit-abi", - "libc", - "winapi 0.3.8", -] - [[package]] name = "autocfg" version = "1.0.0" @@ -160,19 +140,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "env_logger" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -300,12 +267,14 @@ dependencies = [ ] [[package]] -name = "humantime" -version = "1.3.0" +name = "idna" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" dependencies = [ - "quick-error", + "matches", + "unicode-bidi", + "unicode-normalization", ] [[package]] @@ -366,6 +335,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + [[package]] name = "maybe-uninit" version = "2.0.0" @@ -456,6 +431,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + [[package]] name = "pin-project" version = "0.4.16" @@ -509,12 +490,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "quick-error" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" - [[package]] name = "quote" version = "1.0.5" @@ -524,24 +499,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "regex" -version = "1.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6020f034922e3194c711b82a627453881bc4682166cabb07134a10c26ba7692" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", - "thread_local", -] - -[[package]] -name = "regex-syntax" -version = "0.6.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae" - [[package]] name = "ring" version = "0.16.13" @@ -592,6 +549,12 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" +[[package]] +name = "smallvec" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4" + [[package]] name = "spin" version = "0.5.2" @@ -610,21 +573,21 @@ dependencies = [ ] [[package]] -name = "termcolor" -version = "1.1.0" +name = "unicode-bidi" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" dependencies = [ - "winapi-util", + "matches", ] [[package]] -name = "thread_local" -version = "1.0.1" +name = "unicode-normalization" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14" +checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" dependencies = [ - "lazy_static", + "smallvec", ] [[package]] @@ -639,6 +602,17 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" +[[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" +dependencies = [ + "idna", + "matches", + "percent-encoding", +] + [[package]] name = "wasm-bindgen" version = "0.2.62" @@ -750,15 +724,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi 0.3.8", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 766c4c0..46d2577 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ edition = "2018" async-tls = "0.7.0" async-std = "1.5" rustls = "0.17.0" -env_logger = "0.7.1" +url = "2.1" [profile.release] lto = true diff --git a/src/main.rs b/src/main.rs index c09e840..4ab62f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,15 +12,15 @@ use { error::Error, fs::File, io::BufReader, + path::Path, sync::Arc, }, + url::Url, }; pub type Result = std::result::Result>; fn main() -> Result { - env_logger::init(); - let certs = certs(&mut BufReader::new(File::open("tests/cert.pem")?)) .expect("Error reading certificate file"); let mut keys = rsa_private_keys(&mut BufReader::new(File::open("tests/key.rsa")?)) @@ -54,14 +54,15 @@ async fn connection(acceptor: TlsAcceptor, stream: TcpStream) -> Result { let stream = acceptor.accept(stream).await?; let mut stream = async_std::io::BufReader::new(stream); - let mut body = String::new(); - stream.read_line(&mut body).await?; - eprintln!("Got request: {:?}", body); + let mut request = String::new(); + stream.read_line(&mut request).await?; + let url = Url::parse(request.trim())?; + eprintln!("Got request: {:?}", url); let mut stream = stream.into_inner(); stream.write_all(b"20 text/gemini\r\n").await?; stream.write_all(b"=> ").await?; - stream.write_all(body.trim().as_bytes()).await?; + stream.write_all(url.as_str().as_bytes()).await?; stream.write_all(b" Reload\r\n").await?; Ok(()) }