HttpJob.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibCore/TCPSocket.h>
  8. #include <LibHTTP/HttpJob.h>
  9. #include <LibHTTP/HttpResponse.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. namespace HTTP {
  13. void HttpJob::start(NonnullRefPtr<Core::Socket> socket)
  14. {
  15. VERIFY(!m_socket);
  16. m_socket = move(socket);
  17. m_socket->on_error = [this] {
  18. dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_error callback");
  19. deferred_invoke([this] {
  20. did_fail(Core::NetworkJob::Error::ConnectionFailed);
  21. });
  22. };
  23. if (m_socket->is_connected()) {
  24. dbgln("Reusing previous connection for {}", url());
  25. deferred_invoke([this] {
  26. dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
  27. on_socket_connected();
  28. });
  29. } else {
  30. dbgln("Creating new connection for {}", url());
  31. m_socket->on_connected = [this] {
  32. dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
  33. on_socket_connected();
  34. };
  35. bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default());
  36. if (!success) {
  37. deferred_invoke([this] {
  38. return did_fail(Core::NetworkJob::Error::ConnectionFailed);
  39. });
  40. }
  41. };
  42. }
  43. void HttpJob::shutdown(ShutdownMode mode)
  44. {
  45. if (!m_socket)
  46. return;
  47. if (mode == ShutdownMode::CloseSocket) {
  48. m_socket->close();
  49. } else {
  50. m_socket->on_ready_to_read = nullptr;
  51. m_socket->on_connected = nullptr;
  52. m_socket = nullptr;
  53. }
  54. }
  55. void HttpJob::register_on_ready_to_read(Function<void()> callback)
  56. {
  57. m_socket->on_ready_to_read = [callback = move(callback), this] {
  58. callback();
  59. // As IODevice so graciously buffers everything, there's a possible
  60. // scenario where it buffers the entire response, and we get stuck waiting
  61. // for select() in the notifier (which will never return).
  62. // So handle this case by exhausting the buffer here.
  63. if (m_socket->can_read_only_from_buffer() && m_state != State::Finished && !has_error()) {
  64. deferred_invoke([this] {
  65. if (m_socket && m_socket->on_ready_to_read)
  66. m_socket->on_ready_to_read();
  67. });
  68. }
  69. };
  70. }
  71. void HttpJob::register_on_ready_to_write(Function<void()> callback)
  72. {
  73. // There is no need to wait, the connection is already established
  74. callback();
  75. }
  76. bool HttpJob::can_read_line() const
  77. {
  78. return m_socket->can_read_line();
  79. }
  80. String HttpJob::read_line(size_t size)
  81. {
  82. return m_socket->read_line(size);
  83. }
  84. ByteBuffer HttpJob::receive(size_t size)
  85. {
  86. return m_socket->receive(size);
  87. }
  88. bool HttpJob::can_read() const
  89. {
  90. return m_socket->can_read();
  91. }
  92. bool HttpJob::eof() const
  93. {
  94. return m_socket->eof();
  95. }
  96. bool HttpJob::write(ReadonlyBytes bytes)
  97. {
  98. return m_socket->write(bytes);
  99. }
  100. }