Cancelling a WebInputStream

Hi there, I’m working on some code that makes a web call and have been testing the function for cancelling the connection. Since the cancel function goes into some fairly deep cross-platform JUCE code, I thought I would reach out for some help.

Here’s an example:

String ExampleClass::makeWebRequest()
{
    auto url = URL { "https://www.example.com" }.
    auto streamOptions = URL::InputStreamOptions{}.withConnectionTimeoutMs(2000);

    /*1*/
    {
        ScopedLock lock (m_streamLock);
        // The class has a WebInputStream object which is re-used
/*2*/   m_stream.reset(dynamic_cast<WebInputStream*>(url.createInputStream(streamOptions).release()));
    }

    /*3*/

    if (m_stream != nullptr)
            return m_stream->readEntireStreamAsString();
        else
            return {};
}

void ExampleClass::userCancelled()
{
    ScopedLock lock (m_streamLock);

    if (m_stream != nullptr)
        m_stream->cancel();
}

The web call will be made on a background thread and the cancel call will be made on the message thread. As such, there are locks in place to prevent concurrent access. So, the stream could be cancelled at many points in makeWebRequest, but not during the createInputStream call (position 2).

So, a couple of questions…
Is there a thread-safe (and resource-safe) way to cancel the web call during createInputStream (position 2)?
How can I check if the WebInputStream has been cancelled before and after createInputStream (positions 1 and 3)?