mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 15:07:44 +00:00
Libraries: Move to Userland/Libraries/
This commit is contained in:
parent
dc28c07fa5
commit
13d7c09125
1857 changed files with 266 additions and 274 deletions
11
Userland/Libraries/LibGemini/CMakeLists.txt
Normal file
11
Userland/Libraries/LibGemini/CMakeLists.txt
Normal file
|
@ -0,0 +1,11 @@
|
|||
set(SOURCES
|
||||
Document.cpp
|
||||
GeminiJob.cpp
|
||||
GeminiRequest.cpp
|
||||
GeminiResponse.cpp
|
||||
Job.cpp
|
||||
Line.cpp
|
||||
)
|
||||
|
||||
serenity_lib(LibGemini gemini)
|
||||
target_link_libraries(LibGemini LibCore LibTLS)
|
113
Userland/Libraries/LibGemini/Document.cpp
Normal file
113
Userland/Libraries/LibGemini/Document.cpp
Normal file
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGemini/Document.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
String Document::render_to_html() const
|
||||
{
|
||||
StringBuilder html_builder;
|
||||
html_builder.append("<!DOCTYPE html>\n<html>\n");
|
||||
html_builder.append("<head>\n<title>");
|
||||
html_builder.append(m_url.path());
|
||||
html_builder.append("</title>\n</head>\n");
|
||||
html_builder.append("<body>\n");
|
||||
for (auto& line : m_lines) {
|
||||
html_builder.append(line.render_to_html());
|
||||
}
|
||||
html_builder.append("</body>");
|
||||
html_builder.append("</html>");
|
||||
return html_builder.build();
|
||||
}
|
||||
|
||||
NonnullRefPtr<Document> Document::parse(const StringView& lines, const URL& url)
|
||||
{
|
||||
auto document = adopt(*new Document(url));
|
||||
document->read_lines(lines);
|
||||
return document;
|
||||
}
|
||||
|
||||
void Document::read_lines(const StringView& source)
|
||||
{
|
||||
auto close_list_if_needed = [&] {
|
||||
if (m_inside_unordered_list) {
|
||||
m_inside_unordered_list = false;
|
||||
m_lines.append(make<Control>(Control::UnorderedListEnd));
|
||||
}
|
||||
};
|
||||
|
||||
for (auto& line : source.lines()) {
|
||||
if (line.starts_with("```")) {
|
||||
close_list_if_needed();
|
||||
|
||||
m_inside_preformatted_block = !m_inside_preformatted_block;
|
||||
if (m_inside_preformatted_block) {
|
||||
m_lines.append(make<Control>(Control::PreformattedStart));
|
||||
} else {
|
||||
m_lines.append(make<Control>(Control::PreformattedEnd));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_inside_preformatted_block) {
|
||||
m_lines.append(make<Preformatted>(move(line)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.starts_with("*")) {
|
||||
if (!m_inside_unordered_list)
|
||||
m_lines.append(make<Control>(Control::UnorderedListStart));
|
||||
m_lines.append(make<UnorderedList>(move(line)));
|
||||
m_inside_unordered_list = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
close_list_if_needed();
|
||||
|
||||
if (line.starts_with("=>")) {
|
||||
m_lines.append(make<Link>(move(line), *this));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.starts_with("#")) {
|
||||
size_t level = 0;
|
||||
while (line.length() > level && line[level] == '#')
|
||||
++level;
|
||||
|
||||
m_lines.append(make<Heading>(move(line), level));
|
||||
continue;
|
||||
}
|
||||
|
||||
m_lines.append(make<Text>(move(line)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
149
Userland/Libraries/LibGemini/Document.h
Normal file
149
Userland/Libraries/LibGemini/Document.h
Normal file
|
@ -0,0 +1,149 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/NonnullOwnPtrVector.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
#include <AK/Vector.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
class Line {
|
||||
public:
|
||||
Line(String string)
|
||||
: m_text(move(string))
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Line();
|
||||
|
||||
virtual String render_to_html() const = 0;
|
||||
|
||||
protected:
|
||||
String m_text;
|
||||
};
|
||||
|
||||
class Document : public RefCounted<Document> {
|
||||
public:
|
||||
String render_to_html() const;
|
||||
|
||||
static NonnullRefPtr<Document> parse(const StringView& source, const URL&);
|
||||
|
||||
const URL& url() const { return m_url; };
|
||||
|
||||
private:
|
||||
explicit Document(const URL& url)
|
||||
: m_url(url)
|
||||
{
|
||||
}
|
||||
|
||||
void read_lines(const StringView&);
|
||||
|
||||
NonnullOwnPtrVector<Line> m_lines;
|
||||
URL m_url;
|
||||
bool m_inside_preformatted_block { false };
|
||||
bool m_inside_unordered_list { false };
|
||||
};
|
||||
|
||||
class Text : public Line {
|
||||
public:
|
||||
Text(String line)
|
||||
: Line(move(line))
|
||||
{
|
||||
}
|
||||
virtual ~Text() override;
|
||||
virtual String render_to_html() const override;
|
||||
};
|
||||
|
||||
class Link : public Line {
|
||||
public:
|
||||
Link(String line, const Document&);
|
||||
virtual ~Link() override;
|
||||
virtual String render_to_html() const override;
|
||||
|
||||
private:
|
||||
URL m_url;
|
||||
String m_name;
|
||||
};
|
||||
|
||||
class Preformatted : public Line {
|
||||
public:
|
||||
Preformatted(String line)
|
||||
: Line(move(line))
|
||||
{
|
||||
}
|
||||
virtual ~Preformatted() override;
|
||||
virtual String render_to_html() const override;
|
||||
};
|
||||
|
||||
class UnorderedList : public Line {
|
||||
public:
|
||||
UnorderedList(String line)
|
||||
: Line(move(line))
|
||||
{
|
||||
}
|
||||
virtual ~UnorderedList() override;
|
||||
virtual String render_to_html() const override;
|
||||
};
|
||||
|
||||
class Control : public Line {
|
||||
public:
|
||||
enum Kind {
|
||||
UnorderedListStart,
|
||||
UnorderedListEnd,
|
||||
PreformattedStart,
|
||||
PreformattedEnd,
|
||||
};
|
||||
Control(Kind kind)
|
||||
: Line("")
|
||||
, m_kind(kind)
|
||||
{
|
||||
}
|
||||
virtual ~Control() override;
|
||||
virtual String render_to_html() const override;
|
||||
|
||||
private:
|
||||
Kind m_kind;
|
||||
};
|
||||
|
||||
class Heading : public Line {
|
||||
public:
|
||||
Heading(String line, int level)
|
||||
: Line(move(line))
|
||||
, m_level(level)
|
||||
{
|
||||
}
|
||||
virtual ~Heading() override;
|
||||
virtual String render_to_html() const override;
|
||||
|
||||
private:
|
||||
int m_level { 1 };
|
||||
};
|
||||
|
||||
}
|
37
Userland/Libraries/LibGemini/Forward.h
Normal file
37
Userland/Libraries/LibGemini/Forward.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
class Document;
|
||||
class GeminiRequest;
|
||||
class GeminiResponse;
|
||||
class GeminiJob;
|
||||
class Job;
|
||||
|
||||
}
|
150
Userland/Libraries/LibGemini/GeminiJob.cpp
Normal file
150
Userland/Libraries/LibGemini/GeminiJob.cpp
Normal file
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibGemini/GeminiJob.h>
|
||||
#include <LibGemini/GeminiResponse.h>
|
||||
#include <LibTLS/TLSv12.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
//#define GEMINIJOB_DEBUG
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
void GeminiJob::start()
|
||||
{
|
||||
ASSERT(!m_socket);
|
||||
m_socket = TLS::TLSv12::construct(this);
|
||||
m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
|
||||
m_socket->on_tls_connected = [this] {
|
||||
#ifdef GEMINIJOB_DEBUG
|
||||
dbgln("GeminiJob: on_connected callback");
|
||||
#endif
|
||||
on_socket_connected();
|
||||
};
|
||||
m_socket->on_tls_error = [this](TLS::AlertDescription error) {
|
||||
if (error == TLS::AlertDescription::HandshakeFailure) {
|
||||
deferred_invoke([this](auto&) {
|
||||
return did_fail(Core::NetworkJob::Error::ProtocolFailed);
|
||||
});
|
||||
} else if (error == TLS::AlertDescription::DecryptError) {
|
||||
deferred_invoke([this](auto&) {
|
||||
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
} else {
|
||||
deferred_invoke([this](auto&) {
|
||||
return did_fail(Core::NetworkJob::Error::TransmissionFailed);
|
||||
});
|
||||
}
|
||||
};
|
||||
m_socket->on_tls_finished = [this] {
|
||||
finish_up();
|
||||
};
|
||||
m_socket->on_tls_certificate_request = [this](auto&) {
|
||||
if (on_certificate_requested)
|
||||
on_certificate_requested(*this);
|
||||
};
|
||||
bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port());
|
||||
if (!success) {
|
||||
deferred_invoke([this](auto&) {
|
||||
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void GeminiJob::shutdown()
|
||||
{
|
||||
if (!m_socket)
|
||||
return;
|
||||
m_socket->on_tls_ready_to_read = nullptr;
|
||||
m_socket->on_tls_connected = nullptr;
|
||||
remove_child(*m_socket);
|
||||
m_socket = nullptr;
|
||||
}
|
||||
|
||||
void GeminiJob::read_while_data_available(Function<IterationDecision()> read)
|
||||
{
|
||||
while (m_socket->can_read()) {
|
||||
if (read() == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GeminiJob::set_certificate(String certificate, String private_key)
|
||||
{
|
||||
if (!m_socket->add_client_key(certificate.bytes(), private_key.bytes())) {
|
||||
dbgln("LibGemini: Failed to set a client certificate");
|
||||
// FIXME: Do something about this failure
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
void GeminiJob::register_on_ready_to_read(Function<void()> callback)
|
||||
{
|
||||
m_socket->on_tls_ready_to_read = [callback = move(callback)](auto&) {
|
||||
callback();
|
||||
};
|
||||
}
|
||||
|
||||
void GeminiJob::register_on_ready_to_write(Function<void()> callback)
|
||||
{
|
||||
m_socket->on_tls_ready_to_write = [callback = move(callback)](auto&) {
|
||||
callback();
|
||||
};
|
||||
}
|
||||
|
||||
bool GeminiJob::can_read_line() const
|
||||
{
|
||||
return m_socket->can_read_line();
|
||||
}
|
||||
|
||||
String GeminiJob::read_line(size_t size)
|
||||
{
|
||||
return m_socket->read_line(size);
|
||||
}
|
||||
|
||||
ByteBuffer GeminiJob::receive(size_t size)
|
||||
{
|
||||
return m_socket->read(size);
|
||||
}
|
||||
|
||||
bool GeminiJob::can_read() const
|
||||
{
|
||||
return m_socket->can_read();
|
||||
}
|
||||
|
||||
bool GeminiJob::eof() const
|
||||
{
|
||||
return m_socket->eof();
|
||||
}
|
||||
|
||||
bool GeminiJob::write(ReadonlyBytes bytes)
|
||||
{
|
||||
return m_socket->write(bytes);
|
||||
}
|
||||
|
||||
}
|
74
Userland/Libraries/LibGemini/GeminiJob.h
Normal file
74
Userland/Libraries/LibGemini/GeminiJob.h
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <LibCore/NetworkJob.h>
|
||||
#include <LibGemini/GeminiRequest.h>
|
||||
#include <LibGemini/GeminiResponse.h>
|
||||
#include <LibGemini/Job.h>
|
||||
#include <LibTLS/TLSv12.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
class GeminiJob final : public Job {
|
||||
C_OBJECT(GeminiJob)
|
||||
public:
|
||||
explicit GeminiJob(const GeminiRequest& request, OutputStream& output_stream, const Vector<Certificate>* override_certificates = nullptr)
|
||||
: Job(request, output_stream)
|
||||
, m_override_ca_certificates(override_certificates)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~GeminiJob() override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void start() override;
|
||||
virtual void shutdown() override;
|
||||
void set_certificate(String certificate, String key);
|
||||
|
||||
Function<void(GeminiJob&)> on_certificate_requested;
|
||||
|
||||
protected:
|
||||
virtual void register_on_ready_to_read(Function<void()>) override;
|
||||
virtual void register_on_ready_to_write(Function<void()>) override;
|
||||
virtual bool can_read_line() const override;
|
||||
virtual String read_line(size_t) override;
|
||||
virtual bool can_read() const override;
|
||||
virtual ByteBuffer receive(size_t) override;
|
||||
virtual bool eof() const override;
|
||||
virtual bool write(ReadonlyBytes) override;
|
||||
virtual bool is_established() const override { return m_socket->is_established(); }
|
||||
virtual bool should_fail_on_empty_payload() const override { return false; }
|
||||
virtual void read_while_data_available(Function<IterationDecision()>) override;
|
||||
|
||||
private:
|
||||
RefPtr<TLS::TLSv12> m_socket;
|
||||
const Vector<Certificate>* m_override_ca_certificates { nullptr };
|
||||
};
|
||||
|
||||
}
|
60
Userland/Libraries/LibGemini/GeminiRequest.cpp
Normal file
60
Userland/Libraries/LibGemini/GeminiRequest.cpp
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibGemini/GeminiJob.h>
|
||||
#include <LibGemini/GeminiRequest.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
GeminiRequest::GeminiRequest()
|
||||
{
|
||||
}
|
||||
|
||||
GeminiRequest::~GeminiRequest()
|
||||
{
|
||||
}
|
||||
|
||||
ByteBuffer GeminiRequest::to_raw_request() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append(m_url.to_string());
|
||||
builder.append("\r\n");
|
||||
return builder.to_byte_buffer();
|
||||
}
|
||||
|
||||
Optional<GeminiRequest> GeminiRequest::from_raw_request(const ByteBuffer& raw_request)
|
||||
{
|
||||
URL url = StringView(raw_request);
|
||||
if (!url.is_valid())
|
||||
return {};
|
||||
GeminiRequest request;
|
||||
request.m_url = url;
|
||||
return request;
|
||||
}
|
||||
|
||||
}
|
52
Userland/Libraries/LibGemini/GeminiRequest.h
Normal file
52
Userland/Libraries/LibGemini/GeminiRequest.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/Forward.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
class GeminiRequest {
|
||||
public:
|
||||
GeminiRequest();
|
||||
~GeminiRequest();
|
||||
|
||||
const URL& url() const { return m_url; }
|
||||
void set_url(const URL& url) { m_url = url; }
|
||||
|
||||
ByteBuffer to_raw_request() const;
|
||||
|
||||
static Optional<GeminiRequest> from_raw_request(const ByteBuffer&);
|
||||
|
||||
private:
|
||||
URL m_url;
|
||||
};
|
||||
|
||||
}
|
41
Userland/Libraries/LibGemini/GeminiResponse.cpp
Normal file
41
Userland/Libraries/LibGemini/GeminiResponse.cpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibGemini/GeminiResponse.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
GeminiResponse::GeminiResponse(int status, String meta)
|
||||
: m_status(status)
|
||||
, m_meta(meta)
|
||||
{
|
||||
}
|
||||
|
||||
GeminiResponse::~GeminiResponse()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
52
Userland/Libraries/LibGemini/GeminiResponse.h
Normal file
52
Userland/Libraries/LibGemini/GeminiResponse.h
Normal file
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/NetworkResponse.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
class GeminiResponse : public Core::NetworkResponse {
|
||||
public:
|
||||
virtual ~GeminiResponse() override;
|
||||
static NonnullRefPtr<GeminiResponse> create(int status, String meta)
|
||||
{
|
||||
return adopt(*new GeminiResponse(status, meta));
|
||||
}
|
||||
|
||||
int status() const { return m_status; }
|
||||
String meta() const { return m_meta; }
|
||||
|
||||
private:
|
||||
GeminiResponse(int status, String);
|
||||
|
||||
int m_status { 0 };
|
||||
String m_meta;
|
||||
};
|
||||
|
||||
}
|
183
Userland/Libraries/LibGemini/Job.cpp
Normal file
183
Userland/Libraries/LibGemini/Job.cpp
Normal file
|
@ -0,0 +1,183 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <LibGemini/GeminiResponse.h>
|
||||
#include <LibGemini/Job.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
//#define JOB_DEBUG
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
Job::Job(const GeminiRequest& request, OutputStream& output_stream)
|
||||
: Core::NetworkJob(output_stream)
|
||||
, m_request(request)
|
||||
{
|
||||
}
|
||||
|
||||
Job::~Job()
|
||||
{
|
||||
}
|
||||
|
||||
void Job::flush_received_buffers()
|
||||
{
|
||||
for (size_t i = 0; i < m_received_buffers.size(); ++i) {
|
||||
auto& payload = m_received_buffers[i];
|
||||
auto written = do_write(payload);
|
||||
m_received_size -= written;
|
||||
if (written == payload.size()) {
|
||||
// FIXME: Make this a take-first-friendly object?
|
||||
m_received_buffers.take_first();
|
||||
continue;
|
||||
}
|
||||
ASSERT(written < payload.size());
|
||||
payload = payload.slice(written, payload.size() - written);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Job::on_socket_connected()
|
||||
{
|
||||
register_on_ready_to_write([this] {
|
||||
if (m_sent_data)
|
||||
return;
|
||||
m_sent_data = true;
|
||||
auto raw_request = m_request.to_raw_request();
|
||||
#ifdef JOB_DEBUG
|
||||
dbgln("Job: raw_request:");
|
||||
dbg() << String::copy(raw_request).characters();
|
||||
#endif
|
||||
bool success = write(raw_request);
|
||||
if (!success)
|
||||
deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
||||
});
|
||||
register_on_ready_to_read([this] {
|
||||
if (is_cancelled())
|
||||
return;
|
||||
|
||||
if (m_state == State::InStatus) {
|
||||
if (!can_read_line())
|
||||
return;
|
||||
|
||||
auto line = read_line(PAGE_SIZE);
|
||||
if (line.is_null()) {
|
||||
fprintf(stderr, "Job: Expected status line\n");
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
||||
}
|
||||
|
||||
auto parts = line.split_limit(' ', 2);
|
||||
if (parts.size() != 2) {
|
||||
warnln("Job: Expected 2-part status line, got '{}'", line);
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
|
||||
auto status = parts[0].to_uint();
|
||||
if (!status.has_value()) {
|
||||
fprintf(stderr, "Job: Expected numeric status code\n");
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
|
||||
m_status = status.value();
|
||||
m_meta = parts[1];
|
||||
|
||||
if (m_status >= 10 && m_status < 20) {
|
||||
m_state = State::Finished;
|
||||
} else if (m_status >= 20 && m_status < 30) {
|
||||
m_state = State::InBody;
|
||||
} else if (m_status >= 30 && m_status < 40) {
|
||||
m_state = State::Finished;
|
||||
} else if (m_status >= 40 && m_status < 50) {
|
||||
m_state = State::Finished;
|
||||
} else if (m_status >= 50 && m_status < 60) {
|
||||
m_state = State::Finished;
|
||||
} else if (m_status >= 60 && m_status < 70) {
|
||||
m_state = State::InBody;
|
||||
} else {
|
||||
fprintf(stderr, "Job: Expected status between 10 and 69; instead got %d\n", m_status);
|
||||
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ASSERT(m_state == State::InBody || m_state == State::Finished);
|
||||
|
||||
read_while_data_available([&] {
|
||||
auto read_size = 64 * KiB;
|
||||
|
||||
auto payload = receive(read_size);
|
||||
if (!payload) {
|
||||
if (eof()) {
|
||||
finish_up();
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
|
||||
if (should_fail_on_empty_payload()) {
|
||||
deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
}
|
||||
|
||||
m_received_buffers.append(payload);
|
||||
m_received_size += payload.size();
|
||||
flush_received_buffers();
|
||||
|
||||
deferred_invoke([this](auto&) { did_progress({}, m_received_size); });
|
||||
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
if (!is_established()) {
|
||||
#ifdef JOB_DEBUG
|
||||
dbgln("Connection appears to have closed, finishing up");
|
||||
#endif
|
||||
finish_up();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Job::finish_up()
|
||||
{
|
||||
m_state = State::Finished;
|
||||
flush_received_buffers();
|
||||
if (m_received_size != 0) {
|
||||
// We have to wait for the client to consume all the downloaded data
|
||||
// before we can actually call `did_finish`. in a normal flow, this should
|
||||
// never be hit since the client is reading as we are writing, unless there
|
||||
// are too many concurrent downloads going on.
|
||||
deferred_invoke([this](auto&) {
|
||||
finish_up();
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
auto response = GeminiResponse::create(m_status, m_meta);
|
||||
deferred_invoke([this, response](auto&) {
|
||||
did_finish(move(response));
|
||||
});
|
||||
}
|
||||
}
|
80
Userland/Libraries/LibGemini/Job.h
Normal file
80
Userland/Libraries/LibGemini/Job.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <LibCore/NetworkJob.h>
|
||||
#include <LibCore/TCPSocket.h>
|
||||
#include <LibGemini/GeminiRequest.h>
|
||||
#include <LibGemini/GeminiResponse.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
class Job : public Core::NetworkJob {
|
||||
public:
|
||||
explicit Job(const GeminiRequest&, OutputStream&);
|
||||
virtual ~Job() override;
|
||||
|
||||
virtual void start() override = 0;
|
||||
virtual void shutdown() override = 0;
|
||||
|
||||
GeminiResponse* response() { return static_cast<GeminiResponse*>(Core::NetworkJob::response()); }
|
||||
const GeminiResponse* response() const { return static_cast<const GeminiResponse*>(Core::NetworkJob::response()); }
|
||||
|
||||
protected:
|
||||
void finish_up();
|
||||
void on_socket_connected();
|
||||
void flush_received_buffers();
|
||||
virtual void register_on_ready_to_read(Function<void()>) = 0;
|
||||
virtual void register_on_ready_to_write(Function<void()>) = 0;
|
||||
virtual bool can_read_line() const = 0;
|
||||
virtual String read_line(size_t) = 0;
|
||||
virtual bool can_read() const = 0;
|
||||
virtual ByteBuffer receive(size_t) = 0;
|
||||
virtual bool eof() const = 0;
|
||||
virtual bool write(ReadonlyBytes) = 0;
|
||||
virtual bool is_established() const = 0;
|
||||
virtual bool should_fail_on_empty_payload() const { return false; }
|
||||
virtual void read_while_data_available(Function<IterationDecision()> read) { read(); };
|
||||
|
||||
enum class State {
|
||||
InStatus,
|
||||
InBody,
|
||||
Finished,
|
||||
};
|
||||
|
||||
GeminiRequest m_request;
|
||||
State m_state { State::InStatus };
|
||||
int m_status { -1 };
|
||||
String m_meta;
|
||||
Vector<ByteBuffer, 2> m_received_buffers;
|
||||
size_t m_received_size { 0 };
|
||||
bool m_sent_data { false };
|
||||
bool m_should_have_payload { false };
|
||||
};
|
||||
|
||||
}
|
143
Userland/Libraries/LibGemini/Line.cpp
Normal file
143
Userland/Libraries/LibGemini/Line.cpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Copyright (c) 2020, The SerenityOS developers.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibGemini/Document.h>
|
||||
|
||||
namespace Gemini {
|
||||
|
||||
String Text::render_to_html() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append(escape_html_entities(m_text));
|
||||
builder.append("<br>\n");
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
Text::~Text()
|
||||
{
|
||||
}
|
||||
|
||||
String Heading::render_to_html() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.appendf("<h%d>", m_level);
|
||||
builder.append(escape_html_entities(m_text.substring_view(m_level, m_text.length() - m_level)));
|
||||
builder.appendf("</h%d>", m_level);
|
||||
return builder.build();
|
||||
}
|
||||
Heading::~Heading()
|
||||
{
|
||||
}
|
||||
|
||||
String UnorderedList::render_to_html() const
|
||||
{
|
||||
// 1.3.5.4.2 "Advanced clients can take the space of the bullet symbol into account"
|
||||
// FIXME: The spec is unclear about what the space means, or where it goes
|
||||
// somehow figure this out
|
||||
StringBuilder builder;
|
||||
builder.append("<li>");
|
||||
builder.append(escape_html_entities(m_text.substring_view(1, m_text.length() - 1)));
|
||||
builder.append("</li>");
|
||||
return builder.build();
|
||||
}
|
||||
UnorderedList::~UnorderedList()
|
||||
{
|
||||
}
|
||||
|
||||
String Control::render_to_html() const
|
||||
{
|
||||
switch (m_kind) {
|
||||
case Kind::PreformattedEnd:
|
||||
return "</pre>";
|
||||
case Kind::PreformattedStart:
|
||||
return "<pre>";
|
||||
case Kind::UnorderedListStart:
|
||||
return "<ul>";
|
||||
case Kind::UnorderedListEnd:
|
||||
return "</ul>";
|
||||
default:
|
||||
dbgln("Unknown control kind _{}_", (int)m_kind);
|
||||
ASSERT_NOT_REACHED();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
Control::~Control()
|
||||
{
|
||||
}
|
||||
|
||||
Link::Link(String text, const Document& document)
|
||||
: Line(move(text))
|
||||
{
|
||||
size_t index = 2;
|
||||
while (index < m_text.length() && (m_text[index] == ' ' || m_text[index] == '\t'))
|
||||
++index;
|
||||
auto url_string = m_text.substring_view(index, m_text.length() - index);
|
||||
auto space_offset = url_string.find_first_of(" \t");
|
||||
String url = url_string;
|
||||
if (space_offset.has_value()) {
|
||||
url = url_string.substring_view(0, space_offset.value());
|
||||
auto offset = space_offset.value();
|
||||
while (offset < url_string.length() && (url_string[offset] == ' ' || url_string[offset] == '\t'))
|
||||
++offset;
|
||||
m_name = url_string.substring_view(offset, url_string.length() - offset);
|
||||
}
|
||||
m_url = document.url().complete_url(url);
|
||||
if (m_name.is_null())
|
||||
m_name = m_url.to_string();
|
||||
}
|
||||
Link::~Link()
|
||||
{
|
||||
}
|
||||
|
||||
String Link::render_to_html() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append("<a href=\"");
|
||||
builder.append(escape_html_entities(m_url.to_string()));
|
||||
builder.append("\">");
|
||||
builder.append(escape_html_entities(m_name));
|
||||
builder.append("</a><br>\n");
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
String Preformatted::render_to_html() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append(escape_html_entities(m_text));
|
||||
builder.append("\n");
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
Preformatted::~Preformatted()
|
||||
{
|
||||
}
|
||||
|
||||
Line::~Line()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue