2
* This file is part of nzbget
4
* Copyright (C) 2007-2013 Andrey Prygunkov <hugbug@users.sourceforge.net>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
* $Date: 2013-08-31 23:14:39 +0200 (Sat, 31 Aug 2013) $
25
#import <Cocoa/Cocoa.h>
28
@implementation WebClient
30
- (id)initWithURLString:(NSString*)urlStr
32
success:(SEL)successCallback
33
failure:(SEL)failureCallback {
37
_successCallback = successCallback;
38
_failureCallback = failureCallback;
39
NSURL *url = [NSURL URLWithString:urlStr];
40
NSURLRequest *request = [NSURLRequest requestWithURL:url];
41
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
42
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
51
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSHTTPURLResponse *)aResponse {
52
responseHeaderFields = [aResponse allHeaderFields];
54
if ([aResponse statusCode] != 200)
56
failureCode = [aResponse statusCode];
62
NSInteger contentLength = [[responseHeaderFields objectForKey:@"Content-Length"] integerValue];
63
if (contentLength > 0) {
64
data = [[NSMutableData alloc] initWithCapacity:contentLength];
66
data = [[NSMutableData alloc] init];
70
- (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)newData {
71
[data appendData:newData];
74
- (void)connectionDidFinishLoading:(NSURLConnection *)aConnection {
79
- (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error {
80
if ([[error domain] isEqual:NSURLErrorDomain])
82
failureCode = [error code];
90
SuppressPerformSelectorLeakWarning([_receiver performSelector:_successCallback withObject:data];);
94
SuppressPerformSelectorLeakWarning([_receiver performSelector:_failureCallback];);