Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/src/client/streamable_https.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ class StreamableHttpClientTransport implements Transport {
// Process the buffer line by line
while (buffer.contains('\n')) {
final index = buffer.indexOf('\n');
final line = buffer.substring(0, index);
var line = buffer.substring(0, index);
if (line.endsWith('\r')) {
line = line.substring(0, line.length - 1);
}
buffer = buffer.substring(index + 1);

if (line.isEmpty) {
Expand Down
60 changes: 60 additions & 0 deletions test/client/streamable_https_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,65 @@ void main() {
// server received and processed our DELETE request
expect(true, isTrue);
});

test('handles CRLF line endings in SSE events', () async {
transport = StreamableHttpClientTransport(serverUrl);

final messageCompleter = Completer<JsonRpcMessage>();
transport.onmessage = (message) {
print('Transport received message: ${jsonEncode(message.toJson())}');
messageCompleter.complete(message);
};

transport.onerror = (error) {
print('Transport error: $error');
};

await transport.start();

final notification = JsonRpcInitializedNotification();
await transport.send(notification);

await Future.delayed(Duration(milliseconds: 1000));

if (currentSseConnections.isEmpty) {
fail('No SSE connections established');
}

print(
'About to send SSE event, active connections: ${currentSseConnections.length}');

for (final connection in List<HttpResponse>.from(currentSseConnections)) {
try {
final message = {
'jsonrpc': '2.0',
'method': 'notifications/initialized',
};

final data = jsonEncode(message);
print('Sending SSE event with data: $data');

connection.write('event: message\r\n\r\n');
connection.write('data: $data\r\n\r\n');
await connection.flush();
print('Sent SSE event');
} catch (e) {
print('Error sending SSE event: $e');
fail('Failed to send SSE event: $e');
}
}

final message = await messageCompleter.future.timeout(
Duration(seconds: 5),
onTimeout: () {
print('*** TIMEOUT: No message received via SSE after 5 seconds');
throw TimeoutException('No message received via SSE');
},
);

expect(message, isA<JsonRpcNotification>());
expect((message as JsonRpcNotification).method,
equals('notifications/initialized'));
}, timeout: Timeout(Duration(seconds: 10)));
});
}