github.com/psiphon-labs/psiphon-tunnel-core@v2.0.28+incompatible/MobileLibrary/iOS/SampleApps/TunneledWebView/External/JiveAuthenticatingHTTPProtocol/JAHPQNSURLSessionDemux.m (about) 1 /* 2 File: JAHPQNSURLSessionDemux.m 3 Abstract: A general class to demux NSURLSession delegate callbacks. 4 Version: 1.1 5 6 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 Inc. ("Apple") in consideration of your agreement to the following 8 terms, and your use, installation, modification or redistribution of 9 this Apple software constitutes acceptance of these terms. If you do 10 not agree with these terms, please do not use, install, modify or 11 redistribute this Apple software. 12 13 In consideration of your agreement to abide by the following terms, and 14 subject to these terms, Apple grants you a personal, non-exclusive 15 license, under Apple's copyrights in this original Apple software (the 16 "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 Software, with or without modifications, in source and/or binary forms; 18 provided that if you redistribute the Apple Software in its entirety and 19 without modifications, you must retain this notice and the following 20 text and disclaimers in all such redistributions of the Apple Software. 21 Neither the name, trademarks, service marks or logos of Apple Inc. may 22 be used to endorse or promote products derived from the Apple Software 23 without specific prior written permission from Apple. Except as 24 expressly stated in this notice, no other rights or licenses, express or 25 implied, are granted by Apple herein, including but not limited to any 26 patent rights that may be infringed by your derivative works or by other 27 works in which the Apple Software may be incorporated. 28 29 The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 35 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 POSSIBILITY OF SUCH DAMAGE. 43 44 Copyright (C) 2014 Apple Inc. All Rights Reserved. 45 46 */ 47 48 #import "JAHPQNSURLSessionDemux.h" 49 50 @interface JAHPQNSURLSessionDemuxTaskInfo : NSObject 51 52 - (instancetype)initWithTask:(NSURLSessionDataTask *)task delegate:(id<NSURLSessionDataDelegate>)delegate modes:(NSArray *)modes; 53 54 @property (atomic, strong, readonly ) NSURLSessionDataTask * task; 55 @property (atomic, strong, readonly ) id<NSURLSessionDataDelegate> delegate; 56 @property (atomic, strong, readonly ) NSThread * thread; 57 @property (atomic, copy, readonly ) NSArray * modes; 58 59 - (void)performBlock:(dispatch_block_t)block; 60 61 - (void)invalidate; 62 63 @end 64 65 @interface JAHPQNSURLSessionDemuxTaskInfo () 66 67 @property (atomic, strong, readwrite) id<NSURLSessionDataDelegate> delegate; 68 @property (atomic, strong, readwrite) NSThread * thread; 69 70 @end 71 72 @implementation JAHPQNSURLSessionDemuxTaskInfo 73 74 - (instancetype)initWithTask:(NSURLSessionDataTask *)task delegate:(id<NSURLSessionDataDelegate>)delegate modes:(NSArray *)modes 75 { 76 assert(task != nil); 77 assert(delegate != nil); 78 assert(modes != nil); 79 80 self = [super init]; 81 if (self != nil) { 82 self->_task = task; 83 self->_delegate = delegate; 84 self->_thread = [NSThread currentThread]; 85 self->_modes = [modes copy]; 86 } 87 return self; 88 } 89 90 - (void)performBlock:(dispatch_block_t)block 91 { 92 assert(self.delegate != nil); 93 assert(self.thread != nil); 94 [self performSelector:@selector(performBlockOnClientThread:) onThread:self.thread withObject:[block copy] waitUntilDone:NO modes:self.modes]; 95 } 96 97 - (void)performBlockOnClientThread:(dispatch_block_t)block 98 { 99 assert([NSThread currentThread] == self.thread); 100 block(); 101 } 102 103 - (void)invalidate 104 { 105 self.delegate = nil; 106 self.thread = nil; 107 } 108 109 @end 110 111 @interface JAHPQNSURLSessionDemux () <NSURLSessionDataDelegate> 112 113 @property (atomic, strong, readonly ) NSMutableDictionary * taskInfoByTaskID; // keys NSURLSessionTask taskIdentifier, values are SessionManager 114 @property (atomic, strong, readonly ) NSOperationQueue * sessionDelegateQueue; 115 116 @end 117 118 @implementation JAHPQNSURLSessionDemux 119 120 - (instancetype)init 121 { 122 return [self initWithConfiguration:nil]; 123 } 124 125 - (instancetype)initWithConfiguration:(NSURLSessionConfiguration *)configuration 126 { 127 // configuration may be nil 128 self = [super init]; 129 if (self != nil) { 130 if (configuration == nil) { 131 configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 132 } 133 self->_configuration = [configuration copy]; 134 135 self->_taskInfoByTaskID = [[NSMutableDictionary alloc] init]; 136 137 self->_sessionDelegateQueue = [[NSOperationQueue alloc] init]; 138 [self->_sessionDelegateQueue setMaxConcurrentOperationCount:1]; 139 [self->_sessionDelegateQueue setName:@"JAHPQNSURLSessionDemux"]; 140 141 self->_session = [NSURLSession sessionWithConfiguration:self->_configuration delegate:self delegateQueue:self->_sessionDelegateQueue]; 142 self->_session.sessionDescription = @"JAHPQNSURLSessionDemux"; 143 } 144 return self; 145 } 146 147 - (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request delegate:(id<NSURLSessionDataDelegate>)delegate modes:(NSArray *)modes 148 { 149 NSURLSessionDataTask * task; 150 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 151 152 assert(request != nil); 153 assert(delegate != nil); 154 // modes may be nil 155 156 if ([modes count] == 0) { 157 modes = @[ NSDefaultRunLoopMode ]; 158 } 159 160 task = [self.session dataTaskWithRequest:request]; 161 assert(task != nil); 162 163 taskInfo = [[JAHPQNSURLSessionDemuxTaskInfo alloc] initWithTask:task delegate:delegate modes:modes]; 164 165 @synchronized (self) { 166 self.taskInfoByTaskID[@(task.taskIdentifier)] = taskInfo; 167 } 168 169 return task; 170 } 171 172 - (JAHPQNSURLSessionDemuxTaskInfo *)taskInfoForTask:(NSURLSessionTask *)task 173 { 174 // rdar://21484589 175 // This is called from each NSURLSessionTaskDelegate 176 // method implemented by JAHPQNSURLSessionDemux, 177 // which are called from the NSURLSession delegateQueue, 178 // which is a different thread than self.clientThread. 179 // It is possible that -stopLoading was called on self.clientThread 180 // just before this method if so, we cannot make the 181 // assertion (task != nil). 182 183 // rdar://24042545 184 // URLSession:task:didCompleteWithError: should always 185 // be the last NSURLSessionTaskDelegate method called. 186 // Although, it is possible that this is not always 187 // the case. Therefore we cannot make the assertion 188 // (self.taskInfoByTaskID[@(task.taskIdentifier)] != nil) 189 // because [self.taskInfoByTaskID removeObjectForKey:@(taskInfo.task.taskIdentifier)] 190 // is called in URLSession:task:didCompleteWithError:. 191 JAHPQNSURLSessionDemuxTaskInfo * result; 192 193 @synchronized (self) { 194 result = self.taskInfoByTaskID[@(task.taskIdentifier)]; 195 } 196 return result; 197 } 198 199 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)newRequest completionHandler:(void (^)(NSURLRequest *))completionHandler 200 { 201 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 202 203 taskInfo = [self taskInfoForTask:task]; 204 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:)]) { 205 [taskInfo performBlock:^{ 206 [taskInfo.delegate URLSession:session task:task willPerformHTTPRedirection:response newRequest:newRequest completionHandler:completionHandler]; 207 }]; 208 } else { 209 completionHandler(newRequest); 210 } 211 } 212 213 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler 214 { 215 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 216 217 taskInfo = [self taskInfoForTask:task]; 218 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:task:didReceiveChallenge:completionHandler:)]) { 219 [taskInfo performBlock:^{ 220 [taskInfo.delegate URLSession:session task:task didReceiveChallenge:challenge completionHandler:completionHandler]; 221 }]; 222 } else { 223 completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil); 224 } 225 } 226 227 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler 228 { 229 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 230 231 taskInfo = [self taskInfoForTask:task]; 232 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:task:needNewBodyStream:)]) { 233 [taskInfo performBlock:^{ 234 [taskInfo.delegate URLSession:session task:task needNewBodyStream:completionHandler]; 235 }]; 236 } else { 237 completionHandler(nil); 238 } 239 } 240 241 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend 242 { 243 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 244 245 taskInfo = [self taskInfoForTask:task]; 246 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)]) { 247 [taskInfo performBlock:^{ 248 [taskInfo.delegate URLSession:session task:task didSendBodyData:bytesSent totalBytesSent:totalBytesSent totalBytesExpectedToSend:totalBytesExpectedToSend]; 249 }]; 250 } 251 } 252 253 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error 254 { 255 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 256 257 taskInfo = [self taskInfoForTask:task]; 258 259 // This is our last delegate callback so we remove our task info record. 260 261 @synchronized (self) { 262 [self.taskInfoByTaskID removeObjectForKey:@(taskInfo.task.taskIdentifier)]; 263 } 264 265 // Call the delegate if required. In that case we invalidate the task info on the client thread 266 // after calling the delegate, otherwise the client thread side of the -performBlock: code can 267 // find itself with an invalidated task info. 268 269 if ([taskInfo.delegate respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]) { 270 [taskInfo performBlock:^{ 271 [taskInfo.delegate URLSession:session task:task didCompleteWithError:error]; 272 [taskInfo invalidate]; 273 }]; 274 } else { 275 [taskInfo invalidate]; 276 } 277 } 278 279 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler 280 { 281 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 282 283 taskInfo = [self taskInfoForTask:dataTask]; 284 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:dataTask:didReceiveResponse:completionHandler:)]) { 285 [taskInfo performBlock:^{ 286 [taskInfo.delegate URLSession:session dataTask:dataTask didReceiveResponse:response completionHandler:completionHandler]; 287 }]; 288 } else { 289 completionHandler(NSURLSessionResponseAllow); 290 } 291 } 292 293 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask 294 { 295 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 296 297 taskInfo = [self taskInfoForTask:dataTask]; 298 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:dataTask:didBecomeDownloadTask:)]) { 299 [taskInfo performBlock:^{ 300 [taskInfo.delegate URLSession:session dataTask:dataTask didBecomeDownloadTask:downloadTask]; 301 }]; 302 } 303 } 304 305 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data 306 { 307 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 308 309 taskInfo = [self taskInfoForTask:dataTask]; 310 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:dataTask:didReceiveData:)]) { 311 [taskInfo performBlock:^{ 312 [taskInfo.delegate URLSession:session dataTask:dataTask didReceiveData:data]; 313 }]; 314 } 315 } 316 317 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask willCacheResponse:(NSCachedURLResponse *)proposedResponse completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler 318 { 319 JAHPQNSURLSessionDemuxTaskInfo * taskInfo; 320 321 taskInfo = [self taskInfoForTask:dataTask]; 322 if (taskInfo && [taskInfo.delegate respondsToSelector:@selector(URLSession:dataTask:willCacheResponse:completionHandler:)]) { 323 [taskInfo performBlock:^{ 324 [taskInfo.delegate URLSession:session dataTask:dataTask willCacheResponse:proposedResponse completionHandler:completionHandler]; 325 }]; 326 } else { 327 completionHandler(proposedResponse); 328 } 329 } 330 331 @end 332