1717import com .facebook .react .bridge .ReadableArray ;
1818import com .facebook .react .bridge .ReadableMap ;
1919import com .facebook .react .bridge .ReadableMapKeySetIterator ;
20- import com .facebook .react .bridge .WritableArray ;
2120import com .facebook .react .bridge .WritableMap ;
2221import com .facebook .react .modules .core .DeviceEventManagerModule ;
2322
3332import java .nio .charset .CharacterCodingException ;
3433import java .nio .charset .Charset ;
3534import java .nio .charset .CharsetEncoder ;
36- import java .util .ArrayList ;
3735import java .util .HashMap ;
3836import java .util .concurrent .TimeUnit ;
3937
@@ -74,7 +72,6 @@ enum ResponseType {
7472
7573 ReactApplicationContext ctx ;
7674 RNFetchBlobConfig options ;
77- ArrayList <String > redirects = new ArrayList <>();
7875 String taskId ;
7976 String method ;
8077 String url ;
@@ -107,7 +104,7 @@ public RNFetchBlobReq(ReadableMap options, String taskId, String method, String
107104 responseType = ResponseType .KeepInMemory ;
108105
109106
110- if (body != null )
107+ if (body != null )
111108 requestType = RequestType .SingleFile ;
112109 else if (arrayBody != null )
113110 requestType = RequestType .Form ;
@@ -159,21 +156,21 @@ public void run() {
159156
160157 // find cached result if `key` property exists
161158 String cacheKey = this .taskId ;
162- String ext = this .options .appendExt .isEmpty () ? "." + this .options .appendExt : "" ;
159+ String ext = this .options .appendExt .isEmpty () ? "." + this .options .appendExt : "" ;
163160
164- if (this .options .key != null ) {
165- cacheKey = RNFetchBlobUtils .getMD5 (this .options .key );
166- if (cacheKey == null ) {
167- cacheKey = this .taskId ;
168- }
161+ if (this .options .key != null ) {
162+ cacheKey = RNFetchBlobUtils .getMD5 (this .options .key );
163+ if (cacheKey == null ) {
164+ cacheKey = this .taskId ;
165+ }
169166
170- File file = new File (RNFetchBlobFS .getTmpPath (RNFetchBlob .RCTContext , cacheKey ) + ext );
167+ File file = new File (RNFetchBlobFS .getTmpPath (RNFetchBlob .RCTContext , cacheKey ) + ext );
171168
172- if (file .exists ()) {
173- callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , file .getAbsolutePath ());
174- return ;
175- }
176- }
169+ if (file .exists ()) {
170+ callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , file .getAbsolutePath ());
171+ return ;
172+ }
173+ }
177174
178175 if (this .options .path != null )
179176 this .destPath = this .options .path ;
@@ -239,35 +236,33 @@ else if(cType.isEmpty()) {
239236 requestType = RequestType .WithoutBody ;
240237 }
241238
239+ boolean isChunkedRequest = getHeaderIgnoreCases (mheaders , "Transfer-Encoding" ).equalsIgnoreCase ("chunked" );
242240
243241 // set request body
244242 switch (requestType ) {
245243 case SingleFile :
246- requestBody = new RNFetchBlobBody (
247- taskId ,
248- requestType ,
249- rawRequestBody ,
250- MediaType .parse (getHeaderIgnoreCases (mheaders , "content-type" ))
251- );
244+ requestBody = new RNFetchBlobBody (taskId )
245+ .chunkedEncoding (isChunkedRequest )
246+ .setRequestType (requestType )
247+ .setBody (rawRequestBody )
248+ .setMIME (MediaType .parse (getHeaderIgnoreCases (mheaders , "content-type" )));
252249 builder .method (method , requestBody );
253250 break ;
254251 case AsIs :
255- requestBody = new RNFetchBlobBody (
256- taskId ,
257- requestType ,
258- rawRequestBody ,
259- MediaType .parse (getHeaderIgnoreCases (mheaders , "content-type" ))
260- );
252+ requestBody = new RNFetchBlobBody (taskId )
253+ .chunkedEncoding (isChunkedRequest )
254+ .setRequestType (requestType )
255+ .setBody (rawRequestBody )
256+ .setMIME (MediaType .parse (getHeaderIgnoreCases (mheaders , "content-type" )));
261257 builder .method (method , requestBody );
262258 break ;
263259 case Form :
264260 String boundary = "RNFetchBlob-" + taskId ;
265- requestBody = new RNFetchBlobBody (
266- taskId ,
267- requestType ,
268- rawRequestBodyArray ,
269- MediaType .parse ("multipart/form-data; boundary=" + boundary )
270- );
261+ requestBody = new RNFetchBlobBody (taskId )
262+ .chunkedEncoding (isChunkedRequest )
263+ .setRequestType (requestType )
264+ .setBody (rawRequestBodyArray )
265+ .setMIME (MediaType .parse ("multipart/form-data; boundary=" + boundary ));
271266 builder .method (method , requestBody );
272267 break ;
273268
@@ -283,15 +278,6 @@ else if(cType.isEmpty()) {
283278
284279 final Request req = builder .build ();
285280
286- // intercept network redirections
287- clientBuilder .addNetworkInterceptor (new Interceptor () {
288- @ Override
289- public Response intercept (Chain chain ) throws IOException {
290- redirects .add (chain .request ().url ().toString ());
291- return chain .proceed (chain .request ());
292- }
293- });
294-
295281 // Add request interceptor for upload progress event
296282 clientBuilder .addInterceptor (new Interceptor () {
297283 @ Override
@@ -468,7 +454,9 @@ private void done(Response resp) {
468454 // It uses customized response body which is able to report download progress
469455 // and write response data to destination path.
470456 resp .body ().bytes ();
471- } catch (Exception ignored ) { }
457+ } catch (Exception ignored ) {
458+ ignored .printStackTrace ();
459+ }
472460 callback .invoke (null , RNFetchBlobConst .RNFB_RESPONSE_PATH , this .destPath );
473461 break ;
474462 default :
@@ -521,11 +509,6 @@ private WritableMap getResponseInfo(Response resp, boolean isBlobResp) {
521509 headers .putString (resp .headers ().name (i ), resp .headers ().value (i ));
522510 }
523511 info .putMap ("headers" , headers );
524- WritableArray redirectList = Arguments .createArray ();
525- for (String r : redirects ) {
526- redirectList .pushString (r );
527- }
528- info .putArray ("redirects" , redirectList );
529512 Headers h = resp .headers ();
530513 if (isBlobResp ) {
531514 info .putString ("respType" , "blob" );
0 commit comments