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
                             responseFormat = ResponseFormat.UTF8;
220
                             responseFormat = ResponseFormat.UTF8;
221
                     }
221
                     }
222
                     else {
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
             clientBuilder.addNetworkInterceptor(new Interceptor() {
304
             clientBuilder.addNetworkInterceptor(new Interceptor() {
305
                 @Override
305
                 @Override
306
                 public Response intercept(Chain chain) throws IOException {
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
             // Add request interceptor for upload progress event
311
             // Add request interceptor for upload progress event
312
             clientBuilder.addInterceptor(new Interceptor() {
312
             clientBuilder.addInterceptor(new Interceptor() {
500
                     // It uses customized response body which is able to report download progress
500
                     // It uses customized response body which is able to report download progress
501
                     // and write response data to destination path.
501
                     // and write response data to destination path.
502
                     resp.body().bytes();
502
                     resp.body().bytes();
503
-
504
                 } catch (Exception ignored) {
503
                 } catch (Exception ignored) {
505
 //                    ignored.printStackTrace();
504
 //                    ignored.printStackTrace();
506
                 }
505
                 }
507
                 this.destPath = this.destPath.replace("?append=true", "");
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
                 break;
508
                 break;
530
             default:
509
             default:
531
                 try {
510
                 try {
536
                 break;
515
                 break;
537
         }
516
         }
538
 //        if(!resp.isSuccessful())
517
 //        if(!resp.isSuccessful())
539
-            resp.body().close();
518
+        resp.body().close();
540
         releaseTaskResource();
519
         releaseTaskResource();
541
     }
520
     }
542
 
521
 
578
         }
557
         }
579
         WritableArray redirectList = Arguments.createArray();
558
         WritableArray redirectList = Arguments.createArray();
580
         for(String r : redirects) {
559
         for(String r : redirects) {
581
-                redirectList.pushString(r);
560
+            redirectList.pushString(r);
582
         }
561
         }
583
         info.putArray("redirects", redirectList);
562
         info.putArray("redirects", redirectList);
584
         info.putMap("headers", headers);
563
         info.putMap("headers", headers);
629
     private String getHeaderIgnoreCases(HashMap<String,String> headers, String field) {
608
     private String getHeaderIgnoreCases(HashMap<String,String> headers, String field) {
630
         String val = headers.get(field);
609
         String val = headers.get(field);
631
         if(val != null) return val;
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
     private void emitStateEvent(WritableMap args) {
615
     private void emitStateEvent(WritableMap args) {