HttpsJob.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <LibCore/EventLoop.h>
  27. #include <LibCore/Gzip.h>
  28. #include <LibHTTP/HttpResponse.h>
  29. #include <LibHTTP/HttpsJob.h>
  30. #include <LibTLS/TLSv12.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. //#define HTTPSJOB_DEBUG
  34. namespace HTTP {
  35. void HttpsJob::start()
  36. {
  37. ASSERT(!m_socket);
  38. m_socket = TLS::TLSv12::construct(this);
  39. m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
  40. m_socket->on_tls_connected = [this] {
  41. #ifdef HTTPSJOB_DEBUG
  42. dbg() << "HttpsJob: on_connected callback";
  43. #endif
  44. on_socket_connected();
  45. };
  46. m_socket->on_tls_error = [&](TLS::AlertDescription error) {
  47. if (error == TLS::AlertDescription::HandshakeFailure) {
  48. deferred_invoke([this](auto&) {
  49. return did_fail(Core::NetworkJob::Error::ProtocolFailed);
  50. });
  51. } else if (error == TLS::AlertDescription::DecryptError) {
  52. deferred_invoke([this](auto&) {
  53. return did_fail(Core::NetworkJob::Error::ConnectionFailed);
  54. });
  55. } else {
  56. deferred_invoke([this](auto&) {
  57. return did_fail(Core::NetworkJob::Error::TransmissionFailed);
  58. });
  59. }
  60. };
  61. m_socket->on_tls_finished = [&] {
  62. finish_up();
  63. };
  64. m_socket->on_tls_certificate_request = [this](auto&) {
  65. if (on_certificate_requested)
  66. on_certificate_requested(*this);
  67. };
  68. bool success = ((TLS::TLSv12&)*m_socket).connect(m_request.url().host(), m_request.url().port());
  69. if (!success) {
  70. deferred_invoke([this](auto&) {
  71. return did_fail(Core::NetworkJob::Error::ConnectionFailed);
  72. });
  73. }
  74. }
  75. void HttpsJob::shutdown()
  76. {
  77. if (!m_socket)
  78. return;
  79. m_socket->on_tls_ready_to_read = nullptr;
  80. m_socket->on_tls_connected = nullptr;
  81. remove_child(*m_socket);
  82. m_socket = nullptr;
  83. }
  84. void HttpsJob::set_certificate(String certificate, String private_key)
  85. {
  86. if (!m_socket->add_client_key(
  87. ByteBuffer::wrap(const_cast<char*>(certificate.characters()), certificate.length()),
  88. ByteBuffer::wrap(const_cast<char*>(private_key.characters()), private_key.length()))) {
  89. dbg() << "LibHTTP: Failed to set a client certificate";
  90. // FIXME: Do something about this failure
  91. ASSERT_NOT_REACHED();
  92. }
  93. }
  94. void HttpsJob::read_while_data_available(Function<IterationDecision()> read)
  95. {
  96. while (m_socket->can_read()) {
  97. if (read() == IterationDecision::Break)
  98. break;
  99. }
  100. }
  101. void HttpsJob::register_on_ready_to_read(Function<void()> callback)
  102. {
  103. m_socket->on_tls_ready_to_read = [callback = move(callback)](auto&) {
  104. callback();
  105. };
  106. }
  107. void HttpsJob::register_on_ready_to_write(Function<void()> callback)
  108. {
  109. m_socket->on_tls_ready_to_write = [callback = move(callback)](auto&) {
  110. callback();
  111. };
  112. }
  113. bool HttpsJob::can_read_line() const
  114. {
  115. return m_socket->can_read_line();
  116. }
  117. String HttpsJob::read_line(size_t size)
  118. {
  119. return m_socket->read_line(size);
  120. }
  121. ByteBuffer HttpsJob::receive(size_t size)
  122. {
  123. return m_socket->read(size);
  124. }
  125. bool HttpsJob::can_read() const
  126. {
  127. return m_socket->can_read();
  128. }
  129. bool HttpsJob::eof() const
  130. {
  131. return m_socket->eof();
  132. }
  133. bool HttpsJob::write(const ByteBuffer& data)
  134. {
  135. return m_socket->write(data);
  136. }
  137. }