diff --git a/Cargo.lock b/Cargo.lock index a25ba70..04171bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,20 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "addr2line" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "602d785912f476e480434627e8732e6766b760c045bbf897d9dfaa9f4fbd399c" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler32" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b077b825e468cc974f0020d4082ee6e03132512f207ef1a02fd5d00d1f32d" + [[package]] name = "ahash" version = "0.3.5" @@ -46,6 +61,7 @@ dependencies = [ "anyhow", "aquatic_cli_helpers", "aquatic_common", + "bendy", "either", "flume", "hashbrown", @@ -219,12 +235,37 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" +[[package]] +name = "backtrace" +version = "0.3.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05100821de9e028f12ae3d189176b41ee198341eb8f369956407fea2f5cc666c" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" +[[package]] +name = "bendy" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f35e865a4aae9618063f67b1cd1ff6d0ae28b9478f1cdc0dd88d61884c8fe71" +dependencies = [ + "failure", + "serde", + "serde_bytes", +] + [[package]] name = "bitflags" version = "1.2.1" @@ -402,6 +443,28 @@ dependencies = [ "regex", ] +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + [[package]] name = "fake-simd" version = "0.1.2" @@ -520,6 +583,12 @@ dependencies = [ "wasi", ] +[[package]] +name = "gimli" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc8e0c9bce37868955864dbecd2b1ab2bdf967e6f28066d65aaac620444b65c" + [[package]] name = "gumdrop" version = "0.7.0" @@ -693,6 +762,15 @@ dependencies = [ "libmimalloc-sys", ] +[[package]] +name = "miniz_oxide" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791daaae1ed6889560f8c4359194f56648355540573244a5448a83ba1ecc7435" +dependencies = [ + "adler32", +] + [[package]] name = "mio" version = "0.7.0" @@ -866,6 +944,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a" +[[package]] +name = "object" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5" + [[package]] name = "opaque-debug" version = "0.2.3" @@ -1140,6 +1224,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "rustc-demangle" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" + [[package]] name = "ryu" version = "1.0.4" @@ -1194,6 +1284,15 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde_bytes" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.110" @@ -1286,6 +1385,18 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "synstructure" +version = "0.12.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "tempfile" version = "3.1.0" diff --git a/TODO.md b/TODO.md index f286c52..fa27a93 100644 --- a/TODO.md +++ b/TODO.md @@ -9,9 +9,8 @@ * handshake stuff * fix overcomplicated and probably incorrect implementation * support TLS and plain at the same time?? -* response serialization, https://crates.io/crates/bendy -* response content length -* scrape info hash parsing + * really close connections after sending response?? +* scrape info hash parsing: multiple ought to be accepted * move stuff to common crate with ws: what about Request/InMessage etc? ## aquatic_ws diff --git a/aquatic_http/Cargo.toml b/aquatic_http/Cargo.toml index 5c57b97..c8f2e13 100644 --- a/aquatic_http/Cargo.toml +++ b/aquatic_http/Cargo.toml @@ -17,6 +17,7 @@ path = "src/bin/main.rs" anyhow = "1" aquatic_cli_helpers = { path = "../aquatic_cli_helpers" } aquatic_common = { path = "../aquatic_common" } +bendy = { version = "0.3", features = ["std", "serde"] } either = "1" flume = "0.7" hashbrown = { version = "0.7", features = ["serde"] } diff --git a/aquatic_http/src/lib/network/connection.rs b/aquatic_http/src/lib/network/connection.rs index 2b7e902..852044f 100644 --- a/aquatic_http/src/lib/network/connection.rs +++ b/aquatic_http/src/lib/network/connection.rs @@ -160,14 +160,17 @@ impl EstablishedConnection { request } - pub fn send_response(&mut self, body: &str) -> ::std::io::Result<()> { - let mut response = String::new(); + pub fn send_response(&mut self, body: &[u8]) -> ::std::io::Result<()> { + let mut response = Vec::new(); - response.push_str("HTTP/1.1 200 OK\r\n\r\n"); // FIXME: content-length - response.push_str(body); - response.push_str("\r\n"); + response.extend_from_slice(b"HTTP/1.1 200 OK\r\n"); + response.extend_from_slice(b"Content-Length: "); + response.extend_from_slice(format!("{}", body.len() + 2).as_bytes()); + response.extend_from_slice(b"\r\n\r\n"); + response.extend_from_slice(body); + response.extend_from_slice(b"\r\n"); - self.stream.write(response.as_bytes())?; + self.stream.write(&response)?; self.stream.flush()?; Ok(()) diff --git a/aquatic_http/src/lib/network/mod.rs b/aquatic_http/src/lib/network/mod.rs index 2de3745..0c54503 100644 --- a/aquatic_http/src/lib/network/mod.rs +++ b/aquatic_http/src/lib/network/mod.rs @@ -249,7 +249,7 @@ pub fn send_responses( continue; } - match established.send_response(&response.to_http_string()){ + match established.send_response(&response.to_bytes()){ Ok(()) => { debug!("sent response"); diff --git a/aquatic_http/src/lib/protocol/mod.rs b/aquatic_http/src/lib/protocol/mod.rs index 59f73ba..20ca46b 100644 --- a/aquatic_http/src/lib/protocol/mod.rs +++ b/aquatic_http/src/lib/protocol/mod.rs @@ -169,7 +169,7 @@ pub enum Response { impl Response { - pub fn to_http_string(self) -> String { - "(response)".to_string() + pub fn to_bytes(self) -> Vec { + bendy::serde::to_bytes(&self).unwrap() } } \ No newline at end of file