ClientConnection.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/Badge.h>
  27. #include <AK/SharedBuffer.h>
  28. #include <ProtocolServer/ClientConnection.h>
  29. #include <ProtocolServer/Download.h>
  30. #include <ProtocolServer/Protocol.h>
  31. #include <ProtocolServer/ProtocolClientEndpoint.h>
  32. namespace ProtocolServer {
  33. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  34. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  35. : IPC::ClientConnection<ProtocolClientEndpoint, ProtocolServerEndpoint>(*this, move(socket), client_id)
  36. {
  37. s_connections.set(client_id, *this);
  38. }
  39. ClientConnection::~ClientConnection()
  40. {
  41. }
  42. void ClientConnection::die()
  43. {
  44. s_connections.remove(client_id());
  45. if (s_connections.is_empty())
  46. Core::EventLoop::current().quit(0);
  47. }
  48. OwnPtr<Messages::ProtocolServer::IsSupportedProtocolResponse> ClientConnection::handle(const Messages::ProtocolServer::IsSupportedProtocol& message)
  49. {
  50. bool supported = Protocol::find_by_name(message.protocol().to_lowercase());
  51. return make<Messages::ProtocolServer::IsSupportedProtocolResponse>(supported);
  52. }
  53. OwnPtr<Messages::ProtocolServer::StartDownloadResponse> ClientConnection::handle(const Messages::ProtocolServer::StartDownload& message)
  54. {
  55. const auto& url = message.url();
  56. if (!url.is_valid()) {
  57. dbgln("StartDownload: Invalid URL requested: '{}'", url);
  58. return make<Messages::ProtocolServer::StartDownloadResponse>(-1, Optional<IPC::File> {});
  59. }
  60. auto* protocol = Protocol::find_by_name(url.protocol());
  61. if (!protocol) {
  62. dbgln("StartDownload: No protocol handler for URL: '{}'", url);
  63. return make<Messages::ProtocolServer::StartDownloadResponse>(-1, Optional<IPC::File> {});
  64. }
  65. auto download = protocol->start_download(*this, message.method(), url, message.request_headers().entries(), message.request_body());
  66. if (!download) {
  67. dbgln("StartDownload: Protocol handler failed to start download: '{}'", url);
  68. return make<Messages::ProtocolServer::StartDownloadResponse>(-1, Optional<IPC::File> {});
  69. }
  70. auto id = download->id();
  71. auto fd = download->download_fd();
  72. m_downloads.set(id, move(download));
  73. auto response = make<Messages::ProtocolServer::StartDownloadResponse>(id, fd);
  74. response->on_destruction = [fd] { close(fd); };
  75. return response;
  76. }
  77. OwnPtr<Messages::ProtocolServer::StopDownloadResponse> ClientConnection::handle(const Messages::ProtocolServer::StopDownload& message)
  78. {
  79. auto* download = const_cast<Download*>(m_downloads.get(message.download_id()).value_or(nullptr));
  80. bool success = false;
  81. if (download) {
  82. download->stop();
  83. m_downloads.remove(message.download_id());
  84. success = true;
  85. }
  86. return make<Messages::ProtocolServer::StopDownloadResponse>(success);
  87. }
  88. void ClientConnection::did_receive_headers(Badge<Download>, Download& download)
  89. {
  90. IPC::Dictionary response_headers;
  91. for (auto& it : download.response_headers())
  92. response_headers.add(it.key, it.value);
  93. post_message(Messages::ProtocolClient::HeadersBecameAvailable(download.id(), move(response_headers), download.status_code()));
  94. }
  95. void ClientConnection::did_finish_download(Badge<Download>, Download& download, bool success)
  96. {
  97. ASSERT(download.total_size().has_value());
  98. post_message(Messages::ProtocolClient::DownloadFinished(download.id(), success, download.total_size().value()));
  99. m_downloads.remove(download.id());
  100. }
  101. void ClientConnection::did_progress_download(Badge<Download>, Download& download)
  102. {
  103. post_message(Messages::ProtocolClient::DownloadProgress(download.id(), download.total_size(), download.downloaded_size()));
  104. }
  105. void ClientConnection::did_request_certificates(Badge<Download>, Download& download)
  106. {
  107. post_message(Messages::ProtocolClient::CertificateRequested(download.id()));
  108. }
  109. OwnPtr<Messages::ProtocolServer::GreetResponse> ClientConnection::handle(const Messages::ProtocolServer::Greet&)
  110. {
  111. return make<Messages::ProtocolServer::GreetResponse>(client_id());
  112. }
  113. OwnPtr<Messages::ProtocolServer::SetCertificateResponse> ClientConnection::handle(const Messages::ProtocolServer::SetCertificate& message)
  114. {
  115. auto* download = const_cast<Download*>(m_downloads.get(message.download_id()).value_or(nullptr));
  116. bool success = false;
  117. if (download) {
  118. download->set_certificate(message.certificate(), message.key());
  119. success = true;
  120. }
  121. return make<Messages::ProtocolServer::SetCertificateResponse>(success);
  122. }
  123. }