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

View file

@ -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(())

View file

@ -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");

View file

@ -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<u8> {
bendy::serde::to_bytes(&self).unwrap()
}
}