GeminiDownload.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <LibGemini/GeminiJob.h>
  27. #include <LibGemini/GeminiResponse.h>
  28. #include <ProtocolServer/GeminiDownload.h>
  29. namespace ProtocolServer {
  30. GeminiDownload::GeminiDownload(ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
  31. : Download(client, move(output_stream))
  32. , m_job(job)
  33. {
  34. m_job->on_finish = [this](bool success) {
  35. if (auto* response = m_job->response()) {
  36. set_downloaded_size(this->output_stream().size());
  37. if (!response->meta().is_empty()) {
  38. HashMap<String, String, CaseInsensitiveStringTraits> headers;
  39. headers.set("meta", response->meta());
  40. // Note: We're setting content-type to meta only on status==SUCCESS
  41. // we should perhaps have a better mechanism for this, since we
  42. // are already shoehorning the concept of "headers" here
  43. if (response->status() >= 20 && response->status() < 30) {
  44. headers.set("content-type", response->meta());
  45. }
  46. set_response_headers(headers);
  47. }
  48. }
  49. // signal 100% download progress so any listeners can react
  50. // appropriately
  51. did_progress(downloaded_size(), downloaded_size());
  52. did_finish(success);
  53. };
  54. m_job->on_progress = [this](Optional<u32> total, u32 current) {
  55. did_progress(total, current);
  56. };
  57. m_job->on_certificate_requested = [this](auto&) {
  58. did_request_certificates();
  59. };
  60. }
  61. void GeminiDownload::set_certificate(String certificate, String key)
  62. {
  63. m_job->set_certificate(move(certificate), move(key));
  64. }
  65. GeminiDownload::~GeminiDownload()
  66. {
  67. m_job->on_finish = nullptr;
  68. m_job->on_progress = nullptr;
  69. m_job->shutdown();
  70. }
  71. NonnullOwnPtr<GeminiDownload> GeminiDownload::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
  72. {
  73. return adopt_own(*new GeminiDownload(client, move(job), move(output_stream)));
  74. }
  75. }