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

Fixes a CFNetwork runtime warning #191

Merged
merged 1 commit into from
Oct 11, 2023

Conversation

jszumski
Copy link
Contributor

@jszumski jszumski commented Oct 6, 2023

Work towards #186.

In Xcode 15+ CFNetwork emits a runtime warning when an upload task contains a body:

The request of a upload task should not contain a body or a body stream, use upload(for:fromFile:),upload(for:from:), or supply the body stream through the urlSession(_:needNewBodyStreamForTask:) delegate method.

To work around this, we keep track of requests originating from upload tasks with the existing swizzling in NSURLSession+HTTPBodyFix. For those tasks, we save the original body via NSURLProtocol and remove it from the request to avoid the warning.

When using a request body (e.g., when matching stubs), previously marked upload requests must exclusively reference the copy from NSURLProtocol because the request's HTTPBody was cleared.

- (NSData *)swz_HTTPBody
{
// upload tasks will trigger a runtime warning if their body is non-nil, see note above
if ([self sbt_isUploadTaskRequest]) {
return nil;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the actual warning is avoided

if (![matcher matches:requestBody]) {

// an upload task previously stored its body contents in NSURLProtocol to avoid a CFNetwork runtime warning
NSData *body = ([request sbt_isUploadTaskRequest]) ? [request sbt_uploadHTTPBody] : request.HTTPBody;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we fetch the original body differently for upload tasks


NSString * const SBTUITunneledNSURLProtocolIsUploadTaskKey = @"SBTUITunneledNSURLProtocolIsUploadTaskKey";

- (NSData *)sbt_uploadHTTPBody
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test that covers these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests

@tcamin tcamin merged commit e0f131f into Subito-it:master Oct 11, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants