2 * Copyright (C) 2004, 2006, 2011, 2013 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.
26 #ifndef ResourceHandle_h
27 #define ResourceHandle_h
29 #include "AuthenticationClient.h"
30 #include "HTTPHeaderMap.h"
31 #include "ResourceHandleTypes.h"
32 #include "ResourceLoadPriority.h"
33 #include <wtf/OwnPtr.h>
34 #include <wtf/RefCounted.h>
36 #if PLATFORM(COCOA) || USE(CFNETWORK)
37 #include <wtf/RetainPtr.h>
41 #include "QuickLook.h"
45 typedef struct _GTlsCertificate GTlsCertificate;
46 typedef struct _SoupSession SoupSession;
47 typedef struct _SoupRequest SoupRequest;
51 typedef const struct __CFData * CFDataRef;
55 typedef unsigned long DWORD;
56 typedef unsigned long DWORD_PTR;
58 typedef LPVOID HINTERNET;
62 OBJC_CLASS NSCachedURLResponse;
64 OBJC_CLASS NSDictionary;
66 OBJC_CLASS NSURLConnection;
68 typedef struct objc_object *id;
73 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
74 typedef struct _CFURLConnection* CFURLConnectionRef;
75 typedef int CFHTTPCookieStorageAcceptPolicy;
76 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
79 #if PLATFORM(COCOA) || USE(CFNETWORK)
80 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
89 class AuthenticationChallenge;
93 class NetworkingContext;
94 class ProtectionSpace;
96 class ResourceHandleClient;
97 class ResourceHandleInternal;
98 class ResourceLoadTiming;
99 class ResourceRequest;
100 class ResourceResponse;
103 template <typename T> class Timer;
105 class ResourceHandle : public RefCounted<ResourceHandle>
106 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
107 , public AuthenticationClient
111 WEBCORE_EXPORT static PassRefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
112 WEBCORE_EXPORT static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
114 WEBCORE_EXPORT virtual ~ResourceHandle();
116 #if PLATFORM(COCOA) || USE(CFNETWORK)
117 void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
120 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
121 bool shouldUseCredentialStorage();
122 void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
123 virtual void receivedCredential(const AuthenticationChallenge&, const Credential&) override;
124 virtual void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) override;
125 virtual void receivedCancellation(const AuthenticationChallenge&) override;
126 virtual void receivedRequestToPerformDefaultHandling(const AuthenticationChallenge&) override;
127 virtual void receivedChallengeRejection(const AuthenticationChallenge&) override;
130 #if PLATFORM(COCOA) && USE(PROTECTION_SPACE_AUTH_CALLBACK)
131 bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
134 #if PLATFORM(COCOA) && !USE(CFNETWORK)
135 void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
136 WEBCORE_EXPORT NSURLConnection *connection() const;
137 id makeDelegate(bool);
139 void releaseDelegate();
142 #if PLATFORM(COCOA) && ENABLE(WEB_TIMING)
143 void setCollectsTimingData();
145 static void getConnectionTimingData(CFURLConnectionRef, ResourceLoadTiming&);
147 static void getConnectionTimingData(NSURLConnection *, ResourceLoadTiming&);
152 void schedule(WTF::SchedulePair&);
153 void unschedule(WTF::SchedulePair&);
157 CFURLStorageSessionRef storageSession() const;
158 CFURLConnectionRef connection() const;
159 WEBCORE_EXPORT RetainPtr<CFURLConnectionRef> releaseConnectionForDownload();
160 const ResourceRequest& currentRequest() const;
161 static void setHostAllowsAnyHTTPSCertificate(const String&);
162 static void setClientCertificate(const String& host, CFDataRef);
166 QuickLookHandle* quickLookHandle() { return m_quickLook.get(); }
167 void setQuickLookHandle(std::unique_ptr<QuickLookHandle> handle) { m_quickLook = WTF::move(handle); }
170 #if PLATFORM(WIN) && USE(CURL)
171 static void setHostAllowsAnyHTTPSCertificate(const String&);
172 static void setClientCertificateInfo(const String&, const String&, const String&);
175 #if PLATFORM(WIN) && USE(CURL) && USE(CF)
176 static void setClientCertificate(const String& host, CFDataRef);
179 bool shouldContentSniff() const;
180 static bool shouldContentSniffURL(const URL&);
182 WEBCORE_EXPORT static void forceContentSniffing();
185 void setSynchronousInternetHandle(HINTERNET);
186 void fileLoadTimer(Timer<ResourceHandle>*);
188 bool onRequestComplete();
189 static void CALLBACK internetStatusCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
192 #if USE(CURL) || USE(SOUP)
193 ResourceHandleInternal* getInternal() { return d.get(); }
197 void continueDidReceiveAuthenticationChallenge(const Credential& credentialFromPersistentStorage);
198 void sendPendingRequest();
199 bool cancelledOrClientless();
200 void ensureReadBuffer();
201 size_t currentStreamPosition() const;
202 void didStartRequest();
203 static void setHostAllowsAnyHTTPSCertificate(const String&);
204 static void setClientCertificate(const String& host, GTlsCertificate*);
205 static void setIgnoreSSLErrors(bool);
206 double m_requestTime;
209 // Used to work around the fact that you don't get any more NSURLConnection callbacks until you return from the one you're in.
210 static bool loadsBlocked();
212 bool hasAuthenticationChallenge() const;
213 void clearAuthentication();
214 WEBCORE_EXPORT virtual void cancel();
216 // The client may be 0, in which case no callbacks will be made.
217 ResourceHandleClient* client() const;
218 WEBCORE_EXPORT void setClient(ResourceHandleClient*);
220 // Called in response to ResourceHandleClient::willSendRequestAsync().
221 WEBCORE_EXPORT void continueWillSendRequest(const ResourceRequest&);
223 // Called in response to ResourceHandleClient::didReceiveResponseAsync().
224 WEBCORE_EXPORT virtual void continueDidReceiveResponse();
226 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
227 // Called in response to ResourceHandleClient::canAuthenticateAgainstProtectionSpaceAsync().
228 WEBCORE_EXPORT void continueCanAuthenticateAgainstProtectionSpace(bool);
231 // Called in response to ResourceHandleClient::willCacheResponseAsync().
233 WEBCORE_EXPORT void continueWillCacheResponse(CFCachedURLResponseRef);
235 #if PLATFORM(COCOA) && !USE(CFNETWORK)
236 WEBCORE_EXPORT void continueWillCacheResponse(NSCachedURLResponse *);
239 WEBCORE_EXPORT void setDefersLoading(bool);
241 WEBCORE_EXPORT ResourceRequest& firstRequest();
242 const String& lastHTTPMethod() const;
244 void failureTimerFired(Timer<ResourceHandle>&);
246 NetworkingContext* context() const;
248 using RefCounted<ResourceHandle>::ref;
249 using RefCounted<ResourceHandle>::deref;
251 #if PLATFORM(COCOA) || USE(CFNETWORK)
252 WEBCORE_EXPORT static CFStringRef synchronousLoadRunLoopMode();
255 #if PLATFORM(IOS) && USE(CFNETWORK)
256 static CFMutableDictionaryRef createSSLPropertiesFromNSURLRequest(const ResourceRequest&);
259 typedef PassRefPtr<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
260 static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
262 typedef void (*BuiltinSynchronousLoader)(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
263 static void registerBuiltinSynchronousLoader(const AtomicString& protocol, BuiltinSynchronousLoader);
266 ResourceHandle(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
275 void platformSetDefersLoading(bool);
277 void scheduleFailure(FailureType);
280 static void platformLoadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
282 virtual void refAuthenticationClient() override { ref(); }
283 virtual void derefAuthenticationClient() override { deref(); }
285 #if PLATFORM(COCOA) || USE(CFNETWORK)
286 enum class SchedulingBehavior { Asynchronous, Synchronous };
290 void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior, CFDictionaryRef clientProperties);
293 #if PLATFORM(MAC) && !USE(CFNETWORK)
294 void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior);
297 #if PLATFORM(IOS) && !USE(CFNETWORK)
298 void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior, NSDictionary *connectionProperties);
301 #if PLATFORM(COCOA) && ENABLE(WEB_TIMING)
302 static void getConnectionTimingData(NSDictionary *timingData, ResourceLoadTiming&);
305 friend class ResourceHandleInternal;
306 OwnPtr<ResourceHandleInternal> d;
309 std::unique_ptr<QuickLookHandle> m_quickLook;
315 #endif // ResourceHandle_h