mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 03:18:11 +00:00
Http[s]Protocol: Make the code start_download DRY
Problem: - `HttpProtocol::start_download` and `HttpsProtocol::start_download` implementations are the same except for a few types. Solution: - Follow the "Don't Repeat Yourself" mantra and de-duplicate the code using templates.
This commit is contained in:
parent
6d6b3f9523
commit
7e71de8f1f
9 changed files with 120 additions and 56 deletions
67
Userland/Services/ProtocolServer/HttpCommon.h
Normal file
67
Userland/Services/ProtocolServer/HttpCommon.h
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2018-2021, 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/OwnPtr.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <LibHTTP/HttpRequest.h>
|
||||||
|
#include <ProtocolServer/ClientConnection.h>
|
||||||
|
#include <ProtocolServer/Download.h>
|
||||||
|
|
||||||
|
namespace ProtocolServer::Detail {
|
||||||
|
|
||||||
|
template<typename TBadgedProtocol, typename TPipeResult>
|
||||||
|
OwnPtr<Download> start_download(TBadgedProtocol&& protocol, ClientConnection& client, const String& method, const URL& url, const HashMap<String, String>& 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<OutputFileStream>(pipe_result.value().write_fd);
|
||||||
|
output_stream->make_unbuffered();
|
||||||
|
auto job = TJob::construct(request, *output_stream);
|
||||||
|
auto download = TDownload::create_with_job(forward<TBadgedProtocol>(protocol), client, (TJob&)*job, move(output_stream));
|
||||||
|
download->set_download_fd(pipe_result.value().read_fd);
|
||||||
|
job->start();
|
||||||
|
return download;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -27,6 +27,7 @@
|
||||||
#include <LibHTTP/HttpJob.h>
|
#include <LibHTTP/HttpJob.h>
|
||||||
#include <LibHTTP/HttpResponse.h>
|
#include <LibHTTP/HttpResponse.h>
|
||||||
#include <ProtocolServer/HttpDownload.h>
|
#include <ProtocolServer/HttpDownload.h>
|
||||||
|
#include <ProtocolServer/HttpProtocol.h>
|
||||||
|
|
||||||
namespace ProtocolServer {
|
namespace ProtocolServer {
|
||||||
|
|
||||||
|
@ -66,7 +67,7 @@ HttpDownload::~HttpDownload()
|
||||||
m_job->shutdown();
|
m_job->shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullOwnPtr<HttpDownload> HttpDownload::create_with_job(Badge<HttpProtocol>, ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
NonnullOwnPtr<HttpDownload> HttpDownload::create_with_job(Badge<HttpProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||||
{
|
{
|
||||||
return adopt_own(*new HttpDownload(client, move(job), move(output_stream)));
|
return adopt_own(*new HttpDownload(client, move(job), move(output_stream)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/Badge.h>
|
#include <AK/Badge.h>
|
||||||
|
#include <AK/NonnullOwnPtr.h>
|
||||||
#include <LibCore/Forward.h>
|
#include <LibCore/Forward.h>
|
||||||
#include <LibHTTP/Forward.h>
|
#include <LibHTTP/Forward.h>
|
||||||
#include <ProtocolServer/Download.h>
|
#include <ProtocolServer/Download.h>
|
||||||
|
@ -36,7 +37,7 @@ namespace ProtocolServer {
|
||||||
class HttpDownload final : public Download {
|
class HttpDownload final : public Download {
|
||||||
public:
|
public:
|
||||||
virtual ~HttpDownload() override;
|
virtual ~HttpDownload() override;
|
||||||
static NonnullOwnPtr<HttpDownload> create_with_job(Badge<HttpProtocol>, ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
|
static NonnullOwnPtr<HttpDownload> create_with_job(Badge<HttpProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit HttpDownload(ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
|
explicit HttpDownload(ClientConnection&, NonnullRefPtr<HTTP::HttpJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||||
|
|
|
@ -24,8 +24,16 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Badge.h>
|
||||||
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
#include <LibHTTP/HttpJob.h>
|
#include <LibHTTP/HttpJob.h>
|
||||||
#include <LibHTTP/HttpRequest.h>
|
#include <ProtocolServer/ClientConnection.h>
|
||||||
|
#include <ProtocolServer/Download.h>
|
||||||
|
#include <ProtocolServer/HttpCommon.h>
|
||||||
#include <ProtocolServer/HttpDownload.h>
|
#include <ProtocolServer/HttpDownload.h>
|
||||||
#include <ProtocolServer/HttpProtocol.h>
|
#include <ProtocolServer/HttpProtocol.h>
|
||||||
|
|
||||||
|
@ -36,32 +44,9 @@ HttpProtocol::HttpProtocol()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpProtocol::~HttpProtocol()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
OwnPtr<Download> HttpProtocol::start_download(ClientConnection& client, const String& method, const URL& url, const HashMap<String, String>& headers, ReadonlyBytes body)
|
OwnPtr<Download> HttpProtocol::start_download(ClientConnection& client, const String& method, const URL& url, const HashMap<String, String>& headers, ReadonlyBytes body)
|
||||||
{
|
{
|
||||||
HTTP::HttpRequest request;
|
return Detail::start_download(AK::Badge<HttpProtocol> {}, client, method, url, headers, body, get_pipe_for_download());
|
||||||
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<OutputFileStream>(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,14 +26,26 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
|
#include <LibHTTP/HttpJob.h>
|
||||||
|
#include <ProtocolServer/ClientConnection.h>
|
||||||
|
#include <ProtocolServer/Download.h>
|
||||||
|
#include <ProtocolServer/HttpDownload.h>
|
||||||
#include <ProtocolServer/Protocol.h>
|
#include <ProtocolServer/Protocol.h>
|
||||||
|
|
||||||
namespace ProtocolServer {
|
namespace ProtocolServer {
|
||||||
|
|
||||||
class HttpProtocol final : public Protocol {
|
class HttpProtocol final : public Protocol {
|
||||||
public:
|
public:
|
||||||
|
using JobType = HTTP::HttpJob;
|
||||||
|
using DownloadType = HttpDownload;
|
||||||
|
|
||||||
HttpProtocol();
|
HttpProtocol();
|
||||||
virtual ~HttpProtocol() override;
|
~HttpProtocol() override = default;
|
||||||
|
|
||||||
virtual OwnPtr<Download> start_download(ClientConnection&, const String& method, const URL&, const HashMap<String, String>& headers, ReadonlyBytes body) override;
|
virtual OwnPtr<Download> start_download(ClientConnection&, const String& method, const URL&, const HashMap<String, String>& headers, ReadonlyBytes body) override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#include <LibHTTP/HttpResponse.h>
|
#include <LibHTTP/HttpResponse.h>
|
||||||
#include <LibHTTP/HttpsJob.h>
|
#include <LibHTTP/HttpsJob.h>
|
||||||
#include <ProtocolServer/HttpsDownload.h>
|
#include <ProtocolServer/HttpsDownload.h>
|
||||||
|
#include <ProtocolServer/HttpsProtocol.h>
|
||||||
|
|
||||||
namespace ProtocolServer {
|
namespace ProtocolServer {
|
||||||
|
|
||||||
|
@ -74,7 +75,7 @@ HttpsDownload::~HttpsDownload()
|
||||||
m_job->shutdown();
|
m_job->shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullOwnPtr<HttpsDownload> HttpsDownload::create_with_job(Badge<HttpsProtocol>, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
NonnullOwnPtr<HttpsDownload> HttpsDownload::create_with_job(Badge<HttpsProtocol>&&, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
|
||||||
{
|
{
|
||||||
return adopt_own(*new HttpsDownload(client, move(job), move(output_stream)));
|
return adopt_own(*new HttpsDownload(client, move(job), move(output_stream)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ namespace ProtocolServer {
|
||||||
class HttpsDownload final : public Download {
|
class HttpsDownload final : public Download {
|
||||||
public:
|
public:
|
||||||
virtual ~HttpsDownload() override;
|
virtual ~HttpsDownload() override;
|
||||||
static NonnullOwnPtr<HttpsDownload> create_with_job(Badge<HttpsProtocol>, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
|
static NonnullOwnPtr<HttpsDownload> create_with_job(Badge<HttpsProtocol>&&, ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit HttpsDownload(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
|
explicit HttpsDownload(ClientConnection&, NonnullRefPtr<HTTP::HttpsJob>, NonnullOwnPtr<OutputFileStream>&&);
|
||||||
|
|
|
@ -24,8 +24,16 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibHTTP/HttpRequest.h>
|
#include <AK/Badge.h>
|
||||||
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
#include <LibHTTP/HttpsJob.h>
|
#include <LibHTTP/HttpsJob.h>
|
||||||
|
#include <ProtocolServer/ClientConnection.h>
|
||||||
|
#include <ProtocolServer/Download.h>
|
||||||
|
#include <ProtocolServer/HttpCommon.h>
|
||||||
#include <ProtocolServer/HttpsDownload.h>
|
#include <ProtocolServer/HttpsDownload.h>
|
||||||
#include <ProtocolServer/HttpsProtocol.h>
|
#include <ProtocolServer/HttpsProtocol.h>
|
||||||
|
|
||||||
|
@ -36,32 +44,9 @@ HttpsProtocol::HttpsProtocol()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpsProtocol::~HttpsProtocol()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
OwnPtr<Download> HttpsProtocol::start_download(ClientConnection& client, const String& method, const URL& url, const HashMap<String, String>& headers, ReadonlyBytes body)
|
OwnPtr<Download> HttpsProtocol::start_download(ClientConnection& client, const String& method, const URL& url, const HashMap<String, String>& headers, ReadonlyBytes body)
|
||||||
{
|
{
|
||||||
HTTP::HttpRequest request;
|
return Detail::start_download(AK::Badge<HttpsProtocol> {}, client, method, url, headers, body, get_pipe_for_download());
|
||||||
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<OutputFileStream>(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,14 +26,26 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/ByteBuffer.h>
|
||||||
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/String.h>
|
||||||
|
#include <AK/URL.h>
|
||||||
|
#include <LibHTTP/HttpsJob.h>
|
||||||
|
#include <ProtocolServer/ClientConnection.h>
|
||||||
|
#include <ProtocolServer/Download.h>
|
||||||
|
#include <ProtocolServer/HttpsDownload.h>
|
||||||
#include <ProtocolServer/Protocol.h>
|
#include <ProtocolServer/Protocol.h>
|
||||||
|
|
||||||
namespace ProtocolServer {
|
namespace ProtocolServer {
|
||||||
|
|
||||||
class HttpsProtocol final : public Protocol {
|
class HttpsProtocol final : public Protocol {
|
||||||
public:
|
public:
|
||||||
|
using JobType = HTTP::HttpsJob;
|
||||||
|
using DownloadType = HttpsDownload;
|
||||||
|
|
||||||
HttpsProtocol();
|
HttpsProtocol();
|
||||||
virtual ~HttpsProtocol() override;
|
~HttpsProtocol() override = default;
|
||||||
|
|
||||||
virtual OwnPtr<Download> start_download(ClientConnection&, const String& method, const URL&, const HashMap<String, String>& headers, ReadonlyBytes body) override;
|
virtual OwnPtr<Download> start_download(ClientConnection&, const String& method, const URL&, const HashMap<String, String>& headers, ReadonlyBytes body) override;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue