-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(client): add support for title case header names at the socket level #1497
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,7 @@ where | |
mut head: MessageHead<Self::Outgoing>, | ||
body: Option<BodyLength>, | ||
method: &mut Option<Method>, | ||
_title_case_headers: bool, | ||
dst: &mut Vec<u8>, | ||
) -> ::Result<Encoder> { | ||
trace!("Server::encode body={:?}, method={:?}", body, method); | ||
|
@@ -367,6 +368,7 @@ where | |
mut head: MessageHead<Self::Outgoing>, | ||
body: Option<BodyLength>, | ||
method: &mut Option<Method>, | ||
title_case_headers: bool, | ||
dst: &mut Vec<u8>, | ||
) -> ::Result<Encoder> { | ||
trace!("Client::encode body={:?}, method={:?}", body, method); | ||
|
@@ -391,7 +393,11 @@ where | |
} | ||
extend(dst, b"\r\n"); | ||
|
||
write_headers(&head.headers, dst); | ||
if title_case_headers { | ||
write_headers_title_case(&head.headers, dst); | ||
} else { | ||
write_headers(&head.headers, dst); | ||
} | ||
extend(dst, b"\r\n"); | ||
|
||
Ok(body) | ||
|
@@ -635,6 +641,44 @@ impl<'a> Iterator for HeadersAsBytesIter<'a> { | |
} | ||
} | ||
|
||
// 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. | ||
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' '); | ||
} | ||
} | ||
|
||
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' '); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn write_headers_title_case(headers: &HeaderMap, dst: &mut Vec<u8>) { | ||
for (name, value) in headers { | ||
title_case(dst, name.as_str().as_bytes()); | ||
extend(dst, b": "); | ||
extend(dst, value.as_bytes()); | ||
extend(dst, b"\r\n"); | ||
} | ||
} | ||
|
||
fn write_headers(headers: &HeaderMap, dst: &mut Vec<u8>) { | ||
for (name, value) in headers { | ||
extend(dst, name.as_str().as_bytes()); | ||
|
@@ -857,6 +901,21 @@ mod tests { | |
Client::decoder(&head, method).unwrap_err(); | ||
} | ||
|
||
#[test] | ||
fn test_client_request_encode_title_case() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, awesome! |
||
use http::header::HeaderValue; | ||
use proto::BodyLength; | ||
|
||
let mut head = MessageHead::default(); | ||
head.headers.insert("content-length", HeaderValue::from_static("10")); | ||
head.headers.insert("content-type", HeaderValue::from_static("application/json")); | ||
|
||
let mut vec = Vec::new(); | ||
Client::encode(head, Some(BodyLength::Known(10)), &mut None, true, &mut vec).unwrap(); | ||
|
||
assert_eq!(vec, b"GET / HTTP/1.1\r\nContent-Length: 10\r\nContent-Type: application/json\r\n\r\n".to_vec()); | ||
} | ||
|
||
#[cfg(feature = "nightly")] | ||
use test::Bencher; | ||
|
||
|
@@ -914,7 +973,7 @@ mod tests { | |
|
||
b.iter(|| { | ||
let mut vec = Vec::new(); | ||
Server::encode(head.clone(), Some(BodyLength::Known(10)), &mut None, &mut vec).unwrap(); | ||
Server::encode(head.clone(), Some(BodyLength::Known(10)), &mut None, false, &mut vec).unwrap(); | ||
assert_eq!(vec.len(), len); | ||
::test::black_box(vec); | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this will get a couple characters changed completely. Specifically,
|
and~
will become\\
and^
, respectively. Probably not super common, but it'd be a semantic change...Also, it'd be nice to include some tests of
title_case
, perhaps at the bottom of the file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I forgot to think of those. A check to see if the byte falls between 97 (
a
) and 172 (z
) should suffice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
z
byte is 122, not 172 :DActually, to make it clearer, you can just write
b'a'
andb'z'
, which the compiler sees as the meaning the ASCII byte of the character.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read the octal field by accident from the man page. I fixed it to a
>= b'a'
and<= b'z'
.