facebook / proxygen

A collection of C++ HTTP libraries including an easy to use HTTP server.
Other
8.03k stars 1.47k forks source link

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

Closed Long199for closed 2 months ago

Long199for commented 2 months ago

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_websocketclient"); request.getHeaders().add(HTTP_HEADERHOST, "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?

afrind commented 2 months ago

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

Long199for commented 2 months ago

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_websocketclient"); request.getHeaders().add(HTTP_HEADERHOST, 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?

Long199for commented 2 months ago

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.

afrind commented 2 months ago

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

Long199for commented 2 months ago

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)