mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 22:37:36 +00:00
Http[s]Download: Make the constructor's initialization DRY
Problem: - `HttpDownload()` and `HttpsDownload()` implementations are the same except for types and certificates. Solution: - Follow the "Don't Repeat Yourself" mantra and de-duplicate the code using templates.
This commit is contained in:
parent
0f7efd5bf1
commit
9f64424661
4 changed files with 43 additions and 54 deletions
|
@ -28,14 +28,50 @@
|
|||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibHTTP/HttpRequest.h>
|
||||
#include <ProtocolServer/ClientConnection.h>
|
||||
#include <ProtocolServer/Download.h>
|
||||
|
||||
namespace ProtocolServer::Detail {
|
||||
|
||||
template<typename TSelf, typename TJob>
|
||||
void init(TSelf* self, TJob job)
|
||||
{
|
||||
job->on_headers_received = [&](auto& headers, auto response_code) {
|
||||
if (response_code.has_value())
|
||||
self->set_status_code(response_code.value());
|
||||
self->set_response_headers(headers);
|
||||
};
|
||||
|
||||
job->on_finish = [&](bool success) {
|
||||
if (auto* response = job->response()) {
|
||||
self->set_status_code(response->code());
|
||||
self->set_response_headers(response->headers());
|
||||
self->set_downloaded_size(self->output_stream().size());
|
||||
}
|
||||
|
||||
// if we didn't know the total size, pretend that the download finished successfully
|
||||
// and set the total size to the downloaded size
|
||||
if (!self->total_size().has_value())
|
||||
self->did_progress(self->downloaded_size(), self->downloaded_size());
|
||||
|
||||
self->did_finish(success);
|
||||
};
|
||||
job->on_progress = [&](Optional<u32> total, u32 current) {
|
||||
self->did_progress(total, current);
|
||||
};
|
||||
if constexpr (requires { job->on_certificate_requested; }) {
|
||||
job->on_certificate_requested = [&](auto&) {
|
||||
self->did_request_certificates();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue