@@ -355,6 +355,28 @@ pub struct UpgradeRequestParts<E> {
355
355
pub extension_header : Option < HeaderValue > ,
356
356
}
357
357
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
+
358
380
/// Represents a parsed WebSocket connection upgrade HTTP request.
359
381
#[ derive( Debug ) ]
360
382
#[ non_exhaustive]
@@ -457,15 +479,7 @@ pub fn build_response(
457
479
///
458
480
/// # Returns
459
481
///
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.
469
483
///
470
484
/// # Type Parameters
471
485
///
@@ -484,9 +498,9 @@ pub fn build_response(
484
498
/// - Failure to negotiate the WebSocket extensions or subprotocols.
485
499
pub fn handshake < E , B > (
486
500
request : http:: Request < B > ,
487
- extension : & E ,
501
+ extension : E ,
488
502
subprotocols : & SubprotocolRegistry ,
489
- ) -> Result < ( Response < ( ) > , Option < E :: Extension > ) , Error >
503
+ ) -> Result < UpgradeResponseParts < E :: Extension > , Error >
490
504
where
491
505
E : ExtensionProvider ,
492
506
{
@@ -504,10 +518,11 @@ where
504
518
extension_header,
505
519
..
506
520
} = 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,
509
524
extension,
510
- ) )
525
+ } )
511
526
}
512
527
513
528
/// Generates a WebSocket upgrade response from the provided headers.
@@ -522,15 +537,7 @@ where
522
537
///
523
538
/// # Returns
524
539
///
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.
534
541
///
535
542
/// # Type Parameters
536
543
///
@@ -547,7 +554,7 @@ pub fn response_from_headers<E>(
547
554
headers : & HeaderMap ,
548
555
extension : E ,
549
556
subprotocols : & SubprotocolRegistry ,
550
- ) -> Result < ( Response < ( ) > , Option < E :: Extension > ) , Error >
557
+ ) -> Result < UpgradeResponseParts < E :: Extension > , Error >
551
558
where
552
559
E : ExtensionProvider ,
553
560
{
@@ -568,9 +575,12 @@ where
568
575
. version ( Version :: HTTP_11 )
569
576
. status ( StatusCode :: SWITCHING_PROTOCOLS )
570
577
. 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
+ } )
574
584
}
575
585
576
586
/// Constructs the HTTP response headers for a WebSocket handshake response.
0 commit comments