HttpJob.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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()
  44. {
  45. if (!m_socket)
  46. return;
  47. m_socket->on_ready_to_read = nullptr;
  48. m_socket->on_connected = nullptr;
  49. m_socket = nullptr;
  50. }
  51. void HttpJob::register_on_ready_to_read(Function<void()> callback)
  52. {
  53. m_socket->on_ready_to_read = [callback = move(callback), this] {
  54. callback();
  55. // As IODevice so graciously buffers everything, there's a possible
  56. // scenario where it buffers the entire response, and we get stuck waiting
  57. // for select() in the notifier (which will never return).
  58. // So handle this case by exhausting the buffer here.
  59. if (m_socket->can_read_only_from_buffer() && m_state != State::Finished && !has_error()) {
  60. deferred_invoke([this] {
  61. if (m_socket && m_socket->on_ready_to_read)
  62. m_socket->on_ready_to_read();
  63. });
  64. }
  65. };
  66. }
  67. void HttpJob::register_on_ready_to_write(Function<void()> callback)
  68. {
  69. // There is no need to wait, the connection is already established
  70. callback();
  71. }
  72. bool HttpJob::can_read_line() const
  73. {
  74. return m_socket->can_read_line();
  75. }
  76. String HttpJob::read_line(size_t size)
  77. {
  78. return m_socket->read_line(size);
  79. }
  80. ByteBuffer HttpJob::receive(size_t size)
  81. {
  82. return m_socket->receive(size);
  83. }
  84. bool HttpJob::can_read() const
  85. {
  86. return m_socket->can_read();
  87. }
  88. bool HttpJob::eof() const
  89. {
  90. return m_socket->eof();
  91. }
  92. bool HttpJob::write(ReadonlyBytes bytes)
  93. {
  94. return m_socket->write(bytes);
  95. }
  96. }