Skip to content

Commit 55bc399

Browse files
committed
Fix Content-Length header value, make Response 'static for now and export native_tls::Identity
1 parent 5649508 commit 55bc399

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

mashrl/server.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use super::http;
55

66
use std::{
77
borrow::Cow,
8-
io::{BufRead, BufReader, Write},
8+
io::{BufRead, BufReader, Read, Write},
99
net::TcpListener,
1010
};
1111

12+
pub use native_tls::Identity;
13+
1214
pub struct LocalHost {
1315
port: u16,
1416
}
@@ -64,7 +66,7 @@ pub type Body<'a> = Box<dyn std::io::Read + Send + 'a>;
6466
pub fn open_server(
6567
port: impl std::net::ToSocketAddrs,
6668
configuration: ServerConfiguration,
67-
callback: impl for<'a> Fn(http::Request<'a, Body<'a>>) -> http::Response<'a>,
69+
callback: impl for<'a> Fn(http::Request<'a, Body<'a>>) -> http::Response<'static>,
6870
) {
6971
let listener = TcpListener::bind(port).expect("could not open listener on port");
7072

@@ -85,9 +87,10 @@ pub fn open_server(
8587
let method;
8688
let path;
8789

88-
let mut transfer_encoding_range = None;
89-
let mut content_encoding_range = None;
90-
let mut _content_length = 0;
90+
let mut transfer_encoding_range: Option<std::ops::Range<usize>> = None;
91+
let mut content_encoding_range: Option<std::ops::Range<usize>> = None;
92+
// Not sure what the default value should be here
93+
let mut content_length: u64 = 0;
9194

9295
// Parse response
9396
{
@@ -135,11 +138,10 @@ pub fn open_server(
135138
break;
136139
}
137140

138-
// octets!!!?
139141
if let Some(value) = line.strip_prefix("Content-Length: ")
140-
&& let Ok(value) = u64::from_str_radix(value, 8)
142+
&& let Ok(value) = <u64 as std::str::FromStr>::from_str(value)
141143
{
142-
_content_length = value;
144+
content_length = value;
143145
}
144146

145147
if line.starts_with("Transfer-Encoding: ") {
@@ -148,7 +150,7 @@ pub fn open_server(
148150

149151
// Chunked responses should not be limited
150152
if line.contains("chunked") {
151-
_content_length = u64::MAX;
153+
content_length = u64::MAX;
152154
}
153155
}
154156

@@ -159,9 +161,8 @@ pub fn open_server(
159161
}
160162
}
161163

162-
#[allow(unused_mut)]
163-
let reference = stream.get_ref();
164-
let mut reader: Body<'_> = Box::new(reference.try_clone().unwrap());
164+
let mut reader: Body<'_> =
165+
Box::new(std::io::Read::by_ref(&mut stream).take(content_length));
165166

166167
// Add transformations to reader dependending on flags
167168
{

0 commit comments

Comments
 (0)