Skip to content

Commit

Permalink
Verify that the parameter to +[NSURLComponents initWithURL:resolvingA…
Browse files Browse the repository at this point in the history
…gainstBaseURL:] is not nil
  • Loading branch information
kstenerud committed Nov 17, 2021
1 parent 2f8866f commit 75cb965
Showing 1 changed file with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,32 @@ + (nonnull NSString *)URLStringWithoutQueryForComponents:(nonnull NSURLComponent
return string;
}

- (NSURLRequest *)findRequestFromTask:(NSURLSessionTask *)task andMetrics:(NSURLSessionTaskMetrics *)metrics
API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)) {
NSURLRequest *req = task.originalRequest;
if (req != nil) {
return req;
}

NSArray<NSURLSessionTaskTransactionMetrics *> *transactionMetrics = metrics.transactionMetrics;
if (transactionMetrics.count > 0) {
// Try to find a phase with a request HTTP body.
for (NSUInteger i = 0; i < transactionMetrics.count; i++) {
req = transactionMetrics[i].request;
if (req.HTTPBody.length > 0) {
return req;
}
}
// Failing that, just use the last phase.
req = transactionMetrics[transactionMetrics.count - 1].request;
}

return req;
}

- (void)URLSession:(__unused NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics
API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0)) {
if (g_sink != nil) {
// Note: Cannot use metrics transaction request because it might have a 0 length HTTP body.
NSURLRequest *req = task.originalRequest;
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:req.URL resolvingAgainstBaseURL:YES];
// Note: Cannot use metrics transaction response because it will be nil if a custom NSURLProtocol is present.
// Note: If there was an error, task.response will be nil, and the following values will be set accordingly.
NSHTTPURLResponse *httpResp = [task.response isKindOfClass:NSHTTPURLResponse.class] ? (NSHTTPURLResponse *)task.response : nil;
Expand All @@ -97,9 +117,17 @@ - (void)URLSession:(__unused NSURLSession *)session task:(NSURLSessionTask *)tas

NSMutableDictionary *metadata = [NSMutableDictionary dictionary];
metadata[@"duration"] = @((unsigned)(metrics.taskInterval.duration * 1000));
metadata[@"method"] = req.HTTPMethod;
metadata[@"url"] = [BSGURLSessionTracingDelegate URLStringWithoutQueryForComponents:urlComponents];
metadata[@"urlParams"] = [BSGURLSessionTracingDelegate urlParamsForQueryItems:urlComponents.queryItems];

NSURLRequest *req = [self findRequestFromTask:task andMetrics:metrics];
if (req) {
metadata[@"method"] = req.HTTPMethod;
if (req.URL) {
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:req.URL resolvingAgainstBaseURL:YES];
metadata[@"url"] = [BSGURLSessionTracingDelegate URLStringWithoutQueryForComponents:urlComponents];
metadata[@"urlParams"] = [BSGURLSessionTracingDelegate urlParamsForQueryItems:urlComponents.queryItems];
}
}

if (task.countOfBytesSent) {
metadata[@"requestContentLength"] = @(task.countOfBytesSent);
} else if (req.HTTPBody) {
Expand Down

0 comments on commit 75cb965

Please sign in to comment.