summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/WebSockets/WebSocket.cpp
blob: 05b9432814d379fd99d3861b5710f63e897e870a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2021-2022, Dex♪ <dexes.ttp@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/QuickSort.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/FunctionObject.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/EventDispatcher.h>
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/HTML/CloseEvent.h>
#include <LibWeb/HTML/EventHandler.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/MessageEvent.h>
#include <LibWeb/HTML/Origin.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWeb/WebSockets/WebSocket.h>

namespace Web::WebSockets {

static RefPtr<WebSocketClientManager> s_websocket_client_manager;

void WebSocketClientManager::initialize(RefPtr<WebSocketClientManager> websocket_client_manager)
{
    s_websocket_client_manager = websocket_client_manager;
}

WebSocketClientManager& WebSocketClientManager::the()
{
    if (!s_websocket_client_manager) [[unlikely]] {
        dbgln("Web::WebSockets::WebSocketClientManager was not initialized!");
        VERIFY_NOT_REACHED();
    }
    return *s_websocket_client_manager;
}

WebSocketClientSocket::WebSocketClientSocket() = default;

WebSocketClientSocket::~WebSocketClientSocket() = default;

WebSocketClientManager::WebSocketClientManager() = default;

// https://websockets.spec.whatwg.org/#dom-websocket-websocket
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> WebSocket::construct_impl(JS::Realm& realm, DeprecatedString const& url, Optional<Variant<DeprecatedString, Vector<DeprecatedString>>> const& protocols)
{
    auto& window = verify_cast<HTML::Window>(realm.global_object());
    AK::URL url_record(url);
    if (!url_record.is_valid())
        return WebIDL::SyntaxError::create(realm, "Invalid URL");
    if (!url_record.scheme().is_one_of("ws", "wss"))
        return WebIDL::SyntaxError::create(realm, "Invalid protocol");
    if (!url_record.fragment().is_empty())
        return WebIDL::SyntaxError::create(realm, "Presence of URL fragment is invalid");
    Vector<DeprecatedString> protocols_sequence;
    if (protocols.has_value()) {
        // 5. If `protocols` is a string, set `protocols` to a sequence consisting of just that string
        if (protocols.value().has<DeprecatedString>())
            protocols_sequence = { protocols.value().get<DeprecatedString>() };
        else
            protocols_sequence = protocols.value().get<Vector<DeprecatedString>>();
        // 6. If any of the values in `protocols` occur more than once or otherwise fail to match the requirements, throw SyntaxError
        auto sorted_protocols = protocols_sequence;
        quick_sort(sorted_protocols);
        for (size_t i = 0; i < sorted_protocols.size(); i++) {
            // https://datatracker.ietf.org/doc/html/rfc6455
            // The elements that comprise this value MUST be non-empty strings with characters in the range U+0021 to U+007E not including
            // separator characters as defined in [RFC2616] and MUST all be unique strings.
            auto protocol = sorted_protocols[i];
            if (i < sorted_protocols.size() - 1 && protocol == sorted_protocols[i + 1])
                return WebIDL::SyntaxError::create(realm, "Found a duplicate protocol name in the specified list");
            for (auto character : protocol) {
                if (character < '\x21' || character > '\x7E')
                    return WebIDL::SyntaxError::create(realm, "Found invalid character in subprotocol name");
            }
        }
    }
    return MUST_OR_THROW_OOM(realm.heap().allocate<WebSocket>(realm, window, url_record, protocols_sequence));
}

WebSocket::WebSocket(HTML::Window& window, AK::URL& url, Vector<DeprecatedString> const& protocols)
    : EventTarget(window.realm())
    , m_window(window)
{
    // FIXME: Integrate properly with FETCH as per https://fetch.spec.whatwg.org/#websocket-opening-handshake
    auto origin_string = m_window->associated_document().origin().serialize();
    m_websocket = WebSocketClientManager::the().connect(url, origin_string, protocols);
    m_websocket->on_open = [weak_this = make_weak_ptr<WebSocket>()] {
        if (!weak_this)
            return;
        auto& websocket = const_cast<WebSocket&>(*weak_this);
        websocket.on_open();
    };
    m_websocket->on_message = [weak_this = make_weak_ptr<WebSocket>()](auto message) {
        if (!weak_this)
            return;
        auto& websocket = const_cast<WebSocket&>(*weak_this);
        websocket.on_message(move(message.data), message.is_text);
    };
    m_websocket->on_close = [weak_this = make_weak_ptr<WebSocket>()](auto code, auto reason, bool was_clean) {
        if (!weak_this)
            return;
        auto& websocket = const_cast<WebSocket&>(*weak_this);
        websocket.on_close(code, reason, was_clean);
    };
    m_websocket->on_error = [weak_this = make_weak_ptr<WebSocket>()](auto) {
        if (!weak_this)
            return;
        auto& websocket = const_cast<WebSocket&>(*weak_this);
        websocket.on_error();
    };
}

WebSocket::~WebSocket() = default;

JS::ThrowCompletionOr<void> WebSocket::initialize(JS::Realm& realm)
{
    MUST_OR_THROW_OOM(Base::initialize(realm));
    set_prototype(&Bindings::ensure_web_prototype<Bindings::WebSocketPrototype>(realm, "WebSocket"));

    return {};
}

void WebSocket::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_window.ptr());
}

// https://websockets.spec.whatwg.org/#dom-websocket-readystate
WebSocket::ReadyState WebSocket::ready_state() const
{
    if (!m_websocket)
        return WebSocket::ReadyState::Closed;
    return const_cast<WebSocketClientSocket&>(*m_websocket).ready_state();
}

// https://websockets.spec.whatwg.org/#dom-websocket-extensions
DeprecatedString WebSocket::extensions() const
{
    if (!m_websocket)
        return DeprecatedString::empty();
    // https://websockets.spec.whatwg.org/#feedback-from-the-protocol
    // FIXME: Change the extensions attribute's value to the extensions in use, if it is not the null value.
    return DeprecatedString::empty();
}

// https://websockets.spec.whatwg.org/#dom-websocket-protocol
DeprecatedString WebSocket::protocol() const
{
    if (!m_websocket)
        return DeprecatedString::empty();
    return m_websocket->subprotocol_in_use();
}

// https://websockets.spec.whatwg.org/#dom-websocket-close
WebIDL::ExceptionOr<void> WebSocket::close(Optional<u16> code, Optional<DeprecatedString> reason)
{
    // 1. If code is present, but is neither an integer equal to 1000 nor an integer in the range 3000 to 4999, inclusive, throw an "InvalidAccessError" DOMException.
    if (code.has_value() && *code != 1000 && (*code < 3000 || *code > 4099))
        return WebIDL::InvalidAccessError::create(realm(), "The close error code is invalid");
    // 2. If reason is present, then run these substeps:
    if (reason.has_value()) {
        // 1. Let reasonBytes be the result of encoding reason.
        // 2. If reasonBytes is longer than 123 bytes, then throw a "SyntaxError" DOMException.
        if (reason->bytes().size() > 123)
            return WebIDL::SyntaxError::create(realm(), "The close reason is longer than 123 bytes");
    }
    // 3. Run the first matching steps from the following list:
    auto state = ready_state();
    // -> If this's ready state is CLOSING (2) or CLOSED (3)
    if (state == WebSocket::ReadyState::Closing || state == WebSocket::ReadyState::Closed)
        return {};
    // -> If the WebSocket connection is not yet established [WSP]
    // -> If the WebSocket closing handshake has not yet been started [WSP]
    // -> Otherwise
    // NOTE: All of these are handled by the WebSocket Protocol when calling close()
    // FIXME: LibProtocol does not yet support sending empty Close messages, so we use default values for now
    m_websocket->close(code.value_or(1000), reason.value_or(DeprecatedString::empty()));
    return {};
}

// https://websockets.spec.whatwg.org/#dom-websocket-send
WebIDL::ExceptionOr<void> WebSocket::send(DeprecatedString const& data)
{
    auto state = ready_state();
    if (state == WebSocket::ReadyState::Connecting)
        return WebIDL::InvalidStateError::create(realm(), "Websocket is still CONNECTING");
    if (state == WebSocket::ReadyState::Open) {
        m_websocket->send(data);
        // TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection.
        // TODO : Any invocation of this method with a string argument that does not throw an exception must increase the bufferedAmount attribute by the number of bytes needed to express the argument as UTF-8.
    }
    return {};
}

// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_open()
{
    // 1. Change the readyState attribute's value to OPEN (1).
    // 2. Change the extensions attribute's value to the extensions in use, if it is not the null value. [WSP]
    // 3. Change the protocol attribute's value to the subprotocol in use, if it is not the null value. [WSP]
    dispatch_event(DOM::Event::create(realm(), HTML::EventNames::open).release_value_but_fixme_should_propagate_errors());
}

// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_error()
{
    dispatch_event(DOM::Event::create(realm(), HTML::EventNames::error).release_value_but_fixme_should_propagate_errors());
}

// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_close(u16 code, DeprecatedString reason, bool was_clean)
{
    // 1. Change the readyState attribute's value to CLOSED. This is handled by the Protocol's WebSocket
    // 2. If [needed], fire an event named error at the WebSocket object. This is handled by the Protocol's WebSocket
    HTML::CloseEventInit event_init {};
    event_init.was_clean = was_clean;
    event_init.code = code;
    event_init.reason = String::from_deprecated_string(reason).release_value_but_fixme_should_propagate_errors();
    dispatch_event(HTML::CloseEvent::create(realm(), HTML::EventNames::close, event_init).release_value_but_fixme_should_propagate_errors());
}

// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_message(ByteBuffer message, bool is_text)
{
    if (m_websocket->ready_state() != WebSocket::ReadyState::Open)
        return;
    if (is_text) {
        auto text_message = DeprecatedString(ReadonlyBytes(message));
        HTML::MessageEventInit event_init;
        event_init.data = JS::PrimitiveString::create(vm(), text_message);
        event_init.origin = String::from_deprecated_string(url()).release_value_but_fixme_should_propagate_errors();
        dispatch_event(HTML::MessageEvent::create(realm(), HTML::EventNames::message, event_init).release_value_but_fixme_should_propagate_errors());
        return;
    }

    if (m_binary_type == "blob") {
        // type indicates that the data is Binary and binaryType is "blob"
        TODO();
    } else if (m_binary_type == "arraybuffer") {
        // type indicates that the data is Binary and binaryType is "arraybuffer"
        HTML::MessageEventInit event_init;
        event_init.data = JS::ArrayBuffer::create(realm(), message);
        event_init.origin = String::from_deprecated_string(url()).release_value_but_fixme_should_propagate_errors();
        dispatch_event(HTML::MessageEvent::create(realm(), HTML::EventNames::message, event_init).release_value_but_fixme_should_propagate_errors());
        return;
    }

    dbgln("Unsupported WebSocket message type {}", m_binary_type);
    TODO();
}

#undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name)                       \
    void WebSocket::set_##attribute_name(WebIDL::CallbackType* value) \
    {                                                                 \
        set_event_handler_attribute(event_name, value);               \
    }                                                                 \
    WebIDL::CallbackType* WebSocket::attribute_name()                 \
    {                                                                 \
        return event_handler_attribute(event_name);                   \
    }
ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
#undef __ENUMERATE

}