summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWebSocket/Impl/TLSv12WebSocketConnectionImpl.cpp
blob: 370f77c4ebeab7fb4b2ae4587b05ea0af68b34f0 (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
/*
 * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibWebSocket/Impl/TLSv12WebSocketConnectionImpl.h>

namespace WebSocket {

TLSv12WebSocketConnectionImpl::TLSv12WebSocketConnectionImpl(Core::Object* parent)
    : AbstractWebSocketImpl(parent)
{
}

TLSv12WebSocketConnectionImpl::~TLSv12WebSocketConnectionImpl()
{
    discard_connection();
}

void TLSv12WebSocketConnectionImpl::connect(ConnectionInfo const& connection)
{
    VERIFY(!m_socket);
    VERIFY(on_connected);
    VERIFY(on_connection_error);
    VERIFY(on_ready_to_read);
    m_socket = TLS::TLSv12::construct(this);

    m_socket->set_root_certificates(DefaultRootCACertificates::the().certificates());
    m_socket->on_tls_error = [this](TLS::AlertDescription) {
        on_connection_error();
    };
    m_socket->on_tls_ready_to_read = [this](auto&) {
        on_ready_to_read();
    };
    m_socket->on_tls_ready_to_write = [this](auto&) {
        on_connected();
    };
    m_socket->on_tls_finished = [this] {
        on_connection_error();
    };
    m_socket->on_tls_certificate_request = [](auto&) {
        // FIXME : Once we handle TLS certificate requests, handle it here as well.
    };
    bool success = m_socket->connect(connection.url().host(), connection.url().port_or_default());
    if (!success) {
        deferred_invoke([this] {
            on_connection_error();
        });
    }
}

bool TLSv12WebSocketConnectionImpl::send(ReadonlyBytes data)
{
    return m_socket->write(data);
}

bool TLSv12WebSocketConnectionImpl::can_read_line()
{
    return m_socket->can_read_line();
}

String TLSv12WebSocketConnectionImpl::read_line(size_t size)
{
    return m_socket->read_line(size);
}

bool TLSv12WebSocketConnectionImpl::can_read()
{
    return m_socket->can_read();
}

ByteBuffer TLSv12WebSocketConnectionImpl::read(int max_size)
{
    return m_socket->read(max_size);
}

bool TLSv12WebSocketConnectionImpl::eof()
{
    return m_socket->eof();
}

void TLSv12WebSocketConnectionImpl::discard_connection()
{
    if (!m_socket)
        return;
    m_socket->on_tls_connected = nullptr;
    m_socket->on_tls_error = nullptr;
    m_socket->on_tls_finished = nullptr;
    m_socket->on_tls_certificate_request = nullptr;
    m_socket->on_ready_to_read = nullptr;
    remove_child(*m_socket);
    m_socket = nullptr;
}

}