mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
Add missing NSURLSession APIs
This commit is contained in:
parent
55e53caf28
commit
2f5b26df06
3 changed files with 396 additions and 52 deletions
|
@ -1,3 +1,9 @@
|
|||
2023-01-13 Frederik Seiffert <frederik@algoriddim.com>
|
||||
|
||||
* Headers/Foundation/NSURLSession.h:
|
||||
* Source/NSURLSession.m:
|
||||
Add missing NSURLSession APIs.
|
||||
|
||||
2022-02-09 Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Source/Additions/GSMime.m: ([GSMimeHeader setValue:]) do not set
|
||||
|
|
|
@ -23,7 +23,9 @@
|
|||
@class NSURLRequest;
|
||||
@class NSURLResponse;
|
||||
@class NSURLSessionConfiguration;
|
||||
@class NSURLSessionTask;
|
||||
@class NSURLSessionDataTask;
|
||||
@class NSURLSessionUploadTask;
|
||||
@class NSURLSessionDownloadTask;
|
||||
|
||||
|
||||
|
@ -62,6 +64,10 @@ GS_EXPORT_CLASS
|
|||
GSMultiHandle *_multiHandle;
|
||||
}
|
||||
|
||||
+ (NSURLSession*) sharedSession;
|
||||
|
||||
+ (NSURLSession*) sessionWithConfiguration: (NSURLSessionConfiguration*)configuration;
|
||||
|
||||
/*
|
||||
* Customization of NSURLSession occurs during creation of a new session.
|
||||
* If you do specify a delegate, the delegate will be retained until after
|
||||
|
@ -111,11 +117,80 @@ GS_EXPORT_CLASS
|
|||
/* Creates a data task to retrieve the contents of the given URL. */
|
||||
- (NSURLSessionDataTask*) dataTaskWithURL: (NSURL*)url;
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromFile: (NSURL*)fileURL;
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromData: (NSData*)bodyData;
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithStreamedRequest: (NSURLRequest*)request;
|
||||
|
||||
/* Creates a download task with the given request. */
|
||||
- (NSURLSessionDownloadTask *) downloadTaskWithRequest: (NSURLRequest *)request;
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithRequest: (NSURLRequest*)request;
|
||||
|
||||
/* Creates a download task to download the contents of the given URL. */
|
||||
- (NSURLSessionDownloadTask *) downloadTaskWithURL: (NSURL *)url;
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithURL: (NSURL*)url;
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionDownloadTask *) downloadTaskWithResumeData: (NSData *)resumeData;
|
||||
|
||||
- (void) getTasksWithCompletionHandler: (void (^)(GS_GENERIC_CLASS(NSArray, NSURLSessionDataTask*) *dataTasks, GS_GENERIC_CLASS(NSArray, NSURLSessionUploadTask*) *uploadTasks, GS_GENERIC_CLASS(NSArray, NSURLSessionDownloadTask*) *downloadTasks))completionHandler;
|
||||
|
||||
- (void) getAllTasksWithCompletionHandler: (void (^)(GS_GENERIC_CLASS(NSArray, __kindof NSURLSessionTask*) *tasks))completionHandler;
|
||||
|
||||
@end
|
||||
|
||||
/*
|
||||
* NSURLSession convenience routines deliver results to
|
||||
* a completion handler block. These convenience routines
|
||||
* are not available to NSURLSessions that are configured
|
||||
* as background sessions.
|
||||
*
|
||||
* Task objects are always created in a suspended state and
|
||||
* must be sent the -resume message before they will execute.
|
||||
*/
|
||||
@interface NSURLSession (NSURLSessionAsynchronousConvenience)
|
||||
/*
|
||||
* data task convenience methods. These methods create tasks that
|
||||
* bypass the normal delegate calls for response and data delivery,
|
||||
* and provide a simple cancelable asynchronous interface to receiving
|
||||
* data. Errors will be returned in the NSURLErrorDomain,
|
||||
* see <Foundation/NSURLError.h>. The delegate, if any, will still be
|
||||
* called for authentication challenges.
|
||||
*/
|
||||
- (NSURLSessionDataTask*) dataTaskWithRequest: (NSURLRequest*)request
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
|
||||
- (NSURLSessionDataTask*) dataTaskWithURL: (NSURL*)url
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
|
||||
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromFile: (NSURL*)fileURL
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromData: (NSData*)bodyData
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
|
||||
|
||||
/*
|
||||
* download task convenience methods. When a download successfully
|
||||
* completes, the NSURL will point to a file that must be read or
|
||||
* copied during the invocation of the completion routine. The file
|
||||
* will be removed automatically.
|
||||
*/
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithRequest: (NSURLRequest*)request
|
||||
completionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithURL: (NSURL *)url
|
||||
completionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
|
||||
|
||||
/* Not implemented */
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithResumeData: (NSData*)resumeData
|
||||
completionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -131,6 +206,12 @@ typedef NS_ENUM(NSUInteger, NSURLSessionTaskState) {
|
|||
NSURLSessionTaskStateCompleted = 3,
|
||||
};
|
||||
|
||||
GS_EXPORT const float NSURLSessionTaskPriorityDefault;
|
||||
GS_EXPORT const float NSURLSessionTaskPriorityLow;
|
||||
GS_EXPORT const float NSURLSessionTaskPriorityHigh;
|
||||
|
||||
GS_EXPORT const int64_t NSURLSessionTransferSizeUnknown;
|
||||
|
||||
/*
|
||||
* NSURLSessionTask - a cancelable object that refers to the lifetime
|
||||
* of processing a given request.
|
||||
|
@ -245,6 +326,9 @@ GS_EXPORT_CLASS
|
|||
- (void) suspend;
|
||||
- (void) resume;
|
||||
|
||||
- (float) priority;
|
||||
- (void) setPriority: (float)priority;
|
||||
|
||||
@end
|
||||
|
||||
GS_EXPORT_CLASS
|
||||
|
@ -273,6 +357,7 @@ GS_EXPORT_CLASS
|
|||
GS_EXPORT_CLASS
|
||||
@interface NSURLSessionConfiguration : NSObject <NSCopying>
|
||||
{
|
||||
NSString *_identifier;
|
||||
NSURLCache *_URLCache;
|
||||
NSURLRequestCachePolicy _requestCachePolicy;
|
||||
NSArray *_protocolClasses;
|
||||
|
@ -288,34 +373,40 @@ GS_EXPORT_CLASS
|
|||
|
||||
- (NSURLRequest*) configureRequest: (NSURLRequest*)request;
|
||||
|
||||
@property (class, readonly, strong)
|
||||
NSURLSessionConfiguration *defaultSessionConfiguration;
|
||||
+ (NSURLSessionConfiguration*) defaultSessionConfiguration;
|
||||
+ (NSURLSessionConfiguration*) ephemeralSessionConfiguration;
|
||||
+ (NSURLSessionConfiguration*) backgroundSessionConfigurationWithIdentifier:(NSString*)identifier;
|
||||
|
||||
- (NSDictionary*) HTTPAdditionalHeaders;
|
||||
- (void) setHTTPAdditionalHeaders: (NSDictionary*)headers;
|
||||
|
||||
- (NSHTTPCookieAcceptPolicy) HTTPCookieAcceptPolicy;
|
||||
- (void) setHTTPCookieAcceptPolicy: (NSHTTPCookieAcceptPolicy)policy;
|
||||
|
||||
- (NSHTTPCookieStorage*) HTTPCookieStorage;
|
||||
|
||||
#if !NO_GNUSTEP
|
||||
- (NSInteger) HTTPMaximumConnectionLifetime;
|
||||
#endif
|
||||
- (void) setHTTPCookieStorage: (NSHTTPCookieStorage*)storage;
|
||||
|
||||
- (NSInteger) HTTPMaximumConnectionsPerHost;
|
||||
- (void) setHTTPMaximumConnectionsPerHost: (NSInteger)n;
|
||||
|
||||
- (BOOL) HTTPShouldSetCookies;
|
||||
- (void) setHTTPShouldSetCookies: (BOOL)flag;
|
||||
|
||||
- (BOOL) HTTPShouldUsePipelining;
|
||||
- (void) setHTTPShouldUsePipelining: (BOOL)flag;
|
||||
|
||||
- (NSString*) identifier;
|
||||
|
||||
- (NSArray*) protocolClasses;
|
||||
|
||||
- (NSURLRequestCachePolicy) requestCachePolicy;
|
||||
- (void) setRequestCachePolicy: (NSURLRequestCachePolicy)policy;
|
||||
|
||||
- (void) setHTTPAdditionalHeaders: (NSDictionary*)headers;
|
||||
- (NSURLCache*) URLCache;
|
||||
- (void) setURLCache: (NSURLCache*)cache;
|
||||
|
||||
- (void) setHTTPCookieAcceptPolicy: (NSHTTPCookieAcceptPolicy)policy;
|
||||
|
||||
- (void) setHTTPCookieStorage: (NSHTTPCookieStorage*)storage;
|
||||
- (NSURLCredentialStorage*) URLCredentialStorage;
|
||||
- (void) setURLCredentialStorage: (NSURLCredentialStorage*)storage;
|
||||
|
||||
#if !NO_GNUSTEP
|
||||
/** Permits a session to be configured so that older connections are reused.
|
||||
|
@ -323,25 +414,10 @@ GS_EXPORT_CLASS
|
|||
* reused as long as they are not older than 118 seconds, which is reasonable
|
||||
* for the vast majority if situations.
|
||||
*/
|
||||
- (NSInteger) HTTPMaximumConnectionLifetime;
|
||||
- (void) setHTTPMaximumConnectionLifetime: (NSInteger)n;
|
||||
#endif
|
||||
|
||||
- (void) setHTTPMaximumConnectionsPerHost: (NSInteger)n;
|
||||
|
||||
- (void) setHTTPShouldSetCookies: (BOOL)flag;
|
||||
|
||||
- (void) setHTTPShouldUsePipelining: (BOOL)flag;
|
||||
|
||||
- (void) setRequestCachePolicy: (NSURLRequestCachePolicy)policy;
|
||||
|
||||
- (void) setURLCache: (NSURLCache*)cache;
|
||||
|
||||
- (void) setURLCredentialStorage: (NSURLCredentialStorage*)storage;
|
||||
|
||||
- (NSURLCache*) URLCache;
|
||||
|
||||
- (NSURLCredentialStorage*) URLCredentialStorage;
|
||||
|
||||
@end
|
||||
|
||||
typedef NS_ENUM(NSInteger, NSURLSessionAuthChallengeDisposition) {
|
||||
|
|
|
@ -11,11 +11,17 @@
|
|||
#import "Foundation/NSError.h"
|
||||
#import "Foundation/NSException.h"
|
||||
#import "Foundation/NSOperation.h"
|
||||
#import "Foundation/NSPredicate.h"
|
||||
#import "Foundation/NSURLError.h"
|
||||
#import "Foundation/NSURLSession.h"
|
||||
#import "Foundation/NSURLRequest.h"
|
||||
#import "Foundation/NSValue.h"
|
||||
|
||||
GS_DECLARE const float NSURLSessionTaskPriorityDefault = 0.5;
|
||||
GS_DECLARE const float NSURLSessionTaskPriorityLow = 0.0;
|
||||
GS_DECLARE const float NSURLSessionTaskPriorityHigh = 1.0;
|
||||
|
||||
GS_DECLARE const int64_t NSURLSessionTransferSizeUnknown = -1;
|
||||
|
||||
/* NSURLSession API implementation overview
|
||||
*
|
||||
|
@ -66,6 +72,12 @@
|
|||
- (void) setState: (NSURLSessionTaskState)state;
|
||||
|
||||
- (void) invalidateProtocol;
|
||||
|
||||
- (void (^)(NSData *data, NSURLResponse *response, NSError *error)) dataCompletionHandler;
|
||||
- (void) setDataCompletionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;
|
||||
|
||||
- (void (^)(NSURL *location, NSURLResponse *response, NSError *error)) downloadCompletionHandler;
|
||||
- (void) setDownloadCompletionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler;
|
||||
@end
|
||||
|
||||
@interface NSURLSessionTask (URLProtocolClient) <NSURLProtocolClient>
|
||||
|
@ -103,9 +115,36 @@ static int nextSessionIdentifier()
|
|||
GSTaskRegistry *_taskRegistry;
|
||||
}
|
||||
|
||||
+ (NSURLSession *) sessionWithConfiguration: (NSURLSessionConfiguration*)configuration
|
||||
delegate: (id <NSURLSessionDelegate>)delegate
|
||||
delegateQueue: (NSOperationQueue*)queue
|
||||
|
||||
+ (NSURLSession*) sharedSession
|
||||
{
|
||||
static NSURLSession *session = nil;
|
||||
static dispatch_once_t predicate;
|
||||
|
||||
dispatch_once(&predicate, ^{
|
||||
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
||||
session = [[NSURLSession alloc] initWithConfiguration: configuration
|
||||
delegate: nil
|
||||
delegateQueue: nil];
|
||||
});
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
+ (NSURLSession*) sessionWithConfiguration: (NSURLSessionConfiguration*)configuration
|
||||
{
|
||||
NSURLSession *session;
|
||||
|
||||
session = [[NSURLSession alloc] initWithConfiguration: configuration
|
||||
delegate: nil
|
||||
delegateQueue: nil];
|
||||
|
||||
return AUTORELEASE(session);
|
||||
}
|
||||
|
||||
+ (NSURLSession*) sessionWithConfiguration: (NSURLSessionConfiguration*)configuration
|
||||
delegate: (id <NSURLSessionDelegate>)delegate
|
||||
delegateQueue: (NSOperationQueue*)queue
|
||||
{
|
||||
NSURLSession *session;
|
||||
|
||||
|
@ -284,7 +323,24 @@ static int nextSessionIdentifier()
|
|||
return [self dataTaskWithRequest: request];
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask *) downloadTaskWithRequest: (NSURLRequest *)request
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromFile: (NSURL*)fileURL
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromData: (NSData*)bodyData;
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithStreamedRequest: (NSURLRequest*)request
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithRequest: (NSURLRequest*)request
|
||||
{
|
||||
NSURLSessionDownloadTask *task;
|
||||
|
||||
|
@ -302,7 +358,7 @@ static int nextSessionIdentifier()
|
|||
return AUTORELEASE(task);
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask *) downloadTaskWithURL: (NSURL *)url
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithURL: (NSURL*)url
|
||||
{
|
||||
NSMutableURLRequest *request;
|
||||
|
||||
|
@ -312,6 +368,39 @@ static int nextSessionIdentifier()
|
|||
return [self downloadTaskWithRequest: request];
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithResumeData: (NSData*)resumeData
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
- (void) getTasksWithCompletionHandler: (void (^)(GS_GENERIC_CLASS(NSArray, NSURLSessionDataTask*) *dataTasks, GS_GENERIC_CLASS(NSArray, NSURLSessionUploadTask*) *uploadTasks, GS_GENERIC_CLASS(NSArray, NSURLSessionDownloadTask*) *downloadTasks))completionHandler
|
||||
{
|
||||
NSArray *allTasks, *dataTasks, *uploadTasks, *downloadTasks;
|
||||
|
||||
allTasks = [_taskRegistry allTasks];
|
||||
dataTasks = [allTasks filteredArrayUsingPredicate:
|
||||
[NSPredicate predicateWithBlock:^BOOL(id task, NSDictionary* bindings) {
|
||||
return [task isKindOfClass:[NSURLSessionDataTask class]];
|
||||
}]];
|
||||
uploadTasks = [allTasks filteredArrayUsingPredicate:
|
||||
[NSPredicate predicateWithBlock:^BOOL(id task, NSDictionary* bindings) {
|
||||
return [task isKindOfClass:[NSURLSessionUploadTask class]];
|
||||
}]];
|
||||
downloadTasks = [allTasks filteredArrayUsingPredicate:
|
||||
[NSPredicate predicateWithBlock:^BOOL(id task, NSDictionary* bindings) {
|
||||
return [task isKindOfClass:[NSURLSessionDownloadTask class]];
|
||||
}]];
|
||||
|
||||
completionHandler(dataTasks, uploadTasks, downloadTasks);
|
||||
}
|
||||
|
||||
- (void) getAllTasksWithCompletionHandler: (void (^)(GS_GENERIC_CLASS(NSArray, __kindof NSURLSessionTask*) *tasks))completionHandler
|
||||
{
|
||||
NSArray *allTasks = [_taskRegistry allTasks];
|
||||
|
||||
completionHandler(allTasks);
|
||||
}
|
||||
|
||||
- (void) addTask: (NSURLSessionTask*)task
|
||||
{
|
||||
[_taskRegistry addTask: task];
|
||||
|
@ -324,6 +413,94 @@ static int nextSessionIdentifier()
|
|||
|
||||
@end
|
||||
|
||||
@implementation NSURLSession (NSURLSessionAsynchronousConvenience)
|
||||
|
||||
- (NSURLSessionDataTask*) dataTaskWithRequest: (NSURLRequest*)request
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
NSURLSessionDataTask *task;
|
||||
|
||||
if (_invalidated)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
task = [[NSURLSessionDataTask alloc] initWithSession: self
|
||||
request: request
|
||||
taskIdentifier: _nextTaskIdentifier++];
|
||||
[task setDataCompletionHandler: completionHandler];
|
||||
|
||||
[self addTask: task];
|
||||
|
||||
return AUTORELEASE(task);
|
||||
}
|
||||
|
||||
- (NSURLSessionDataTask*) dataTaskWithURL: (NSURL*)url
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
NSMutableURLRequest *request;
|
||||
|
||||
request = [NSMutableURLRequest requestWithURL: url];
|
||||
[request setHTTPMethod: @"POST"];
|
||||
|
||||
return [self dataTaskWithRequest: request
|
||||
completionHandler: completionHandler];
|
||||
}
|
||||
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromFile: (NSURL*)fileURL
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
- (NSURLSessionUploadTask*) uploadTaskWithRequest: (NSURLRequest*)request
|
||||
fromData: (NSData*)bodyData
|
||||
completionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithRequest: (NSURLRequest*)request
|
||||
completionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
NSURLSessionDataTask *task;
|
||||
|
||||
if (_invalidated)
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
task = [[NSURLSessionDataTask alloc] initWithSession: self
|
||||
request: request
|
||||
taskIdentifier: _nextTaskIdentifier++];
|
||||
[task setDownloadCompletionHandler: completionHandler];
|
||||
|
||||
[self addTask: task];
|
||||
|
||||
return AUTORELEASE(task);
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithURL: (NSURL*)url
|
||||
completionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
NSMutableURLRequest *request;
|
||||
|
||||
request = [NSMutableURLRequest requestWithURL: url];
|
||||
[request setHTTPMethod: @"GET"];
|
||||
|
||||
return [self downloadTaskWithRequest: request
|
||||
completionHandler: completionHandler];
|
||||
}
|
||||
|
||||
- (NSURLSessionDownloadTask*) downloadTaskWithResumeData: (NSData*)resumeData
|
||||
completionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
return [self notImplemented: _cmd];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation _NSURLProtocolClient
|
||||
|
||||
|
@ -503,16 +680,20 @@ static int nextSessionIdentifier()
|
|||
[[session delegateQueue] addOperationWithBlock:
|
||||
^{
|
||||
if ([delegate respondsToSelector: @selector
|
||||
(URLSession:dataTask:didReceiveResponse:completionHandler:)])
|
||||
(URLSession:dataTask:didReceiveResponse:completionHandler:)])
|
||||
{
|
||||
NSURLSessionDataTask *dataTask = (NSURLSessionDataTask*)task;
|
||||
NSURLSessionDataTask *dataTask = (NSURLSessionDataTask*)task;
|
||||
|
||||
[(id<NSURLSessionDataDelegate>)delegate URLSession: session
|
||||
dataTask: dataTask
|
||||
didReceiveResponse: response
|
||||
completionHandler:
|
||||
^(NSURLSessionResponseDisposition disposition) {
|
||||
NSLog(@"Ignoring disposition from completion handler.");
|
||||
dataTask: dataTask
|
||||
didReceiveResponse: response
|
||||
completionHandler:
|
||||
^(NSURLSessionResponseDisposition disposition) {
|
||||
if (disposition != NSURLSessionResponseAllow)
|
||||
{
|
||||
NSLog(@"Warning: ignoring disposition %d from completion handler",
|
||||
(int)disposition);
|
||||
}
|
||||
}];
|
||||
}
|
||||
}];
|
||||
|
@ -547,13 +728,13 @@ static int nextSessionIdentifier()
|
|||
method = [[auth componentsSeparatedByString: @" "] firstObject];
|
||||
range = [auth rangeOfString: @"realm="];
|
||||
realm = range.length > 0
|
||||
? [auth substringFromIndex: NSMaxRange(range)] : @"";
|
||||
? [auth substringFromIndex: NSMaxRange(range)] : @"";
|
||||
space = AUTORELEASE([[NSURLProtectionSpace alloc]
|
||||
initWithHost: host
|
||||
port: [port integerValue]
|
||||
protocol: scheme
|
||||
realm: realm
|
||||
authenticationMethod: method]);
|
||||
initWithHost: host
|
||||
port: [port integerValue]
|
||||
protocol: scheme
|
||||
realm: realm
|
||||
authenticationMethod: method]);
|
||||
}
|
||||
return space;
|
||||
}
|
||||
|
@ -566,6 +747,8 @@ static int nextSessionIdentifier()
|
|||
NSURLCache *cache;
|
||||
NSOperationQueue *delegateQueue;
|
||||
id<NSURLSessionDelegate> delegate;
|
||||
void (^dataCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error);
|
||||
void (^downloadCompletionHandler)(NSURL *location, NSURLResponse *response, NSError *error);
|
||||
|
||||
NSAssert(nil != task, @"Missing task");
|
||||
|
||||
|
@ -577,12 +760,14 @@ static int nextSessionIdentifier()
|
|||
NSURLProtectionSpace *space;
|
||||
|
||||
if (nil != (space = [self _protectionSpaceFrom: urlResponse]))
|
||||
{
|
||||
}
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
delegate = [session delegate];
|
||||
delegateQueue = [session delegateQueue];
|
||||
dataCompletionHandler = [task dataCompletionHandler];
|
||||
downloadCompletionHandler = [task downloadCompletionHandler];
|
||||
|
||||
if (nil != (cache = [[session configuration] URLCache])
|
||||
&& [task isKindOfClass: [NSURLSessionDataTask class]]
|
||||
|
@ -610,7 +795,29 @@ static int nextSessionIdentifier()
|
|||
RELEASE(cacheable);
|
||||
}
|
||||
|
||||
if (nil != delegate)
|
||||
if (nil != dataCompletionHandler
|
||||
&& ([task isKindOfClass: [NSURLSessionDataTask class]]
|
||||
|| [task isKindOfClass: [NSURLSessionUploadTask class]]))
|
||||
{
|
||||
[delegateQueue addOperationWithBlock:
|
||||
^{
|
||||
dataCompletionHandler(nil, urlResponse, nil);
|
||||
}];
|
||||
}
|
||||
else if (nil != downloadCompletionHandler
|
||||
&& [task isKindOfClass: [NSURLSessionDownloadTask class]])
|
||||
{
|
||||
NSURL *fileURL;
|
||||
|
||||
fileURL = [NSURLProtocol propertyForKey: @"tempFileURL"
|
||||
inRequest: [protocol request]];
|
||||
|
||||
[delegateQueue addOperationWithBlock:
|
||||
^{
|
||||
downloadCompletionHandler(fileURL, urlResponse, nil);
|
||||
}];
|
||||
}
|
||||
else if (nil != delegate)
|
||||
{
|
||||
// Send delegate with temporary fileURL
|
||||
if ([task isKindOfClass: [NSURLSessionDownloadTask class]]
|
||||
|
@ -641,7 +848,7 @@ static int nextSessionIdentifier()
|
|||
}
|
||||
|
||||
if ([delegate respondsToSelector:
|
||||
@selector(URLSession:task:didCompleteWithError:)])
|
||||
@selector(URLSession:task:didCompleteWithError:)])
|
||||
{
|
||||
[(id<NSURLSessionTaskDelegate>)delegate URLSession: session
|
||||
task: task
|
||||
|
@ -687,7 +894,10 @@ static int nextSessionIdentifier()
|
|||
NSURLProtocol *_protocol;
|
||||
NSMutableArray *_protocolBag;
|
||||
Class _protocolClass;
|
||||
void (^_dataCompletionHandler)(NSData *data, NSURLResponse *response, NSError *error);
|
||||
void (^_downloadCompletionHandler)(NSURL *location, NSURLResponse *response, NSError *error);
|
||||
BOOL _hasTriggeredResume;
|
||||
float _priority;
|
||||
}
|
||||
|
||||
- (instancetype) initWithSession: (NSURLSession*)session
|
||||
|
@ -733,6 +943,7 @@ static int nextSessionIdentifier()
|
|||
_protocolState = NSURLSessionTaskProtocolStateToBeCreated;
|
||||
_protocol = nil;
|
||||
_hasTriggeredResume = NO;
|
||||
_priority = NSURLSessionTaskPriorityDefault;
|
||||
e = [[[session configuration] protocolClasses] objectEnumerator];
|
||||
while (nil != (protocolClass = [e nextObject]))
|
||||
{
|
||||
|
@ -757,6 +968,7 @@ static int nextSessionIdentifier()
|
|||
DESTROY(_protocolLock);
|
||||
DESTROY(_protocol);
|
||||
DESTROY(_protocolBag);
|
||||
DESTROY(_dataCompletionHandler);
|
||||
DESTROY(_knownBody);
|
||||
[super dealloc];
|
||||
}
|
||||
|
@ -972,6 +1184,16 @@ static int nextSessionIdentifier()
|
|||
});
|
||||
}
|
||||
|
||||
- (float) priority
|
||||
{
|
||||
return _priority;;
|
||||
}
|
||||
|
||||
- (void) setPriority: (float)priority
|
||||
{
|
||||
_priority = priority;
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone*)zone
|
||||
{
|
||||
NSURLSessionTask *copy = [[[self class] alloc] init];
|
||||
|
@ -1139,6 +1361,26 @@ static int nextSessionIdentifier()
|
|||
[_protocolLock unlock];
|
||||
}
|
||||
|
||||
- (void (^)(NSData *data, NSURLResponse *response, NSError *error)) dataCompletionHandler
|
||||
{
|
||||
return _dataCompletionHandler;
|
||||
}
|
||||
|
||||
- (void) setDataCompletionHandler: (void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
ASSIGN(_dataCompletionHandler, completionHandler);
|
||||
}
|
||||
|
||||
- (void (^)(NSURL *location, NSURLResponse *response, NSError *error)) downloadCompletionHandler
|
||||
{
|
||||
return _downloadCompletionHandler;
|
||||
}
|
||||
|
||||
- (void) setDownloadCompletionHandler: (void (^)(NSURL *location, NSURLResponse *response, NSError *error))completionHandler
|
||||
{
|
||||
ASSIGN(_downloadCompletionHandler, completionHandler);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSURLSessionDataTask
|
||||
|
@ -1170,6 +1412,19 @@ static NSURLSessionConfiguration *def = nil;
|
|||
return AUTORELEASE([def copy]);
|
||||
}
|
||||
|
||||
+ (NSURLSessionConfiguration*) ephemeralSessionConfiguration
|
||||
{
|
||||
// return default session since we don't store any data on disk anyway
|
||||
return AUTORELEASE([def copy]);
|
||||
}
|
||||
|
||||
+ (NSURLSessionConfiguration*) backgroundSessionConfigurationWithIdentifier:(NSString*)identifier
|
||||
{
|
||||
NSURLSessionConfiguration *configuration = [def copy];
|
||||
configuration->_identifier = [identifier copy];
|
||||
return AUTORELEASE(configuration);
|
||||
}
|
||||
|
||||
- (instancetype) init
|
||||
{
|
||||
if (nil != (self = [super init]))
|
||||
|
@ -1190,6 +1445,7 @@ static NSURLSessionConfiguration *def = nil;
|
|||
|
||||
- (void) dealloc
|
||||
{
|
||||
DESTROY(_identifier);
|
||||
DESTROY(_HTTPAdditionalHeaders);
|
||||
DESTROY(_HTTPCookieStorage);
|
||||
DESTROY(_protocolClasses);
|
||||
|
@ -1198,6 +1454,11 @@ static NSURLSessionConfiguration *def = nil;
|
|||
[super dealloc];
|
||||
}
|
||||
|
||||
- (NSString*) identifier
|
||||
{
|
||||
return _identifier;
|
||||
}
|
||||
|
||||
- (NSURLCache*) URLCache
|
||||
{
|
||||
return _URLCache;
|
||||
|
@ -1318,7 +1579,7 @@ static NSURLSessionConfiguration *def = nil;
|
|||
NSString *cookieValue;
|
||||
|
||||
cookiesHeaderFields
|
||||
= [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
|
||||
= [NSHTTPCookie requestHeaderFieldsWithCookies: cookies];
|
||||
cookieValue = [cookiesHeaderFields objectForKey: @"Cookie"];
|
||||
if (nil != cookieValue && [cookieValue length] > 0)
|
||||
{
|
||||
|
@ -1342,6 +1603,7 @@ static NSURLSessionConfiguration *def = nil;
|
|||
|
||||
if (copy)
|
||||
{
|
||||
copy->_identifier = [_identifier copy];
|
||||
copy->_URLCache = [_URLCache copy];
|
||||
copy->_URLCredentialStorage = [_URLCredentialStorage copy];
|
||||
copy->_protocolClasses = [_protocolClasses copyWithZone: zone];
|
||||
|
@ -1351,7 +1613,7 @@ static NSURLSessionConfiguration *def = nil;
|
|||
copy->_HTTPCookieStorage = [_HTTPCookieStorage copy];
|
||||
copy->_HTTPShouldSetCookies = _HTTPShouldSetCookies;
|
||||
copy->_HTTPAdditionalHeaders
|
||||
= [_HTTPAdditionalHeaders copyWithZone: zone];
|
||||
= [_HTTPAdditionalHeaders copyWithZone: zone];
|
||||
}
|
||||
|
||||
return copy;
|
||||
|
|
Loading…
Reference in a new issue