-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for negotiating a subprotocol (#1150)
- Loading branch information
1 parent
e71e739
commit 8d3c647
Showing
11 changed files
with
226 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
## 0.1.0-wip | ||
|
||
- Abstract interface definition. | ||
- Basic functionality in place. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
pkgs/web_socket_conformance_tests/lib/src/connect_uri_tests.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:test/test.dart'; | ||
import 'package:web_socket/web_socket.dart'; | ||
|
||
/// Tests that the [WebSocket] rejects invalid connection URIs. | ||
void testConnectUri( | ||
Future<WebSocket> Function(Uri uri, {Iterable<String>? protocols}) | ||
channelFactory) { | ||
group('connect uri', () { | ||
test('no protocol', () async { | ||
await expectLater(() => channelFactory(Uri.https('www.example.com', '/')), | ||
throwsA(isA<ArgumentError>())); | ||
}); | ||
}); | ||
} |
47 changes: 47 additions & 0 deletions
47
pkgs/web_socket_conformance_tests/lib/src/protocol_server.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:crypto/crypto.dart'; | ||
import 'package:stream_channel/stream_channel.dart'; | ||
|
||
const _webSocketGuid = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; | ||
|
||
/// Starts an WebSocket server that responds with a scripted subprotocol. | ||
void hybridMain(StreamChannel<Object?> channel) async { | ||
late final HttpServer server; | ||
server = (await HttpServer.bind('localhost', 0)) | ||
..listen((request) async { | ||
final serverProtocol = request.requestedUri.queryParameters['protocol']; | ||
var key = request.headers.value('Sec-WebSocket-Key'); | ||
var digest = sha1.convert('$key$_webSocketGuid'.codeUnits); | ||
var accept = base64.encode(digest.bytes); | ||
channel.sink.add(request.headers['Sec-WebSocket-Protocol']); | ||
request.response | ||
..statusCode = HttpStatus.switchingProtocols | ||
..headers.add(HttpHeaders.connectionHeader, 'Upgrade') | ||
..headers.add(HttpHeaders.upgradeHeader, 'websocket') | ||
..headers.add('Sec-WebSocket-Accept', accept); | ||
if (serverProtocol != null) { | ||
request.response.headers.add('Sec-WebSocket-Protocol', serverProtocol); | ||
} | ||
request.response.contentLength = 0; | ||
final socket = await request.response.detachSocket(); | ||
final webSocket = WebSocket.fromUpgradedSocket(socket, | ||
protocol: serverProtocol, serverSide: true); | ||
webSocket.listen((e) async { | ||
webSocket.add(e); | ||
await webSocket.close(); | ||
}); | ||
}); | ||
|
||
channel.sink.add(server.port); | ||
|
||
await channel | ||
.stream.first; // Any writes indicates that the server should exit. | ||
unawaited(server.close()); | ||
} |
12 changes: 12 additions & 0 deletions
12
pkgs/web_socket_conformance_tests/lib/src/protocol_server_vm.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
pkgs/web_socket_conformance_tests/lib/src/protocol_server_web.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
pkgs/web_socket_conformance_tests/lib/src/protocol_tests.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:async/async.dart'; | ||
import 'package:stream_channel/stream_channel.dart'; | ||
import 'package:test/test.dart'; | ||
import 'package:web_socket/web_socket.dart'; | ||
|
||
import 'protocol_server_vm.dart' | ||
if (dart.library.html) 'protocol_server_web.dart'; | ||
|
||
/// Tests that the [WebSocket] can correctly negotiate a subprotocol with the | ||
/// peer. | ||
/// | ||
/// See | ||
/// [RFC-6455 1.9](https://datatracker.ietf.org/doc/html/rfc6455#section-1.9). | ||
void testProtocols( | ||
Future<WebSocket> Function(Uri uri, {Iterable<String>? protocols}) | ||
channelFactory) { | ||
group('protocols', () { | ||
late Uri uri; | ||
late StreamChannel<Object?> httpServerChannel; | ||
late StreamQueue<Object?> httpServerQueue; | ||
|
||
setUp(() async { | ||
httpServerChannel = await startServer(); | ||
httpServerQueue = StreamQueue(httpServerChannel.stream); | ||
uri = Uri.parse('ws://localhost:${await httpServerQueue.next}'); | ||
}); | ||
tearDown(() => httpServerChannel.sink.add(null)); | ||
|
||
test('no protocol', () async { | ||
final socket = await channelFactory(uri); | ||
|
||
expect(await httpServerQueue.next, null); | ||
expect(socket.protocol, ''); | ||
socket.sendText('Hello World!'); | ||
}); | ||
|
||
test('single protocol', () async { | ||
final socket = await channelFactory( | ||
uri.replace(queryParameters: {'protocol': 'chat.example.com'}), | ||
protocols: ['chat.example.com']); | ||
|
||
expect(await httpServerQueue.next, ['chat.example.com']); | ||
expect(socket.protocol, 'chat.example.com'); | ||
socket.sendText('Hello World!'); | ||
}); | ||
|
||
test('mutiple protocols', () async { | ||
final socket = await channelFactory( | ||
uri.replace(queryParameters: {'protocol': 'text.example.com'}), | ||
protocols: ['chat.example.com', 'text.example.com']); | ||
|
||
expect( | ||
await httpServerQueue.next, ['chat.example.com, text.example.com']); | ||
expect(socket.protocol, 'text.example.com'); | ||
socket.sendText('Hello World!'); | ||
}); | ||
|
||
test('protocol mismatch', () async { | ||
await expectLater( | ||
() => channelFactory( | ||
uri.replace(queryParameters: {'protocol': 'example.example.com'}), | ||
protocols: ['chat.example.com']), | ||
throwsA(isA<WebSocketException>())); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters