From 6abacc893b499979c6402f7bfa74d21a1893bb2a Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Thu, 1 Sep 2016 19:37:56 -0700 Subject: [PATCH] Use NSURLComponents.queryItems instead of parsing query strings Reviewed By: mmmulani Differential Revision: D3742617 fbshipit-source-id: 2d6580919fa546d89266943a917165fbc44aa8e8 --- React/Base/RCTUtils.m | 43 ++++++++++++------------------------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/React/Base/RCTUtils.m b/React/Base/RCTUtils.m index d4dedea5a..9f456d47d 100644 --- a/React/Base/RCTUtils.m +++ b/React/Base/RCTUtils.m @@ -741,17 +741,15 @@ NSString *__nullable RCTGetURLQueryParam(NSURL *__nullable URL, NSString *param) if (!URL) { return nil; } + NSURLComponents *components = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:YES]; - - // TODO: use NSURLComponents.queryItems once we drop support for iOS 7 - for (NSString *item in [components.percentEncodedQuery componentsSeparatedByString:@"&"].reverseObjectEnumerator) { - NSArray *keyValue = [item componentsSeparatedByString:@"="]; - NSString *key = [keyValue.firstObject stringByRemovingPercentEncoding]; - if ([key isEqualToString:param]) { - return [keyValue.lastObject stringByRemovingPercentEncoding]; + for (NSURLQueryItem *queryItem in [components.queryItems reverseObjectEnumerator]) { + if ([queryItem.name isEqualToString:param]) { + return queryItem.value; } } + return nil; } @@ -761,30 +759,15 @@ NSURL *__nullable RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *p if (!URL) { return nil; } + NSURLComponents *components = [NSURLComponents componentsWithURL:URL resolvingAgainstBaseURL:YES]; - // TODO: use NSURLComponents.queryItems once we drop support for iOS 7 - - // Unhelpfully, iOS doesn't provide this set as a constant - static NSCharacterSet *URLParamCharacterSet; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - NSMutableCharacterSet *characterSet = [NSMutableCharacterSet new]; - [characterSet formUnionWithCharacterSet:[NSCharacterSet URLQueryAllowedCharacterSet]]; - [characterSet removeCharactersInString:@"&=?"]; - URLParamCharacterSet = [characterSet copy]; - }); - - NSString *encodedParam = - [param stringByAddingPercentEncodingWithAllowedCharacters:URLParamCharacterSet]; - __block NSInteger paramIndex = NSNotFound; - NSMutableArray *queryItems = [[components.percentEncodedQuery componentsSeparatedByString:@"&"] mutableCopy]; + NSMutableArray *queryItems = [components.queryItems mutableCopy]; [queryItems enumerateObjectsWithOptions:NSEnumerationReverse usingBlock: - ^(NSString *item, NSUInteger i, BOOL *stop) { - NSArray *keyValue = [item componentsSeparatedByString:@"="]; - if ([keyValue.firstObject isEqualToString:encodedParam]) { + ^(NSURLQueryItem *item, NSUInteger i, BOOL *stop) { + if ([item.name isEqualToString:param]) { paramIndex = i; *stop = YES; } @@ -795,16 +778,14 @@ NSURL *__nullable RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *p [queryItems removeObjectAtIndex:paramIndex]; } } else { - NSString *encodedValue = - [value stringByAddingPercentEncodingWithAllowedCharacters:URLParamCharacterSet]; - - NSString *newItem = [encodedParam stringByAppendingFormat:@"=%@", encodedValue]; + NSURLQueryItem *newItem = [NSURLQueryItem queryItemWithName:param + value:value]; if (paramIndex == NSNotFound) { [queryItems addObject:newItem]; } else { [queryItems replaceObjectAtIndex:paramIndex withObject:newItem]; } } - components.percentEncodedQuery = [queryItems componentsJoinedByString:@"&"]; + components.queryItems = queryItems; return components.URL; }