Repository: LadybirdBrowser/ladybird
File: Libraries/LibRequests/Request.cpp (C++)

Code before:
{
}

bool Request::stop()
{
    on_headers_received = nullptr;
    on_finish = nullptr;
    on_certificate_requested = nullptr;

    m_internal_buffered_data = nullptr;
    m_internal_stream_data = nullptr;
    m_mode = Mode::Unknown;

    return m_client->stop_request({}, *this);
}

void Request::set_request_fd(Badge<Requests::RequestClient>, int fd)
{
    // If the request was stopped while this IPC was in-flight, just bail.
    if (!m_internal_stream_data)
        return;

    VERIFY(m_fd == -1);
    m_fd = fd;

    auto read_stream = MUST(ReadStream::try_create(fd));
    auto notifier = read_stream->notifier();
    notifier->on_activation = move(m_internal_stream_data->read_notifier->on_activation);
    m_internal_stream_data->read_notifier = move(notifier);
    m_internal_stream_data->read_stream = move(read_stream);
}

void Request::set_buffered_request_finished_callback(BufferedRequestFinished on_buffered_request_finished)
{
    VERIFY(m_mode == Mode::Unknown);
    m_mode = Mode::Buffered;

    m_internal_buffered_data = make<InternalBufferedData>();

    on_headers_received = [this](auto& headers, auto response_code, auto const& reason_phrase) {
        m_internal_buffered_data->response_headers = headers;
        m_internal_buffered_data->response_code = move(response_code);
        m_internal_buffered_data->reason_phrase = reason_phrase;
    };

    on_finish = [this, on_buffered_request_finished = move(on_buffered_request_finished)](auto total_size, auto& timing_info, auto network_error) {
        auto output_buffer = ByteBuffer::create_uninitialized(m_internal_buffered_data->payload_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
        m_internal_buffered_data->payload_stream.read_until_filled(output_buffer).release_value_but_fixme_should_propagate_errors();

        on_buffered_request_finished(
            total_size,

Review comment (suggestion):
We previously had a `NonnullRefPtr<Notifier>` here by way of `Notifier::construct`. We now have a `RefPtr` that we're assuming is nonnull. 

If we're going to do that, let's have `ReadStream` hold a `NonnullRefPtr` to begin with. The Windows factory would then look like:

```c++
ErrorOr<NonnullOwnPtr<ReadStream>> ReadStream::try_create(int const reader_fd)
{
    auto local_socket = TRY(Core::LocalSocket::adopt_fd(reader_fd));

    auto notifier = local_socket->notifier();
    VERIFY(notifier);

    return adopt_own(*new ReadStream(move(local_socket), notifier.release_nonnull()));
}
```

Code after:
}

bool Request::stop()
{
    on_headers_received = nullptr;
    on_finish = nullptr;
    on_certificate_requested = nullptr;

    m_internal_buffered_data = nullptr;
    m_internal_stream_data = nullptr;
    m_mode = Mode::Unknown;

    return m_client->stop_request({}, *this);
}

void Request::set_request_fd(Badge<Requests::RequestClient>, int fd)
{
    // If the request was stopped while this IPC was in-flight, just bail.
    if (!m_internal_stream_data)
        return;

    VERIFY(m_fd == -1);
    m_fd = fd;

    auto read_stream = MUST(ReadStream::create(fd));
    auto notifier = read_stream->notifier();
    notifier->on_activation = move(m_internal_stream_data->read_notifier->on_activation);
    m_internal_stream_data->read_notifier = notifier;
    m_internal_stream_data->read_stream = move(read_stream);
}

void Request::set_buffered_request_finished_callback(BufferedRequestFinished on_buffered_request_finished)
{
    VERIFY(m_mode == Mode::Unknown);
    m_mode = Mode::Buffered;

    m_internal_buffered_data = make<InternalBufferedData>();

    on_headers_received = [this](auto& headers, auto response_code, auto const& reason_phrase) {
        m_internal_buffered_data->response_headers = headers;
        m_internal_buffered_data->response_code = move(response_code);
        m_internal_buffered_data->reason_phrase = reason_phrase;
    };

    on_finish = [this, on_buffered_request_finished = move(on_buffered_request_finished)](auto total_size, auto& timing_info, auto network_error) {
        auto output_buffer = ByteBuffer::create_uninitialized(m_internal_buffered_data->payload_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
        m_internal_buffered_data->payload_stream.read_until_filled(output_buffer).release_value_but_fixme_should_propagate_errors();

        on_buffered_request_finished(
            total_size,
            timing_info,