Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit af3c86a

Browse files
committed
#29 add IOS implementation for self-signed server
1 parent 90f6579 commit af3c86a

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

src/ios/RNFetchBlobConst.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern NSString *const FILE_PREFIX;
2222
extern NSString *const CONFIG_USE_TEMP;
2323
extern NSString *const CONFIG_FILE_PATH;
2424
extern NSString *const CONFIG_FILE_EXT;
25+
extern NSString *const CONFIG_TRUSTY;
2526

2627
// fs events
2728
extern NSString *const FS_EVENT_DATA;

src/ios/RNFetchBlobConst.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
extern NSString *const CONFIG_USE_TEMP = @"fileCache";
1414
extern NSString *const CONFIG_FILE_PATH = @"path";
1515
extern NSString *const CONFIG_FILE_EXT = @"appendExt";
16+
extern NSString *const CONFIG_TRUSTY = @"trusty";
17+
1618

1719
extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
1820
extern NSString *const MSG_EVENT_LOG = @"log";

src/ios/RNFetchBlobNetwork.h

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,29 @@
1212
#import <Foundation/Foundation.h>
1313
#import "RCTBridgeModule.h"
1414

15-
@interface RNFetchBlobNetwork : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate> {
16-
17-
NSString * taskId;
18-
int expectedBytes;
19-
int receivedBytes;
20-
NSMutableData * respData;
21-
RCTResponseSenderBlock callback;
22-
RCTBridge * bridge;
23-
NSDictionary * options;
24-
RNFetchBlobFS * fileStream;
25-
}
26-
@property (nonatomic) NSString * taskId;
15+
typedef void(^CompletionHander)(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error);
16+
typedef void(^DataTaskCompletionHander) (NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error);
17+
18+
@interface RNFetchBlobNetwork : NSObject <NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate, UIApplicationDelegate>
19+
20+
@property (nullable, nonatomic) NSString * taskId;
2721
@property (nonatomic) int expectedBytes;
2822
@property (nonatomic) int receivedBytes;
29-
@property (nonatomic) NSMutableData * respData;
30-
@property (nonatomic) RCTResponseSenderBlock callback;
31-
@property (nonatomic) RCTBridge * bridge;
32-
@property (nonatomic) NSDictionary * options;
33-
@property (nonatomic) RNFetchBlobFS * fileStream;
23+
@property (nullable, nonatomic) NSMutableData * respData;
24+
@property (nullable, nonatomic) RCTResponseSenderBlock callback;
25+
@property (nullable, nonatomic) RCTBridge * bridge;
26+
@property (nullable, nonatomic) NSDictionary * options;
27+
@property (nullable, nonatomic) RNFetchBlobFS * fileStream;
28+
@property (nullable, nonatomic) CompletionHander fileTaskCompletionHandler;
29+
@property (nullable, nonatomic) DataTaskCompletionHander dataTaskCompletionHandler;
30+
@property (nullable, nonatomic) NSError * error;
3431

3532

36-
- (id) init;
33+
- (nullable id) init;
3734
- (void) sendRequest;
3835

39-
+ (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers;
40-
- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback;
36+
+ (NSMutableDictionary * _Nullable ) normalizeHeaders:(NSDictionary * _Nullable)headers;
37+
- (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback;
4138

4239

4340
@end

src/ios/RNFetchBlobNetwork.m

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
@implementation RNFetchBlobNetwork
2525

26+
NSOperationQueue *taskQueue;
2627

2728
@synthesize taskId;
2829
@synthesize expectedBytes;
@@ -31,10 +32,17 @@ @implementation RNFetchBlobNetwork
3132
@synthesize callback;
3233
@synthesize bridge;
3334
@synthesize options;
35+
@synthesize fileTaskCompletionHandler;
36+
@synthesize dataTaskCompletionHandler;
37+
@synthesize error;
38+
3439

3540
// constructor
3641
- (id)init {
3742
self = [super init];
43+
if(taskQueue == nil) {
44+
taskQueue = [[NSOperationQueue alloc] init];
45+
}
3846
return self;
3947
}
4048

@@ -51,7 +59,8 @@ + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers {
5159
}
5260

5361
// send HTTP request
54-
- (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskId:(NSString *)taskId withRequest:(NSURLRequest *)req callback:(RCTResponseSenderBlock) callback {
62+
- (void) sendRequest:(NSDictionary * _Nullable )options bridge:(RCTBridge * _Nullable)bridgeRef taskId:(NSString * _Nullable)taskId withRequest:(NSURLRequest * _Nullable)req callback:(_Nullable RCTResponseSenderBlock) callback
63+
{
5564
self.taskId = taskId;
5665
self.respData = [[NSMutableData alloc] initWithLength:0];
5766
self.callback = callback;
@@ -62,12 +71,15 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
6271

6372
NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
6473
NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
65-
66-
NSURLSession * session = [NSURLSession sharedSession];
74+
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
75+
NSURLSession * session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:taskQueue];
76+
77+
// NSURLSession * session = [NSURLSession sharedSession];
6778

6879
// file will be stored at a specific path
6980
if( path != nil) {
70-
NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
81+
82+
self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
7183
if(error != nil) {
7284
callback(@[[error localizedDescription]]);
7385
return;
@@ -81,12 +93,14 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
8193
return;
8294
}
8395
callback(@[[NSNull null], path]);
84-
}];
96+
};
97+
NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
8598
[task resume];
8699
}
87100
// file will be stored at tmp path
88101
else if ( [self.options valueForKey:CONFIG_USE_TEMP]!= nil ) {
89-
NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
102+
103+
self.fileTaskCompletionHandler = ^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
90104
if(error != nil) {
91105
callback(@[[error localizedDescription]]);
92106
return;
@@ -101,22 +115,22 @@ - (void) sendRequest:(NSDictionary *)options bridge:(RCTBridge *)bridgeRef taskI
101115
return;
102116
}
103117
callback(@[[NSNull null], tmpPath]);
104-
}];
118+
};
119+
NSURLSessionDownloadTask * task = [session downloadTaskWithRequest:req completionHandler:fileTaskCompletionHandler];
105120
[task resume];
106121
}
107122
// base64 response
108123
else {
109-
NSURLSessionUploadTask * task =
110-
111-
[session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
124+
self.dataTaskCompletionHandler = ^(NSData * _Nullable resp, NSURLResponse * _Nullable response, NSError * _Nullable error) {
112125
if(error != nil) {
113126
callback(@[[error localizedDescription]]);
114127
return;
115128
}
116129
else {
117130
callback(@[[NSNull null], [resp base64EncodedStringWithOptions:0]]);
118131
}
119-
}];
132+
};
133+
NSURLSessionDataTask * task = [session dataTaskWithRequest:req completionHandler:dataTaskCompletionHandler];
120134
[task resume];
121135
}
122136
}
@@ -160,6 +174,7 @@ - (void) URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dat
160174

161175
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
162176
NSLog([error localizedDescription]);
177+
self.error = error;
163178
}
164179

165180
// upload progress handler
@@ -177,4 +192,28 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSen
177192
];
178193
}
179194

195+
- (void) application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
196+
197+
}
198+
199+
- (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session
200+
{
201+
if(self.dataTaskCompletionHandler != nil)
202+
{
203+
dataTaskCompletionHandler(self.respData, nil, error);
204+
}
205+
else if(self.fileTaskCompletionHandler != nil)
206+
{
207+
fileTaskCompletionHandler(nil, nil, self.error);
208+
}
209+
}
210+
211+
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
212+
{
213+
if([options valueForKey:CONFIG_TRUSTY] == YES)
214+
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
215+
else
216+
RCTLogError(@"counld not create connection with an unstrusted SSL certification, if you're going to create connection anyway, add `trusty:true` to RNFetchBlob.config");
217+
}
218+
180219
@end

0 commit comments

Comments
 (0)