HttpJob.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <LibCore/Gzip.h>
  27. #include <LibCore/TCPSocket.h>
  28. #include <LibHTTP/HttpJob.h>
  29. #include <LibHTTP/HttpResponse.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. //#define HTTPJOB_DEBUG
  33. namespace HTTP {
  34. void HttpJob::start()
  35. {
  36. ASSERT(!m_socket);
  37. m_socket = Core::TCPSocket::construct(this);
  38. m_socket->on_connected = [this] {
  39. #ifdef CHTTPJOB_DEBUG
  40. dbg() << "HttpJob: on_connected callback";
  41. #endif
  42. on_socket_connected();
  43. };
  44. bool success = m_socket->connect(m_request.url().host(), m_request.url().port());
  45. if (!success) {
  46. deferred_invoke([this](auto&) {
  47. return did_fail(Core::NetworkJob::Error::ConnectionFailed);
  48. });
  49. }
  50. }
  51. void HttpJob::shutdown()
  52. {
  53. if (!m_socket)
  54. return;
  55. m_socket->on_ready_to_read = nullptr;
  56. m_socket->on_connected = nullptr;
  57. remove_child(*m_socket);
  58. m_socket = nullptr;
  59. }
  60. void HttpJob::register_on_ready_to_read(Function<void()> callback)
  61. {
  62. m_socket->on_ready_to_read = move(callback);
  63. }
  64. void HttpJob::register_on_ready_to_write(Function<void()> callback)
  65. {
  66. // There is no need to wait, the connection is already established
  67. callback();
  68. }
  69. bool HttpJob::can_read_line() const
  70. {
  71. return m_socket->can_read_line();
  72. }
  73. String HttpJob::read_line(size_t size)
  74. {
  75. return m_socket->read_line(size);
  76. }
  77. ByteBuffer HttpJob::receive(size_t size)
  78. {
  79. return m_socket->receive(size);
  80. }
  81. bool HttpJob::can_read() const
  82. {
  83. return m_socket->can_read();
  84. }
  85. bool HttpJob::eof() const
  86. {
  87. return m_socket->eof();
  88. }
  89. bool HttpJob::write(const ByteBuffer& data)
  90. {
  91. return m_socket->write(data);
  92. }
  93. }