HttpsJob.cpp 4.5 KB

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