Browse Source

Fix content type encoding suffix stripping issue #293

Ben Hsieh 7 years ago
parent
commit
90e6b4ad3b
1 changed files with 10 additions and 30 deletions
  1. 10
    30
      android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+ 10
- 30
android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

@@ -220,8 +220,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
220 220
                             responseFormat = ResponseFormat.UTF8;
221 221
                     }
222 222
                     else {
223
-                        builder.header(key, value);
224
-                        mheaders.put(key, value);
223
+                        builder.header(key.toLowerCase(), value);
224
+                        mheaders.put(key.toLowerCase(), value);
225 225
                     }
226 226
                 }
227 227
             }
@@ -304,9 +304,9 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
304 304
             clientBuilder.addNetworkInterceptor(new Interceptor() {
305 305
                 @Override
306 306
                 public Response intercept(Chain chain) throws IOException {
307
-                        redirects.add(chain.request().url().toString());
308
-                        return chain.proceed(chain.request());
309
-                    }
307
+                    redirects.add(chain.request().url().toString());
308
+                    return chain.proceed(chain.request());
309
+                }
310 310
             });
311 311
             // Add request interceptor for upload progress event
312 312
             clientBuilder.addInterceptor(new Interceptor() {
@@ -500,32 +500,11 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
500 500
                     // It uses customized response body which is able to report download progress
501 501
                     // and write response data to destination path.
502 502
                     resp.body().bytes();
503
-
504 503
                 } catch (Exception ignored) {
505 504
 //                    ignored.printStackTrace();
506 505
                 }
507 506
                 this.destPath = this.destPath.replace("?append=true", "");
508
-
509
-                try {
510
-                    long expectedLength = resp.body().contentLength();
511
-                    // when response contains Content-Length, check if the stream length is correct
512
-                    if(expectedLength > 0) {
513
-                        long actualLength = new File(this.destPath).length();
514
-                        if(actualLength != expectedLength) {
515
-                            callback.invoke("RNFetchBlob failed to write data to storage : expected " + expectedLength + " bytes but got " + actualLength + " bytes", null);
516
-                        }
517
-                        else {
518
-                            callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
519
-                        }
520
-                    }
521
-                    else {
522
-                        callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
523
-                    }
524
-                }
525
-                catch (Exception err) {
526
-                    callback.invoke(err.getMessage());
527
-                    err.printStackTrace();
528
-                }
507
+                callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, this.destPath);
529 508
                 break;
530 509
             default:
531 510
                 try {
@@ -536,7 +515,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
536 515
                 break;
537 516
         }
538 517
 //        if(!resp.isSuccessful())
539
-            resp.body().close();
518
+        resp.body().close();
540 519
         releaseTaskResource();
541 520
     }
542 521
 
@@ -578,7 +557,7 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
578 557
         }
579 558
         WritableArray redirectList = Arguments.createArray();
580 559
         for(String r : redirects) {
581
-                redirectList.pushString(r);
560
+            redirectList.pushString(r);
582 561
         }
583 562
         info.putArray("redirects", redirectList);
584 563
         info.putMap("headers", headers);
@@ -629,7 +608,8 @@ public class RNFetchBlobReq extends BroadcastReceiver implements Runnable {
629 608
     private String getHeaderIgnoreCases(HashMap<String,String> headers, String field) {
630 609
         String val = headers.get(field);
631 610
         if(val != null) return val;
632
-        return headers.get(field.toLowerCase()) == null ? "" : headers.get(field.toLowerCase());
611
+        String lowerCasedValue = headers.get(field.toLowerCase());
612
+        return lowerCasedValue == null ? "" : lowerCasedValue;
633 613
     }
634 614
 
635 615
     private void emitStateEvent(WritableMap args) {