Skip to content

Commit

Permalink
fix: return method string from buffer
Browse files Browse the repository at this point in the history
Instead of static constants for GET/POST, fixes #124
  • Loading branch information
AaronO committed Apr 24, 2023
1 parent fbb0bdd commit 625b7fb
Showing 1 changed file with 33 additions and 8 deletions.
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);
}
}

0 comments on commit 625b7fb

Please sign in to comment.