Skip to content

Add a new constructor and an accessor to Upgrade #105

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions src/common/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,23 @@ impl Upgrade {
pub fn websocket() -> Upgrade {
Upgrade(HeaderValue::from_static("websocket"))
}

/// Constructs an `Upgrade: HTTP/2.0` header.
pub fn http2() -> Upgrade {
// must be uppercase, see
// <https://datatracker.ietf.org/doc/html/rfc7230#section-2.6>
Upgrade(HeaderValue::from_static("HTTP/2.0"))
Copy link
Member

Choose a reason for hiding this comment

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

The spec says the header value would be h2c.

Copy link
Member

Choose a reason for hiding this comment

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

But I'm not sure we need this, it's not really used.

Copy link
Author

Choose a reason for hiding this comment

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

Understood - I added an h2c constructor for that case and noted that ALPN/prior knowledge are overwhelmingly more common for the "http/2 over TLS" case.

}

/// Constructs an `Upgrade` header with the given `HeaderValue`
pub fn new(value: HeaderValue) -> Upgrade {
Upgrade(value)
}

/// Returns the header value, e.g. "websocket" or "HTTP/2.0", or a
/// comma-separated list of protocols, see RFC 7230:
/// <https://datatracker.ietf.org/doc/html/rfc7230#section-6.7>
pub fn value(&self) -> &HeaderValue {
Copy link
Member

Choose a reason for hiding this comment

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

Some of the typed headers don't hold onto a HeaderValue internally, we might change it here too. So I wouldn't want to return a reference to that type specifically...

Copy link
Author

Choose a reason for hiding this comment

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

Got it. For now I changed it to return an Option<&str> instead.

&self.0
}
}