Browse Source

Merge branch '0.9.5-issue-122' into 0.9.5

Ben Hsieh 8 years ago
parent
commit
b3b62c1ad3

+ 28
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

63
         FileStorage
63
         FileStorage
64
     }
64
     }
65
 
65
 
66
+    enum ResponseFormat {
67
+        Auto,
68
+        UTF8,
69
+        BASE64
70
+    }
71
+
66
     public static HashMap<String, Call> taskTable = new HashMap<>();
72
     public static HashMap<String, Call> taskTable = new HashMap<>();
67
     static HashMap<String, Boolean> progressReport = new HashMap<>();
73
     static HashMap<String, Boolean> progressReport = new HashMap<>();
68
     static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
74
     static HashMap<String, Boolean> uploadProgressReport = new HashMap<>();
83
     RNFetchBlobBody requestBody;
89
     RNFetchBlobBody requestBody;
84
     RequestType requestType;
90
     RequestType requestType;
85
     ResponseType responseType;
91
     ResponseType responseType;
92
+    ResponseFormat responseFormat = ResponseFormat.Auto;
86
     WritableMap respInfo;
93
     WritableMap respInfo;
87
     boolean timeout = false;
94
     boolean timeout = false;
95
+
88
     ArrayList<String> redirects = new ArrayList<>();
96
     ArrayList<String> redirects = new ArrayList<>();
89
 
97
 
90
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
98
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
200
                 while (it.hasNextKey()) {
208
                 while (it.hasNextKey()) {
201
                     String key = it.nextKey();
209
                     String key = it.nextKey();
202
                     String value = headers.getString(key);
210
                     String value = headers.getString(key);
203
-                    builder.header(key, value);
204
-                    mheaders.put(key,value);
211
+                    if(key.equalsIgnoreCase("RNFB-Response")) {
212
+                        if(value.equalsIgnoreCase("base64"))
213
+                            responseFormat = ResponseFormat.BASE64;
214
+                        else if (value.equalsIgnoreCase("utf8"))
215
+                            responseFormat = ResponseFormat.UTF8;
216
+                    }
217
+                    else {
218
+                        builder.header(key, value);
219
+                        mheaders.put(key, value);
220
+                    }
205
                 }
221
                 }
206
             }
222
             }
207
 
223
 
437
                         // string correctly, we should do URL encoding before BASE64.
453
                         // string correctly, we should do URL encoding before BASE64.
438
                         byte[] b = resp.body().bytes();
454
                         byte[] b = resp.body().bytes();
439
                         CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
455
                         CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
456
+                        if(responseFormat == ResponseFormat.BASE64) {
457
+                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
458
+                            return;
459
+                        }
440
                         try {
460
                         try {
441
                             encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
461
                             encoder.encode(ByteBuffer.wrap(b).asCharBuffer());
442
                             // if the data contains invalid characters the following lines will be
462
                             // if the data contains invalid characters the following lines will be
447
                         // This usually mean the data is contains invalid unicode characters, it's
467
                         // This usually mean the data is contains invalid unicode characters, it's
448
                         // binary data
468
                         // binary data
449
                         catch(CharacterCodingException ignored) {
469
                         catch(CharacterCodingException ignored) {
450
-                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
470
+                            if(responseFormat == ResponseFormat.UTF8) {
471
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_UTF8, "");
472
+                            }
473
+                            else {
474
+                                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_BASE64, android.util.Base64.encodeToString(b, Base64.NO_WRAP));
475
+                            }
451
                         }
476
                         }
452
                     }
477
                     }
453
                 } catch (IOException e) {
478
                 } catch (IOException e) {

+ 33
- 5
src/ios/RNFetchBlobNetwork.m View File

27
 NSMutableDictionary * progressTable;
27
 NSMutableDictionary * progressTable;
28
 NSMutableDictionary * uploadProgressTable;
28
 NSMutableDictionary * uploadProgressTable;
29
 
29
 
30
+typedef NS_ENUM(NSUInteger, ResponseFormat) {
31
+    UTF8,
32
+    BASE64,
33
+    AUTO
34
+};
35
+
30
 
36
 
31
 @interface RNFetchBlobNetwork ()
37
 @interface RNFetchBlobNetwork ()
32
 {
38
 {
37
     NSMutableDictionary * respInfo;
43
     NSMutableDictionary * respInfo;
38
     NSInteger respStatus;
44
     NSInteger respStatus;
39
     NSMutableArray * redirects;
45
     NSMutableArray * redirects;
46
+    ResponseFormat responseFormat;
40
 }
47
 }
41
 
48
 
42
 @end
49
 @end
128
     self.options = options;
135
     self.options = options;
129
     redirects = [[NSMutableArray alloc] init];
136
     redirects = [[NSMutableArray alloc] init];
130
     [redirects addObject:req.URL.absoluteString];
137
     [redirects addObject:req.URL.absoluteString];
138
+    
139
+    // set response format
140
+    NSString * rnfbResp = [req.allHTTPHeaderFields valueForKey:@"RNFB-Response"];
141
+    if([[rnfbResp lowercaseString] isEqualToString:@"base64"])
142
+        responseFormat = BASE64;
143
+    else if([[rnfbResp lowercaseString] isEqualToString:@"utf8"])
144
+        responseFormat = UTF8;
145
+    else
146
+        responseFormat = AUTO;
131
 
147
 
132
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
148
     NSString * path = [self.options valueForKey:CONFIG_FILE_PATH];
133
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
149
     NSString * ext = [self.options valueForKey:CONFIG_FILE_EXT];
357
             // if it turns out not to be `nil` that means the response data contains valid UTF8 string,
373
             // if it turns out not to be `nil` that means the response data contains valid UTF8 string,
358
             // in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
374
             // in order to properly encode the UTF8 string, use URL encoding before BASE64 encoding.
359
             NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
375
             NSString * utf8 = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
360
-
361
-            if(utf8 != nil)
376
+            
377
+            if(responseFormat == BASE64)
378
+            {
379
+                rnfbRespType = RESP_TYPE_BASE64;
380
+                respStr = [respData base64EncodedStringWithOptions:0];
381
+            }
382
+            else if (responseFormat == UTF8)
362
             {
383
             {
363
                 rnfbRespType = RESP_TYPE_UTF8;
384
                 rnfbRespType = RESP_TYPE_UTF8;
364
                 respStr = utf8;
385
                 respStr = utf8;
365
             }
386
             }
366
             else
387
             else
367
             {
388
             {
368
-                rnfbRespType = RESP_TYPE_BASE64;
369
-                respStr = [respData base64EncodedStringWithOptions:0];
389
+                if(utf8 != nil)
390
+                {
391
+                    rnfbRespType = RESP_TYPE_UTF8;
392
+                    respStr = utf8;
393
+                }
394
+                else
395
+                {
396
+                    rnfbRespType = RESP_TYPE_BASE64;
397
+                    respStr = [respData base64EncodedStringWithOptions:0];
398
+                }
370
             }
399
             }
371
-
372
         }
400
         }
373
     }
401
     }
374
 
402