aquatic_http: bencoded response with content-length

This commit is contained in:
Joakim Frostegård 2020-07-02 13:45:56 +02:00
parent a487347a0d
commit 6b1f11635b
6 changed files with 126 additions and 12 deletions

111
Cargo.lock generated
View file

@ -1,5 +1,20 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # 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]] [[package]]
name = "ahash" name = "ahash"
version = "0.3.5" version = "0.3.5"
@ -46,6 +61,7 @@ dependencies = [
"anyhow", "anyhow",
"aquatic_cli_helpers", "aquatic_cli_helpers",
"aquatic_common", "aquatic_common",
"bendy",
"either", "either",
"flume", "flume",
"hashbrown", "hashbrown",
@ -219,12 +235,37 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" 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]] [[package]]
name = "base64" name = "base64"
version = "0.11.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" 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]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.2.1"
@ -402,6 +443,28 @@ dependencies = [
"regex", "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]] [[package]]
name = "fake-simd" name = "fake-simd"
version = "0.1.2" version = "0.1.2"
@ -520,6 +583,12 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "gimli"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcc8e0c9bce37868955864dbecd2b1ab2bdf967e6f28066d65aaac620444b65c"
[[package]] [[package]]
name = "gumdrop" name = "gumdrop"
version = "0.7.0" version = "0.7.0"
@ -693,6 +762,15 @@ dependencies = [
"libmimalloc-sys", "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]] [[package]]
name = "mio" name = "mio"
version = "0.7.0" version = "0.7.0"
@ -866,6 +944,12 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a" checksum = "17b02fc0ff9a9e4b35b3342880f48e896ebf69f2967921fe8646bf5b7125956a"
[[package]]
name = "object"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ab52be62400ca80aa00285d25253d7f7c437b7375c4de678f5405d3afe82ca5"
[[package]] [[package]]
name = "opaque-debug" name = "opaque-debug"
version = "0.2.3" version = "0.2.3"
@ -1140,6 +1224,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.4" version = "1.0.4"
@ -1194,6 +1284,15 @@ dependencies = [
"serde_derive", "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]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.110" version = "1.0.110"
@ -1286,6 +1385,18 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.1.0" version = "3.1.0"

View file

@ -9,9 +9,8 @@
* handshake stuff * handshake stuff
* fix overcomplicated and probably incorrect implementation * fix overcomplicated and probably incorrect implementation
* support TLS and plain at the same time?? * support TLS and plain at the same time??
* response serialization, https://crates.io/crates/bendy * really close connections after sending response??
* response content length * scrape info hash parsing: multiple ought to be accepted
* scrape info hash parsing
* move stuff to common crate with ws: what about Request/InMessage etc? * move stuff to common crate with ws: what about Request/InMessage etc?
## aquatic_ws ## aquatic_ws

View file

@ -17,6 +17,7 @@ path = "src/bin/main.rs"
anyhow = "1" anyhow = "1"
aquatic_cli_helpers = { path = "../aquatic_cli_helpers" } aquatic_cli_helpers = { path = "../aquatic_cli_helpers" }
aquatic_common = { path = "../aquatic_common" } aquatic_common = { path = "../aquatic_common" }
bendy = { version = "0.3", features = ["std", "serde"] }
either = "1" either = "1"
flume = "0.7" flume = "0.7"
hashbrown = { version = "0.7", features = ["serde"] } hashbrown = { version = "0.7", features = ["serde"] }

View file

@ -160,14 +160,17 @@ impl EstablishedConnection {
request request
} }
pub fn send_response(&mut self, body: &str) -> ::std::io::Result<()> { pub fn send_response(&mut self, body: &[u8]) -> ::std::io::Result<()> {
let mut response = String::new(); let mut response = Vec::new();
response.push_str("HTTP/1.1 200 OK\r\n\r\n"); // FIXME: content-length response.extend_from_slice(b"HTTP/1.1 200 OK\r\n");
response.push_str(body); response.extend_from_slice(b"Content-Length: ");
response.push_str("\r\n"); 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()?; self.stream.flush()?;
Ok(()) Ok(())

View file

@ -249,7 +249,7 @@ pub fn send_responses(
continue; continue;
} }
match established.send_response(&response.to_http_string()){ match established.send_response(&response.to_bytes()){
Ok(()) => { Ok(()) => {
debug!("sent response"); debug!("sent response");

View file

@ -169,7 +169,7 @@ pub enum Response {
impl Response { impl Response {
pub fn to_http_string(self) -> String { pub fn to_bytes(self) -> Vec<u8> {
"(response)".to_string() bendy::serde::to_bytes(&self).unwrap()
} }
} }