Skip to content

header name: add from_static method #195

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

Merged
merged 2 commits into from
May 1, 2018
Merged

header name: add from_static method #195

merged 2 commits into from
May 1, 2018

Conversation

smithsps
Copy link
Contributor

@smithsps smithsps commented Apr 3, 2018

Implements: #168 : HeaderName::from_static

Converts a static string to a HTTP header name.
Function panics on an invalid headers, and requires lowercase characters, as so the static string will have the same format as the internal format.

Function should also be zero copy, beyond what is done in parse_hdr, with the static slice being used for the ByteStr in custom headers.

@smithsps
Copy link
Contributor Author

smithsps commented Apr 4, 2018

Nightly build breaking on std::ascii::AsciiExt deprecation, which is currently fixed with the #194 PR's added flags.

Copy link
Member

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay! Thanks for doing this! I've left some thoughts inline. (And merged to master a fix for the AsciiExt deprecation errors.)

///
/// // Parsing a header that contains invalid uppercase characters.
/// let a = HeaderName::from_static("foobar");
/// let b = HeaderName::from_static("FOOBAR"); // This line panics!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does? Looking at the implementation, it looks like the block with lower: false will still parse successfully. I might not understand all the details of this file, though...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little weird, but should be correct with the same checking method in from_lower.
However, I agree it is unclear and can be improved.

let b = HeaderName::from_static("vaary");
assert_ne!(a, b);

let result = panic::catch_unwind(|| HeaderName::from_static("Vary"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if each of these that should panic were separate #[test] #[should_panic] fn foo() {} test cases, so that catch_unwind wouldn't be needed? I think it might be clearer in reading. For example:

#[test]
#[should_panic]
fn from_static_uppercase() {
    HeaderName::from_static("Vary");
}

Since this would make a bunch of test functions, it might also be a good idea to push these into an inner #[cfg(test)] mod tests { ... }. Whatcha think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely agree, and the other tests could also be moved into the block too.

Copy link
Member

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making these changes! (And sorry I forgot about this!).

@seanmonstar seanmonstar merged commit 6df1b6f into hyperium:master May 1, 2018
@smithsps
Copy link
Contributor Author

smithsps commented May 1, 2018

No worries, and thanks for all your work on Hyperium!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants