Request.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/ByteString.h>
  10. #include <AK/Function.h>
  11. #include <AK/MemoryStream.h>
  12. #include <AK/RefCounted.h>
  13. #include <AK/WeakPtr.h>
  14. #include <LibCore/Notifier.h>
  15. #include <LibIPC/Forward.h>
  16. namespace Protocol {
  17. class RequestClient;
  18. class Request : public RefCounted<Request> {
  19. public:
  20. struct CertificateAndKey {
  21. ByteString certificate;
  22. ByteString key;
  23. };
  24. static NonnullRefPtr<Request> create_from_id(Badge<RequestClient>, RequestClient& client, i32 request_id)
  25. {
  26. return adopt_ref(*new Request(client, request_id));
  27. }
  28. int id() const { return m_request_id; }
  29. int fd() const { return m_fd; }
  30. bool stop();
  31. void stream_into(Stream&);
  32. bool should_buffer_all_input() const { return m_should_buffer_all_input; }
  33. /// Note: Will override `on_finish', and `on_headers_received', and expects `on_buffered_request_finish' to be set!
  34. void set_should_buffer_all_input(bool);
  35. /// Note: Must be set before `set_should_buffer_all_input(true)`.
  36. Function<void(bool success, u64 total_size, HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> response_code, ReadonlyBytes payload)> on_buffered_request_finish;
  37. Function<void(bool success, u64 total_size)> on_finish;
  38. Function<void(Optional<u64> total_size, u64 downloaded_size)> on_progress;
  39. Function<void(HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> response_code)> on_headers_received;
  40. Function<CertificateAndKey()> on_certificate_requested;
  41. void did_finish(Badge<RequestClient>, bool success, u64 total_size);
  42. void did_progress(Badge<RequestClient>, Optional<u64> total_size, u64 downloaded_size);
  43. void did_receive_headers(Badge<RequestClient>, HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> const& response_headers, Optional<u32> response_code);
  44. void did_request_certificates(Badge<RequestClient>);
  45. RefPtr<Core::Notifier>& write_notifier(Badge<RequestClient>) { return m_write_notifier; }
  46. void set_request_fd(Badge<RequestClient>, int fd) { m_fd = fd; }
  47. private:
  48. explicit Request(RequestClient&, i32 request_id);
  49. WeakPtr<RequestClient> m_client;
  50. int m_request_id { -1 };
  51. RefPtr<Core::Notifier> m_write_notifier;
  52. int m_fd { -1 };
  53. bool m_should_buffer_all_input { false };
  54. struct InternalBufferedData {
  55. AllocatingMemoryStream payload_stream;
  56. HashMap<ByteString, ByteString, CaseInsensitiveStringTraits> response_headers;
  57. Optional<u32> response_code;
  58. };
  59. struct InternalStreamData {
  60. InternalStreamData(NonnullOwnPtr<Stream> stream)
  61. : read_stream(move(stream))
  62. {
  63. }
  64. NonnullOwnPtr<Stream> read_stream;
  65. RefPtr<Core::Notifier> read_notifier;
  66. bool success;
  67. u32 total_size { 0 };
  68. bool request_done { false };
  69. Function<void()> on_finish {};
  70. bool user_finish_called { false };
  71. };
  72. OwnPtr<InternalBufferedData> m_internal_buffered_data;
  73. OwnPtr<InternalStreamData> m_internal_stream_data;
  74. };
  75. }