Skip to content

Commit d821c39

Browse files
dturingenchev
authored andcommitted
add HttpRequestOptions.dontFollowRedirects (NativeScript#3473)
This implements the option to have http.request() not follow server's 3xx redirection replies but instead return the exact redirection code and headers. * on iOS, it uses a different NSURLSession instance for non-following request, with a NSURLSessionTaskDelegate implementing URLSessinTaskWillPerformHTTPRedirection... * on Android, it just passes the option on to org.nativescript.widgets.Async.Http; so this requires the respective commit in tns-core-modules-widgets to work
1 parent 7cc4830 commit d821c39

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

tns-core-modules/http/http-request.android.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ function buildJavaOptions(options: http.HttpRequestOptions) {
155155
if (types.isNumber(options.timeout)) {
156156
javaOptions.timeout = options.timeout;
157157
}
158+
if (types.isBoolean(options.dontFollowRedirects)) {
159+
javaOptions.dontFollowRedirects = options.dontFollowRedirects;
160+
}
158161

159162
if (options.headers) {
160163
var arrayList = new java.util.ArrayList<org.nativescript.widgets.Async.Http.KeyValuePair>();

tns-core-modules/http/http-request.ios.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,28 @@ var USER_AGENT_HEADER = "User-Agent";
2020
var USER_AGENT = `Mozilla/5.0 (i${device}; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25`;
2121
var sessionConfig = getter(NSURLSessionConfiguration, NSURLSessionConfiguration.defaultSessionConfiguration);
2222
var queue = getter(NSOperationQueue, NSOperationQueue.mainQueue);
23-
var session = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
23+
24+
class NSURLSessionTaskDelegateImpl extends NSObject implements NSURLSessionTaskDelegate {
25+
public static ObjCProtocols = [NSURLSessionTaskDelegate];
26+
public URLSessionTaskWillPerformHTTPRedirectionNewRequestCompletionHandler(session: NSURLSession, task: NSURLSessionTask, response: NSHTTPURLResponse, request: NSURLRequest, completionHandler: (p1: NSURLRequest) => void): void {
27+
completionHandler(null);
28+
}
29+
}
30+
var sessionTaskDelegateInstance: NSURLSessionTaskDelegateImpl = <NSURLSessionTaskDelegateImpl>NSURLSessionTaskDelegateImpl.new();
31+
32+
var defaultSession;
33+
function ensureDefaultSession() {
34+
if (!defaultSession) {
35+
defaultSession = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, null, queue);
36+
}
37+
}
38+
39+
var sessionNotFollowingRedirects;
40+
function ensureSessionNotFollowingRedirects() {
41+
if (!sessionNotFollowingRedirects) {
42+
sessionNotFollowingRedirects = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(sessionConfig, sessionTaskDelegateInstance, queue);
43+
}
44+
}
2445

2546
var imageSource: typeof imageSourceModule;
2647
function ensureImageSource() {
@@ -57,6 +78,15 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
5778
urlRequest.timeoutInterval = options.timeout / 1000;
5879
}
5980

81+
var session;
82+
if (types.isBoolean(options.dontFollowRedirects) && options.dontFollowRedirects) {
83+
ensureSessionNotFollowingRedirects();
84+
session = sessionNotFollowingRedirects;
85+
} else {
86+
ensureDefaultSession();
87+
session = defaultSession;
88+
}
89+
6090
var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
6191
function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
6292
if (error) {

tns-core-modules/http/http.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ declare module "http" {
8989
* Gets or sets the request timeout in milliseconds.
9090
*/
9191
timeout?: number;
92+
93+
/**
94+
* Gets or sets wether to *not* follow server's redirection responses.
95+
*/
96+
dontFollowRedirects?: boolean;
9297
}
9398

9499
/**

tns-platform-declarations/android/org.nativescript.widgets.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public timeout: number;
3131
public screenWidth: number;
3232
public screenHeight: number;
33+
public dontFollowRedirects: boolean;
3334
}
3435

3536
export class RequestResult {

0 commit comments

Comments
 (0)