|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, The SerenityOS developers. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <LibCore/EventLoop.h> |
| 28 | +#include <LibGemini/GeminiJob.h> |
| 29 | +#include <LibGemini/GeminiResponse.h> |
| 30 | +#include <LibTLS/TLSv12.h> |
| 31 | +#include <stdio.h> |
| 32 | +#include <unistd.h> |
| 33 | + |
| 34 | +//#define GEMINIJOB_DEBUG |
| 35 | + |
| 36 | +namespace Gemini { |
| 37 | + |
| 38 | +void GeminiJob::start() |
| 39 | +{ |
| 40 | + ASSERT(!m_socket); |
| 41 | + m_socket = TLS::TLSv12::construct(this); |
| 42 | + m_socket->on_tls_connected = [this] { |
| 43 | +#ifdef GEMINIJOB_DEBUG |
| 44 | + dbg() << "GeminiJob: on_connected callback"; |
| 45 | +#endif |
| 46 | + on_socket_connected(); |
| 47 | + }; |
| 48 | + m_socket->on_tls_error = [this](TLS::AlertDescription error) { |
| 49 | + if (error == TLS::AlertDescription::HandshakeFailure) { |
| 50 | + deferred_invoke([this](auto&) { |
| 51 | + return did_fail(Core::NetworkJob::Error::ProtocolFailed); |
| 52 | + }); |
| 53 | + } else if (error == TLS::AlertDescription::DecryptError) { |
| 54 | + deferred_invoke([this](auto&) { |
| 55 | + return did_fail(Core::NetworkJob::Error::ConnectionFailed); |
| 56 | + }); |
| 57 | + } else { |
| 58 | + deferred_invoke([this](auto&) { |
| 59 | + return did_fail(Core::NetworkJob::Error::TransmissionFailed); |
| 60 | + }); |
| 61 | + } |
| 62 | + }; |
| 63 | + m_socket->on_tls_finished = [this] { |
| 64 | + finish_up(); |
| 65 | + }; |
| 66 | + bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port()); |
| 67 | + if (!success) { |
| 68 | + deferred_invoke([this](auto&) { |
| 69 | + return did_fail(Core::NetworkJob::Error::ConnectionFailed); |
| 70 | + }); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void GeminiJob::shutdown() |
| 75 | +{ |
| 76 | + if (!m_socket) |
| 77 | + return; |
| 78 | + m_socket->on_tls_ready_to_read = nullptr; |
| 79 | + m_socket->on_tls_connected = nullptr; |
| 80 | + remove_child(*m_socket); |
| 81 | + m_socket = nullptr; |
| 82 | +} |
| 83 | + |
| 84 | +void GeminiJob::read_while_data_available(Function<IterationDecision()> read) |
| 85 | +{ |
| 86 | + while (m_socket->can_read()) { |
| 87 | + if (read() == IterationDecision::Break) |
| 88 | + break; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void GeminiJob::register_on_ready_to_read(Function<void()> callback) |
| 93 | +{ |
| 94 | + m_socket->on_tls_ready_to_read = [callback = move(callback)](auto&) { |
| 95 | + callback(); |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +void GeminiJob::register_on_ready_to_write(Function<void()> callback) |
| 100 | +{ |
| 101 | + m_socket->on_tls_ready_to_write = [callback = move(callback)](auto&) { |
| 102 | + callback(); |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +bool GeminiJob::can_read_line() const |
| 107 | +{ |
| 108 | + return m_socket->can_read_line(); |
| 109 | +} |
| 110 | + |
| 111 | +ByteBuffer GeminiJob::read_line(size_t size) |
| 112 | +{ |
| 113 | + return m_socket->read_line(size); |
| 114 | +} |
| 115 | + |
| 116 | +ByteBuffer GeminiJob::receive(size_t size) |
| 117 | +{ |
| 118 | + return m_socket->read(size); |
| 119 | +} |
| 120 | + |
| 121 | +bool GeminiJob::can_read() const |
| 122 | +{ |
| 123 | + return m_socket->can_read(); |
| 124 | +} |
| 125 | + |
| 126 | +bool GeminiJob::eof() const |
| 127 | +{ |
| 128 | + return m_socket->eof(); |
| 129 | +} |
| 130 | + |
| 131 | +bool GeminiJob::write(const ByteBuffer& data) |
| 132 | +{ |
| 133 | + return m_socket->write(data); |
| 134 | +} |
| 135 | + |
| 136 | +} |
0 commit comments