-
Notifications
You must be signed in to change notification settings - Fork 7
/
JSONRequest.m
243 lines (200 loc) · 7.45 KB
/
JSONRequest.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// JsonRequest.m
//
// Created by Maxime Guilbot on 6/4/10.
// Copyright 2010 ekohe. All rights reserved.
//
#import "JSONRequest.h"
#import "Authorization.h"
@interface JSONRequest (PrivateMethods)
-(void) initiateRequest;
-(void) saveCookiesInResponse:(NSHTTPURLResponse*)response;
-(void) addCookiesToRequest:(NSMutableURLRequest*)request;
@end
@implementation JSONRequest
@synthesize path, delegate, temp, method, body, response, cookies;
+(NSString*) endPoint {
return @"undefined";
}
+(NSString*) alterUrl:(NSString*)url {
return url;
}
- (id) initGetWithPath:(NSString*)aPath delegate:(id<JSONRequestDelegate>)aDelegate {
if (self = [super init])
{
self.path = [[self class] alterUrl:aPath];
self.delegate = aDelegate;
self.method = @"GET";
self.body = nil;
self.cookies = nil;
[self initiateRequest];
}
return self;
}
- (id) initPostWithPath:(NSString*)aPath httpBody:(NSString*)aBody delegate:(id<JSONRequestDelegate>)aDelegate {
if (self = [super init])
{
self.path = [[self class] alterUrl:aPath];
self.delegate = aDelegate;
self.method = @"POST";
self.body = aBody;
self.cookies = nil;
[self initiateRequest];
}
return self;
}
- (id) initPostWithPath:(NSString*)aPath httpBody:(NSString*)aBody cookie:(NSString*)aCookie delegate:(id<JSONRequestDelegate>)aDelegate {
if (self = [super init])
{
self.path = [[self class] alterUrl:aPath];
self.delegate = aDelegate;
self.method = @"POST";
self.body = aBody;
self.cookies = [NSArray arrayWithObject:aCookie];
[self initiateRequest];
}
return self;
}
- (id) initPostWithPath:(NSString*)aPath parameters:(NSDictionary*)params delegate:(id<JSONRequestDelegate>)aDelegate {
NSMutableArray *bodyComponents = [NSMutableArray array];
for (NSString *key in [params allKeys]) {
if ([[params valueForKey:key] isKindOfClass:[NSString class]]) {
[bodyComponents addObject:[NSString stringWithFormat:@"%@=%@", [key stringByUrlEncoding], [[params valueForKey:key] stringByUrlEncoding]]];
} else if ([[params valueForKey:key] isKindOfClass:[NSArray class]]) {
for (NSString* s in (NSArray*)[params valueForKey:key]) {
[bodyComponents addObject:[NSString stringWithFormat:@"%@[]=%@", [key stringByUrlEncoding], [s stringByUrlEncoding]]];
}
} else {
NSString *value = [NSString stringWithFormat:@"%@", [params valueForKey:key]];
[bodyComponents addObject:[NSString stringWithFormat:@"%@=%@", [key stringByUrlEncoding], [value stringByUrlEncoding]]];
}
}
return [self initPostWithPath:aPath httpBody:[bodyComponents componentsJoinedByString:@"&"] delegate:aDelegate];
}
#pragma mark -
#pragma mark Network Operations
-(void) initiateRequest {
// Create URL from path and completeURL
NSURL *url = [NSURL URLWithString:[[[self class] endPoint] stringByAppendingString:path]];
// Create URL Request
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
if (![method isEqualToString:@"GET"]) {
[urlRequest setHTTPMethod:method];
[urlRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
}
#ifdef DEBUG_NETWORK
NSLog(@"[Network] Sending request: %@: %@", method, [url absoluteURL]);
#endif
[self addCookiesToRequest:urlRequest];
// Initiate request
[NSURLConnection connectionWithRequest:urlRequest delegate:self];
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
if (redirectResponse!=nil)
{
#ifdef DEBUG_NETWORK
NSLog(@"[Network] Redirection to request: %@ - redirectResponse: %@", request, redirectResponse);
#endif
[self saveCookiesInResponse:(NSHTTPURLResponse*)redirectResponse];
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:[request URL]];
[self addCookiesToRequest:(NSMutableURLRequest*)newRequest];
return newRequest;
}
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)resp {
if ([resp respondsToSelector:@selector(statusCode)])
self.response = (NSHTTPURLResponse*)resp;
// Initialize temporary data storage
NSMutableData *data = [[NSMutableData alloc] init];
self.temp = data;
[data release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[temp appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *jsonString = [[NSString alloc] initWithData:temp encoding:NSUTF8StringEncoding];
if (jsonString==nil) {
jsonString = [[NSString alloc] initWithData:temp encoding:NSWindowsCP1252StringEncoding];
}
#ifdef DEBUG_NETWORK
NSLog(@"[Network] Got response: %@", jsonString);
#endif
NSDictionary *jsonResults = [jsonString JSONValue];
[self saveCookiesInResponse:self.response];
[jsonString release];
[self jsonFinishedLoading:jsonResults];
}
- (void) jsonFinishedLoading:(NSDictionary*)json {
if (delegate!=nil) {
if (json==nil) {
[delegate jsonDidFailWithError:nil jsonRequest:self];
} else {
[delegate jsonDidFinishLoading:json jsonRequest:self];
}
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
#ifdef DEBUG_NETWORK
NSLog(@"[Network] Failed with error %@", error);
#endif
if ((error.domain == NSURLErrorDomain) && (error.code == -1009)) {
[ErrorHUD displayErrorMessage:@"We can't connect to the internet right now.\nCheck your network or try again in a few minutes!"];
}
if (delegate!=nil)
[delegate jsonDidFailWithError:error jsonRequest:self];
}
-(void) saveCookiesInResponse:(NSHTTPURLResponse*)resp {
// Cookie handling
if ([[[resp allHeaderFields] allKeys] indexOfObject:@"Set-Cookie"]!=NSNotFound) {
NSString *received_cookies = [[resp allHeaderFields] objectForKey:@"Set-Cookie"];
NSArray *receivedCookies = [received_cookies componentsSeparatedByString:@","];
NSMutableArray *cookiesToSave = [NSMutableArray array];
for (NSString *received_cookie in receivedCookies) {
NSRange semicolon_range = [received_cookie rangeOfString:@";"];
if (semicolon_range.location != NSNotFound) {
NSString *strippedCookie = [received_cookie substringToIndex:semicolon_range.location];
#ifdef DEBUG_NETWORK_COOKIE
NSLog(@"[Network] Save Cookie: %@", strippedCookie);
#endif
[cookiesToSave addObject:strippedCookie];
}
}
[Authorization setCookies:(NSArray*)cookiesToSave];
} else {
#ifdef DEBUG_NETWORK_COOKIE
NSLog(@"[Network] No Cookies found in headers: %@", [resp allHeaderFields]);
#endif
}
}
-(void) addCookiesToRequest:(NSMutableURLRequest*)request {
if (self.cookies) {
for (NSString *cookie in cookies) {
[request addValue:cookie forHTTPHeaderField:@"Cookie"];
#ifdef DEBUG_NETWORK_COOKIE
NSLog(@"[Network] With cookie: %@", cookie);
#endif
}
} else {
for (NSString *cookie in [Authorization cookies]) {
[request addValue:cookie forHTTPHeaderField:@"Cookie"];
#ifdef DEBUG_NETWORK_COOKIE
NSLog(@"[Network] With cookie: %@", cookie);
#endif
}
}
}
#pragma mark -
#pragma mark Memory Management
- (void) dealloc {
[path release];
[temp release];
[method release];
[body release];
[response release];
[cookies release];
[super dealloc];
}
@end