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

Axum integration #36

Open
SirCipher opened this issue Jul 25, 2024 · 3 comments
Open

Axum integration #36

SirCipher opened this issue Jul 25, 2024 · 3 comments

Comments

@SirCipher
Copy link
Member

No description provided.

@nakedible-p
Copy link

FWIW, we implemented a custom axum integration, since we need manual control over headers during negotiation. It was quite straightforward. Deflate extension negotiation was a bit annoying at the lower level, but all in all not a problem.

@SirCipher
Copy link
Member Author

@nakedible-p, thank you for the integration. I have got an open ticket for providing user defined headers that needs implementing #16. Regarding your last comment, what was annoying about the deflate extension negotiation? Was it the API or just integrating it into the Axum integration?

@nakedible-p
Copy link

Well, I don't know if I'm using the API wrong, but this took a bit of trial and error:

            let deflate_config = ratchet_rs::deflate::DeflateConfig::default(); // XXX: tweak config
            let deflate_ext_provider = ratchet_rs::deflate::DeflateExtProvider::with_config(deflate_config);
            let sec_websocket_extensions: Vec<ratchet_rs::Header> = if ENV_CONFIG.disable_ws_compression {
                // XXX: we disable all extensions
                vec![]
            } else {
                parts
                    .headers
                    .get_all(axum::http::header::SEC_WEBSOCKET_EXTENSIONS)
                    .into_iter()
                    .map(|v| ratchet_rs::Header {
                        name: axum::http::header::SEC_WEBSOCKET_EXTENSIONS.as_str(),
                        value: v.as_bytes(),
                    })
                    .collect()
            };
            let (extension, sec_websocket_extensions) = match deflate_ext_provider.negotiate_server(sec_websocket_extensions.as_slice()) {
                Ok(Some((extension, sec_websocket_extensions))) => (
                    ratchet_rs::NegotiatedExtension::from(Some(extension)),
                    Some(sec_websocket_extensions),
                ),
                Ok(None) => (ratchet_rs::NegotiatedExtension::from(None), None),
                Err(e) => {
                    return RpcError::new(RpcError::BAD_REQUEST, anyhow!("failed to negotiate deflate extension: {}", e)).into_response();
                }
            };

In general, for any websocket implementation, it would be nice if there was a more convenient wrapper for when none of the HTTP stuff is in ratchet's control. This means API should take in a read-only HeaderMap (or iterator of headers or something) and produce out statuscode with headers, but not write them out to anything. Passing "user defined headers" should not be needed, as ratchet should only output the websocket relevant headers and the caller is responsible for putting them in the response that the caller produces along with everything else it wants to do with the response.

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

No branches or pull requests

2 participants