HttpsDownload.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2020, The SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibHTTP/HttpResponse.h>
  27. #include <LibHTTP/HttpsJob.h>
  28. #include <ProtocolServer/HttpsDownload.h>
  29. namespace ProtocolServer {
  30. HttpsDownload::HttpsDownload(ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
  31. : Download(client, move(output_stream))
  32. , m_job(job)
  33. {
  34. m_job->on_headers_received = [this](auto& headers, auto response_code) {
  35. if (response_code.has_value())
  36. set_status_code(response_code.value());
  37. set_response_headers(headers);
  38. };
  39. m_job->on_finish = [this](bool success) {
  40. if (auto* response = m_job->response()) {
  41. set_status_code(response->code());
  42. set_response_headers(response->headers());
  43. set_downloaded_size(this->output_stream().size());
  44. }
  45. // if we didn't know the total size, pretend that the download finished successfully
  46. // and set the total size to the downloaded size
  47. if (!total_size().has_value())
  48. did_progress(downloaded_size(), downloaded_size());
  49. did_finish(success);
  50. };
  51. m_job->on_progress = [this](Optional<u32> total, u32 current) {
  52. did_progress(total, current);
  53. };
  54. m_job->on_certificate_requested = [this](auto&) {
  55. did_request_certificates();
  56. };
  57. }
  58. void HttpsDownload::set_certificate(String certificate, String key)
  59. {
  60. m_job->set_certificate(move(certificate), move(key));
  61. }
  62. HttpsDownload::~HttpsDownload()
  63. {
  64. m_job->on_finish = nullptr;
  65. m_job->on_progress = nullptr;
  66. m_job->shutdown();
  67. }
  68. NonnullOwnPtr<HttpsDownload> HttpsDownload::create_with_job(Badge<HttpsProtocol>, ClientConnection& client, NonnullRefPtr<HTTP::HttpsJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
  69. {
  70. return adopt_own(*new HttpsDownload(client, move(job), move(output_stream)));
  71. }
  72. }