diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-08 21:57:44 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-08 21:57:44 +0200 |
commit | cf3b58fbe8f836c13e44d6152d78960aff6089ef (patch) | |
tree | dcc7664f0004ee9c495f9d948cfb16d12f8a70bf /Services/ProtocolServer | |
parent | 042b1f68145ad3754fd98429b405c5c1a173d3fc (diff) | |
download | serenity-cf3b58fbe8f836c13e44d6152d78960aff6089ef.zip |
Services: Renamed from Servers
It didn't feel right to have a "DHCPClient" in a "Servers" directory.
Rename this to Services to better reflect the type of programs we'll
be putting in there.
Diffstat (limited to 'Services/ProtocolServer')
-rw-r--r-- | Services/ProtocolServer/Download.cpp | 92 | ||||
-rw-r--r-- | Services/ProtocolServer/Download.h | 70 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpDownload.cpp | 60 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpDownload.h | 45 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpProtocol.cpp | 50 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpProtocol.h | 37 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpsDownload.cpp | 60 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpsDownload.h | 45 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpsProtocol.cpp | 50 | ||||
-rw-r--r-- | Services/ProtocolServer/HttpsProtocol.h | 37 | ||||
-rw-r--r-- | Services/ProtocolServer/Makefile | 25 | ||||
-rw-r--r-- | Services/ProtocolServer/PSClientConnection.cpp | 112 | ||||
-rw-r--r-- | Services/ProtocolServer/PSClientConnection.h | 55 | ||||
-rw-r--r-- | Services/ProtocolServer/Protocol.cpp | 49 | ||||
-rw-r--r-- | Services/ProtocolServer/Protocol.h | 49 | ||||
-rw-r--r-- | Services/ProtocolServer/ProtocolClient.ipc | 6 | ||||
-rw-r--r-- | Services/ProtocolServer/ProtocolServer.ipc | 15 | ||||
-rw-r--r-- | Services/ProtocolServer/main.cpp | 71 |
18 files changed, 928 insertions, 0 deletions
diff --git a/Services/ProtocolServer/Download.cpp b/Services/ProtocolServer/Download.cpp new file mode 100644 index 0000000000..e4bbce082b --- /dev/null +++ b/Services/ProtocolServer/Download.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/Badge.h> +#include <ProtocolServer/Download.h> +#include <ProtocolServer/PSClientConnection.h> + +// FIXME: What about rollover? +static i32 s_next_id = 1; + +static HashMap<i32, RefPtr<Download>>& all_downloads() +{ + static HashMap<i32, RefPtr<Download>> map; + return map; +} + +Download* Download::find_by_id(i32 id) +{ + return const_cast<Download*>(all_downloads().get(id).value_or(nullptr)); +} + +Download::Download(PSClientConnection& client) + : m_id(s_next_id++) + , m_client(client.make_weak_ptr()) +{ + all_downloads().set(m_id, this); +} + +Download::~Download() +{ +} + +void Download::stop() +{ + all_downloads().remove(m_id); +} + +void Download::set_payload(const ByteBuffer& payload) +{ + m_payload = payload; + m_total_size = payload.size(); +} + +void Download::set_response_headers(const HashMap<String, String>& response_headers) +{ + m_response_headers = response_headers; +} + +void Download::did_finish(bool success) +{ + if (!m_client) { + dbg() << "Download::did_finish() after the client already disconnected."; + return; + } + m_client->did_finish_download({}, *this, success); + all_downloads().remove(m_id); +} + +void Download::did_progress(Optional<u32> total_size, u32 downloaded_size) +{ + if (!m_client) { + // FIXME: We should also abort the download in this situation, I guess! + dbg() << "Download::did_progress() after the client already disconnected."; + return; + } + m_total_size = total_size; + m_downloaded_size = downloaded_size; + m_client->did_progress_download({}, *this); +} diff --git a/Services/ProtocolServer/Download.h b/Services/ProtocolServer/Download.h new file mode 100644 index 0000000000..5609cd15b4 --- /dev/null +++ b/Services/ProtocolServer/Download.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/ByteBuffer.h> +#include <AK/HashMap.h> +#include <AK/Optional.h> +#include <AK/RefCounted.h> +#include <AK/URL.h> +#include <AK/WeakPtr.h> + +class PSClientConnection; + +class Download : public RefCounted<Download> { +public: + virtual ~Download(); + + static Download* find_by_id(i32); + + i32 id() const { return m_id; } + URL url() const { return m_url; } + + Optional<u32> total_size() const { return m_total_size; } + size_t downloaded_size() const { return m_downloaded_size; } + const ByteBuffer& payload() const { return m_payload; } + const HashMap<String, String>& response_headers() const { return m_response_headers; } + + void stop(); + +protected: + explicit Download(PSClientConnection&); + + void did_finish(bool success); + void did_progress(Optional<u32> total_size, u32 downloaded_size); + void set_payload(const ByteBuffer&); + void set_response_headers(const HashMap<String, String>&); + +private: + i32 m_id; + URL m_url; + Optional<u32> m_total_size {}; + size_t m_downloaded_size { 0 }; + ByteBuffer m_payload; + HashMap<String, String> m_response_headers; + WeakPtr<PSClientConnection> m_client; +}; diff --git a/Services/ProtocolServer/HttpDownload.cpp b/Services/ProtocolServer/HttpDownload.cpp new file mode 100644 index 0000000000..5ffa30362b --- /dev/null +++ b/Services/ProtocolServer/HttpDownload.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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 <LibHTTP/HttpJob.h> +#include <LibHTTP/HttpResponse.h> +#include <ProtocolServer/HttpDownload.h> + +HttpDownload::HttpDownload(PSClientConnection& client, NonnullRefPtr<HTTP::HttpJob>&& job) + : Download(client) + , m_job(job) +{ + m_job->on_finish = [this](bool success) { + if (auto* response = m_job->response()) { + set_payload(response->payload()); + set_response_headers(response->headers()); + } + + // if we didn't know the total size, pretend that the download finished successfully + // and set the total size to the downloaded size + if (!total_size().has_value()) + did_progress(downloaded_size(), downloaded_size()); + + did_finish(success); + }; + m_job->on_progress = [this](Optional<u32> total, u32 current) { + did_progress(total, current); + }; +} + +HttpDownload::~HttpDownload() +{ +} + +NonnullRefPtr<HttpDownload> HttpDownload::create_with_job(Badge<HttpProtocol>, PSClientConnection& client, NonnullRefPtr<HTTP::HttpJob>&& job) +{ + return adopt(*new HttpDownload(client, move(job))); +} diff --git a/Services/ProtocolServer/HttpDownload.h b/Services/ProtocolServer/HttpDownload.h new file mode 100644 index 0000000000..364fe6aef2 --- /dev/null +++ b/Services/ProtocolServer/HttpDownload.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/Badge.h> +#include <LibCore/Forward.h> +#include <LibHTTP/HttpJob.h> +#include <ProtocolServer/Download.h> + +class HttpProtocol; + +class HttpDownload final : public Download { +public: + virtual ~HttpDownload() override; + static NonnullRefPtr<HttpDownload> create_with_job(Badge<HttpProtocol>, PSClientConnection&, NonnullRefPtr<HTTP::HttpJob>&&); + +private: + explicit HttpDownload(PSClientConnection&, NonnullRefPtr<HTTP::HttpJob>&&); + + NonnullRefPtr<HTTP::HttpJob> m_job; +}; diff --git a/Services/ProtocolServer/HttpProtocol.cpp b/Services/ProtocolServer/HttpProtocol.cpp new file mode 100644 index 0000000000..1513a32e66 --- /dev/null +++ b/Services/ProtocolServer/HttpProtocol.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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 <LibHTTP/HttpJob.h> +#include <LibHTTP/HttpRequest.h> +#include <ProtocolServer/HttpDownload.h> +#include <ProtocolServer/HttpProtocol.h> + +HttpProtocol::HttpProtocol() + : Protocol("http") +{ +} + +HttpProtocol::~HttpProtocol() +{ +} + +RefPtr<Download> HttpProtocol::start_download(PSClientConnection& client, const URL& url) +{ + HTTP::HttpRequest request; + request.set_method(HTTP::HttpRequest::Method::GET); + request.set_url(url); + auto job = request.schedule(); + if (!job) + return nullptr; + return HttpDownload::create_with_job({}, client, (HTTP::HttpJob&)*job); +} diff --git a/Services/ProtocolServer/HttpProtocol.h b/Services/ProtocolServer/HttpProtocol.h new file mode 100644 index 0000000000..ea6c15d2a4 --- /dev/null +++ b/Services/ProtocolServer/HttpProtocol.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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 <ProtocolServer/Protocol.h> + +class HttpProtocol final : public Protocol { +public: + HttpProtocol(); + virtual ~HttpProtocol() override; + + virtual RefPtr<Download> start_download(PSClientConnection&, const URL&) override; +}; diff --git a/Services/ProtocolServer/HttpsDownload.cpp b/Services/ProtocolServer/HttpsDownload.cpp new file mode 100644 index 0000000000..8e068c2b04 --- /dev/null +++ b/Services/ProtocolServer/HttpsDownload.cpp @@ -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 <LibHTTP/HttpResponse.h> +#include <LibHTTP/HttpsJob.h> +#include <ProtocolServer/HttpsDownload.h> + +HttpsDownload::HttpsDownload(PSClientConnection& client, NonnullRefPtr<HTTP::HttpsJob>&& job) + : Download(client) + , m_job(job) +{ + m_job->on_finish = [this](bool success) { + if (auto* response = m_job->response()) { + set_payload(response->payload()); + set_response_headers(response->headers()); + } + + // if we didn't know the total size, pretend that the download finished successfully + // and set the total size to the downloaded size + if (!total_size().has_value()) + did_progress(downloaded_size(), downloaded_size()); + + did_finish(success); + }; + m_job->on_progress = [this](Optional<u32> total, u32 current) { + did_progress(total, current); + }; +} + +HttpsDownload::~HttpsDownload() +{ +} + +NonnullRefPtr<HttpsDownload> HttpsDownload::create_with_job(Badge<HttpsProtocol>, PSClientConnection& client, NonnullRefPtr<HTTP::HttpsJob>&& job) +{ + return adopt(*new HttpsDownload(client, move(job))); +} diff --git a/Services/ProtocolServer/HttpsDownload.h b/Services/ProtocolServer/HttpsDownload.h new file mode 100644 index 0000000000..a6c75bc4d7 --- /dev/null +++ b/Services/ProtocolServer/HttpsDownload.h @@ -0,0 +1,45 @@ +/* + * 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/Badge.h> +#include <LibCore/Forward.h> +#include <LibHTTP/HttpsJob.h> +#include <ProtocolServer/Download.h> + +class HttpsProtocol; + +class HttpsDownload final : public Download { +public: + virtual ~HttpsDownload() override; + static NonnullRefPtr<HttpsDownload> create_with_job(Badge<HttpsProtocol>, PSClientConnection&, NonnullRefPtr<HTTP::HttpsJob>&&); + +private: + explicit HttpsDownload(PSClientConnection&, NonnullRefPtr<HTTP::HttpsJob>&&); + + NonnullRefPtr<HTTP::HttpsJob> m_job; +}; diff --git a/Services/ProtocolServer/HttpsProtocol.cpp b/Services/ProtocolServer/HttpsProtocol.cpp new file mode 100644 index 0000000000..07020affe5 --- /dev/null +++ b/Services/ProtocolServer/HttpsProtocol.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018-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 <LibHTTP/HttpRequest.h> +#include <LibHTTP/HttpsJob.h> +#include <ProtocolServer/HttpsDownload.h> +#include <ProtocolServer/HttpsProtocol.h> + +HttpsProtocol::HttpsProtocol() + : Protocol("https") +{ +} + +HttpsProtocol::~HttpsProtocol() +{ +} + +RefPtr<Download> HttpsProtocol::start_download(PSClientConnection& client, const URL& url) +{ + HTTP::HttpRequest request; + request.set_method(HTTP::HttpRequest::Method::GET); + request.set_url(url); + auto job = HTTP::HttpsJob::construct(request); + auto download = HttpsDownload::create_with_job({}, client, (HTTP::HttpsJob&)*job); + job->start(); + return download; +} diff --git a/Services/ProtocolServer/HttpsProtocol.h b/Services/ProtocolServer/HttpsProtocol.h new file mode 100644 index 0000000000..e446178751 --- /dev/null +++ b/Services/ProtocolServer/HttpsProtocol.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2018-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 <ProtocolServer/Protocol.h> + +class HttpsProtocol final : public Protocol { +public: + HttpsProtocol(); + virtual ~HttpsProtocol() override; + + virtual RefPtr<Download> start_download(PSClientConnection&, const URL&) override; +}; diff --git a/Services/ProtocolServer/Makefile b/Services/ProtocolServer/Makefile new file mode 100644 index 0000000000..fa39255c53 --- /dev/null +++ b/Services/ProtocolServer/Makefile @@ -0,0 +1,25 @@ +OBJS = \ + PSClientConnection.o \ + Protocol.o \ + Download.o \ + HttpProtocol.o \ + HttpDownload.o \ + HttpsProtocol.o \ + HttpsDownload.o \ + main.o + +PROGRAM = ProtocolServer + +LIB_DEPS = HTTP TLS Crypto Core IPC + +EXTRA_CLEAN = ProtocolServerEndpoint.h ProtocolClientEndpoint.h + +*.cpp: ProtocolServerEndpoint.h ProtocolClientEndpoint.h + +ProtocolServerEndpoint.h: ProtocolServer.ipc | IPCCOMPILER + @echo "IPC $<"; $(IPCCOMPILER) $< > $@ + +ProtocolClientEndpoint.h: ProtocolClient.ipc | IPCCOMPILER + @echo "IPC $<"; $(IPCCOMPILER) $< > $@ + +include ../../Makefile.common diff --git a/Services/ProtocolServer/PSClientConnection.cpp b/Services/ProtocolServer/PSClientConnection.cpp new file mode 100644 index 0000000000..1d07fdb8b2 --- /dev/null +++ b/Services/ProtocolServer/PSClientConnection.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/Badge.h> +#include <AK/SharedBuffer.h> +#include <ProtocolServer/Download.h> +#include <ProtocolServer/PSClientConnection.h> +#include <ProtocolServer/Protocol.h> +#include <ProtocolServer/ProtocolClientEndpoint.h> + +static HashMap<int, RefPtr<PSClientConnection>> s_connections; + +PSClientConnection::PSClientConnection(Core::LocalSocket& socket, int client_id) + : IPC::ClientConnection<ProtocolServerEndpoint>(*this, socket, client_id) +{ + s_connections.set(client_id, *this); +} + +PSClientConnection::~PSClientConnection() +{ +} + +void PSClientConnection::die() +{ + s_connections.remove(client_id()); +} + +OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> PSClientConnection::handle(const Messages::ProtocolServer::IsSupportedProtocol& message) +{ + bool supported = Protocol::find_by_name(message.protocol().to_lowercase()); + return make<Messages::ProtocolServer::IsSupportedProtocolResponse>(supported); +} + +OwnPtr<Messages::ProtocolServer::StartDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StartDownload& message) +{ + URL url(message.url()); + if (!url.is_valid()) + return make<Messages::ProtocolServer::StartDownloadResponse>(-1); + auto* protocol = Protocol::find_by_name(url.protocol()); + if (!protocol) + return make<Messages::ProtocolServer::StartDownloadResponse>(-1); + auto download = protocol->start_download(*this, url); + return make<Messages::ProtocolServer::StartDownloadResponse>(download->id()); +} + +OwnPtr<Messages::ProtocolServer::StopDownloadResponse> PSClientConnection::handle(const Messages::ProtocolServer::StopDownload& message) +{ + auto* download = Download::find_by_id(message.download_id()); + bool success = false; + if (download) { + download->stop(); + success = true; + } + return make<Messages::ProtocolServer::StopDownloadResponse>(success); +} + +void PSClientConnection::did_finish_download(Badge<Download>, Download& download, bool success) +{ + RefPtr<SharedBuffer> buffer; + if (success && download.payload().size() > 0 && !download.payload().is_null()) { + buffer = SharedBuffer::create_with_size(download.payload().size()); + memcpy(buffer->data(), download.payload().data(), download.payload().size()); + buffer->seal(); + buffer->share_with(client_pid()); + m_shared_buffers.set(buffer->shbuf_id(), buffer); + } + ASSERT(download.total_size().has_value()); + + IPC::Dictionary response_headers; + for (auto& it : download.response_headers()) + response_headers.add(it.key, it.value); + post_message(Messages::ProtocolClient::DownloadFinished(download.id(), success, download.total_size().value(), buffer ? buffer->shbuf_id() : -1, response_headers)); +} + +void PSClientConnection::did_progress_download(Badge<Download>, Download& download) +{ + post_message(Messages::ProtocolClient::DownloadProgress(download.id(), download.total_size(), download.downloaded_size())); +} + +OwnPtr<Messages::ProtocolServer::GreetResponse> PSClientConnection::handle(const Messages::ProtocolServer::Greet&) +{ + return make<Messages::ProtocolServer::GreetResponse>(client_id()); +} + +OwnPtr<Messages::ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const Messages::ProtocolServer::DisownSharedBuffer& message) +{ + m_shared_buffers.remove(message.shbuf_id()); + return make<Messages::ProtocolServer::DisownSharedBufferResponse>(); +} diff --git a/Services/ProtocolServer/PSClientConnection.h b/Services/ProtocolServer/PSClientConnection.h new file mode 100644 index 0000000000..56a7c63c0c --- /dev/null +++ b/Services/ProtocolServer/PSClientConnection.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/HashMap.h> +#include <LibIPC/ClientConnection.h> +#include <ProtocolServer/ProtocolServerEndpoint.h> + +class Download; + +class PSClientConnection final : public IPC::ClientConnection<ProtocolServerEndpoint> + , public ProtocolServerEndpoint { + C_OBJECT(PSClientConnection) +public: + explicit PSClientConnection(Core::LocalSocket&, int client_id); + ~PSClientConnection() override; + + virtual void die() override; + + void did_finish_download(Badge<Download>, Download&, bool success); + void did_progress_download(Badge<Download>, Download&); + +private: + virtual OwnPtr<Messages::ProtocolServer::GreetResponse> handle(const Messages::ProtocolServer::Greet&) override; + virtual OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> handle(const Messages::ProtocolServer::IsSupportedProtocol&) override; + virtual OwnPtr<Messages::ProtocolServer::StartDownloadResponse> handle(const Messages::ProtocolServer::StartDownload&) override; + virtual OwnPtr<Messages::ProtocolServer::StopDownloadResponse> handle(const Messages::ProtocolServer::StopDownload&) override; + virtual OwnPtr<Messages::ProtocolServer::DisownSharedBufferResponse> handle(const Messages::ProtocolServer::DisownSharedBuffer&) override; + + HashMap<i32, RefPtr<AK::SharedBuffer>> m_shared_buffers; +}; diff --git a/Services/ProtocolServer/Protocol.cpp b/Services/ProtocolServer/Protocol.cpp new file mode 100644 index 0000000000..bf53895eaa --- /dev/null +++ b/Services/ProtocolServer/Protocol.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/HashMap.h> +#include <ProtocolServer/Protocol.h> + +static HashMap<String, Protocol*>& all_protocols() +{ + static HashMap<String, Protocol*> map; + return map; +} + +Protocol* Protocol::find_by_name(const String& name) +{ + return all_protocols().get(name).value_or(nullptr); +} + +Protocol::Protocol(const String& name) +{ + all_protocols().set(name, this); +} + +Protocol::~Protocol() +{ + ASSERT_NOT_REACHED(); +} diff --git a/Services/ProtocolServer/Protocol.h b/Services/ProtocolServer/Protocol.h new file mode 100644 index 0000000000..19bd38f517 --- /dev/null +++ b/Services/ProtocolServer/Protocol.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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/RefPtr.h> +#include <AK/URL.h> + +class Download; +class PSClientConnection; + +class Protocol { +public: + virtual ~Protocol(); + + const String& name() const { return m_name; } + virtual RefPtr<Download> start_download(PSClientConnection&, const URL&) = 0; + + static Protocol* find_by_name(const String&); + +protected: + explicit Protocol(const String& name); + +private: + String m_name; +}; diff --git a/Services/ProtocolServer/ProtocolClient.ipc b/Services/ProtocolServer/ProtocolClient.ipc new file mode 100644 index 0000000000..575b3ddd2b --- /dev/null +++ b/Services/ProtocolServer/ProtocolClient.ipc @@ -0,0 +1,6 @@ +endpoint ProtocolClient = 13 +{ + // Download notifications + DownloadProgress(i32 download_id, Optional<u32> total_size, u32 downloaded_size) =| + DownloadFinished(i32 download_id, bool success, u32 total_size, i32 shbuf_id, IPC::Dictionary response_headers) =| +} diff --git a/Services/ProtocolServer/ProtocolServer.ipc b/Services/ProtocolServer/ProtocolServer.ipc new file mode 100644 index 0000000000..74ec138ab5 --- /dev/null +++ b/Services/ProtocolServer/ProtocolServer.ipc @@ -0,0 +1,15 @@ +endpoint ProtocolServer = 9 +{ + // Basic protocol + Greet() => (i32 client_id) + + // FIXME: It would be nice if the kernel provided a way to avoid this + DisownSharedBuffer(i32 shbuf_id) => () + + // Test if a specific protocol is supported, e.g "http" + IsSupportedProtocol(String protocol) => (bool supported) + + // Download API + StartDownload(String url) => (i32 download_id) + StopDownload(i32 download_id) => (bool success) +} diff --git a/Services/ProtocolServer/main.cpp b/Services/ProtocolServer/main.cpp new file mode 100644 index 0000000000..9a3da67b78 --- /dev/null +++ b/Services/ProtocolServer/main.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> + * 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 <LibCore/LocalServer.h> +#include <LibIPC/ClientConnection.h> +#include <ProtocolServer/HttpProtocol.h> +#include <ProtocolServer/HttpsProtocol.h> +#include <ProtocolServer/PSClientConnection.h> + +int main(int, char**) +{ + if (pledge("stdio inet shared_buffer accept unix rpath cpath fattr", nullptr) < 0) { + perror("pledge"); + return 1; + } + Core::EventLoop event_loop; + // FIXME: Establish a connection to LookupServer and then drop "unix"? + if (pledge("stdio inet shared_buffer accept unix", nullptr) < 0) { + perror("pledge"); + return 1; + } + if (unveil("/tmp/portal/lookup", "rw") < 0) { + perror("unveil"); + return 1; + } + if (unveil(nullptr, nullptr) < 0) { + perror("unveil"); + return 1; + } + + (void)*new HttpProtocol; + (void)*new HttpsProtocol; + auto server = Core::LocalServer::construct(); + bool ok = server->take_over_from_system_server(); + ASSERT(ok); + server->on_ready_to_accept = [&] { + auto client_socket = server->accept(); + if (!client_socket) { + dbg() << "ProtocolServer: accept failed."; + return; + } + static int s_next_client_id = 0; + int client_id = ++s_next_client_id; + IPC::new_client_connection<PSClientConnection>(*client_socket, client_id); + }; + return event_loop.exec(); +} |