Skip to content

Commit d299df2

Browse files
ayeteadoetrflynn89
authored andcommitted
LibRequests: Create ReadStream abstraction for reading request data
Given the RequestServer created the request fd with socketpair() on Windows and pipe2() on Unix, this abstraction avoids inlined ifdef soup by hiding the details of how the AK::Stream and Core::Notifier are created.
1 parent 11ec7c9 commit d299df2

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

Libraries/LibRequests/Request.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99

1010
namespace Requests {
1111

12+
ErrorOr<NonnullOwnPtr<ReadStream>> ReadStream::create(int reader_fd)
13+
{
14+
#if defined(AK_OS_WINDOWS)
15+
auto local_socket = TRY(Core::LocalSocket::adopt_fd(reader_fd));
16+
auto notifier = local_socket->notifier();
17+
VERIFY(notifier);
18+
return adopt_own(*new ReadStream(move(local_socket), notifier.release_nonnull()));
19+
#else
20+
auto file = TRY(Core::File::adopt_fd(reader_fd, Core::File::OpenMode::Read));
21+
auto notifier = Core::Notifier::construct(reader_fd, Core::Notifier::Type::Read);
22+
return adopt_own(*new ReadStream(move(file), move(notifier)));
23+
#endif
24+
}
25+
1226
Request::Request(RequestClient& client, i32 request_id)
1327
: m_client(client)
1428
, m_request_id(request_id)
@@ -37,11 +51,11 @@ void Request::set_request_fd(Badge<Requests::RequestClient>, int fd)
3751
VERIFY(m_fd == -1);
3852
m_fd = fd;
3953

40-
auto notifier = Core::Notifier::construct(fd, Core::Notifier::Type::Read);
41-
auto stream = MUST(Core::File::adopt_fd(fd, Core::File::OpenMode::Read));
54+
auto read_stream = MUST(ReadStream::create(fd));
55+
auto notifier = read_stream->notifier();
4256
notifier->on_activation = move(m_internal_stream_data->read_notifier->on_activation);
43-
m_internal_stream_data->read_notifier = move(notifier);
44-
m_internal_stream_data->read_stream = move(stream);
57+
m_internal_stream_data->read_notifier = notifier;
58+
m_internal_stream_data->read_stream = move(read_stream);
4559
}
4660

4761
void Request::set_buffered_request_finished_callback(BufferedRequestFinished on_buffered_request_finished)
@@ -117,7 +131,7 @@ void Request::set_up_internal_stream_data(DataReceived on_data_available)
117131
m_internal_stream_data = make<InternalStreamData>();
118132
m_internal_stream_data->read_notifier = Core::Notifier::construct(fd(), Core::Notifier::Type::Read);
119133
if (fd() != -1)
120-
m_internal_stream_data->read_stream = MUST(Core::File::adopt_fd(fd(), Core::File::OpenMode::Read));
134+
m_internal_stream_data->read_stream = MUST(ReadStream::create(fd()));
121135

122136
auto user_on_finish = move(on_finish);
123137
on_finish = [this](auto total_size, auto const& timing_info, auto network_error) {

Libraries/LibRequests/Request.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ namespace Requests {
2121

2222
class RequestClient;
2323

24+
class ReadStream {
25+
public:
26+
static ErrorOr<NonnullOwnPtr<ReadStream>> create(int reader_fd);
27+
28+
NonnullRefPtr<Core::Notifier> const& notifier() const { return m_notifier; }
29+
30+
bool is_eof() const { return m_stream->is_eof(); }
31+
32+
ErrorOr<Bytes> read_some(Bytes bytes) { return m_stream->read_some(bytes); }
33+
34+
private:
35+
ReadStream(NonnullOwnPtr<Stream> stream, NonnullRefPtr<Core::Notifier> notifier)
36+
: m_stream(move(stream))
37+
, m_notifier(move(notifier))
38+
{
39+
}
40+
41+
NonnullOwnPtr<Stream> m_stream;
42+
NonnullRefPtr<Core::Notifier> m_notifier;
43+
};
44+
2445
class Request : public RefCounted<Request> {
2546
public:
2647
struct CertificateAndKey {
@@ -90,7 +111,7 @@ class Request : public RefCounted<Request> {
90111
struct InternalStreamData {
91112
InternalStreamData() { }
92113

93-
OwnPtr<Stream> read_stream;
114+
OwnPtr<ReadStream> read_stream;
94115
RefPtr<Core::Notifier> read_notifier;
95116
u32 total_size { 0 };
96117
Optional<NetworkError> network_error;

0 commit comments

Comments
 (0)