7c4de7a3808202d5c5c68470b7a926becd991c82
[WebKit-https.git] / Source / WebCore / platform / network / ResourceHandle.h
1 /*
2  * Copyright (C) 2004, 2006, 2011, 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #ifndef ResourceHandle_h
27 #define ResourceHandle_h
28
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>
35
36 #if PLATFORM(COCOA) || USE(CFNETWORK)
37 #include <wtf/RetainPtr.h>
38 #endif
39
40 #if USE(QUICK_LOOK)
41 #include "QuickLook.h"
42 #endif
43
44 #if USE(SOUP)
45 typedef struct _GTlsCertificate GTlsCertificate;
46 typedef struct _SoupSession SoupSession;
47 typedef struct _SoupRequest SoupRequest;
48 #endif
49
50 #if USE(CF)
51 typedef const struct __CFData * CFDataRef;
52 #endif
53
54 #if USE(WININET)
55 typedef unsigned long DWORD;
56 typedef unsigned long DWORD_PTR;
57 typedef void* LPVOID;
58 typedef LPVOID HINTERNET;
59 #endif
60
61 #if PLATFORM(COCOA)
62 OBJC_CLASS NSCachedURLResponse;
63 OBJC_CLASS NSData;
64 OBJC_CLASS NSDictionary;
65 OBJC_CLASS NSError;
66 OBJC_CLASS NSURLConnection;
67 #ifndef __OBJC__
68 typedef struct objc_object *id;
69 #endif
70 #endif
71
72 #if USE(CFNETWORK)
73 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
74 typedef struct _CFURLConnection* CFURLConnectionRef;
75 typedef int CFHTTPCookieStorageAcceptPolicy;
76 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
77 #endif
78
79 #if PLATFORM(COCOA) || USE(CFNETWORK)
80 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
81 #endif
82
83 namespace WTF {
84 class SchedulePair;
85 }
86
87 namespace WebCore {
88
89 class AuthenticationChallenge;
90 class Credential;
91 class Frame;
92 class URL;
93 class NetworkingContext;
94 class ProtectionSpace;
95 class ResourceError;
96 class ResourceHandleClient;
97 class ResourceHandleInternal;
98 class ResourceLoadTiming;
99 class ResourceRequest;
100 class ResourceResponse;
101 class SharedBuffer;
102
103 template <typename T> class Timer;
104
105 class ResourceHandle : public RefCounted<ResourceHandle>
106 #if PLATFORM(COCOA) || USE(CFNETWORK) || USE(CURL) || USE(SOUP)
107     , public AuthenticationClient
108 #endif
109     {
110 public:
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);
113
114     WEBCORE_EXPORT virtual ~ResourceHandle();
115
116 #if PLATFORM(COCOA) || USE(CFNETWORK)
117     void willSendRequest(ResourceRequest&, const ResourceResponse& redirectResponse);
118 #endif
119
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;
128 #endif
129
130 #if PLATFORM(COCOA) && USE(PROTECTION_SPACE_AUTH_CALLBACK)
131     bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
132 #endif
133
134 #if PLATFORM(COCOA) && !USE(CFNETWORK)
135     void didCancelAuthenticationChallenge(const AuthenticationChallenge&);
136     WEBCORE_EXPORT NSURLConnection *connection() const;
137     id makeDelegate(bool);
138     id delegate();
139     void releaseDelegate();
140 #endif
141         
142 #if PLATFORM(COCOA) && ENABLE(WEB_TIMING)
143 #if USE(CFNETWORK)
144     void setCollectsTimingData();
145     static void getConnectionTimingData(CFURLConnectionRef, ResourceLoadTiming&);
146 #else
147     static void getConnectionTimingData(NSURLConnection *, ResourceLoadTiming&);
148 #endif
149 #endif
150         
151 #if PLATFORM(COCOA)
152     void schedule(WTF::SchedulePair&);
153     void unschedule(WTF::SchedulePair&);
154 #endif
155
156 #if USE(CFNETWORK)
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);
163 #endif
164
165 #if USE(QUICK_LOOK)
166     QuickLookHandle* quickLookHandle() { return m_quickLook.get(); }
167     void setQuickLookHandle(std::unique_ptr<QuickLookHandle> handle) { m_quickLook = WTF::move(handle); }
168 #endif
169
170 #if PLATFORM(WIN) && USE(CURL)
171     static void setHostAllowsAnyHTTPSCertificate(const String&);
172     static void setClientCertificateInfo(const String&, const String&, const String&);
173 #endif
174
175 #if PLATFORM(WIN) && USE(CURL) && USE(CF)
176     static void setClientCertificate(const String& host, CFDataRef);
177 #endif
178
179     bool shouldContentSniff() const;
180     static bool shouldContentSniffURL(const URL&);
181
182     WEBCORE_EXPORT static void forceContentSniffing();
183
184 #if USE(WININET)
185     void setSynchronousInternetHandle(HINTERNET);
186     void fileLoadTimer(Timer<ResourceHandle>*);
187     void onRedirect();
188     bool onRequestComplete();
189     static void CALLBACK internetStatusCallback(HINTERNET, DWORD_PTR, DWORD, LPVOID, DWORD);
190 #endif
191
192 #if USE(CURL) || USE(SOUP)
193     ResourceHandleInternal* getInternal() { return d.get(); }
194 #endif
195
196 #if USE(SOUP)
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;
207 #endif
208
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();    
211
212     bool hasAuthenticationChallenge() const;
213     void clearAuthentication();
214     WEBCORE_EXPORT virtual void cancel();
215
216     // The client may be 0, in which case no callbacks will be made.
217     ResourceHandleClient* client() const;
218     WEBCORE_EXPORT void setClient(ResourceHandleClient*);
219
220     // Called in response to ResourceHandleClient::willSendRequestAsync().
221     WEBCORE_EXPORT void continueWillSendRequest(const ResourceRequest&);
222
223     // Called in response to ResourceHandleClient::didReceiveResponseAsync().
224     WEBCORE_EXPORT virtual void continueDidReceiveResponse();
225
226 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
227     // Called in response to ResourceHandleClient::canAuthenticateAgainstProtectionSpaceAsync().
228     WEBCORE_EXPORT void continueCanAuthenticateAgainstProtectionSpace(bool);
229 #endif
230
231     // Called in response to ResourceHandleClient::willCacheResponseAsync().
232 #if USE(CFNETWORK)
233     WEBCORE_EXPORT void continueWillCacheResponse(CFCachedURLResponseRef);
234 #endif
235 #if PLATFORM(COCOA) && !USE(CFNETWORK)
236     WEBCORE_EXPORT void continueWillCacheResponse(NSCachedURLResponse *);
237 #endif
238
239     WEBCORE_EXPORT void setDefersLoading(bool);
240
241     WEBCORE_EXPORT ResourceRequest& firstRequest();
242     const String& lastHTTPMethod() const;
243
244     void failureTimerFired(Timer<ResourceHandle>&);
245
246     NetworkingContext* context() const;
247
248     using RefCounted<ResourceHandle>::ref;
249     using RefCounted<ResourceHandle>::deref;
250
251 #if PLATFORM(COCOA) || USE(CFNETWORK)
252     WEBCORE_EXPORT static CFStringRef synchronousLoadRunLoopMode();
253 #endif
254
255 #if PLATFORM(IOS) && USE(CFNETWORK)
256     static CFMutableDictionaryRef createSSLPropertiesFromNSURLRequest(const ResourceRequest&);
257 #endif
258
259     typedef PassRefPtr<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
260     static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
261
262     typedef void (*BuiltinSynchronousLoader)(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
263     static void registerBuiltinSynchronousLoader(const AtomicString& protocol, BuiltinSynchronousLoader);
264
265 protected:
266     ResourceHandle(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff);
267
268 private:
269     enum FailureType {
270         NoFailure,
271         BlockedFailure,
272         InvalidURLFailure
273     };
274
275     void platformSetDefersLoading(bool);
276
277     void scheduleFailure(FailureType);
278
279     bool start();
280     static void platformLoadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentials, ResourceError&, ResourceResponse&, Vector<char>& data);
281
282     virtual void refAuthenticationClient() override { ref(); }
283     virtual void derefAuthenticationClient() override { deref(); }
284
285 #if PLATFORM(COCOA) || USE(CFNETWORK)
286     enum class SchedulingBehavior { Asynchronous, Synchronous };
287 #endif
288
289 #if USE(CFNETWORK)
290     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior, CFDictionaryRef clientProperties);
291 #endif
292
293 #if PLATFORM(MAC) && !USE(CFNETWORK)
294     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior);
295 #endif
296
297 #if PLATFORM(IOS) && !USE(CFNETWORK)
298     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, SchedulingBehavior, NSDictionary *connectionProperties);
299 #endif
300
301 #if PLATFORM(COCOA) && ENABLE(WEB_TIMING)
302 static void getConnectionTimingData(NSDictionary *timingData, ResourceLoadTiming&);
303 #endif
304
305     friend class ResourceHandleInternal;
306     OwnPtr<ResourceHandleInternal> d;
307
308 #if USE(QUICK_LOOK)
309     std::unique_ptr<QuickLookHandle> m_quickLook;
310 #endif
311 };
312
313 }
314
315 #endif // ResourceHandle_h