2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <WebCore/ResourceError.h>
29 #include <WebCore/ResourceHandle.h>
30 #include <WebCore/ResourceHandleClient.h>
31 #include <WebCore/ResourceRequest.h>
32 #include <WebCore/ResourceResponse.h>
33 #include <WebCore/Timer.h>
34 #include <wtf/CompletionHandler.h>
36 // This class triggers asynchronous loads independent of the networking context staying alive (i.e., auditing pingbacks).
37 // The object just needs to live long enough to ensure the message was actually sent.
38 // As soon as any callback is received from the ResourceHandle, this class will cancel the load and delete itself.
40 class PingHandle final : private WebCore::ResourceHandleClient {
41 WTF_MAKE_NONCOPYABLE(PingHandle); WTF_MAKE_FAST_ALLOCATED;
43 PingHandle(WebCore::NetworkingContext* networkingContext, const WebCore::ResourceRequest& request, bool shouldUseCredentialStorage, bool shouldFollowRedirects, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&& completionHandler)
44 : m_currentRequest(request)
45 , m_timeoutTimer(*this, &PingHandle::timeoutTimerFired)
46 , m_shouldUseCredentialStorage(shouldUseCredentialStorage)
47 , m_shouldFollowRedirects(shouldFollowRedirects)
48 , m_completionHandler(WTFMove(completionHandler))
50 bool defersLoading = false;
51 bool shouldContentSniff = false;
52 bool shouldContentEncodingSniff = true;
53 m_handle = WebCore::ResourceHandle::create(networkingContext, request, this, defersLoading, shouldContentSniff, shouldContentEncodingSniff);
55 // If the server never responds, this object will hang around forever.
56 // Set a very generous timeout, just in case.
57 m_timeoutTimer.startOneShot(60000_s);
61 void willSendRequestAsync(WebCore::ResourceHandle*, WebCore::ResourceRequest&& request, WebCore::ResourceResponse&&, CompletionHandler<void(WebCore::ResourceRequest&&)>&& completionHandler) final
63 m_currentRequest = WTFMove(request);
64 if (m_shouldFollowRedirects) {
65 completionHandler(WebCore::ResourceRequest { m_currentRequest });
68 completionHandler({ });
69 pingLoadComplete(WebCore::ResourceError { String(), 0, m_currentRequest.url(), ASCIILiteral("Not allowed to follow redirects"), WebCore::ResourceError::Type::AccessControl });
71 void didReceiveResponseAsync(WebCore::ResourceHandle*, WebCore::ResourceResponse&& response, CompletionHandler<void()>&& completionHandler) final
74 pingLoadComplete({ }, response);
76 void didReceiveBuffer(WebCore::ResourceHandle*, Ref<WebCore::SharedBuffer>&&, int) final { pingLoadComplete(); }
77 void didFinishLoading(WebCore::ResourceHandle*) final { pingLoadComplete(); }
78 void didFail(WebCore::ResourceHandle*, const WebCore::ResourceError& error) final { pingLoadComplete(error); }
79 bool shouldUseCredentialStorage(WebCore::ResourceHandle*) final { return m_shouldUseCredentialStorage; }
80 void timeoutTimerFired() { pingLoadComplete(WebCore::ResourceError { String(), 0, m_currentRequest.url(), ASCIILiteral("Load timed out"), WebCore::ResourceError::Type::Timeout }); }
81 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
82 void canAuthenticateAgainstProtectionSpaceAsync(WebCore::ResourceHandle*, const WebCore::ProtectionSpace&, CompletionHandler<void(bool)>&& completionHandler)
84 completionHandler(false);
85 pingLoadComplete(WebCore::ResourceError { String { }, 0, m_currentRequest.url(), ASCIILiteral("Not allowed to authenticate"), WebCore::ResourceError::Type::AccessControl });
89 void pingLoadComplete(const WebCore::ResourceError& error = { }, const WebCore::ResourceResponse& response = { })
91 if (auto completionHandler = std::exchange(m_completionHandler, nullptr))
92 completionHandler(error, response);
98 ASSERT(!m_completionHandler);
100 ASSERT(m_handle->client() == this);
101 m_handle->clearClient();
106 RefPtr<WebCore::ResourceHandle> m_handle;
107 WebCore::ResourceRequest m_currentRequest;
108 WebCore::Timer m_timeoutTimer;
109 bool m_shouldUseCredentialStorage;
110 bool m_shouldFollowRedirects;
111 CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)> m_completionHandler;