Skip to content

Commit 9bfd5eb

Browse files
committed
refactor a big match statement on the server code
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent f7f2cde commit 9bfd5eb

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

src/server.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,35 +214,22 @@ where
214214
req = req.set_header(header.name, std::str::from_utf8(header.value)?)?;
215215
}
216216

217-
// Process the body if `Content-Length` was passed.
218-
if let Some(content_length) = httparse_req
219-
.headers
220-
.iter()
221-
.find(|h| h.name.eq_ignore_ascii_case("Content-Length"))
222-
{
223-
let length = std::str::from_utf8(content_length.value)
224-
.ok()
225-
.and_then(|s| s.parse::<usize>().ok());
226-
227-
if let Some(len) = length {
228-
req = req.set_body_reader(reader);
229-
req = req.set_len(len);
230-
231-
Ok(Some(DecodedRequest::WithBody(req)))
232-
} else {
233-
return Err("Invalid value for Content-Length".into());
234-
}
235-
} else {
236-
Ok(Some(DecodedRequest::WithoutBody(req, Box::new(reader))))
237-
}
217+
// Check for content-length, that determines determines whether we can parse
218+
// it with a known length, or need to use chunked encoding.
219+
let len = match req.header("Content-Length") {
220+
Some(len) => len.parse::<usize>()?,
221+
None => return Ok(Some(DecodedRequest::WithoutBody(req, Box::new(reader)))),
222+
};
223+
req = req.set_body_reader(reader).set_len(len);
224+
Ok(Some(DecodedRequest::WithBody(req)))
238225
}
239226

240-
/// A decoded response
241-
///
242-
/// Either a request with body stream OR a request without a
243-
/// a body stream paired with the underlying stream
227+
/// A decoded request
244228
pub enum DecodedRequest {
229+
/// The TCP connection is inside the request already, so the lifetimes match up.
245230
WithBody(Request),
231+
/// The TCP connection is *not* inside the request body, so we need to pass
232+
/// it along with it to make the lifetimes match up.
246233
WithoutBody(Request, Box<dyn BufRead + Unpin + Send + 'static>),
247234
}
248235

0 commit comments

Comments
 (0)