Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-7635] check nil parameter in network breadcrumbs #1230

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,36 @@ + (nonnull NSString *)URLStringWithoutQueryForComponents:(nonnull NSURLComponent
- (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;

NSString *message = [BSGURLSessionTracingDelegate messageForResponse:httpResp];
NSURLRequest *req = task.originalRequest ? task.originalRequest : task.currentRequest;
if (req == nil) {
return;
}

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];
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) {
// Fall back because task.countOfBytesSent is 0 when a custom NSURLProtocol is used
metadata[@"requestContentLength"] = @(req.HTTPBody.length);
}

// 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;
if (httpResp) {
metadata[@"responseContentLength"] = @(task.countOfBytesReceived);
metadata[@"status"] = @(httpResp.statusCode);
}

NSString *message = [BSGURLSessionTracingDelegate messageForResponse:httpResp];
[g_sink leaveBreadcrumbWithMessage:message metadata:metadata andType:BSGBreadcrumbTypeRequest];
}
}
Expand Down