-
Notifications
You must be signed in to change notification settings - Fork 3
/
OAuth+Additions.m
73 lines (63 loc) · 1.62 KB
/
OAuth+Additions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// OAuth+Additions.m
//
// Created by Loren Brichter on 6/9/10.
// Copyright 2010 Loren Brichter. All rights reserved.
//
#import "OAuth+Additions.h"
@implementation NSURL (OAuthAdditions)
+ (NSDictionary *)ab_parseURLQueryString:(NSString *)query
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSArray *pairs = [query componentsSeparatedByString:@"&"];
for(NSString *pair in pairs) {
NSArray *keyValue = [pair componentsSeparatedByString:@"="];
if([keyValue count] == 2) {
NSString *key = [keyValue objectAtIndex:0];
NSString *value = [keyValue objectAtIndex:1];
value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if(key && value)
[dict setObject:value forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:dict];
}
- (NSString *)ab_actualPath
{
NSString* cfPath = [(NSString*)CFURLCopyPath((CFURLRef)self) autorelease];
return cfPath;
}
@end
@implementation NSString (OAuthAdditions)
- (NSString *)ab_RFC3986EncodedString // UTF-8 encodes prior to URL encoding
{
NSMutableString *result = [NSMutableString string];
const char *p = [self UTF8String];
unsigned char c;
for(; (c = *p); p++)
{
switch(c)
{
case '0' ... '9':
case 'A' ... 'Z':
case 'a' ... 'z':
case '.':
case '-':
case '~':
case '_':
[result appendFormat:@"%c", c];
break;
default:
[result appendFormat:@"%%%02X", c];
}
}
return result;
}
+ (NSString *)ab_GUID
{
CFUUIDRef u = CFUUIDCreate(kCFAllocatorDefault);
CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, u);
CFRelease(u);
return [(NSString *)s autorelease];
}
@end