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

Adds missing UpgradeResponseParts export #56

Merged
merged 2 commits into from
Sep 30, 2024
Merged

Adds missing UpgradeResponseParts export #56

merged 2 commits into from
Sep 30, 2024

Conversation

SirCipher
Copy link
Member

@SirCipher SirCipher commented Sep 30, 2024

Closes #55

@nakedible-p
Copy link

FWIW, I can now use the provided function and the code has been tested to work:

            let protocols = ratchet_core::SubprotocolRegistry::new(vec!["xxx"]).expect("creating protocol registry should not fail");
            let provider = if ENV_CONFIG.disable_ws_compression {
                None
            } else {
                let deflate_config = ratchet_deflate::DeflateConfig {
                    server_max_window_bits: ratchet_deflate::WindowBits::try_from(ENV_CONFIG.ws_deflate_server_max_window_bits)
                        .expect("invalid server max window bits"),
                    compression_level: ratchet_deflate::Compression::new(ENV_CONFIG.ws_deflate_level),
                    ..Default::default()
                };
                Some(ratchet_deflate::DeflateExtProvider::with_config(deflate_config))
            };
            let ratchet_core::server::UpgradeResponseParts {
                response,
                subprotocol,
                extension,
                ..
            } = ratchet_core::server::response_from_headers(&parts.headers, provider, &protocols)
                .map_err(|e| RpcError::new(RpcError::BAD_REQUEST, anyhow!(e)))?;
            if subprotocol.is_none() {
                return Err(RpcError::new(
                    RpcError::BAD_REQUEST,
                    anyhow!("no valid websocket protocol selected"),
                ));
            }
            let Some(on_upgrade) = parts.extensions.remove::<hyper::upgrade::OnUpgrade>() else {
                return Err(RpcError::new(
                    RpcError::INTERNAL_ERROR,
                    anyhow!("upgrade handler not found on request"),
                ));
            };

            let mut response = response.map(|_| axum::body::Body::empty());
            // other mutation to response

            // spawn handler that will receive raw stream
            tokio::spawn(async move {
                match on_upgrade.await {
                    Ok(upgraded) => {
                        let upgraded = hyper_util::rt::tokio::TokioIo::new(upgraded);
                        let ws = ratchet_core::WebSocket::from_upgraded(
                            ratchet_core::WebSocketConfig::default(),
                            upgraded,
                            extension,
                            bytes::BytesMut::new(),
                            ratchet_core::Role::Server,
                        );

The custom stuff has been cleaned up from the code so it only shows ratchet relevant bits.

@SirCipher
Copy link
Member Author

@nakedible-p that looks much cleaner. Thanks for all of your feedback

@SirCipher SirCipher merged commit ef05a54 into main Sep 30, 2024
14 checks passed
@SirCipher SirCipher deleted the export branch September 30, 2024 15:30
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.

UpgradeResponseParts is not re-exported under ratchet_core::server
3 participants