Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit fe2f9d2

Browse files
committed
Add new option followRedirect to fetch request and related test case #230
1 parent ac8398d commit fe2f9d2

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class RNFetchBlobConfig {
1919
public Boolean overwrite = true;
2020
public long timeout = 60000;
2121
public Boolean increment = false;
22+
public Boolean followRedirect = true;
2223
public ReadableArray binaryContentTypes = null;
2324

2425
RNFetchBlobConfig(ReadableMap options) {
@@ -36,9 +37,11 @@ public class RNFetchBlobConfig {
3637
if(this.path != null && path.toLowerCase().contains("?append=true")) {
3738
this.overwrite = false;
3839
}
39-
4040
if(options.hasKey("overwrite"))
4141
this.overwrite = options.getBoolean("overwrite");
42+
if(options.hasKey("followRedirect")) {
43+
this.followRedirect = options.getBoolean("followRedirect");
44+
}
4245
this.key = options.hasKey("key") ? options.getString("key") : null;
4346
this.mime = options.hasKey("contentType") ? options.getString("contentType") : null;
4447
this.increment = options.hasKey("increment") ? options.getBoolean("increment") : false;

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ public Response intercept(Chain chain) throws IOException {
362362

363363
clientBuilder.connectionPool(pool);
364364
clientBuilder.retryOnConnectionFailure(false);
365-
clientBuilder.followRedirects(true);
365+
clientBuilder.followRedirects(options.followRedirect);
366+
clientBuilder.followSslRedirects(options.followRedirect);
366367

367368

368369
OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();

src/ios/RNFetchBlobNetwork.m

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ @interface RNFetchBlobNetwork ()
8686
NSInteger respStatus;
8787
NSMutableArray * redirects;
8888
ResponseFormat responseFormat;
89+
BOOL * followRedirect;
8990
}
9091

9192
@end
@@ -214,6 +215,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
214215
self.expectedBytes = 0;
215216
self.receivedBytes = 0;
216217
self.options = options;
218+
followRedirect = [options valueForKey:@"followRedirect"] == nil ? YES : [[options valueForKey:@"followRedirect"] boolValue];
217219
isIncrement = [options valueForKey:@"increment"] == nil ? NO : [[options valueForKey:@"increment"] boolValue];
218220
redirects = [[NSMutableArray alloc] init];
219221
if(req.URL != nil)
@@ -236,8 +238,15 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
236238
bodyLength = contentLength;
237239

238240
// the session trust any SSL certification
239-
// NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
240-
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
241+
NSURLSessionConfiguration *defaultConfigObject;
242+
if(!followRedirect)
243+
{
244+
defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
245+
}
246+
else
247+
{
248+
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:taskId];
249+
}
241250

242251
// set request timeout
243252
float timeout = [options valueForKey:@"timeout"] == nil ? -1 : [[options valueForKey:@"timeout"] floatValue];
@@ -275,7 +284,7 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
275284
respData = [[NSMutableData alloc] init];
276285
respFile = NO;
277286
}
278-
287+
279288
__block NSURLSessionDataTask * task = [session dataTaskWithRequest:req];
280289
[taskTable setObject:task forKey:taskId];
281290
[task resume];
@@ -284,15 +293,14 @@ - (void) sendRequest:(__weak NSDictionary * _Nullable )options
284293
if([[options objectForKey:CONFIG_INDICATOR] boolValue] == YES)
285294
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
286295
__block UIApplication * app = [UIApplication sharedApplication];
287-
296+
288297
// #115 handling task expired when application entering backgound for a long time
289298
UIBackgroundTaskIdentifier tid = [app beginBackgroundTaskWithName:taskId expirationHandler:^{
290299
NSLog([NSString stringWithFormat:@"session %@ expired", taskId ]);
291300
[expirationTable setObject:task forKey:taskId];
292301
[app endBackgroundTask:tid];
293302
}];
294303

295-
296304
}
297305

298306
// #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
@@ -575,7 +583,7 @@ - (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCom
575583
respStr = [respData base64EncodedStringWithOptions:0];
576584
}
577585
}
578-
}
586+
}
579587

580588

581589
callback(@[ errMsg, rnfbRespType, respStr]);
@@ -644,9 +652,17 @@ - (void) URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)sessio
644652

645653
- (void) URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler
646654
{
647-
if(request.URL != nil)
648-
[redirects addObject:[request.URL absoluteString]];
649-
completionHandler(request);
655+
656+
if(followRedirect)
657+
{
658+
if(request.URL != nil)
659+
[redirects addObject:[request.URL absoluteString]];
660+
completionHandler(request);
661+
}
662+
else
663+
{
664+
completionHandler(nil);
665+
}
650666
}
651667

652668
@end

test/test-0.10.2.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,16 @@ describe('#227 IOS file modification date correctness', (report, done) => {
4848
})
4949

5050
})
51+
52+
describe('#230 add and option for setting if the request follow redirect or not', (report, done) => {
53+
54+
RNFetchBlob
55+
.config({ followRedirect : false })
56+
.fetch('GET',`${TEST_SERVER_URL}/redirect`)
57+
.then((res) => {
58+
console.log(res.data)
59+
report(<Assert key="should not redirect twice" expect={1} actual={res.info().redirects.length}/>);
60+
done();
61+
})
62+
63+
})

0 commit comments

Comments
 (0)