mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-21 23:50:06 +00:00
`curl_easy_recv` must be called in a loop until it returns EAGAIN, because it may cache data, but only activate the read notifier once. Additionally, the data received can contain multiple WebSocket frames and only activate the notifier once, so we have to keep reading frames until there isn't enough data. We also have to do this immediately after connecting a WebSocket, since the server may immediately send data when the WebSocket opens and before we create the read notifier. This makes Discord login faster and more reliable, and makes Discord activities start loading.
47 lines
1.2 KiB
C++
47 lines
1.2 KiB
C++
/*
|
|
* Copyright (c) 2025, Andrew Kaster <andrew@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/MemoryStream.h>
|
|
#include <LibCore/Forward.h>
|
|
#include <LibWebSocket/Impl/WebSocketImpl.h>
|
|
#include <curl/curl.h>
|
|
|
|
namespace RequestServer {
|
|
|
|
class WebSocketImplCurl final : public WebSocket::WebSocketImpl {
|
|
public:
|
|
virtual ~WebSocketImplCurl() override;
|
|
|
|
static NonnullRefPtr<WebSocketImplCurl> create(CURLM*);
|
|
|
|
virtual void connect(WebSocket::ConnectionInfo const&) override;
|
|
virtual bool can_read_line() override;
|
|
virtual ErrorOr<ByteString> read_line(size_t) override;
|
|
virtual ErrorOr<ByteBuffer> read(int max_size) override;
|
|
virtual bool send(ReadonlyBytes) override;
|
|
virtual bool eof() override;
|
|
virtual void discard_connection() override;
|
|
|
|
virtual bool handshake_complete_when_connected() const override { return true; }
|
|
|
|
bool did_connect();
|
|
|
|
private:
|
|
explicit WebSocketImplCurl(CURLM*);
|
|
|
|
void read_from_socket();
|
|
|
|
CURLM* m_multi_handle { nullptr };
|
|
CURL* m_easy_handle { nullptr };
|
|
RefPtr<Core::Notifier> m_read_notifier;
|
|
RefPtr<Core::Notifier> m_error_notifier;
|
|
Vector<curl_slist*> m_curl_string_lists;
|
|
AllocatingMemoryStream m_read_buffer;
|
|
};
|
|
|
|
}
|