Skip to content

Commit 09ac22c

Browse files
committed
feat: body is sent as bytes instead of str slice
Signed-off-by: Martin <martin@hotmail.com.br>
1 parent a7cf4cd commit 09ac22c

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/infrastructure/server_impl/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ pub struct Request<'a> {
66
pub method: Method,
77
pub headers: EnumMap<Header, &'a str>,
88
pub resource: &'a str,
9-
pub body: Option<&'a str>,
9+
pub body: Option<&'a [u8]>,
1010
}

src/infrastructure/server_impl/server.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,13 @@ fn to_str(str_like: &[u8]) -> &str {
107107
unsafe { std::str::from_utf8_unchecked(str_like) }
108108
}
109109

110-
fn parse_body(body: &[u8]) -> Option<&str> {
110+
fn parse_body(body: &[u8]) -> Option<&[u8]> {
111111
if body.is_empty() || body.first() == Some(&b'\0') {
112112
return None;
113113
}
114114

115115
let body_content = memchr(b'\0', body).map(|idx| &body[..idx]).unwrap_or(body);
116-
Some(to_str(body_content))
116+
Some(body_content)
117117
}
118118

119119
/// Returns a [Request]
@@ -128,10 +128,11 @@ pub fn parse_http(request: &[u8]) -> AnyResult<Request> {
128128

129129
let method = Method::from_str(req.method.unwrap()).unwrap();
130130
let resource = req.path.unwrap();
131+
131132
let headers = req
132133
.headers
133134
.iter()
134-
.map(|c| (Header::from_str(c.name).unwrap(), to_str(c.value)))
135+
.filter_map(|c| Header::from_str(c.name).ok().map(|h| (h, to_str(c.value))))
135136
.collect::<EnumMap<_, _>>();
136137

137138
let body = match body {
@@ -162,7 +163,7 @@ mod tests {
162163
request.headers[Header::CONTENT_TYPE],
163164
"text/html; charset=ISO-8859-4"
164165
);
165-
assert_eq!(request.body, Some(r#"{"json_key": 10}"#));
166+
assert_eq!(to_str(request.body.unwrap()), r#"{"json_key": 10}"#);
166167
}
167168

168169
#[test]

0 commit comments

Comments
 (0)