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(headers): apply header title case for consecutive dashes #2613

Merged
merged 1 commit into from Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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