Skip to content

Commit b67c064

Browse files
authored
Tidies up handshake functions to return the selected subprotocol (#54)
* Resolves incorrect versioning * Bump version * Enforce semantic versioning in CI * Implements ExtensionProvider for Option<E> * Fixes incorrect argument to handshake function * Updates handshake functions to return subprotocol * Bump versions
1 parent c9221b1 commit b67c064

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ members = [
1414
]
1515

1616
[workspace.package]
17-
version = "1.1.1"
17+
version = "1.2.0"
1818
authors = ["Swim Inc. developers [email protected]"]
1919
edition = "2021"
2020
categories = ["network-programming", "asynchronous", "web-programming::websocket"]
2121
license = "Apache-2.0"
2222

2323
[workspace.dependencies]
24-
ratchet = { package = "ratchet_rs", version = "1.1.1", path = "ratchet_rs" }
25-
ratchet_core = { version = "1.1.1", path = "ratchet_core" }
26-
ratchet_ext = { version = "1.1.1", path = "ratchet_ext" }
27-
ratchet_deflate = { version = "1.1.1", path = "ratchet_deflate" }
28-
ratchet_fixture = { version = "1.1.1", path = "ratchet_fixture" }
24+
ratchet = { package = "ratchet_rs", version = "1.2.0", path = "ratchet_rs" }
25+
ratchet_core = { version = "1.2.0", path = "ratchet_core" }
26+
ratchet_ext = { version = "1.2.0", path = "ratchet_ext" }
27+
ratchet_deflate = { version = "1.2.0", path = "ratchet_deflate" }
28+
ratchet_fixture = { version = "1.2.0", path = "ratchet_fixture" }
2929

3030
url = "2.1.1"
3131
http = "1.1.0"

ratchet_core/src/handshake/server/mod.rs

+37-27
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,28 @@ pub struct UpgradeRequestParts<E> {
355355
pub extension_header: Option<HeaderValue>,
356356
}
357357

358+
/// A negotiated WebSocket response and its configured subprotcol and extension, if any.
359+
#[derive(Debug)]
360+
#[non_exhaustive]
361+
pub struct UpgradeResponseParts<E> {
362+
/// An `http::Response<()>`, which represents the WebSocket handshake response.
363+
pub response: Response<()>,
364+
365+
/// The optional WebSocket subprotocol agreed upon during the handshake.
366+
///
367+
/// The subprotocol is used to define the application-specific communication on top of the
368+
/// WebSocket connection, such as `wamp` or `graphql-ws`. If no subprotocol is requested or
369+
/// agreed upon, this will be `None`.
370+
pub subprotocol: Option<String>,
371+
372+
/// The optional WebSocket extension negotiated during the handshake.
373+
///
374+
/// Extensions allow WebSocket connections to have additional functionality, such as compression
375+
/// or multiplexing. This field represents any such negotiated extension, or `None` if no
376+
/// extensions were negotiated.
377+
pub extension: Option<E>,
378+
}
379+
358380
/// Represents a parsed WebSocket connection upgrade HTTP request.
359381
#[derive(Debug)]
360382
#[non_exhaustive]
@@ -457,15 +479,7 @@ pub fn build_response(
457479
///
458480
/// # Returns
459481
///
460-
/// This function returns a `Result` containing:
461-
/// - A tuple consisting of:
462-
/// - An `http::Response<()>`, which represents the WebSocket handshake response.
463-
/// The response includes headers such as `Sec-WebSocket-Accept` to confirm the upgrade.
464-
/// - An optional `E::Extension`, which represents the negotiated extension, if any.
465-
///
466-
/// If the handshake fails, an `Error` is returned, which may be caused by invalid
467-
/// requests, issues parsing headers, or problems negotiating the WebSocket subprotocols
468-
/// or extensions.
482+
/// The negotiated response and the selected subprotocol and extension, if any.
469483
///
470484
/// # Type Parameters
471485
///
@@ -484,9 +498,9 @@ pub fn build_response(
484498
/// - Failure to negotiate the WebSocket extensions or subprotocols.
485499
pub fn handshake<E, B>(
486500
request: http::Request<B>,
487-
extension: &E,
501+
extension: E,
488502
subprotocols: &SubprotocolRegistry,
489-
) -> Result<(Response<()>, Option<E::Extension>), Error>
503+
) -> Result<UpgradeResponseParts<E::Extension>, Error>
490504
where
491505
E: ExtensionProvider,
492506
{
@@ -504,10 +518,11 @@ where
504518
extension_header,
505519
..
506520
} = parse_request_parts(version, &method, &headers, extension, subprotocols)?;
507-
Ok((
508-
build_response(key, subprotocol, extension_header)?,
521+
Ok(UpgradeResponseParts {
522+
response: build_response(key, subprotocol.clone(), extension_header)?,
523+
subprotocol,
509524
extension,
510-
))
525+
})
511526
}
512527

513528
/// Generates a WebSocket upgrade response from the provided headers.
@@ -522,15 +537,7 @@ where
522537
///
523538
/// # Returns
524539
///
525-
/// This function returns a `Result` containing:
526-
/// - A tuple consisting of:
527-
/// - An `http::Response<()>`, which represents the WebSocket handshake response.
528-
/// The response includes headers such as `Sec-WebSocket-Accept` to confirm the upgrade.
529-
/// - An optional `E::Extension`, which represents the negotiated extension, if any.
530-
///
531-
/// If the handshake fails, an `Error` is returned, which may be caused by invalid
532-
/// requests, issues parsing headers, or problems negotiating the WebSocket subprotocols
533-
/// or extensions.
540+
/// The negotiated response and the selected subprotocol and extension, if any.
534541
///
535542
/// # Type Parameters
536543
///
@@ -547,7 +554,7 @@ pub fn response_from_headers<E>(
547554
headers: &HeaderMap,
548555
extension: E,
549556
subprotocols: &SubprotocolRegistry,
550-
) -> Result<(Response<()>, Option<E::Extension>), Error>
557+
) -> Result<UpgradeResponseParts<E::Extension>, Error>
551558
where
552559
E: ExtensionProvider,
553560
{
@@ -568,9 +575,12 @@ where
568575
.version(Version::HTTP_11)
569576
.status(StatusCode::SWITCHING_PROTOCOLS)
570577
.body(())?;
571-
*response.headers_mut() = build_response_headers(key, subprotocol, extension_header)?;
572-
573-
Ok((response, extension))
578+
*response.headers_mut() = build_response_headers(key, subprotocol.clone(), extension_header)?;
579+
Ok(UpgradeResponseParts {
580+
response,
581+
subprotocol,
582+
extension,
583+
})
574584
}
575585

576586
/// Constructs the HTTP response headers for a WebSocket handshake response.

0 commit comments

Comments
 (0)