diff --git a/Userland/Services/ProtocolServer/Download.h b/Userland/Services/ProtocolServer/Download.h index 35c60269f4..26f75226c2 100644 --- a/Userland/Services/ProtocolServer/Download.h +++ b/Userland/Services/ProtocolServer/Download.h @@ -55,9 +55,6 @@ public: void set_download_fd(int fd) { m_download_fd = fd; } int download_fd() const { return m_download_fd; } -protected: - explicit Download(ClientConnection&, NonnullOwnPtr&&); - void did_finish(bool success); void did_progress(Optional total_size, u32 downloaded_size); void set_status_code(u32 status_code) { m_status_code = status_code; } @@ -66,6 +63,9 @@ protected: void set_downloaded_size(size_t size) { m_downloaded_size = size; } const OutputFileStream& output_stream() const { return *m_output_stream; } +protected: + explicit Download(ClientConnection&, NonnullOwnPtr&&); + private: ClientConnection& m_client; i32 m_id { 0 }; diff --git a/Userland/Services/ProtocolServer/HttpCommon.h b/Userland/Services/ProtocolServer/HttpCommon.h index 0f3d89a558..455af38d59 100644 --- a/Userland/Services/ProtocolServer/HttpCommon.h +++ b/Userland/Services/ProtocolServer/HttpCommon.h @@ -28,14 +28,50 @@ #include #include +#include +#include #include #include +#include #include #include #include namespace ProtocolServer::Detail { +template +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 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 OwnPtr start_download(TBadgedProtocol&& protocol, ClientConnection& client, const String& method, const URL& url, const HashMap& headers, ReadonlyBytes body, TPipeResult&& pipe_result) { diff --git a/Userland/Services/ProtocolServer/HttpDownload.cpp b/Userland/Services/ProtocolServer/HttpDownload.cpp index df9c5b866d..56ab9a24f5 100644 --- a/Userland/Services/ProtocolServer/HttpDownload.cpp +++ b/Userland/Services/ProtocolServer/HttpDownload.cpp @@ -25,7 +25,7 @@ */ #include -#include +#include #include #include @@ -35,29 +35,7 @@ HttpDownload::HttpDownload(ClientConnection& client, NonnullRefPtron_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 total, u32 current) { - did_progress(total, current); - }; + Detail::init(this, job); } HttpDownload::~HttpDownload() diff --git a/Userland/Services/ProtocolServer/HttpsDownload.cpp b/Userland/Services/ProtocolServer/HttpsDownload.cpp index cf88ac387f..301cc8b765 100644 --- a/Userland/Services/ProtocolServer/HttpsDownload.cpp +++ b/Userland/Services/ProtocolServer/HttpsDownload.cpp @@ -24,8 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include +#include #include #include @@ -35,32 +35,7 @@ HttpsDownload::HttpsDownload(ClientConnection& client, NonnullRefPtron_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 total, u32 current) { - did_progress(total, current); - }; - m_job->on_certificate_requested = [this](auto&) { - did_request_certificates(); - }; + Detail::init(this, job); } void HttpsDownload::set_certificate(String certificate, String key)