-
Notifications
You must be signed in to change notification settings - Fork 7
/
EGOHTTPRequest.m
310 lines (243 loc) · 8.28 KB
/
EGOHTTPRequest.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//
// EGOHTTPRequest.m
// EGOHTTPRequest
//
// Created by Shaun Harrison on 12/2/09.
// Copyright (c) 2009-2010 enormego
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "EGOHTTPRequest.h"
static NSMutableArray* __currentRequests;
static NSLock* __requestsLock;
@interface EGOHTTPRequest ()
+ (void)cleanUpRequest:(EGOHTTPRequest*)request;
+ (NSLock*)_requestsLock;
- (NSURLRequest*)_buildURLRequest;
@end
@implementation EGOHTTPRequest
@synthesize url=_URL, response=_response, delegate=_delegate, timeoutInterval=_timeoutInterval,
didFinishSelector=_didFinishSelector, didFailSelector=_didFailSelector, error=_error,
cancelled=isCancelled, started=isStarted, finished=isFinished, requestMethod=_requestMethod,
requestBody=_requestBody;
- (id)initWithURL:(NSURL*)aURL {
return [self initWithURL:aURL delegate:nil];
}
- (id)initWithURL:(NSURL*)aURL delegate:(id)delegate {
if((self = [super init])) {
_URL = [aURL retain];
self.delegate = delegate;
_responseData = [[NSMutableData alloc] init];
_requestHeaders = [[NSMutableDictionary alloc] init];
self.timeoutInterval = 60;
self.didFinishSelector = @selector(requestDidFinish:);
self.didFailSelector = @selector(requestDidFail:withError:);
}
return self;
}
+ (NSMutableArray*)currentRequests {
@synchronized(self) {
if(!__currentRequests) {
__currentRequests = [[NSMutableArray alloc] init];
}
}
return __currentRequests;
}
+ (NSLock*)_requestsLock {
@synchronized(self) {
if(!__requestsLock) {
__requestsLock = [[NSLock alloc] init];
}
}
return __requestsLock;
}
- (void)addRequestHeader:(NSString *)header value:(NSString *)value {
[_requestHeaders setObject:value forKey:header];
}
- (NSURLRequest*)_buildURLRequest {
if(isStarted || isCancelled) return nil;
else isStarted = YES;
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:self.url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:self.timeoutInterval];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
if(_requestMethod) {
[request setHTTPMethod:_requestMethod];
} else {
[request setHTTPMethod:@"GET"];
}
if(_requestBody) {
[request setHTTPBody:_requestBody];
}
for(NSString* key in _requestHeaders) {
[request setValue:[_requestHeaders objectForKey:key] forHTTPHeaderField:key];
}
[[[self class] _requestsLock] lock];
[[[self class] currentRequests] addObject:self];
[[[self class] _requestsLock] unlock];
return [request autorelease];
}
- (void)startAsynchronous {
NSURLRequest* request = [self _buildURLRequest];
if(request) {
[self performSelectorInBackground:@selector(startConnectionInBackgroundWithRequest:) withObject:request];
}
}
- (void)startSynchronous {
NSURLRequest* request = [self _buildURLRequest];
if(!request) return;
NSURLResponse* aResponse = nil;
NSError* anError = nil;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&aResponse error:&anError];
[_responseData setData:responseData];
self.response = aResponse;
if(_error) [_error release];
if(anError) {
_error = [anError retain];
if(!isCancelled) {
if([self.delegate respondsToSelector:self.didFailSelector]) {
[self.delegate performSelector:self.didFailSelector withObject:self withObject:_error];
}
}
} else {
_error = nil;
if(!isCancelled) {
if([self.delegate respondsToSelector:self.didFinishSelector]) {
[self.delegate performSelector:self.didFinishSelector withObject:self];
}
}
}
isFinished = YES;
[[self class] cleanUpRequest:self];
}
- (void)startConnectionInBackgroundWithRequest:(NSURLRequest*)request {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
_backgroundThread = [[NSThread currentThread] retain];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
while(!isCancelled && !isFinished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
if(isCancelled) {
[_connection cancel];
}
[_backgroundThread release];
_backgroundThread = nil;
[_connection release];
_connection = nil;
[pool release];
}
+ (void)cleanUpRequest:(EGOHTTPRequest*)request {
[[[self class] _requestsLock] lock];
request.delegate = nil;
[[self currentRequests] removeObject:request];
[[[self class] _requestsLock] unlock];
}
+ (void)cancelRequestsForDelegate:(id)delegate {
[[self _requestsLock] lock];
NSArray* requests = [[self currentRequests] copy];
[[self _requestsLock] unlock];
for(EGOHTTPRequest* request in requests) {
if([request isKindOfClass:[EGOHTTPRequest class]]){
if(request.delegate == delegate) {
request.delegate = nil;
[request cancel];
}
}
}
[requests release];
}
- (void)cancel {
if(isCancelled) return;
else isCancelled = YES;
isFinished = YES;
// No need to call cancel because flagging as cancelled will cancel the request in the thread.
[[self class] cleanUpRequest:self];
}
- (NSData*)responseData {
return _responseData;
}
- (NSString*)responseString {
NSStringEncoding stringEncoding;
if([self.response textEncodingName].length > 0) {
stringEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[self.response textEncodingName]));
} else {
stringEncoding = NSUTF8StringEncoding;
}
return [[[NSString alloc] initWithData:self.responseData encoding:stringEncoding] autorelease];
}
- (NSDictionary*)responseHeaders {
if([self.response isKindOfClass:[NSHTTPURLResponse class]]) {
return [(NSHTTPURLResponse*)self.response allHeaderFields];
} else {
return nil;
}
}
- (NSInteger)responseStatusCode {
if([self.response isKindOfClass:[NSHTTPURLResponse class]]) {
return [(NSHTTPURLResponse*)self.response statusCode];
} else {
return -NSNotFound;
}
}
#pragma mark -
#pragma mark Asynchrous NSURLConnection Methods
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if(connection != _connection) return;
[_responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if(connection != _connection) return;
self.response = response;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if(connection != _connection) return;
if(!isCancelled) {
if([self.delegate respondsToSelector:self.didFinishSelector]) {
[self.delegate performSelector:self.didFinishSelector withObject:self];
}
}
isFinished = YES;
[[self class] cleanUpRequest:self];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if(connection != _connection) return;
[_error release];
_error = [error retain];
if(!isCancelled) {
if([self.delegate respondsToSelector:self.didFailSelector]) {
[self.delegate performSelector:self.didFailSelector withObject:self withObject:error];
}
}
isFinished = YES;
[[self class] cleanUpRequest:self];
}
#pragma mark -
- (void)dealloc {
[[self class] cleanUpRequest:self];
isFinished = YES;
self.response = nil;
self.delegate = nil;
[_responseData release]; _responseData=nil;
[_requestHeaders release], _requestHeaders = nil;
[_connection release], _connection = nil;
[_error release], _error = nil;
[_URL release], _URL = nil;
[super dealloc];
}
@end