11//
22// RNFetchBlob.m
33//
4- // Created by Ben Hsieh on 2016/4/28.
4+ // Created by suzuri04x2 on 2016/4/28.
55// Copyright © 2016年 Facebook. All rights reserved.
66//
77
1010#import " RCTLog.h"
1111#import < Foundation/Foundation.h>
1212
13- // CalendarManager.m
13+
14+ // //////////////////////////////////////
15+ //
16+ // Util functions
17+ //
18+ // //////////////////////////////////////
19+
20+ @implementation FetchBlobUtils
21+
22+ // callback class method to handle request
23+ + (void ) onBlobResponse : (NSURLResponse * _Nullable)response withData : (NSData * _Nullable)data withError : (NSError * _Nullable)connectionError withCallback : (RCTResponseSenderBlock)callback {
24+
25+ NSHTTPURLResponse * resp = (NSHTTPURLResponse *) response;
26+ NSString * status = [NSString stringWithFormat: @" %d " , resp.statusCode];
27+
28+ if (connectionError)
29+ {
30+ callback (@[[connectionError localizedDescription ], [NSNull null ]]);
31+ }
32+ else if (![status isEqualToString: @" 200" ]) {
33+ callback (@[status, [NSNull null ]]);
34+ }
35+ else {
36+ callback (@[[NSNull null ], [data base64EncodedStringWithOptions: 0 ]]);
37+ }
38+
39+ }
40+
41+ // removing case of headers
42+ + (NSMutableDictionary *) normalizeHeaders : (NSDictionary *)headers {
43+
44+ NSMutableDictionary * mheaders = [[NSMutableDictionary alloc ]init];
45+ for (NSString * key in headers) {
46+ [mheaders setValue: [headers valueForKey: key] forKey: [key lowercaseString ]];
47+ }
48+
49+ return mheaders;
50+ }
51+
52+ @end
53+
54+
55+ // //////////////////////////////////////
56+ //
57+ // Exported native methods
58+ //
59+ // //////////////////////////////////////
60+
1461@implementation RNFetchBlob
1562
1663RCT_EXPORT_MODULE ();
1764
1865// Fetch blob data request
1966RCT_EXPORT_METHOD (fetchBlobForm:(NSString *)method url:(NSString *)url headers:(NSDictionary *)headers form:(NSArray *)form callback:(RCTResponseSenderBlock)callback)
2067{
68+
2169 // send request
2270 NSMutableURLRequest *request = [[NSMutableURLRequest alloc ]
2371 initWithURL: [NSURL
2472 URLWithString: url]];
73+ NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [ FetchBlobUtils normalizeHeaders: headers]];
2574
26- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] init ];
27-
28- // make headers case insensitive
29- for (NSString * key in headers) {
30- [mheaders setValue: [headers valueForKey: key] forKey: [key lowercaseString ]];
31- }
3275
3376 NSTimeInterval timeStamp = [[NSDate date ] timeIntervalSince1970 ];
3477 NSNumber * timeStampObj = [NSNumber numberWithDouble: timeStamp];
@@ -40,7 +83,7 @@ @implementation RNFetchBlob
4083 if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
4184 NSMutableData * postData = [[NSMutableData alloc ] init ];
4285
43- // combine body
86+ // combine multipart/form-data body
4487 for (id field in form) {
4588 NSString * name = [field valueForKey: @" name" ];
4689 NSString * content = [field valueForKey: @" data" ];
@@ -81,19 +124,7 @@ @implementation RNFetchBlob
81124 NSOperationQueue *queue = [[NSOperationQueue alloc ] init ];
82125 [NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
83126
84- NSHTTPURLResponse * resp = (NSHTTPURLResponse *) response;
85- NSString * status = [NSString stringWithFormat: @" %d " , resp.statusCode];
86-
87- if (connectionError)
88- {
89- callback (@[[connectionError localizedDescription ], [NSNull null ]]);
90- }
91- else if (![status isEqualToString: @" 200" ]) {
92- callback (@[status, [NSNull null ]]);
93- }
94- else {
95- callback (@[[NSNull null ], [data base64EncodedStringWithOptions: 0 ]]);
96- }
127+ [FetchBlobUtils onBlobResponse: response withData: data withError: connectionError withCallback: callback];
97128
98129 }];
99130
@@ -107,16 +138,12 @@ @implementation RNFetchBlob
107138 initWithURL: [NSURL
108139 URLWithString: url]];
109140
110- NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] init ];
111-
112- // make headers case insensitive
113- for (NSString * key in headers) {
114- [mheaders setValue: [headers valueForKey: key] forKey: [key lowercaseString ]];
115- }
141+ NSMutableDictionary *mheaders = [[NSMutableDictionary alloc ] initWithDictionary: [FetchBlobUtils normalizeHeaders: headers]];
116142
117143 // if method is POST or PUT, convert data string format
118144 if ([[method lowercaseString ] isEqualToString: @" post" ] || [[method lowercaseString ] isEqualToString: @" put" ]) {
119145
146+ // generate octet-stream body
120147 NSData * blobData = [[NSData alloc ] initWithBase64EncodedString: body options: 0 ];
121148 NSMutableData * postBody = [[NSMutableData alloc ] init ];
122149 [postBody appendData: [NSData dataWithData: blobData]];
@@ -132,23 +159,10 @@ @implementation RNFetchBlob
132159 NSOperationQueue *queue = [[NSOperationQueue alloc ] init ];
133160 [NSURLConnection sendAsynchronousRequest: request queue: queue completionHandler: ^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
134161
135- NSHTTPURLResponse * resp = (NSHTTPURLResponse *) response;
136- NSString * status = [NSString stringWithFormat: @" %d " , resp.statusCode];
137-
138- if (connectionError)
139- {
140- callback (@[[connectionError localizedDescription ], [NSNull null ]]);
141- }
142- else if (![status isEqualToString: @" 200" ]) {
143- callback (@[status, [NSNull null ]]);
144- }
145- else {
146- callback (@[[NSNull null ], [data base64EncodedStringWithOptions: 0 ]]);
147- }
162+ [FetchBlobUtils onBlobResponse: response withData: data withError: connectionError withCallback: callback];
148163
149164 }];
150165
151166}
167+ @end
152168
153-
154- @end
0 commit comments