Przeglądaj źródła

Add response info to HTTP request so that JS context can get response headers and status

Ben Hsieh 8 lat temu
rodzic
commit
72dfbf0c06

+ 43
- 3
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Wyświetl plik

@@ -12,11 +12,13 @@ import android.util.Log;
12 12
 
13 13
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
14 14
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15
+import com.facebook.react.bridge.Arguments;
15 16
 import com.facebook.react.bridge.Callback;
16 17
 import com.facebook.react.bridge.ReactApplicationContext;
17 18
 import com.facebook.react.bridge.ReadableArray;
18 19
 import com.facebook.react.bridge.ReadableMap;
19 20
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
21
+import com.facebook.react.bridge.WritableMap;
20 22
 
21 23
 import java.io.ByteArrayInputStream;
22 24
 import java.io.File;
@@ -28,6 +30,7 @@ import java.net.URL;
28 30
 import java.util.HashMap;
29 31
 
30 32
 import okhttp3.Call;
33
+import okhttp3.Headers;
31 34
 import okhttp3.Interceptor;
32 35
 import okhttp3.MediaType;
33 36
 import okhttp3.OkHttpClient;
@@ -36,6 +39,7 @@ import okhttp3.RequestBody;
36 39
 import okhttp3.Response;
37 40
 import okhttp3.ResponseBody;
38 41
 import okhttp3.FormBody;
42
+import okhttp3.internal.framed.Header;
39 43
 
40 44
 /**
41 45
  * Created by wkh237 on 2016/6/21.
@@ -308,7 +312,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
308 312
             case KeepInMemory:
309 313
                 try {
310 314
                     byte [] b = resp.body().bytes();
311
-                    callback.invoke(null, android.util.Base64.encodeToString(b,Base64.NO_WRAP));
315
+                    callback.invoke(null, getResponseInfo(resp), android.util.Base64.encodeToString(b,Base64.NO_WRAP));
312 316
                 } catch (IOException e) {
313 317
                     callback.invoke("RNFetchBlob failed to encode response data to BASE64 string.", null);
314 318
                 }
@@ -319,11 +323,11 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
319 323
                 } catch (Exception ignored) {
320 324
 
321 325
                 }
322
-                callback.invoke(null, this.destPath);
326
+                callback.invoke(null, getResponseInfo(resp), this.destPath);
323 327
                 break;
324 328
             default:
325 329
                 try {
326
-                    callback.invoke(null, new String(resp.body().bytes(), "UTF-8"));
330
+                    callback.invoke(null, getResponseInfo(resp), new String(resp.body().bytes(), "UTF-8"));
327 331
                 } catch (IOException e) {
328 332
                     callback.invoke("RNFetchBlob failed to encode response data to UTF8 string.", null);
329 333
                 }
@@ -333,6 +337,42 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
333 337
             taskTable.remove(taskId);
334 338
     }
335 339
 
340
+    private WritableMap getResponseInfo(Response resp) {
341
+        WritableMap info = Arguments.createMap();
342
+        info.putInt("status", resp.code());
343
+        info.putString("state", "2");
344
+        info.putString("taskId", this.taskId);
345
+        WritableMap headers = Arguments.createMap();
346
+        for(int i =0;i< resp.headers().size();i++) {
347
+            headers.putString(resp.headers().name(i), resp.headers().value(i));
348
+        }
349
+        info.putMap("headers", headers);
350
+        Headers h = resp.headers();
351
+        if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("text/plain"))
352
+        {
353
+            info.putString("respType", "text");
354
+        }
355
+        else if(getHeaderIgnoreCases(h, "content-type").equalsIgnoreCase("application/json"))
356
+        {
357
+            info.putString("respType", "json");
358
+        }
359
+        else if(getHeaderIgnoreCases(h, "content-type").length() < 1)
360
+        {
361
+            info.putString("respType", "blob");
362
+        }
363
+        else
364
+        {
365
+            info.putString("respType", "text");
366
+        }
367
+        return info;
368
+    }
369
+
370
+    private String getHeaderIgnoreCases(Headers headers, String field) {
371
+        String val = headers.get(field);
372
+        if(val != null) return val;
373
+        return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
374
+    }
375
+
336 376
     /**
337 377
      * Build request body by given string
338 378
      * @param body Content of request body in UTF8 string format.

+ 22
- 9
src/ios/RNFetchBlobNetwork.m Wyświetl plik

@@ -30,6 +30,7 @@ NSMutableDictionary * taskTable;
30 30
     NSString * destPath;
31 31
     NSOutputStream * writeStream;
32 32
     long bodyLength;
33
+    NSMutableDictionary * respInfo;
33 34
 }
34 35
 
35 36
 @end
@@ -134,6 +135,8 @@ NSOperationQueue *taskQueue;
134 135
 
135 136
         if(path != nil)
136 137
             destPath = path;
138
+        else
139
+            destPath = [RNFetchBlobFS getTempPath:cacheKey withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
137 140
     }
138 141
     else
139 142
     {
@@ -186,15 +189,16 @@ NSOperationQueue *taskQueue;
186 189
         }
187 190
         else
188 191
             respType = @"";
192
+        respInfo = @{
193
+                     @"taskId": taskId,
194
+                     @"state": @"2",
195
+                     @"headers": headers,
196
+                     @"respType" : respType,
197
+                     @"status": [NSString stringWithFormat:@"%d", statusCode ]
198
+                     };
189 199
         [self.bridge.eventDispatcher
190 200
          sendDeviceEventWithName: EVENT_STATE_CHANGE
191
-         body:@{
192
-                @"taskId": taskId,
193
-                @"state": @"2",
194
-                @"headers": headers,
195
-                @"respType" : respType,
196
-                @"status": [NSString stringWithFormat:@"%d", statusCode ]
197
-            }
201
+         body:respInfo
198 202
          ];
199 203
     }
200 204
 
@@ -241,15 +245,24 @@ NSOperationQueue *taskQueue;
241 245
     NSLog([error localizedDescription]);
242 246
     self.error = error;
243 247
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
248
+    
249
+    NSString * respType = [respInfo valueForKey:@"respType"];
250
+    
244 251
     if(respFile == YES)
245 252
     {
246 253
         [writeStream close];
247
-        callback(@[error == nil ? [NSNull null] : [error localizedDescription], destPath]);
254
+        callback(@[error == nil ? [NSNull null] : [error localizedDescription],
255
+                   respInfo == nil ? [NSNull null] : respInfo,
256
+                   destPath
257
+                   ]);
248 258
     }
249 259
     // base64 response
250 260
     else {
251 261
         NSString * res = [[NSString alloc] initWithData:respData encoding:NSUTF8StringEncoding];
252
-        callback(@[error == nil ? [NSNull null] : [error localizedDescription], [respData base64EncodedStringWithOptions:0]]);
262
+        callback(@[error == nil ? [NSNull null] : [error localizedDescription],
263
+                   respInfo == nil ? [NSNull null] : respInfo,
264
+                   [respData base64EncodedStringWithOptions:0]
265
+                   ]);
253 266
     }
254 267
 }
255 268