mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 18:25:06 +00:00
Use NSURLComponents.queryItems instead of parsing query strings
Reviewed By: mmmulani Differential Revision: D3742617 fbshipit-source-id: 2d6580919fa546d89266943a917165fbc44aa8e8
This commit is contained in:
parent
07553d0f1c
commit
6abacc893b
@ -741,17 +741,15 @@ NSString *__nullable RCTGetURLQueryParam(NSURL *__nullable URL, NSString *param)
|
|||||||
if (!URL) {
|
if (!URL) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSURLComponents *components = [NSURLComponents componentsWithURL:URL
|
NSURLComponents *components = [NSURLComponents componentsWithURL:URL
|
||||||
resolvingAgainstBaseURL:YES];
|
resolvingAgainstBaseURL:YES];
|
||||||
|
for (NSURLQueryItem *queryItem in [components.queryItems reverseObjectEnumerator]) {
|
||||||
// TODO: use NSURLComponents.queryItems once we drop support for iOS 7
|
if ([queryItem.name isEqualToString:param]) {
|
||||||
for (NSString *item in [components.percentEncodedQuery componentsSeparatedByString:@"&"].reverseObjectEnumerator) {
|
return queryItem.value;
|
||||||
NSArray *keyValue = [item componentsSeparatedByString:@"="];
|
|
||||||
NSString *key = [keyValue.firstObject stringByRemovingPercentEncoding];
|
|
||||||
if ([key isEqualToString:param]) {
|
|
||||||
return [keyValue.lastObject stringByRemovingPercentEncoding];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -761,30 +759,15 @@ NSURL *__nullable RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *p
|
|||||||
if (!URL) {
|
if (!URL) {
|
||||||
return nil;
|
return nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSURLComponents *components = [NSURLComponents componentsWithURL:URL
|
NSURLComponents *components = [NSURLComponents componentsWithURL:URL
|
||||||
resolvingAgainstBaseURL:YES];
|
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;
|
__block NSInteger paramIndex = NSNotFound;
|
||||||
NSMutableArray *queryItems = [[components.percentEncodedQuery componentsSeparatedByString:@"&"] mutableCopy];
|
NSMutableArray<NSURLQueryItem *> *queryItems = [components.queryItems mutableCopy];
|
||||||
[queryItems enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:
|
[queryItems enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:
|
||||||
^(NSString *item, NSUInteger i, BOOL *stop) {
|
^(NSURLQueryItem *item, NSUInteger i, BOOL *stop) {
|
||||||
NSArray *keyValue = [item componentsSeparatedByString:@"="];
|
if ([item.name isEqualToString:param]) {
|
||||||
if ([keyValue.firstObject isEqualToString:encodedParam]) {
|
|
||||||
paramIndex = i;
|
paramIndex = i;
|
||||||
*stop = YES;
|
*stop = YES;
|
||||||
}
|
}
|
||||||
@ -795,16 +778,14 @@ NSURL *__nullable RCTURLByReplacingQueryParam(NSURL *__nullable URL, NSString *p
|
|||||||
[queryItems removeObjectAtIndex:paramIndex];
|
[queryItems removeObjectAtIndex:paramIndex];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
NSString *encodedValue =
|
NSURLQueryItem *newItem = [NSURLQueryItem queryItemWithName:param
|
||||||
[value stringByAddingPercentEncodingWithAllowedCharacters:URLParamCharacterSet];
|
value:value];
|
||||||
|
|
||||||
NSString *newItem = [encodedParam stringByAppendingFormat:@"=%@", encodedValue];
|
|
||||||
if (paramIndex == NSNotFound) {
|
if (paramIndex == NSNotFound) {
|
||||||
[queryItems addObject:newItem];
|
[queryItems addObject:newItem];
|
||||||
} else {
|
} else {
|
||||||
[queryItems replaceObjectAtIndex:paramIndex withObject:newItem];
|
[queryItems replaceObjectAtIndex:paramIndex withObject:newItem];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
components.percentEncodedQuery = [queryItems componentsJoinedByString:@"&"];
|
components.queryItems = queryItems;
|
||||||
return components.URL;
|
return components.URL;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user