Enable HTTP and HTTPS proxies on iOS and make it a property of the NSURLSession
[WebKit-https.git] / Source / WebCore / PAL / pal / spi / cf / CFNetworkSPI.h
1 /*
2  * Copyright (C) 2014-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. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma clang system_header
27
28 #pragma once
29
30 #include <CFNetwork/CFNetwork.h>
31 #include <pal/spi/cf/CFNetworkConnectionCacheSPI.h>
32
33 #if PLATFORM(WIN) || USE(APPLE_INTERNAL_SDK)
34
35 #include <CFNetwork/CFHTTPCookiesPriv.h>
36 #include <CFNetwork/CFHTTPStream.h>
37 #include <CFNetwork/CFProxySupportPriv.h>
38 #include <CFNetwork/CFURLCachePriv.h>
39 #include <CFNetwork/CFURLConnectionPriv.h>
40 #include <CFNetwork/CFURLCredentialStorage.h>
41 #include <CFNetwork/CFURLProtocolPriv.h>
42 #include <CFNetwork/CFURLRequestPriv.h>
43 #include <CFNetwork/CFURLResponsePriv.h>
44 #include <CFNetwork/CFURLStorageSession.h>
45
46 #if PLATFORM(WIN)
47
48 WTF_EXTERN_C_BEGIN
49
50 CFN_EXPORT CFStringRef _CFNetworkErrorGetLocalizedDescription(CFIndex);
51
52 extern const CFStringRef _kCFWindowsSSLLocalCert;
53 extern const CFStringRef _kCFStreamPropertyWindowsSSLCertInfo;
54 extern const CFStringRef _kCFWindowsSSLPeerCert;
55
56 WTF_EXTERN_C_END
57
58 #endif
59
60 // FIXME: Remove the defined(__OBJC__)-guard once we fix <rdar://problem/19033610>.
61 #if defined(__OBJC__) && PLATFORM(COCOA)
62 #import <CFNetwork/CFNSURLConnection.h>
63 #endif
64
65 #else // !PLATFORM(WIN) && !USE(APPLE_INTERNAL_SDK)
66
67 typedef CF_ENUM(int64_t, _TimingDataOptions)
68 {
69     _TimingDataOptionsEnableW3CNavigationTiming = (1 << 0)
70 };
71
72 enum CFURLCacheStoragePolicy {
73     kCFURLCacheStorageAllowed = 0,
74     kCFURLCacheStorageAllowedInMemoryOnly = 1,
75     kCFURLCacheStorageNotAllowed = 2
76 };
77 typedef enum CFURLCacheStoragePolicy CFURLCacheStoragePolicy;
78
79 typedef const struct _CFCachedURLResponse* CFCachedURLResponseRef;
80 typedef const struct _CFURLCache* CFURLCacheRef;
81 typedef const struct _CFURLCredential* CFURLCredentialRef;
82 typedef const struct _CFURLRequest* CFURLRequestRef;
83 typedef const struct __CFURLStorageSession* CFURLStorageSessionRef;
84 typedef const struct __CFData* CFDataRef;
85 typedef const struct OpaqueCFHTTPCookie* CFHTTPCookieRef;
86 typedef struct _CFURLConnection* CFURLConnectionRef;
87 typedef struct _CFURLCredentialStorage* CFURLCredentialStorageRef;
88 typedef struct _CFURLProtectionSpace* CFURLProtectionSpaceRef;
89 typedef struct _CFURLRequest* CFMutableURLRequestRef;
90 typedef struct _CFURLResponse* CFURLResponseRef;
91 typedef struct OpaqueCFHTTPCookieStorage* CFHTTPCookieStorageRef;
92 typedef CFIndex CFURLRequestPriority;
93 typedef int CFHTTPCookieStorageAcceptPolicy;
94
95 #ifdef __BLOCKS__
96 typedef void (^CFCachedURLResponseCallBackBlock)(CFCachedURLResponseRef);
97 #endif
98
99 #if defined(__OBJC__)
100
101 @interface NSURLCache ()
102 - (CFURLCacheRef)_CFURLCache;
103 @end
104
105 @interface NSCachedURLResponse ()
106 - (id)_initWithCFCachedURLResponse:(CFCachedURLResponseRef)cachedResponse;
107 - (CFCachedURLResponseRef)_CFCachedURLResponse;
108 @end
109
110 @interface NSHTTPCookie ()
111 - (CFHTTPCookieRef)_CFHTTPCookie;
112 + (CFArrayRef __nullable)_ns2cfCookies:(NSArray * __nullable)nsCookies CF_RETURNS_RETAINED;
113 - (CFHTTPCookieRef __nullable)_GetInternalCFHTTPCookie;
114 @property (nullable, readonly, copy) NSString *_storagePartition;
115 @end
116
117 @interface NSHTTPCookieStorage ()
118 - (id)_initWithIdentifier:(NSString *)identifier private:(bool)isPrivate;
119 - (void)_getCookiesForURL:(NSURL *)url mainDocumentURL:(NSURL *)mainDocumentURL partition:(NSString *)partition completionHandler:(void (^)(NSArray *))completionHandler;
120 - (void)_getCookiesForURL:(NSURL *)url mainDocumentURL:(NSURL *)mainDocumentURL partition:(NSString *)partition policyProperties:(NSDictionary*)props completionHandler:(void (^)(NSArray *))completionHandler;
121 - (void)_setCookies:(NSArray *)cookies forURL:(NSURL *)URL mainDocumentURL:(NSURL *)mainDocumentURL policyProperties:(NSDictionary*) props;
122 - (void)removeCookiesSinceDate:(NSDate *)date;
123 - (id)_initWithCFHTTPCookieStorage:(CFHTTPCookieStorageRef)cfStorage;
124 - (CFHTTPCookieStorageRef)_cookieStorage;
125 - (void)_saveCookies;
126 #if HAVE(FOUNDATION_WITH_SAVE_COOKIES_WITH_COMPLETION_HANDLER)
127 - (void)_saveCookies:(dispatch_block_t) completionHandler;
128 #endif
129 @end
130
131 @interface NSURLConnection ()
132 - (id)_initWithRequest:(NSURLRequest *)request delegate:(id)delegate usesCache:(BOOL)usesCacheFlag maxContentLength:(long long)maxContentLength startImmediately:(BOOL)startImmediately connectionProperties:(NSDictionary *)connectionProperties;
133 @end
134
135 @interface NSMutableURLRequest ()
136 - (void)setContentDispositionEncodingFallbackArray:(NSArray *)theEncodingFallbackArray;
137 - (void)setBoundInterfaceIdentifier:(NSString *)identifier;
138 - (void)_setPreventHSTSStorage:(BOOL)preventHSTSStorage;
139 - (void)_setIgnoreHSTS:(BOOL)ignoreHSTS;
140 @end
141
142 @interface NSURLProtocol ()
143 + (Class)_protocolClassForRequest:(NSURLRequest *)request;
144 @end
145
146 @interface NSURLRequest ()
147 + (NSArray *)allowsSpecificHTTPSCertificateForHost:(NSString *)host;
148 + (void)setAllowsSpecificHTTPSCertificate:(NSArray *)allow forHost:(NSString *)host;
149 + (void)setDefaultTimeoutInterval:(NSTimeInterval)seconds;
150 - (NSArray *)contentDispositionEncodingFallbackArray;
151 - (CFMutableURLRequestRef)_CFURLRequest;
152 - (id)_initWithCFURLRequest:(CFURLRequestRef)request;
153 - (id)_propertyForKey:(NSString *)key;
154 - (void)_setProperty:(id)value forKey:(NSString *)key;
155 - (BOOL)_schemeWasUpgradedDueToDynamicHSTS;
156 - (BOOL)_preventHSTSStorage;
157 - (BOOL)_ignoreHSTS;
158 @end
159
160 @interface NSURLResponse ()
161 + (NSURLResponse *)_responseWithCFURLResponse:(CFURLResponseRef)response;
162 - (CFURLResponseRef)_CFURLResponse;
163 - (NSDate *)_lastModifiedDate;
164 @end
165
166 #if PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MAX_ALLOWED >= 60000
167 typedef NS_ENUM(NSInteger, NSURLSessionCompanionProxyPreference) {
168     NSURLSessionCompanionProxyPreferenceDefault = 0,
169     NSURLSessionCompanionProxyPreferencePreferCompanion,
170     NSURLSessionCompanionProxyPreferencePreferDirectToCloud,
171 };
172 #endif
173
174 @interface NSURLSessionConfiguration ()
175 @property (assign) _TimingDataOptions _timingDataOptions;
176 @property (copy) NSData *_sourceApplicationAuditTokenData;
177 @property (nullable, copy) NSString *_sourceApplicationBundleIdentifier;
178 @property (nullable, copy) NSString *_sourceApplicationSecondaryIdentifier;
179 @property BOOL _shouldSkipPreferredClientCertificateLookup NS_AVAILABLE(10_10, 8_0);
180 #if PLATFORM(IOS_FAMILY)
181 @property (nullable, copy) NSString *_CTDataConnectionServiceType;
182 #endif
183 #if HAVE(CFNETWORK_WITH_AUTO_ADDED_HTTP_HEADER_SUPPRESSION_SUPPORT)
184 @property (nullable, copy) NSSet *_suppressedAutoAddedHTTPHeaders;
185 #endif
186 #if PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MAX_ALLOWED >= 60000
187 @property NSURLSessionCompanionProxyPreference _companionProxyPreference;
188 #endif
189 @end
190
191 @interface NSURLSessionTask ()
192 - (NSDictionary *)_timingData;
193 @property (readwrite, copy) NSString *_pathToDownloadTaskFile;
194 @property (copy) NSString *_storagePartitionIdentifier;
195 #if HAVE(FOUNDATION_WITH_SAME_SITE_COOKIE_SUPPORT)
196 @property (nullable, readwrite, retain) NSURL *_siteForCookies;
197 @property (readwrite) BOOL _isTopLevelNavigation;
198 #endif
199 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS_FAMILY) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
200 @property (nonatomic, assign) BOOL _preconnect;
201 #endif
202 @end
203
204 #if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS_FAMILY) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000)
205 @interface NSURLSessionTaskTransactionMetrics ()
206 @property (copy, readonly) NSString* _remoteAddressAndPort;
207 @property (copy, readonly) NSUUID* _connectionIdentifier;
208 @property (assign, readonly) NSInteger _requestHeaderBytesSent;
209 @property (assign, readonly) NSInteger _responseHeaderBytesReceived;
210 @property (assign, readonly) int64_t _responseBodyBytesReceived;
211 @property (assign, readonly) int64_t _responseBodyBytesDecoded;
212 @end
213 #endif
214
215 extern NSString * const NSURLAuthenticationMethodOAuth;
216
217 #endif // defined(__OBJC__)
218
219 #endif // !PLATFORM(WIN) && !USE(APPLE_INTERNAL_SDK)
220
221 WTF_EXTERN_C_BEGIN
222
223 #if !PLATFORM(WIN)
224
225 CFURLStorageSessionRef _CFURLStorageSessionCreate(CFAllocatorRef, CFStringRef, CFDictionaryRef);
226 CFURLCacheRef _CFURLStorageSessionCopyCache(CFAllocatorRef, CFURLStorageSessionRef);
227
228 void CFURLRequestSetShouldStartSynchronously(CFURLRequestRef, Boolean);
229
230 CFURLCacheRef CFURLCacheCopySharedURLCache();
231 void CFURLCacheSetMemoryCapacity(CFURLCacheRef, CFIndex memoryCapacity);
232 CFIndex CFURLCacheMemoryCapacity(CFURLCacheRef);
233 void CFURLCacheSetDiskCapacity(CFURLCacheRef, CFIndex);
234 CFCachedURLResponseRef CFURLCacheCopyResponseForRequest(CFURLCacheRef, CFURLRequestRef);
235
236 void CFHTTPCookieStorageDeleteAllCookies(CFHTTPCookieStorageRef);
237 void _CFHTTPCookieStorageFlushCookieStores();
238
239 #if PLATFORM(COCOA)
240 CFDataRef _CFCachedURLResponseGetMemMappedData(CFCachedURLResponseRef);
241 #endif
242
243 #if PLATFORM(COCOA) && defined(__BLOCKS__)
244 void _CFCachedURLResponseSetBecameFileBackedCallBackBlock(CFCachedURLResponseRef, CFCachedURLResponseCallBackBlock, dispatch_queue_t);
245 #endif
246
247 extern CFStringRef const kCFHTTPCookieLocalFileDomain;
248 extern const CFStringRef kCFHTTPVersion1_1;
249 extern const CFStringRef kCFURLRequestAllowAllPOSTCaching;
250 extern const CFStringRef _kCFURLCachePartitionKey;
251 extern const CFStringRef _kCFURLConnectionPropertyShouldSniff;
252 extern const CFStringRef _kCFURLStorageSessionIsPrivate;
253
254 #if HAVE(CFNETWORK_WITH_CONTENT_ENCODING_SNIFFING_OVERRIDE)
255 extern const CFStringRef kCFURLRequestContentDecoderSkipURLCheck;
256 #endif
257
258 CFHTTPCookieStorageRef _CFHTTPCookieStorageGetDefault(CFAllocatorRef);
259 CFHTTPCookieStorageRef CFHTTPCookieStorageCreateFromFile(CFAllocatorRef, CFURLRef, CFHTTPCookieStorageRef);
260 void CFHTTPCookieStorageScheduleWithRunLoop(CFHTTPCookieStorageRef, CFRunLoopRef, CFStringRef);
261 void CFHTTPCookieStorageSetCookie(CFHTTPCookieStorageRef, CFHTTPCookieRef);
262 void CFHTTPCookieStorageSetCookieAcceptPolicy(CFHTTPCookieStorageRef, CFHTTPCookieStorageAcceptPolicy);
263 CFHTTPCookieStorageAcceptPolicy CFHTTPCookieStorageGetCookieAcceptPolicy(CFHTTPCookieStorageRef);
264
265 typedef void (*CFHTTPCookieStorageChangedProcPtr)(CFHTTPCookieStorageRef, void*);
266 void CFHTTPCookieStorageAddObserver(CFHTTPCookieStorageRef, CFRunLoopRef, CFStringRef, CFHTTPCookieStorageChangedProcPtr, void*);
267 void CFHTTPCookieStorageRemoveObserver(CFHTTPCookieStorageRef, CFRunLoopRef, CFStringRef, CFHTTPCookieStorageChangedProcPtr, void*);
268
269 CFURLCredentialStorageRef CFURLCredentialStorageCreate(CFAllocatorRef);
270 CFURLCredentialRef CFURLCredentialStorageCopyDefaultCredentialForProtectionSpace(CFURLCredentialStorageRef, CFURLProtectionSpaceRef);
271 CFURLRequestPriority CFURLRequestGetRequestPriority(CFURLRequestRef);
272 void _CFURLRequestSetProtocolProperty(CFURLRequestRef, CFStringRef, CFTypeRef);
273 void CFURLRequestSetRequestPriority(CFURLRequestRef, CFURLRequestPriority);
274 void CFURLRequestSetShouldPipelineHTTP(CFURLRequestRef, Boolean, Boolean);
275 void _CFURLRequestSetStorageSession(CFMutableURLRequestRef, CFURLStorageSessionRef);
276 CFStringRef CFURLResponseCopySuggestedFilename(CFURLResponseRef);
277 CFHTTPMessageRef CFURLResponseGetHTTPResponse(CFURLResponseRef);
278 CFStringRef CFURLResponseGetMIMEType(CFURLResponseRef);
279 CFDictionaryRef _CFURLResponseGetSSLCertificateContext(CFURLResponseRef);
280 CFURLRef CFURLResponseGetURL(CFURLResponseRef);
281 void CFURLResponseSetMIMEType(CFURLResponseRef, CFStringRef);
282 CFHTTPCookieStorageRef _CFURLStorageSessionCopyCookieStorage(CFAllocatorRef, CFURLStorageSessionRef);
283 CFArrayRef _CFHTTPCookieStorageCopyCookiesForURLWithMainDocumentURL(CFHTTPCookieStorageRef inCookieStorage, CFURLRef inURL, CFURLRef inMainDocumentURL, Boolean sendSecureCookies);
284 CFStringRef CFURLResponseGetTextEncodingName(CFURLResponseRef);
285 SInt64 CFURLResponseGetExpectedContentLength(CFURLResponseRef);
286 CFTypeID CFURLResponseGetTypeID();
287 Boolean CFURLRequestShouldHandleHTTPCookies(CFURLRequestRef);
288 CFURLRef CFURLRequestGetURL(CFURLRequestRef);
289 CFURLResponseRef CFURLResponseCreate(CFAllocatorRef, CFURLRef, CFStringRef mimeType, SInt64 expectedContentLength, CFStringRef textEncodingName, CFURLCacheStoragePolicy);
290 void CFURLResponseSetExpectedContentLength(CFURLResponseRef, SInt64 length);
291 CFURLResponseRef CFURLResponseCreateWithHTTPResponse(CFAllocatorRef, CFURLRef, CFHTTPMessageRef, CFURLCacheStoragePolicy);
292 CFArrayRef CFHTTPCookieStorageCopyCookies(CFHTTPCookieStorageRef);
293 void CFHTTPCookieStorageSetCookies(CFHTTPCookieStorageRef, CFArrayRef cookies, CFURLRef, CFURLRef mainDocumentURL);
294 void CFHTTPCookieStorageDeleteCookie(CFHTTPCookieStorageRef, CFHTTPCookieRef);
295 CFMutableURLRequestRef CFURLRequestCreateMutableCopy(CFAllocatorRef, CFURLRequestRef);
296 CFStringRef _CFURLCacheCopyCacheDirectory(CFURLCacheRef);
297 Boolean _CFHostIsDomainTopLevel(CFStringRef domain);
298 void _CFURLRequestCreateArchiveList(CFAllocatorRef, CFURLRequestRef, CFIndex* version, CFTypeRef** objects, CFIndex* objectCount, CFDictionaryRef* protocolProperties);
299 CFMutableURLRequestRef _CFURLRequestCreateFromArchiveList(CFAllocatorRef, CFIndex version, CFTypeRef* objects, CFIndex objectCount, CFDictionaryRef protocolProperties);
300 void CFURLRequestSetProxySettings(CFMutableURLRequestRef, CFDictionaryRef);
301
302 #endif // !PLATFORM(WIN)
303
304 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTProxy;
305 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTProxyHost;
306 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTProxyPort;
307 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTAdditionalHeaders;
308 CFN_EXPORT const CFStringRef kCFStreamPropertyCONNECTResponse;
309
310 CFN_EXPORT void _CFHTTPMessageSetResponseURL(CFHTTPMessageRef, CFURLRef);
311 CFN_EXPORT void _CFHTTPMessageSetResponseProxyURL(CFHTTPMessageRef, CFURLRef);
312
313 WTF_EXTERN_C_END
314
315 #if defined(__OBJC__) && !USE(APPLE_INTERNAL_SDK)
316
317 enum : NSUInteger {
318     NSHTTPCookieAcceptPolicyExclusivelyFromMainDocumentDomain = 3,
319 };
320
321 #endif
322
323 WTF_EXTERN_C_BEGIN
324
325 CFDataRef _CFNetworkCopyATSContext(void);
326 Boolean _CFNetworkSetATSContext(CFDataRef);
327
328 #if PLATFORM(COCOA)
329 extern const CFStringRef _kCFNetworkHSTSPreloaded;
330 CFDictionaryRef _CFNetworkCopyHSTSPolicies(CFURLStorageSessionRef);
331 void _CFNetworkResetHSTS(CFURLRef, CFURLStorageSessionRef);
332 void _CFNetworkResetHSTSHostsSinceDate(CFURLStorageSessionRef, CFDateRef);
333 Boolean _CFNetworkIsKnownHSTSHostWithSession(CFURLRef, CFURLStorageSessionRef);
334 void _CFNetworkResetHSTSHostsWithSession(CFURLStorageSessionRef);
335 #endif
336
337 CFDataRef CFHTTPCookieStorageCreateIdentifyingData(CFAllocatorRef inAllocator, CFHTTPCookieStorageRef inStorage);
338 CFHTTPCookieStorageRef CFHTTPCookieStorageCreateFromIdentifyingData(CFAllocatorRef inAllocator, CFDataRef inData);
339 CFArrayRef _CFHTTPParsedCookiesWithResponseHeaderFields(CFAllocatorRef inAllocator, CFDictionaryRef headerFields, CFURLRef inURL);
340
341 WTF_EXTERN_C_END
342
343 #if defined(__OBJC__)
344
345 // FIXME: Move these declarations the above section under !USE(APPLE_INTERNAL_SDK) when possible so that
346 // Apple internal SDK builds use headers instead.
347
348 @interface NSHTTPCookie ()
349 #if PLATFORM(MAC)
350 + (NSArray *)_parsedCookiesWithResponseHeaderFields:(NSDictionary *)headerFields forURL:(NSURL *)aURL;
351 #endif
352 + (NSArray *)_cf2nsCookies:(CFArrayRef)cfCookies;
353 @end
354
355 @interface NSHTTPCookieStorage ()
356 + (void)_setSharedHTTPCookieStorage:(NSHTTPCookieStorage *)storage;
357 @end
358
359 @interface NSURLResponse ()
360 - (void)_setMIMEType:(NSString *)type;
361 @end
362
363 @interface NSURLSessionConfiguration ()
364 // FIXME: Remove this once rdar://problem/40650244 is in a build.
365 @property (copy) NSDictionary *_socketStreamProperties;
366 @end
367
368 @interface NSURLSessionTask ()
369 - (void)_setExplicitCookieStorage:(CFHTTPCookieStorageRef)storage;
370 @end
371
372 #endif // defined(__OBJC__)