Skip to content

Commit 7ddbc60

Browse files
Update request.ios.ts to use request IDs and NSHTTPURLResponse
Agent-Logs-Url: https://github.com/nativescript-community/https/sessions/b7e39983-090a-48a9-b8d2-1f08682e72e0 Co-authored-by: farfromrefug <655344+farfromrefug@users.noreply.github.com>
1 parent 83c2306 commit 7ddbc60

1 file changed

Lines changed: 42 additions & 30 deletions

File tree

src/https/request.ios.ts

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,22 @@ class HttpsResponseLegacy implements IHttpsResponseLegacy {
366366
}
367367
}
368368

369-
function AFFailure(resolve, reject, task: NSURLSessionTask, error: NSError, useLegacy: boolean, url) {
369+
function AFFailure(resolve, reject, httpResponse: NSHTTPURLResponse, error: NSError, useLegacy: boolean, url) {
370370
if (error.code === -999) {
371371
return reject(error);
372372
}
373373
let getHeaders = () => ({});
374374
const sendi = {
375-
task,
376-
contentLength: task?.countOfBytesReceived,
375+
httpResponse,
376+
contentLength: httpResponse?.expectedContentLength ?? 0,
377377
reason: error.localizedDescription,
378378
get headers() {
379379
return getHeaders();
380380
}
381381
} as any as HttpsResponse;
382-
const response = error.userInfo.valueForKey(AFNetworkingOperationFailingURLResponseErrorKey) as NSHTTPURLResponse;
382+
383+
// Try to get response from error or use the one passed in
384+
const response = httpResponse || (error.userInfo.valueForKey(AFNetworkingOperationFailingURLResponseErrorKey) as NSHTTPURLResponse);
383385
if (!Utils.isNullOrUndefined(response)) {
384386
sendi.statusCode = response.statusCode;
385387
getHeaders = function () {
@@ -445,17 +447,18 @@ function bodyToNative(cont) {
445447
return dict;
446448
}
447449

448-
const runningRequests: { [k: string]: NSURLSessionTask } = {};
450+
const runningRequests: { [k: string]: string } = {}; // Maps tag to request ID
449451

450452
export function cancelRequest(tag: string) {
451-
if (runningRequests[tag]) {
452-
runningRequests[tag].cancel();
453+
const requestId = runningRequests[tag];
454+
if (requestId) {
455+
manager.cancelRequest(requestId);
453456
}
454457
}
455458

456459
export function cancelAllRequests() {
457-
Object.values(runningRequests).forEach((request) => {
458-
request.cancel();
460+
Object.values(runningRequests).forEach((requestId) => {
461+
manager.cancelRequest(requestId);
459462
});
460463
}
461464

@@ -524,8 +527,9 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
524527
}
525528
}
526529
: null;
527-
let task: NSURLSessionTask;
528530
const tag = opts.tag;
531+
// Generate request ID for tracking
532+
const requestId = tag || `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
529533

530534
function clearRunningRequest() {
531535
if (tag) {
@@ -534,14 +538,18 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
534538
}
535539
return {
536540
get nativeRequest() {
537-
return task;
541+
return null; // We no longer expose the task
542+
},
543+
cancel: () => {
544+
const rid = runningRequests[tag];
545+
if (rid) {
546+
manager.cancelRequest(rid);
547+
}
538548
},
539-
cancel: () => task && task.cancel(),
540549
run(resolve, reject) {
541-
const success = function (task: NSURLSessionTask, data?: any) {
550+
const success = function (response: NSHTTPURLResponse, data?: any) {
542551
clearRunningRequest();
543-
// TODO: refactor this code with failure one.
544-
const contentLength = task?.countOfBytesReceived;
552+
const contentLength = response?.expectedContentLength ?? 0;
545553
console.log('run done', contentLength);
546554
const content = useLegacy ? new HttpsResponseLegacy(data, contentLength, opts.url) : getData(data);
547555
let getHeaders = () => ({});
@@ -553,7 +561,6 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
553561
}
554562
} as any as HttpsResponse;
555563

556-
const response = task.response as NSHTTPURLResponse;
557564
if (!Utils.isNullOrUndefined(response)) {
558565
sendi.statusCode = response.statusCode;
559566
getHeaders = function () {
@@ -572,18 +579,22 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
572579
// sendi.reason = AFResponse.reason;
573580
// }
574581
};
575-
const failure = function (task: NSURLSessionTask, error: any) {
582+
const failure = function (response: NSHTTPURLResponse, error: any) {
576583
clearRunningRequest();
577-
AFFailure(resolve, reject, task, error, useLegacy, opts.url);
584+
AFFailure(resolve, reject, response, error, useLegacy, opts.url);
578585
};
579586
if (type.startsWith('multipart/form-data')) {
580587
switch (opts.method) {
581588
case 'POST':
582589
// we need to remove the Content-Type or the boundary wont be set correctly
583590
headers.removeObjectForKey('Content-Type');
584-
task = manager.uploadMultipart(
591+
if (tag) {
592+
runningRequests[tag] = requestId;
593+
}
594+
manager.uploadMultipart(
585595
opts.url,
586596
headers,
597+
requestId,
587598
(formData) => {
588599
(opts.body as HttpsFormDataParam[]).forEach((param) => {
589600
if (param.fileName && param.contentType) {
@@ -682,17 +693,20 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
682693

683694
if (useConditionalDownload) {
684695
// Use conditional download: check size and decide memory vs file
685-
task = manager.requestWithConditionalDownload(
696+
if (tag) {
697+
runningRequests[tag] = requestId;
698+
}
699+
manager.requestWithConditionalDownload(
686700
opts.method,
687701
opts.url,
688702
dict,
689703
headers,
704+
requestId,
690705
sizeThreshold,
691706
progress,
692-
(dataTask: NSURLSessionTask, responseData: any, tempFilePath: string) => {
707+
(httpResponse: NSHTTPURLResponse, responseData: any, tempFilePath: string) => {
693708
clearRunningRequest();
694709

695-
const httpResponse = dataTask.response as NSHTTPURLResponse;
696710
const contentLength = httpResponse?.expectedContentLength || 0;
697711

698712
// If we got a temp file path, response was saved to file (large)
@@ -726,12 +740,11 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
726740
}
727741
resolve(sendi);
728742
},
729-
(dataTask: NSURLSessionTask, error: NSError) => {
743+
(httpResponse: NSHTTPURLResponse, error: NSError) => {
730744
clearRunningRequest();
731-
failure(dataTask, error);
745+
failure(httpResponse, error);
732746
}
733747
);
734-
task.resume();
735748
} else if (earlyResolve) {
736749
// Use early resolution: resolve when headers arrive, continue download in background
737750
let downloadCompletionResolve: () => void;
@@ -848,13 +861,12 @@ export function createRequest(opts: HttpsRequestOptions, useLegacy: boolean = tr
848861
}
849862
} else {
850863
// For non-GET requests, use regular request (loads into memory)
851-
task = manager.request(opts.method, opts.url, dict, headers, progress, progress, success, failure);
852-
task.resume();
864+
if (tag) {
865+
runningRequests[tag] = requestId;
866+
}
867+
manager.request(opts.method, opts.url, dict, headers, requestId, progress, progress, success, failure);
853868
}
854869
}
855-
if (task && tag) {
856-
runningRequests[tag] = task;
857-
}
858870
}
859871
};
860872
}

0 commit comments

Comments
 (0)