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

Consider adding ExtensionProvider impl for Option<ExtensionProvider> #50

Closed
nakedible-p opened this issue Sep 28, 2024 · 3 comments · Fixed by #53
Closed

Consider adding ExtensionProvider impl for Option<ExtensionProvider> #50

nakedible-p opened this issue Sep 28, 2024 · 3 comments · Fixed by #53

Comments

@nakedible-p
Copy link

We need to be able to disable compression at runtime, based on a flag. Meaning that even if the client proposes to use deflate, the server will not support that extension. How we solved that outside of Ratchet was simply:

struct OptionalProvider<T>(Option<T>);

impl<T: ExtensionProvider> ExtensionProvider for OptionalProvider<T> {
    type Extension = T::Extension;
    type Error = T::Error;
    fn apply_headers(&self, headers: &mut hyper::HeaderMap) {
        if let Some(provider) = &self.0 {
            provider.apply_headers(headers);
        }
    }
    fn negotiate_client(&self, headers: &hyper::HeaderMap) -> Result<Option<Self::Extension>, Self::Error> {
        self.0.as_ref().map_or(Ok(None), |provider| provider.negotiate_client(headers))
    }

    fn negotiate_server(&self, headers: &hyper::HeaderMap) -> Result<Option<(Self::Extension, ratchet_ext::HeaderValue)>, Self::Error> {
        self.0.as_ref().map_or(Ok(None), |provider| provider.negotiate_server(headers))
    }
}

But Ratchet could implement it directly for Option<T>.

Just a suggestion though, might be too obscure.

@SirCipher
Copy link
Member

SirCipher commented Sep 28, 2024

@nakedible-p the last release did actually implement Extension for Option which superseded the NegotiatedExtension system, however, I didn't add one for the ExtensionProvider. I'll get this added for the next release

@nakedible-p
Copy link
Author

This is unrelated, but two other issues quickly:

  • ratchet_core::server::handshake takes in &ExtensionProvider, but ratchet_core::server::response_from_headers takes in ExtensionProvider. Doesn't make a difference, but I don't think this is intentional.
  • Neither handshake nor response_from_headers returns the selected subprotocol, forcing the user to either look into the returned header manually, or to use parse_request_parts and build_response_headers manually.

@SirCipher
Copy link
Member

@nakedible-p I'll get that fixed in the next release too

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 a pull request may close this issue.

2 participants