2016-10-11 19:14:37 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-10-11 19:14:37 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTMultipartDataTask.h"
|
|
|
|
|
|
|
|
@interface RCTMultipartDataTask () <NSURLSessionDataDelegate, NSURLSessionDataDelegate>
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// We need this ugly runtime check because [streamTask captureStreams] below fails on iOS version
|
|
|
|
// earlier than 9.0. Unfortunately none of the proper ways of checking worked:
|
|
|
|
//
|
|
|
|
// - NSURLSessionStreamTask class is available and is not Null on iOS 8
|
|
|
|
// - [[NSURLSessionStreamTask new] respondsToSelector:@selector(captureStreams)] is always NO
|
|
|
|
// - The instance we get in URLSession:dataTask:didBecomeStreamTask: is of __NSCFURLLocalStreamTaskFromDataTask
|
|
|
|
// and it responds to captureStreams on iOS 9+ but doesn't on iOS 8. Which means we can't get direct access
|
|
|
|
// to the streams on iOS 8 and at that point it's too late to change the behavior back to dataTask
|
|
|
|
// - The compile-time #ifdef's can't be used because an app compiled for iOS8 can still run on iOS9
|
|
|
|
|
|
|
|
static BOOL isStreamTaskSupported() {
|
|
|
|
return [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9,0,0}];
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation RCTMultipartDataTask {
|
|
|
|
NSURL *_url;
|
|
|
|
RCTMultipartDataTaskCallback _partHandler;
|
2017-08-14 17:54:33 +00:00
|
|
|
RCTMultipartProgressCallback _progressHandler;
|
2016-10-11 19:14:37 +00:00
|
|
|
NSInteger _statusCode;
|
|
|
|
NSDictionary *_headers;
|
|
|
|
NSString *_boundary;
|
|
|
|
NSMutableData *_data;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:54:33 +00:00
|
|
|
- (instancetype)initWithURL:(NSURL *)url
|
|
|
|
partHandler:(RCTMultipartDataTaskCallback)partHandler
|
|
|
|
progressHandler:(RCTMultipartProgressCallback)progressHandler
|
2016-10-11 19:14:37 +00:00
|
|
|
{
|
|
|
|
if (self = [super init]) {
|
|
|
|
_url = url;
|
|
|
|
_partHandler = [partHandler copy];
|
2017-08-14 17:54:33 +00:00
|
|
|
_progressHandler = [progressHandler copy];
|
2016-10-11 19:14:37 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startTask
|
|
|
|
{
|
|
|
|
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
|
|
|
|
delegate:self delegateQueue:nil];
|
|
|
|
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
|
|
|
|
if (isStreamTaskSupported()) {
|
|
|
|
[request addValue:@"multipart/mixed" forHTTPHeaderField:@"Accept"];
|
|
|
|
}
|
|
|
|
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
|
|
|
|
[dataTask resume];
|
2017-04-20 10:33:04 +00:00
|
|
|
[session finishTasksAndInvalidate];
|
2016-10-11 19:14:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 11:17:43 +00:00
|
|
|
- (void)URLSession:(__unused NSURLSession *)session
|
|
|
|
dataTask:(__unused NSURLSessionDataTask *)dataTask
|
|
|
|
didReceiveResponse:(NSURLResponse *)response
|
|
|
|
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
|
2016-10-11 19:14:37 +00:00
|
|
|
{
|
|
|
|
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
|
|
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
|
|
|
|
_headers = [httpResponse allHeaderFields];
|
|
|
|
_statusCode = [httpResponse statusCode];
|
|
|
|
|
|
|
|
NSString *contentType = @"";
|
|
|
|
for (NSString *key in [_headers keyEnumerator]) {
|
|
|
|
if ([[key lowercaseString] isEqualToString:@"content-type"]) {
|
|
|
|
contentType = [_headers valueForKey:key];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"multipart/mixed;.*boundary=\"([^\"]+)\"" options:0 error:nil];
|
|
|
|
NSTextCheckingResult *match = [regex firstMatchInString:contentType options:0 range:NSMakeRange(0, contentType.length)];
|
|
|
|
if (match) {
|
|
|
|
_boundary = [contentType substringWithRange:[match rangeAtIndex:1]];
|
|
|
|
completionHandler(NSURLSessionResponseBecomeStream);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In case the server doesn't support multipart/mixed responses, fallback to normal download
|
|
|
|
_data = [[NSMutableData alloc] initWithCapacity:1024 * 1024];
|
|
|
|
completionHandler(NSURLSessionResponseAllow);
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:17:43 +00:00
|
|
|
- (void)URLSession:(__unused NSURLSession *)session task:(__unused NSURLSessionTask *)task didCompleteWithError:(NSError *)error
|
2016-10-11 19:14:37 +00:00
|
|
|
{
|
2017-04-20 10:33:04 +00:00
|
|
|
if (_partHandler) {
|
|
|
|
_partHandler(_statusCode, _headers, _data, error, YES);
|
|
|
|
}
|
2016-10-11 19:14:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-27 11:17:43 +00:00
|
|
|
- (void)URLSession:(__unused NSURLSession *)session dataTask:(__unused NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
|
2016-10-11 19:14:37 +00:00
|
|
|
{
|
|
|
|
[_data appendData:data];
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:17:43 +00:00
|
|
|
- (void)URLSession:(__unused NSURLSession *)session dataTask:(__unused NSURLSessionDataTask *)dataTask didBecomeStreamTask:(NSURLSessionStreamTask *)streamTask
|
2016-10-11 19:14:37 +00:00
|
|
|
{
|
|
|
|
[streamTask captureStreams];
|
|
|
|
}
|
|
|
|
|
2016-10-27 11:17:43 +00:00
|
|
|
- (void)URLSession:(__unused NSURLSession *)session
|
|
|
|
streamTask:(__unused NSURLSessionStreamTask *)streamTask
|
|
|
|
didBecomeInputStream:(NSInputStream *)inputStream
|
|
|
|
outputStream:(__unused NSOutputStream *)outputStream
|
2016-10-11 19:14:37 +00:00
|
|
|
{
|
|
|
|
RCTMultipartStreamReader *reader = [[RCTMultipartStreamReader alloc] initWithInputStream:inputStream boundary:_boundary];
|
|
|
|
RCTMultipartDataTaskCallback partHandler = _partHandler;
|
2017-04-20 10:33:04 +00:00
|
|
|
_partHandler = nil;
|
2016-10-11 19:14:37 +00:00
|
|
|
NSInteger statusCode = _statusCode;
|
|
|
|
|
2017-08-14 17:54:33 +00:00
|
|
|
BOOL completed = [reader readAllPartsWithCompletionCallback:^(NSDictionary *headers, NSData *content, BOOL done) {
|
2016-10-11 19:14:37 +00:00
|
|
|
partHandler(statusCode, headers, content, nil, done);
|
2017-08-14 17:54:33 +00:00
|
|
|
} progressCallback:_progressHandler];
|
2016-10-11 19:14:37 +00:00
|
|
|
if (!completed) {
|
|
|
|
partHandler(statusCode, nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil], YES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|