swimos / ratchet

Ratchet is a fast, robust, lightweight and fully asynchronous implementation of RFC6455 (The WebSocket protocol).
Apache License 2.0
36 stars 9 forks source link

Consider adding `ExtensionProvider` impl for `Option<ExtensionProvider>` #50

Closed nakedible-p closed 1 hour ago

nakedible-p commented 2 days ago

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 commented 2 days ago

@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 commented 2 days ago

This is unrelated, but two other issues quickly:

SirCipher commented 2 days ago

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