Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I send websocket headers with WebSocketChannel.connect? #237

Open
iocapps opened this issue Nov 20, 2022 · 7 comments
Open

How do I send websocket headers with WebSocketChannel.connect? #237

iocapps opened this issue Nov 20, 2022 · 7 comments

Comments

@iocapps
Copy link

iocapps commented Nov 20, 2022

No description provided.

@AlexanderBykin
Copy link

i'm was also looking for that possibility and as i see it's impossible because dart2js doesn't have that functionality like native platform code

@itsji10dra
Copy link

You can use IOWebSocketChannel. Example:

WebSocketChannel _channel = IOWebSocketChannel.connect(
        Uri.parse(Constants.connectionUrl),
        headers: Headers.getHeaderParameters(),
);

@AlexanderBykin
Copy link

You can use IOWebSocketChannel. Example:

WebSocketChannel _channel = IOWebSocketChannel.connect(
        Uri.parse(Constants.connectionUrl),
        headers: Headers.getHeaderParameters(),
);

Crossplatform solution needed instead of native only

@brianquinlan
Copy link
Contributor

@AlexanderShniperson I think that you are correct - there is no way to support this on the web because the WebSocket API does not support setting headers.

@NotTsunami
Copy link
Contributor

NotTsunami commented Feb 29, 2024

@AlexanderShniperson I think that you are correct - there is no way to support this on the web because the WebSocket API does not support setting headers.

For the initial HTTP handshake, it does not appear Dart sends an 'Origin' header, which sometimes can be optionally sent (thus why OP may have filed the issue). But a larger problem I have is that I have a WebSocket server that is case-sensitive, and by default Dart likes to convert these headers to lowercase. There is a way to preserve the case if you create the connection yourself:

Random r = Random();
String key = base64.encode(List<int>.generate(8, (_) => r.nextInt(255)));

HttpClient client = HttpClient();
HttpClientRequest request = await client.getUrl(Uri.parse('ws://mypath/'));
request.headers.add('Connection', 'Upgrade', preserveHeaderCase: true);
request.headers.add('Upgrade', 'websocket', preserveHeaderCase: true);
request.headers.add('Origin', 'file://', preserveHeaderCase: true);
request.headers.add('Sec-WebSocket-Extensions', 'permessage-deflate; client_max_window_bits', preserveHeaderCase: true);
request.headers.add('Sec-WebSocket-Key', key, preserveHeaderCase: true);
request.headers.add('Sec-WebSocket-Version', '13', preserveHeaderCase: true);

HttpClientResponse response = await request.close();
Socket socket = await response.detachSocket();

WebSocket ws = WebSocket.fromUpgradedSocket(socket, serverSide: false);

But then there is no way to use this with WebSocketChannel, so I would have to create my own sink handling, which is doable, but this library exists already so if there was a way to either preserve header case directly from the library, that would be cool, but also allowing WebSocketChannel to take in an existing WebSocket could be nice too.

Obviously, the correct solution is for the server to be case-insensitive, but in some scenarios you don't have access to the server to correct the behavior. Someone else had this issue a few years ago and tried submitting a PR for the internal WebSocket class to allow preserving the case, but it appears to be stale: dart-lang/sdk#50378.

@brianquinlan
Copy link
Contributor

@NotTsunami I think that the request here was for the solution to be cross-platform, not require dart:io.

@NotTsunami
Copy link
Contributor

@NotTsunami I think that the request here was for the solution to be cross-platform, not require dart:io.

Sure, I was kind of expanding on why OP might need it and clarifying that there is a valid use for appending additional headers. The rest of it is semi-related but mostly mumbo-jumbo I can extract into another issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants