Added method to handle query encoding. Theoretically URLQueryAllowedCharacter set should cover this. I'm wondering why I had to do this to get the correct result.

This commit is contained in:
Gregory John Casamento 2020-03-30 08:53:42 -04:00
parent 31e7bbb7e6
commit 7c52cd9f72

View file

@ -100,6 +100,7 @@ NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey";
@interface NSString (NSURLPrivate)
- (NSString*) _stringByAddingPercentEscapes;
- (NSString*) _stringByAddingPercentEscapesForQuery;
@end
@implementation NSString (NSURLPrivate)
@ -128,22 +129,84 @@ NSString * const NSErrorFailingURLStringKey = @"NSErrorFailingURLStringKey";
unsigned int lo;
if (c <= 32
|| c > 126
|| c == 34
|| c == 35
|| c == 37
|| c == 59
|| c == 60
|| c == 62
|| c == 63
|| c == 91
|| c == 92
|| c == 93
|| c == 94
|| c == 96
|| c == 123
|| c == 124
|| c == 125)
|| c > 126
|| c == 34
|| c == 35
|| c == 37
|| c == 59
|| c == 60
|| c == 62
|| c == 63
|| c == 91
|| c == 92
|| c == 93
|| c == 94
|| c == 96
|| c == 123
|| c == 124
|| c == 125)
{
dst[dpos++] = '%';
hi = (c & 0xf0) >> 4;
dst[dpos++] = (hi > 9) ? 'A' + hi - 10 : '0' + hi;
lo = (c & 0x0f);
dst[dpos++] = (lo > 9) ? 'A' + lo - 10 : '0' + lo;
}
else
{
dst[dpos++] = c;
}
}
s = [[NSString alloc] initWithBytes: dst
length: dpos
encoding: NSASCIIStringEncoding];
NSZoneFree(NSDefaultMallocZone(), dst);
IF_NO_GC([s autorelease];)
}
return s;
}
/*
* Encode query
*/
- (NSString*) _stringByAddingPercentEscapesForQuery
{
NSData *data = [self dataUsingEncoding: NSUTF8StringEncoding];
NSString *s = nil;
if (data != nil)
{
unsigned char *src = (unsigned char*)[data bytes];
unsigned int slen = [data length];
unsigned char *dst;
unsigned int spos = 0;
unsigned int dpos = 0;
dst = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), slen * 3);
while (spos < slen)
{
unsigned char c = src[spos++];
unsigned int hi;
unsigned int lo;
if (c <= 32
|| c > 126
|| c == 34
|| c == 35
|| c == 37
|| c == 38
|| c == 59
|| c == 60
|| c == 61
|| c == 62
|| c == 91
|| c == 92
|| c == 93
|| c == 94
|| c == 96
|| c == 123
|| c == 124
|| c == 125)
{
dst[dpos++] = '%';
hi = (c & 0xf0) >> 4;
@ -2483,9 +2546,11 @@ GS_PRIVATE_INTERNAL(NSURLComponents)
urlString = [urlString stringByAppendingString: @"?"];
while((qi = [en nextObject]) != nil)
{
NSString *n = [qi name];
NSString *v = [qi value];
NSString *item = [[NSString stringWithFormat: @"%@=%@", n, v] _stringByAddingPercentEscapes];
NSString *n = [[qi name] _stringByAddingPercentEscapesForQuery];
// stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *v = [[qi value]_stringByAddingPercentEscapesForQuery];
// stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *item = [NSString stringWithFormat: @"%@=%@", n, v];
urlString = [urlString stringByAppendingString: item];
if (qi != [internal->_queryItems lastObject])
{