Remove Cocoa CFURLConnection loading code
[WebKit-https.git] / Source / WebCore / platform / network / ResourceHandle.h
1 /*
2  * Copyright (C) 2004-2017 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 #pragma once
27
28 #include "AuthenticationClient.h"
29 #include "StoredCredentialsPolicy.h"
30 #include <wtf/MonotonicTime.h>
31 #include <wtf/RefCounted.h>
32 #include <wtf/RefPtr.h>
33 #include <wtf/text/AtomicString.h>
34
35 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
36 #include <wtf/RetainPtr.h>
37 #endif
38
39 #if USE(SOUP)
40 typedef struct _GTlsCertificate GTlsCertificate;
41 typedef struct _SoupSession SoupSession;
42 typedef struct _SoupRequest SoupRequest;
43 #endif
44
45 #if USE(CF)
46 typedef const struct __CFData * CFDataRef;
47 #endif
48
49 #if PLATFORM(COCOA)
50 OBJC_CLASS NSCachedURLResponse;
51 OBJC_CLASS NSData;
52 OBJC_CLASS NSDictionary;
53 OBJC_CLASS NSError;
54 OBJC_CLASS NSURLConnection;
55 OBJC_CLASS NSURLRequest;
56 #ifndef __OBJC__
57 typedef struct objc_object *id;
58 #endif
59 #endif
60
61 #if USE(CFURLCONNECTION)
62 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
63 typedef struct _CFURLConnection* CFURLConnectionRef;
64 typedef int CFHTTPCookieStorageAcceptPolicy;
65 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
66 #endif
67
68 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
69 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
70 #endif
71
72 namespace WTF {
73 class SchedulePair;
74 template<typename T> class MessageQueue;
75 }
76
77 namespace WebCore {
78
79 class AuthenticationChallenge;
80 class Credential;
81 class Frame;
82 class URL;
83 class NetworkingContext;
84 class ProtectionSpace;
85 class ResourceError;
86 class ResourceHandleClient;
87 class ResourceHandleInternal;
88 class NetworkLoadMetrics;
89 class ResourceRequest;
90 class ResourceResponse;
91 class SoupNetworkSession;
92 class SharedBuffer;
93 class Timer;
94
95 class ResourceHandle : public RefCounted<ResourceHandle>, public AuthenticationClient {
96 public:
97     WEBCORE_EXPORT static RefPtr<ResourceHandle> create(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff, bool shouldContentEncodingSniff);
98     WEBCORE_EXPORT static void loadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentialsPolicy, ResourceError&, ResourceResponse&, Vector<char>& data);
99
100 #if USE(SOUP)
101     static RefPtr<ResourceHandle> create(SoupNetworkSession&, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff, bool shouldContentEncodingSniff);
102 #endif
103
104     WEBCORE_EXPORT virtual ~ResourceHandle();
105
106 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
107     void willSendRequest(ResourceRequest&&, ResourceResponse&&, CompletionHandler<void(ResourceRequest&&)>&&);
108 #endif
109
110     void didReceiveResponse(ResourceResponse&&);
111
112     bool shouldUseCredentialStorage();
113     void didReceiveAuthenticationChallenge(const AuthenticationChallenge&);
114     void receivedCredential(const AuthenticationChallenge&, const Credential&) override;
115     void receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) override;
116     void receivedCancellation(const AuthenticationChallenge&) override;
117     void receivedRequestToPerformDefaultHandling(const AuthenticationChallenge&) override;
118     void receivedChallengeRejection(const AuthenticationChallenge&) override;
119
120 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
121     bool tryHandlePasswordBasedAuthentication(const AuthenticationChallenge&);
122 #endif
123
124 #if PLATFORM(COCOA) && USE(PROTECTION_SPACE_AUTH_CALLBACK)
125     bool canAuthenticateAgainstProtectionSpace(const ProtectionSpace&);
126 #endif
127
128 #if PLATFORM(COCOA)
129     WEBCORE_EXPORT NSURLConnection *connection() const;
130     id makeDelegate(bool, WTF::MessageQueue<WTF::Function<void()>>*);
131     id delegate();
132     void releaseDelegate();
133 #endif
134
135 #if PLATFORM(COCOA)
136 #if USE(CFURLCONNECTION)
137     static void getConnectionTimingData(CFURLConnectionRef, NetworkLoadMetrics&);
138 #else
139     static void getConnectionTimingData(NSURLConnection *, NetworkLoadMetrics&);
140 #endif
141 #endif
142
143 #if PLATFORM(COCOA)
144     void schedule(WTF::SchedulePair&);
145     void unschedule(WTF::SchedulePair&);
146 #endif
147
148 #if USE(CFURLCONNECTION)
149     CFURLStorageSessionRef storageSession() const;
150     CFURLConnectionRef connection() const;
151     WEBCORE_EXPORT RetainPtr<CFURLConnectionRef> releaseConnectionForDownload();
152     const ResourceRequest& currentRequest() const;
153     static void setHostAllowsAnyHTTPSCertificate(const String&);
154     static void setClientCertificate(const String& host, CFDataRef);
155 #endif
156
157 #if OS(WINDOWS) && USE(CURL)
158     static void setHostAllowsAnyHTTPSCertificate(const String&);
159     static void setClientCertificateInfo(const String&, const String&, const String&);
160 #endif
161
162 #if OS(WINDOWS) && USE(CURL) && USE(CF)
163     static void setClientCertificate(const String& host, CFDataRef);
164 #endif
165
166     bool shouldContentSniff() const;
167     static bool shouldContentSniffURL(const URL&);
168
169     bool shouldContentEncodingSniff() const;
170
171     WEBCORE_EXPORT static void forceContentSniffing();
172
173 #if USE(CURL) || USE(SOUP)
174     ResourceHandleInternal* getInternal() { return d.get(); }
175 #endif
176
177 #if USE(SOUP)
178     RefPtr<ResourceHandle> releaseForDownload(ResourceHandleClient*);
179     void continueDidReceiveAuthenticationChallenge(const Credential& credentialFromPersistentStorage);
180     void sendPendingRequest();
181     bool cancelledOrClientless();
182     void ensureReadBuffer();
183     size_t currentStreamPosition() const;
184     void didStartRequest();
185     MonotonicTime m_requestTime;
186 #endif
187
188     bool hasAuthenticationChallenge() const;
189     void clearAuthentication();
190     WEBCORE_EXPORT virtual void cancel();
191
192     // The client may be 0, in which case no callbacks will be made.
193     WEBCORE_EXPORT ResourceHandleClient* client() const;
194     WEBCORE_EXPORT void clearClient();
195
196     // Called in response to ResourceHandleClient::didReceiveResponseAsync().
197     WEBCORE_EXPORT virtual void continueDidReceiveResponse();
198
199 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
200     // Called in response to ResourceHandleClient::canAuthenticateAgainstProtectionSpaceAsync().
201     WEBCORE_EXPORT void continueCanAuthenticateAgainstProtectionSpace(bool);
202 #endif
203
204     // Called in response to ResourceHandleClient::willCacheResponseAsync().
205 #if USE(CFURLCONNECTION)
206     WEBCORE_EXPORT void continueWillCacheResponse(CFCachedURLResponseRef);
207 #endif
208 #if PLATFORM(COCOA)
209     WEBCORE_EXPORT void continueWillCacheResponse(NSCachedURLResponse *);
210 #endif
211
212     WEBCORE_EXPORT void setDefersLoading(bool);
213
214     WEBCORE_EXPORT ResourceRequest& firstRequest();
215     const String& lastHTTPMethod() const;
216
217     void failureTimerFired();
218
219     NetworkingContext* context() const;
220
221     using RefCounted<ResourceHandle>::ref;
222     using RefCounted<ResourceHandle>::deref;
223
224 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
225     WEBCORE_EXPORT static CFStringRef synchronousLoadRunLoopMode();
226 #endif
227
228     typedef Ref<ResourceHandle> (*BuiltinConstructor)(const ResourceRequest& request, ResourceHandleClient* client);
229     static void registerBuiltinConstructor(const AtomicString& protocol, BuiltinConstructor);
230
231     typedef void (*BuiltinSynchronousLoader)(NetworkingContext*, const ResourceRequest&, StoredCredentialsPolicy, ResourceError&, ResourceResponse&, Vector<char>& data);
232     static void registerBuiltinSynchronousLoader(const AtomicString& protocol, BuiltinSynchronousLoader);
233
234 protected:
235     ResourceHandle(NetworkingContext*, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff, bool shouldContentEncodingSniff);
236
237 private:
238     enum FailureType {
239         NoFailure,
240         BlockedFailure,
241         InvalidURLFailure
242     };
243
244 #if USE(SOUP)
245     ResourceHandle(SoupNetworkSession&, const ResourceRequest&, ResourceHandleClient*, bool defersLoading, bool shouldContentSniff, bool shouldContentEncodingSniff);
246 #endif
247
248     void platformSetDefersLoading(bool);
249
250     void platformContinueSynchronousDidReceiveResponse();
251
252     void scheduleFailure(FailureType);
253
254     bool start();
255     static void platformLoadResourceSynchronously(NetworkingContext*, const ResourceRequest&, StoredCredentialsPolicy, ResourceError&, ResourceResponse&, Vector<char>& data);
256
257     void refAuthenticationClient() override { ref(); }
258     void derefAuthenticationClient() override { deref(); }
259
260 #if PLATFORM(COCOA) || USE(CFURLCONNECTION)
261     enum class SchedulingBehavior { Asynchronous, Synchronous };
262 #endif
263
264 #if USE(CFURLCONNECTION)
265     void createCFURLConnection(bool shouldUseCredentialStorage, bool shouldContentSniff, bool shouldContentEncodingSniff, WTF::MessageQueue<WTF::Function<void()>>*, CFDictionaryRef clientProperties);
266 #endif
267
268 #if PLATFORM(MAC)
269     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, bool shouldContentEncodingSniff, SchedulingBehavior);
270 #endif
271
272 #if PLATFORM(IOS)
273     void createNSURLConnection(id delegate, bool shouldUseCredentialStorage, bool shouldContentSniff, bool shouldContentEncodingSniff, SchedulingBehavior, NSDictionary *connectionProperties);
274 #endif
275
276 #if PLATFORM(COCOA)
277     void applySniffingPoliciesAndStoragePartitionIfNeeded(NSURLRequest*&, bool shouldContentSniff, bool shouldContentEncodingSniff);
278 #endif
279
280 #if USE(SOUP)
281     void timeoutFired();
282 #endif
283
284     friend class ResourceHandleInternal;
285     std::unique_ptr<ResourceHandleInternal> d;
286 };
287
288 }