Skip to content

Commit

Permalink
fix(http1): apply header title case for consecutive dashes (#2613)
Browse files Browse the repository at this point in the history
Fix the header title-casing to work with consecutive
dashes. Previously with two dashes in a row the first dash would
uppercase the second dash which would then not count, so
`weird--header` would be cased as `Weird--header` instead of
`Weird--Header`.
  • Loading branch information
blyxxyz committed Aug 6, 2021
1 parent 73bff4e commit 684f2fa
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/proto/h1/role.rs
Expand Up @@ -1308,36 +1308,18 @@ fn record_header_indices(
Ok(())
}

// Write header names as title case. The header name is assumed to be ASCII,
// therefore it is trivial to convert an ASCII character from lowercase to
// uppercase. It is as simple as XORing the lowercase character byte with
// space.
// Write header names as title case. The header name is assumed to be ASCII.
fn title_case(dst: &mut Vec<u8>, name: &[u8]) {
dst.reserve(name.len());

let mut iter = name.iter();

// Uppercase the first character
if let Some(c) = iter.next() {
if *c >= b'a' && *c <= b'z' {
dst.push(*c ^ b' ');
} else {
dst.push(*c);
}
}

while let Some(c) = iter.next() {
dst.push(*c);

if *c == b'-' {
if let Some(c) = iter.next() {
if *c >= b'a' && *c <= b'z' {
dst.push(*c ^ b' ');
} else {
dst.push(*c);
}
}
// Ensure first character is uppercased
let mut prev = b'-';
for &(mut c) in name {
if prev == b'-' {
c.make_ascii_uppercase();
}
dst.push(c);
prev = c;
}
}

Expand Down Expand Up @@ -2316,6 +2298,8 @@ mod tests {
.insert("content-length", HeaderValue::from_static("10"));
head.headers
.insert("content-type", HeaderValue::from_static("application/json"));
head.headers
.insert("weird--header", HeaderValue::from_static(""));

let mut vec = Vec::new();
Server::encode(
Expand All @@ -2331,7 +2315,7 @@ mod tests {
.unwrap();

let expected_response =
b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nContent-Type: application/json\r\n";
b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\nContent-Type: application/json\r\nWeird--Header: \r\n";

assert_eq!(&vec[..expected_response.len()], &expected_response[..]);
}
Expand Down

0 comments on commit 684f2fa

Please sign in to comment.