When dart:io WebSocket attempts to upgrade a socket, it fails to connect to some servers because the dart websocket implementation sends all headers as lowercase.
The sample code below illustrates what I'm talking about.
import 'dart:io';
var server1 = 'ws://192.168.10.50:60157/stagedisplay';
// THIS SERVER RESPONDS WITH THE FOLLOWING HEADERS
// HTTP/1.1 101 Web Socket Protocol Handshake
// WebSocket-Location: ws://192.168.10.50:60157/stagedisplay
// Sec-WebSocket-Accept: cVJJw2Rwllr2QhiEkRWqEoqHfX0=
// Upgrade: WebSocket
// Connection: Upgrade
// WebSocket-Origin: chrome-extension://mefhakmgclhhfbdadeojlkbllmecialg
var server2 = 'ws://192.168.10.50:60777/stagedisplay';
// THIS SERVER RESPONDS WITH THE FOLLOWING HEADERS
// HTTP/1.1 101 Switching Protocols
// Upgrade: WebSocket
// Connection: Upgrade
// Sec-WebSocket-Accept: cnDz5Eny51ADskV2HSpc+jWUgJk=
void main() async {
// ignore: close_sinks
for (var server in [server1, server2]) {
print('Testing server: $server');
try {
var ws = await WebSocket.connect(server);
print('Successfully Connected');
ws.close();
} on HttpException catch (e) {
print(e);
}
}
}
Running this code provides the following output:
$ dart wstest.dart
Testing server: ws://192.168.10.50:60157/stagedisplay
Successfully Connected
Testing server: ws://192.168.10.50:60777/stagedisplay
Connection closed before full header was received
http://192.168.10.50:60777/stagedisplay
HttpException: Connection closed before full header was received, uri = http://192.168.10.50:60777/stagedisplay
Using wireshark to scan the websocket traffic, I have confirmed that dart is sending the headers as all lowercase:
This is a problem, because the server in question expects the http headers to be in camel case. Using curl, I can confirm that the case is the problem:
Clearly, this is a problem with the server failing to implement the HTTP spec correctly, but on the other hand, CamelCase headers are the preferred method for Google Chrome and are a reasonable expectation for servers and clients.
For Google:
HttpException: Connection closed before full header was received
When dart:io WebSocket attempts to upgrade a socket, it fails to connect to some servers because the dart websocket implementation sends all headers as lowercase.
The sample code below illustrates what I'm talking about.
Running this code provides the following output:
Using wireshark to scan the websocket traffic, I have confirmed that dart is sending the headers as all lowercase:
This is a problem, because the server in question expects the http headers to be in camel case. Using curl, I can confirm that the case is the problem:
This successfully results in a connected session.
But this fails:
Clearly, this is a problem with the server failing to implement the HTTP spec correctly, but on the other hand, CamelCase headers are the preferred method for Google Chrome and are a reasonable expectation for servers and clients.
For Google:
HttpException: Connection closed before full header was received