1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:18:12 +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:
Lenny Maiorani 2021-01-14 12:06:54 -07:00 committed by Andreas Kling
parent 0f7efd5bf1
commit 9f64424661
4 changed files with 43 additions and 54 deletions

View file

@ -25,7 +25,7 @@
*/
#include <LibHTTP/HttpJob.h>
#include <LibHTTP/HttpResponse.h>
#include <ProtocolServer/HttpCommon.h>
#include <ProtocolServer/HttpDownload.h>
#include <ProtocolServer/HttpProtocol.h>
@ -35,29 +35,7 @@ HttpDownload::HttpDownload(ClientConnection& client, NonnullRefPtr<HTTP::HttpJob
: Download(client, move(output_stream))
, m_job(job)
{
m_job->on_headers_received = [this](auto& headers, auto response_code) {
if (response_code.has_value())
set_status_code(response_code.value());
set_response_headers(headers);
};
m_job->on_finish = [this](bool success) {
if (auto* response = m_job->response()) {
set_status_code(response->code());
set_response_headers(response->headers());
set_downloaded_size(this->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 (!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);
};
Detail::init(this, job);
}
HttpDownload::~HttpDownload()