From c0834b884bcaf2fd97a47bcb0320369b0b4469d2 Mon Sep 17 00:00:00 2001 From: malacca Date: Thu, 15 Dec 2022 08:14:38 -0800 Subject: [PATCH] fix RCTNetworking contentType issue (#35640) Summary: https://github.com/facebook/react-native/blob/96027f787ade1a465569975059426d2c03ee5ad0/Libraries/Network/RCTNetworking.mm#L418 because `RCTNullIfNil(response.MIMEType)`, `contentType` may be `[NSNull null]` https://github.com/facebook/react-native/blob/96027f787ade1a465569975059426d2c03ee5ad0/Libraries/Network/RCTNetworking.mm#L114 Here only check `partContentType` is not nil, This causes the custom type never used https://github.com/facebook/react-native/blob/96027f787ade1a465569975059426d2c03ee5ad0/Libraries/Network/RCTNetworking.mm#L334 more serious here, if `dataContentType` is `[NSNull null]` , it will crash the application `-[NSNull hasPrefix:]: unrecognized selector sent to instance 0x7fff8004b700` ## Changelog [iOS] [Fixed] - fix dataContentType may be [NSNull null] issue Pull Request resolved: https://github.com/facebook/react-native/pull/35640 Reviewed By: cipolleschi Differential Revision: D42067206 Pulled By: dmytrorykun fbshipit-source-id: 073e6589111f5117486b69b8206a07ef1995c5b8 --- Libraries/Network/RCTNetworking.mm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Libraries/Network/RCTNetworking.mm b/Libraries/Network/RCTNetworking.mm index 24c175294aed95..9b9c12711dea7c 100644 --- a/Libraries/Network/RCTNetworking.mm +++ b/Libraries/Network/RCTNetworking.mm @@ -111,7 +111,7 @@ - (RCTURLRequestCancellationBlock)handleResult:(NSDictionary *)r // Print headers. NSMutableDictionary *headers = [_parts[0][@"headers"] mutableCopy]; NSString *partContentType = result[@"contentType"]; - if (partContentType != nil) { + if (partContentType != nil && ![partContentType isEqual:[NSNull null]]) { headers[@"content-type"] = partContentType; } [headers enumerateKeysAndObjectsUsingBlock:^(NSString *parameterKey, NSString *parameterValue, BOOL *stop) { @@ -331,7 +331,8 @@ - (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary *)q request.HTTPBody = result[@"body"]; NSString *dataContentType = result[@"contentType"]; NSString *requestContentType = [request valueForHTTPHeaderField:@"Content-Type"]; - BOOL isMultipart = [dataContentType hasPrefix:@"multipart"]; + BOOL isMultipart = ![dataContentType isEqual:[NSNull null]] && + [dataContentType hasPrefix:@"multipart"]; // For multipart requests we need to override caller-specified content type with one // from the data object, because it contains the boundary string