facebook/proxygen

websocket client with SSL problem: Server replied to me "Bad Request"

Long199for opened this issue · 5 comments

I tried to write the websocket client with SSL according to the example of the websocket example. The SSL part is OK, so I skip talking about it.
Here is the code:
void WebSocketClient::setupRequest() {
request_.setMethod(HTTPMethod::GET);
request_.setHTTPVersion(1, 1);
request_.setURL("wss://stream.bybit.com/v5/public/spot");
request_.getHeaders().add(HTTP_HEADER_USER_AGENT, "proxygen_websocket_client");
request_.getHeaders().add(HTTP_HEADER_HOST, "stream.bybit.com:443");
request_.getHeaders().add("Accept", "/");
request_.setEgressWebsocketUpgrade();
LOG(INFO) << request_;
}
I got the onBody callback tell me "Bad Request", and I had tried on other websocket like wss://api.gateio.ws/ws/v4/, they return me the same "Bad Request"
I run them with -url https://stream.bybit.com/v5/public/spot
any idea about how to connect on them correctly?

Can you add -v 4 and paste the log here? I suspect it may not like your wss:// URL scheme but not sure.

parameters: -logtostdout true -url https://stream.bybit.com/v5/public/spot -v 4 -http_client_connect_timeout 3000

code:
void WebSocketClient::setupRequest() {
request_.setMethod(HTTPMethod::GET);
request_.setHTTPVersion(1, 1);
request_.setURL(url_.getPath());
request_.getHeaders().add(HTTP_HEADER_USER_AGENT, "proxygen_websocket_client");
request_.getHeaders().add(HTTP_HEADER_HOST, url_.getHostAndPort());
request_.getHeaders().add("Sec-Websocket-Version", "13"); // add this, or failed to connect
request_.getHeaders().add("Accept", "/");
request_.setEgressWebsocketUpgrade();
LOG(INFO) << request_;
}

log:
I20240308 11:32:51.890995 35495345 WebSocketClient.cpp:108] websocket connect successful; sending data
I20240308 11:32:51.891044 35495345 HTTPSessionBase.cpp:159] notifyEgressBodyBuffered pwsd=34
I20240308 11:32:51.891105 35495345 HTTPSession.cpp:2166] proto=http/1.1, local=[2606:4700:110:833d:9c7b:de39:ccc0:aa39]:63013, [2600:9000:24bb:6800:17:57ca:2ec0:93a1]:443=upstream egressing txnID=1 allowed=65536
I20240308 11:32:51.891148 35495345 HTTPTransaction.cpp:1310] Sending 34 bytes of body. eom=no send_window is None trailers=no proto=http/1.1, local=[2606:4700:110:833d:9c7b:de39:ccc0:aa39]:63013, [2600:9000:24bb:6800:17:57ca:2ec0:93a1]:443=upstream, streamID=1
I20240308 11:32:51.891187 35495345 HTTPSessionBase.cpp:159] notifyEgressBodyBuffered pwsd=-34
I20240308 11:32:51.891227 35495345 HTTPSession.cpp:2274] proto=http/1.1, local=[2606:4700:110:833d:9c7b:de39:ccc0:aa39]:63013, [2600:9000:24bb:6800:17:57ca:2ec0:93a1]:443=upstream writing 34, activeWrites=1 cork:0 timestampTx:0 timestampAck:0
I20240308 11:32:52.242440 35495345 AsyncSSLSocket.cpp:1519] AsyncSSLSocket::performReadSingle() this=0x14a820c00, buf=0x14d008200, buflen=4000
I20240308 11:32:52.242612 35495345 AsyncSocket.cpp:3169] this=0x14a820c00, AsyncSocket::handleRead() got 56 bytes
I20240308 11:32:52.242681 35495345 HTTPSessionBase.cpp:148] proto=http/1.1, local=[2606:4700:110:833d:9c7b:de39:ccc0:aa39]:63013, [2600:9000:24bb:6800:17:57ca:2ec0:93a1]:443=upstream Dequeued 56 bytes of ingress. Ingress buffer uses 0 of 65536 bytes.
I20240308 11:32:52.242735 35495345 WebSocketClient.cpp:101] got server reply: �6�RSV1 set, RSV2 set, RSV3 set, bad opcode 7, bad MASK
I20240308 11:32:52.242799 35495345 AsyncSSLSocket.cpp:1519] AsyncSSLSocket::performReadSingle() this=0x14a820c00, buf=0x14d008200, buflen=4000
I20240308 11:32:52.242848 35495345 AsyncSocket.cpp:3169] this=0x14a820c00, AsyncSocket::handleRead() got 0 bytes
I20240308 11:32:52.242880 35495345 HTTPSession.cpp:558] EOF on proto=http/1.1, local=[2606:4700:110:833d:9c7b:de39:ccc0:aa39]:63013, [2600:9000:24bb:6800:17:57ca:2ec0:93a1]:443=upstream
I20240308 11:32:52.242915 35495345 HTTPSession.cpp:2337] shutdown request for proto=http/1.1, local=[2606:4700:110:833d:9c7b:de39:ccc0:aa39]:63013, [2600:9000:24bb:6800:17:57ca:2ec0:93a1]:443=upstream: reads=1 (currently 0), writes=0 (currently 0)

what happened for this bad MASK?

I guess proxygen has nothing of the opcode and mask in the websocket codec? I can see only the 3 simple upgrade handling functions there.
It's far away from the real world.

The websocket codec can be layered on top of proxygen in onBody calls. We have an internal implementation that isn't open source - maybe we move it over?

Any interest in WebTransport - it's the new hotness: https://github.com/facebook/proxygen/blob/main/proxygen/lib/http/webtransport/WebTransport.h

For our hedge fund, we focus on clients for connection. if the server side requires websocket, we have to do with it, even if webTransport is better.

So would you consider about moving the websocket codec into proxygen?

cpprestsdk is using websocketpp, but that framework does not support http2. proxygen+folly is much better if the websocket codec is supported, I think more teams/funds who are using c++ will move to proxygen. (Although most developers prefer go/rust/python for clients, they are much easier for designing business logic)