From 5723915eaacfd3d8a8a7165c550f1fb9316c15ad Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Tue, 3 May 2016 23:41:26 -0700 Subject: [PATCH] Remove all null values for request headers Summary: NSMutableURLRequest appears to be running `[headerValue count]` on every value of the NSDictionary that `allHTTPHeaderFields` is set to, meaning that any `null` values passed to headers in an XHR throw an exception. This differs from the implementation of XHR in browsers, which just ignore any `null` values for headers. PR doesn't currently include tests, happy to add those if this is something you'd consider merging Closes https://github.com/facebook/react-native/pull/6903 Differential Revision: D3256727 fb-gh-sync-id: 58e8db9c86a1fe7fbd95cba7dcf916957d179e0c fbshipit-source-id: 58e8db9c86a1fe7fbd95cba7dcf916957d179e0c --- Libraries/Network/RCTNetworking.m | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Libraries/Network/RCTNetworking.m b/Libraries/Network/RCTNetworking.m index 07b128fd9..27f7e05f5 100644 --- a/Libraries/Network/RCTNetworking.m +++ b/Libraries/Network/RCTNetworking.m @@ -190,6 +190,19 @@ RCT_EXPORT_MODULE() return nil; } +- (NSDictionary *)stripNullsInRequestHeaders:(NSDictionary *)headers +{ + NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:headers.count]; + for (NSString *key in headers.allKeys) { + id val = headers[key]; + if (val != [NSNull null]) { + result[key] = val; + } + } + + return result; +} + - (RCTURLRequestCancellationBlock)buildRequest:(NSDictionary *)query completionBlock:(void (^)(NSURLRequest *request))block { @@ -198,7 +211,7 @@ RCT_EXPORT_MODULE() NSURL *URL = [RCTConvert NSURL:query[@"url"]]; // this is marked as nullable in JS, but should not be null NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; request.HTTPMethod = [RCTConvert NSString:RCTNilIfNull(query[@"method"])].uppercaseString ?: @"GET"; - request.allHTTPHeaderFields = [RCTConvert NSDictionary:query[@"headers"]]; + request.allHTTPHeaderFields = [self stripNullsInRequestHeaders:[RCTConvert NSDictionary:query[@"headers"]]]; request.timeoutInterval = [RCTConvert NSTimeInterval:query[@"timeout"]]; NSDictionary *data = [RCTConvert NSDictionary:RCTNilIfNull(query[@"data"])]; return [self processDataForHTTPQuery:data callback:^(NSError *error, NSDictionary *result) {