diff --git a/Userland/Services/ProtocolServer/HttpCommon.h b/Userland/Services/ProtocolServer/HttpCommon.h new file mode 100644 index 00000000000..0f3d89a5581 --- /dev/null +++ b/Userland/Services/ProtocolServer/HttpCommon.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling + * 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 +#include +#include +#include +#include +#include +#include + +namespace ProtocolServer::Detail { + +template +OwnPtr start_download(TBadgedProtocol&& protocol, ClientConnection& client, const String& method, const URL& url, const HashMap& headers, ReadonlyBytes body, TPipeResult&& pipe_result) +{ + using TJob = TBadgedProtocol::Type::JobType; + using TDownload = TBadgedProtocol::Type::DownloadType; + + if (pipe_result.is_error()) { + return {}; + } + + HTTP::HttpRequest request; + if (method.equals_ignoring_case("post")) + request.set_method(HTTP::HttpRequest::Method::POST); + else + request.set_method(HTTP::HttpRequest::Method::GET); + request.set_url(url); + request.set_headers(headers); + request.set_body(body); + + auto output_stream = make(pipe_result.value().write_fd); + output_stream->make_unbuffered(); + auto job = TJob::construct(request, *output_stream); + auto download = TDownload::create_with_job(forward(protocol), client, (TJob&)*job, move(output_stream)); + download->set_download_fd(pipe_result.value().read_fd); + job->start(); + return download; +} + +} diff --git a/Userland/Services/ProtocolServer/HttpDownload.cpp b/Userland/Services/ProtocolServer/HttpDownload.cpp index 8ba945d3357..df9c5b866d8 100644 --- a/Userland/Services/ProtocolServer/HttpDownload.cpp +++ b/Userland/Services/ProtocolServer/HttpDownload.cpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace ProtocolServer { @@ -66,7 +67,7 @@ HttpDownload::~HttpDownload() m_job->shutdown(); } -NonnullOwnPtr HttpDownload::create_with_job(Badge, ClientConnection& client, NonnullRefPtr job, NonnullOwnPtr&& output_stream) +NonnullOwnPtr HttpDownload::create_with_job(Badge&&, ClientConnection& client, NonnullRefPtr job, NonnullOwnPtr&& output_stream) { return adopt_own(*new HttpDownload(client, move(job), move(output_stream))); } diff --git a/Userland/Services/ProtocolServer/HttpDownload.h b/Userland/Services/ProtocolServer/HttpDownload.h index 50095bd0e54..886d018942e 100644 --- a/Userland/Services/ProtocolServer/HttpDownload.h +++ b/Userland/Services/ProtocolServer/HttpDownload.h @@ -27,6 +27,7 @@ #pragma once #include +#include #include #include #include @@ -36,7 +37,7 @@ namespace ProtocolServer { class HttpDownload final : public Download { public: virtual ~HttpDownload() override; - static NonnullOwnPtr create_with_job(Badge, ClientConnection&, NonnullRefPtr, NonnullOwnPtr&&); + static NonnullOwnPtr create_with_job(Badge&&, ClientConnection&, NonnullRefPtr, NonnullOwnPtr&&); private: explicit HttpDownload(ClientConnection&, NonnullRefPtr, NonnullOwnPtr&&); diff --git a/Userland/Services/ProtocolServer/HttpProtocol.cpp b/Userland/Services/ProtocolServer/HttpProtocol.cpp index acc10a88ec9..57df8be5db6 100644 --- a/Userland/Services/ProtocolServer/HttpProtocol.cpp +++ b/Userland/Services/ProtocolServer/HttpProtocol.cpp @@ -24,8 +24,16 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include +#include +#include +#include +#include +#include #include -#include +#include +#include +#include #include #include @@ -36,32 +44,9 @@ HttpProtocol::HttpProtocol() { } -HttpProtocol::~HttpProtocol() -{ -} - OwnPtr HttpProtocol::start_download(ClientConnection& client, const String& method, const URL& url, const HashMap& headers, ReadonlyBytes body) { - HTTP::HttpRequest request; - if (method.equals_ignoring_case("post")) - request.set_method(HTTP::HttpRequest::Method::POST); - else - request.set_method(HTTP::HttpRequest::Method::GET); - request.set_url(url); - request.set_headers(headers); - request.set_body(body); - - auto pipe_result = get_pipe_for_download(); - if (pipe_result.is_error()) - return {}; - - auto output_stream = make(pipe_result.value().write_fd); - output_stream->make_unbuffered(); - auto job = HTTP::HttpJob::construct(request, *output_stream); - auto download = HttpDownload::create_with_job({}, client, (HTTP::HttpJob&)*job, move(output_stream)); - download->set_download_fd(pipe_result.value().read_fd); - job->start(); - return download; + return Detail::start_download(AK::Badge {}, client, method, url, headers, body, get_pipe_for_download()); } } diff --git a/Userland/Services/ProtocolServer/HttpProtocol.h b/Userland/Services/ProtocolServer/HttpProtocol.h index 8c4a564f377..ef7aaaaec5e 100644 --- a/Userland/Services/ProtocolServer/HttpProtocol.h +++ b/Userland/Services/ProtocolServer/HttpProtocol.h @@ -26,14 +26,26 @@ #pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include #include namespace ProtocolServer { class HttpProtocol final : public Protocol { public: + using JobType = HTTP::HttpJob; + using DownloadType = HttpDownload; + HttpProtocol(); - virtual ~HttpProtocol() override; + ~HttpProtocol() override = default; virtual OwnPtr start_download(ClientConnection&, const String& method, const URL&, const HashMap& headers, ReadonlyBytes body) override; }; diff --git a/Userland/Services/ProtocolServer/HttpsDownload.cpp b/Userland/Services/ProtocolServer/HttpsDownload.cpp index 991dd730d88..cf88ac387fd 100644 --- a/Userland/Services/ProtocolServer/HttpsDownload.cpp +++ b/Userland/Services/ProtocolServer/HttpsDownload.cpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace ProtocolServer { @@ -74,7 +75,7 @@ HttpsDownload::~HttpsDownload() m_job->shutdown(); } -NonnullOwnPtr HttpsDownload::create_with_job(Badge, ClientConnection& client, NonnullRefPtr job, NonnullOwnPtr&& output_stream) +NonnullOwnPtr HttpsDownload::create_with_job(Badge&&, ClientConnection& client, NonnullRefPtr job, NonnullOwnPtr&& output_stream) { return adopt_own(*new HttpsDownload(client, move(job), move(output_stream))); } diff --git a/Userland/Services/ProtocolServer/HttpsDownload.h b/Userland/Services/ProtocolServer/HttpsDownload.h index 254172b3f72..988f3dc6c4f 100644 --- a/Userland/Services/ProtocolServer/HttpsDownload.h +++ b/Userland/Services/ProtocolServer/HttpsDownload.h @@ -36,7 +36,7 @@ namespace ProtocolServer { class HttpsDownload final : public Download { public: virtual ~HttpsDownload() override; - static NonnullOwnPtr create_with_job(Badge, ClientConnection&, NonnullRefPtr, NonnullOwnPtr&&); + static NonnullOwnPtr create_with_job(Badge&&, ClientConnection&, NonnullRefPtr, NonnullOwnPtr&&); private: explicit HttpsDownload(ClientConnection&, NonnullRefPtr, NonnullOwnPtr&&); diff --git a/Userland/Services/ProtocolServer/HttpsProtocol.cpp b/Userland/Services/ProtocolServer/HttpsProtocol.cpp index ef8e63fd0df..79252daa4fb 100644 --- a/Userland/Services/ProtocolServer/HttpsProtocol.cpp +++ b/Userland/Services/ProtocolServer/HttpsProtocol.cpp @@ -24,8 +24,16 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include #include #include @@ -36,32 +44,9 @@ HttpsProtocol::HttpsProtocol() { } -HttpsProtocol::~HttpsProtocol() -{ -} - OwnPtr HttpsProtocol::start_download(ClientConnection& client, const String& method, const URL& url, const HashMap& headers, ReadonlyBytes body) { - HTTP::HttpRequest request; - if (method.equals_ignoring_case("post")) - request.set_method(HTTP::HttpRequest::Method::POST); - else - request.set_method(HTTP::HttpRequest::Method::GET); - request.set_url(url); - request.set_headers(headers); - request.set_body(body); - - auto pipe_result = get_pipe_for_download(); - if (pipe_result.is_error()) - return {}; - - auto output_stream = make(pipe_result.value().write_fd); - output_stream->make_unbuffered(); - auto job = HTTP::HttpsJob::construct(request, *output_stream); - auto download = HttpsDownload::create_with_job({}, client, (HTTP::HttpsJob&)*job, move(output_stream)); - download->set_download_fd(pipe_result.value().read_fd); - job->start(); - return download; + return Detail::start_download(AK::Badge {}, client, method, url, headers, body, get_pipe_for_download()); } } diff --git a/Userland/Services/ProtocolServer/HttpsProtocol.h b/Userland/Services/ProtocolServer/HttpsProtocol.h index 40e59aa2712..dcc34c704d0 100644 --- a/Userland/Services/ProtocolServer/HttpsProtocol.h +++ b/Userland/Services/ProtocolServer/HttpsProtocol.h @@ -26,14 +26,26 @@ #pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include #include namespace ProtocolServer { class HttpsProtocol final : public Protocol { public: + using JobType = HTTP::HttpsJob; + using DownloadType = HttpsDownload; + HttpsProtocol(); - virtual ~HttpsProtocol() override; + ~HttpsProtocol() override = default; virtual OwnPtr start_download(ClientConnection&, const String& method, const URL&, const HashMap& headers, ReadonlyBytes body) override; };