Skip to content
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
21 changes: 20 additions & 1 deletion src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{cmp, fmt, hash, str};

use bytes::Bytes;

use super::{ErrorKind, InvalidUri};
use super::{ErrorKind, InvalidUri, MAX_LEN};
use crate::byte_str::ByteStr;

/// Represents the path component of a URI
Expand Down Expand Up @@ -475,6 +475,10 @@ const fn scan_path_and_query(bytes: &[u8]) -> Result<Scanned, ErrorKind> {
return Err(ErrorKind::Empty);
}

if bytes.len() > MAX_LEN {
return Err(ErrorKind::TooLong);
}

if bytes.len() == 1 && bytes[0] == b'*' {
return Ok(Scanned {
query,
Expand Down Expand Up @@ -676,6 +680,21 @@ mod tests {
PathAndQuery::try_from(&[b'/', b'a', b'?', 0x7F][..]).expect_err("reject DEL");
}

#[test]
fn rejects_too_long_path_and_query() {
let path = format!("/{}?query", "a".repeat(MAX_LEN));
let err = PathAndQuery::try_from(path).expect_err("reject overly long path and query");
assert_eq!(err.0, ErrorKind::TooLong);
}

#[test]
fn accepts_max_length_path_and_query() {
let path = format!("/{}?", "a".repeat(MAX_LEN - 2));
let path_and_query = PathAndQuery::try_from(path).expect("accept maximum length");
assert_eq!(path_and_query.as_str().len(), MAX_LEN);
assert_eq!(path_and_query.query(), Some(""));
}

#[test]
fn json_is_fine() {
assert_eq!(
Expand Down