Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return method string from buffer #136

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,16 +870,22 @@ pub fn parse_method<'a>(bytes: &mut Bytes<'a>) -> Result<&'a str> {
const POST: [u8; 4] = *b"POST";
match bytes.peek_n::<[u8; 4]>(4) {
Some(GET) => {
unsafe {
bytes.advance_and_commit(4);
}
Ok(Status::Complete("GET"))
// SAFETY: matched the ASCII string and boundary checked
let method = unsafe {
bytes.advance(4);
let buf = bytes.slice_skip(1);
str::from_utf8_unchecked(buf)
};
Ok(Status::Complete(method))
}
Some(POST) if bytes.peek_ahead(4) == Some(b' ') => {
unsafe {
bytes.advance_and_commit(5);
}
Ok(Status::Complete("POST"))
// SAFETY: matched the ASCII string and boundary checked
let method = unsafe {
bytes.advance(5);
let buf = bytes.slice_skip(1);
str::from_utf8_unchecked(buf)
};
Ok(Status::Complete(method))
}
_ => parse_token(bytes),
}
Expand Down Expand Up @@ -2421,4 +2427,23 @@ mod tests {
assert_eq!(offsetnz(x), i);
}
}

#[test]
fn test_method_within_buffer() {
const REQUEST: &[u8] = b"GET / HTTP/1.1\r\n\r\n";

let mut headers = [EMPTY_HEADER; 0];
let mut request = Request::new(&mut headers[..]);

crate::ParserConfig::default()
.parse_request(&mut request, REQUEST)
.unwrap();

// SAFETY: will not wrap
let buf_end = unsafe { REQUEST.as_ptr().add(REQUEST.len()) };
// Check that the method str is within the buffer
let method = request.method.unwrap();
assert!(REQUEST.as_ptr() <= method.as_ptr());
assert!(method.as_ptr() <= buf_end);
}
}