2021-04-17 15:20:24 +00:00
|
|
|
/*
|
2021-04-25 09:30:39 +00:00
|
|
|
* Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
|
2022-03-04 21:08:30 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-04-17 15:20:24 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-04-17 15:20:24 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Span.h>
|
|
|
|
#include <LibCore/Object.h>
|
|
|
|
#include <LibWebSocket/ConnectionInfo.h>
|
2022-02-04 10:43:30 +00:00
|
|
|
#include <LibWebSocket/Impl/WebSocketImpl.h>
|
2021-04-17 15:20:24 +00:00
|
|
|
#include <LibWebSocket/Message.h>
|
|
|
|
|
|
|
|
namespace WebSocket {
|
|
|
|
|
|
|
|
enum class ReadyState {
|
|
|
|
Connecting = 0,
|
|
|
|
Open = 1,
|
|
|
|
Closing = 2,
|
|
|
|
Closed = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebSocket final : public Core::Object {
|
|
|
|
C_OBJECT(WebSocket)
|
|
|
|
public:
|
2022-11-08 18:17:29 +00:00
|
|
|
static NonnullRefPtr<WebSocket> create(ConnectionInfo, RefPtr<WebSocketImpl> = nullptr);
|
2022-03-04 21:08:30 +00:00
|
|
|
virtual ~WebSocket() override = default;
|
2021-04-17 15:20:24 +00:00
|
|
|
|
|
|
|
URL const& url() const { return m_connection.url(); }
|
|
|
|
|
|
|
|
ReadyState ready_state();
|
|
|
|
|
2022-12-31 10:37:10 +00:00
|
|
|
DeprecatedString subprotocol_in_use();
|
|
|
|
|
2021-04-17 15:20:24 +00:00
|
|
|
// Call this to start the WebSocket connection.
|
|
|
|
void start();
|
|
|
|
|
|
|
|
// This can only be used if the `ready_state` is `ReadyState::Open`
|
2021-09-18 10:48:34 +00:00
|
|
|
void send(Message const&);
|
2021-04-17 15:20:24 +00:00
|
|
|
|
|
|
|
// This can only be used if the `ready_state` is `ReadyState::Open`
|
2022-12-04 18:02:33 +00:00
|
|
|
void close(u16 code = 1005, DeprecatedString const& reason = {});
|
2021-04-17 15:20:24 +00:00
|
|
|
|
|
|
|
Function<void()> on_open;
|
2022-12-04 18:02:33 +00:00
|
|
|
Function<void(u16 code, DeprecatedString reason, bool was_clean)> on_close;
|
2021-04-17 15:20:24 +00:00
|
|
|
Function<void(Message message)> on_message;
|
|
|
|
|
|
|
|
enum class Error {
|
|
|
|
CouldNotEstablishConnection,
|
|
|
|
ConnectionUpgradeFailed,
|
|
|
|
ServerClosedSocket,
|
|
|
|
};
|
|
|
|
|
|
|
|
Function<void(Error)> on_error;
|
|
|
|
|
|
|
|
private:
|
2022-11-08 18:17:29 +00:00
|
|
|
WebSocket(ConnectionInfo, RefPtr<WebSocketImpl>);
|
2021-04-17 15:20:24 +00:00
|
|
|
|
|
|
|
// As defined in section 5.2
|
|
|
|
enum class OpCode : u8 {
|
|
|
|
Continuation = 0x0,
|
|
|
|
Text = 0x1,
|
|
|
|
Binary = 0x2,
|
|
|
|
ConnectionClose = 0x8,
|
|
|
|
Ping = 0x9,
|
|
|
|
Pong = 0xA,
|
|
|
|
};
|
|
|
|
|
|
|
|
void drain_read();
|
|
|
|
|
|
|
|
void send_client_handshake();
|
|
|
|
void read_server_handshake();
|
|
|
|
|
|
|
|
void read_frame();
|
|
|
|
void send_frame(OpCode, ReadonlyBytes, bool is_final);
|
|
|
|
|
|
|
|
void notify_open();
|
2022-12-04 18:02:33 +00:00
|
|
|
void notify_close(u16 code, DeprecatedString reason, bool was_clean);
|
2021-04-17 15:20:24 +00:00
|
|
|
void notify_error(Error);
|
|
|
|
void notify_message(Message);
|
|
|
|
|
|
|
|
void fatal_error(Error);
|
|
|
|
void discard_connection();
|
|
|
|
|
|
|
|
enum class InternalState {
|
|
|
|
NotStarted,
|
|
|
|
EstablishingProtocolConnection,
|
|
|
|
SendingClientHandshake,
|
|
|
|
WaitingForServerHandshake,
|
|
|
|
Open,
|
|
|
|
Closing,
|
|
|
|
Closed,
|
|
|
|
Errored,
|
|
|
|
};
|
|
|
|
|
|
|
|
InternalState m_state { InternalState::NotStarted };
|
|
|
|
|
2022-12-31 10:37:10 +00:00
|
|
|
DeprecatedString m_subprotocol_in_use { DeprecatedString::empty() };
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_websocket_key;
|
2021-04-17 15:20:24 +00:00
|
|
|
bool m_has_read_server_handshake_first_line { false };
|
|
|
|
bool m_has_read_server_handshake_upgrade { false };
|
|
|
|
bool m_has_read_server_handshake_connection { false };
|
|
|
|
bool m_has_read_server_handshake_accept { false };
|
|
|
|
|
|
|
|
u16 m_last_close_code { 1005 };
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString m_last_close_message;
|
2021-04-17 15:20:24 +00:00
|
|
|
|
|
|
|
ConnectionInfo m_connection;
|
2022-02-04 10:43:30 +00:00
|
|
|
RefPtr<WebSocketImpl> m_impl;
|
2022-11-08 18:57:44 +00:00
|
|
|
|
|
|
|
Vector<u8> m_buffered_data;
|
2021-04-17 15:20:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|