Procházet zdrojové kódy

Merge branch '0.10.7' of github.com:wkh237/react-native-fetch-blob into 0.10.7

Ben Hsieh před 7 roky
rodič
revize
5f3c018b0a

+ 33
- 1
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java Zobrazit soubor

@@ -7,10 +7,12 @@ import android.content.Intent;
7 7
 import android.content.IntentFilter;
8 8
 import android.database.Cursor;
9 9
 import android.net.Uri;
10
+import android.os.Build;
10 11
 import android.util.Base64;
11 12
 
12 13
 import com.RNFetchBlob.Response.RNFetchBlobDefaultResp;
13 14
 import com.RNFetchBlob.Response.RNFetchBlobFileResp;
15
+import com.facebook.common.logging.FLog;
14 16
 import com.facebook.react.bridge.Arguments;
15 17
 import com.facebook.react.bridge.Callback;
16 18
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -21,6 +23,7 @@ import com.facebook.react.bridge.WritableArray;
21 23
 import com.facebook.react.bridge.WritableMap;
22 24
 import com.facebook.react.modules.core.DeviceEventManagerModule;
23 25
 import com.facebook.react.modules.network.OkHttpClientProvider;
26
+import com.facebook.react.modules.network.TLSSocketFactory;
24 27
 
25 28
 import java.io.File;
26 29
 import java.io.FileOutputStream;
@@ -35,11 +38,14 @@ import java.nio.charset.CharacterCodingException;
35 38
 import java.nio.charset.Charset;
36 39
 import java.nio.charset.CharsetEncoder;
37 40
 import java.util.ArrayList;
41
+import java.util.List;
38 42
 import java.util.HashMap;
43
+
39 44
 import java.util.concurrent.TimeUnit;
40 45
 
41 46
 import okhttp3.Call;
42 47
 import okhttp3.ConnectionPool;
48
+import okhttp3.ConnectionSpec;
43 49
 import okhttp3.Headers;
44 50
 import okhttp3.Interceptor;
45 51
 import okhttp3.MediaType;
@@ -48,6 +54,8 @@ import okhttp3.Request;
48 54
 import okhttp3.RequestBody;
49 55
 import okhttp3.Response;
50 56
 import okhttp3.ResponseBody;
57
+import okhttp3.TlsVersion;
58
+
51 59
 
52 60
 public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
53 61
 
@@ -366,9 +374,10 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
366 374
             clientBuilder.retryOnConnectionFailure(false);
367 375
             clientBuilder.followRedirects(options.followRedirect);
368 376
             clientBuilder.followSslRedirects(options.followRedirect);
377
+            clientBuilder.retryOnConnectionFailure(true);
369 378
 
379
+            OkHttpClient client = enableTls12OnPreLollipop(clientBuilder).build();
370 380
 
371
-            OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
372 381
             Call call =  client.newCall(req);
373 382
             taskTable.put(taskId, call);
374 383
             call.enqueue(new okhttp3.Callback() {
@@ -683,5 +692,28 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
683 692
         }
684 693
     }
685 694
 
695
+    public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
696
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
697
+            try {
698
+                client.sslSocketFactory(new TLSSocketFactory());
699
+
700
+                ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
701
+                        .tlsVersions(TlsVersion.TLS_1_2)
702
+                        .build();
703
+
704
+                List< ConnectionSpec > specs = new ArrayList < > ();
705
+                specs.add(cs);
706
+                specs.add(ConnectionSpec.COMPATIBLE_TLS);
707
+                specs.add(ConnectionSpec.CLEARTEXT);
708
+
709
+                client.connectionSpecs(specs);
710
+            } catch (Exception exc) {
711
+                FLog.e("OkHttpClientProvider", "Error while enabling TLS 1.2", exc);
712
+            }
713
+        }
714
+
715
+        return client;
716
+    }
717
+
686 718
 
687 719
 }

+ 5
- 5
ios/RNFetchBlobFS.m Zobrazit soubor

@@ -568,11 +568,11 @@ NSMutableDictionary *fileStreams = nil;
568 568
 
569 569
 // Write file chunk into an opened stream
570 570
 - (void)writeEncodeChunk:(NSString *) chunk {
571
-    NSMutableData * decodedData = [NSData alloc];
571
+    NSData * decodedData = nil;
572 572
     if([[self.encoding lowercaseString] isEqualToString:@"base64"]) {
573
-        decodedData = [[NSData alloc] initWithBase64EncodedData:chunk options:0];
574
-    }
575
-    if([[self.encoding lowercaseString] isEqualToString:@"utf8"]) {
573
+        decodedData = [[NSData alloc] initWithBase64EncodedString:chunk options: NSDataBase64DecodingIgnoreUnknownCharacters];
574
+    } 
575
+    else if([[self.encoding lowercaseString] isEqualToString:@"utf8"]) {
576 576
         decodedData = [chunk dataUsingEncoding:NSUTF8StringEncoding];
577 577
     }
578 578
     else if([[self.encoding lowercaseString] isEqualToString:@"ascii"]) {
@@ -793,4 +793,4 @@ NSMutableDictionary *fileStreams = nil;
793 793
     return;
794 794
 }
795 795
 
796
-@end
796
+@end

+ 16
- 0
polyfill/Blob.js Zobrazit soubor

@@ -130,6 +130,8 @@ export default class Blob extends EventTarget {
130 130
     // Blob data from file path
131 131
     else if(typeof data === 'string' && data.startsWith('RNFetchBlob-file://')) {
132 132
       log.verbose('create Blob cache file from file path', data)
133
+      // set this flag so that we know this blob is a wrapper of an existing file
134
+      this._isReference = true
133 135
       this._ref = String(data).replace('RNFetchBlob-file://', '')
134 136
       let orgPath = this._ref
135 137
       if(defer)
@@ -282,6 +284,20 @@ export default class Blob extends EventTarget {
282 284
     })
283 285
   }
284 286
 
287
+  safeClose() {
288
+    if(this._closed)
289
+      return Promise.reject('Blob has been released.)
290
+    this._closed = true
291
+    if(!this._isReference) {
292
+      return fs.unlink(this._ref).catch((err) => {
293
+        console.warn(err)
294
+      })   
295
+    }
296
+    else {
297
+      return Promise.resolve()
298
+    }
299
+  }
300
+
285 301
   _invokeOnCreateEvent() {
286 302
     log.verbose('invoke create event', this._onCreated)
287 303
     this._blobCreated = true