add explicit memory management again

This commit is contained in:
Sami Samhuri 2012-11-14 22:10:38 -08:00
parent a1e5adf606
commit 0e8065290e
3 changed files with 42 additions and 27 deletions

View file

@ -7,16 +7,15 @@
//
#import "MMHTTPClient.h"
#import "NSString+marshmallows.h"
// Encode a string to embed in an URL.
NSString* MMHTTPURLEncode(NSString *string) {
return (__bridge NSString *)
CFURLCreateStringByAddingPercentEscapes(NULL,
(__bridge CFStringRef) string,
NULL,
(CFStringRef) @"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
CFStringRef urlStringRef = CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef) string,
NULL,
(CFStringRef) @"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
return [(NSString *)urlStringRef autorelease];
}
MMHTTPClient *_sharedMMHTTPClient;
@ -47,17 +46,17 @@ NSString *JoinURLComponents(NSString *first, va_list args)
+ (id) client
{
return [[self alloc] init];
return [[[self alloc] init] autorelease];
}
+ (id) clientWithBaseURL: (NSString *)baseURL
{
return [[self alloc] initWithBaseURL: baseURL];
return [[[self alloc] initWithBaseURL: baseURL] autorelease];
}
+ (id) clientWithBaseURL: (NSString *)baseURL timeout: (NSUInteger)timeout
{
return [[self alloc] initWithBaseURL: baseURL timeout: timeout];
return [[[self alloc] initWithBaseURL: baseURL timeout: timeout] autorelease];
}
+ (NSString *) pathFor: (NSString *)first, ...
@ -283,7 +282,13 @@ NSString *JoinURLComponents(NSString *first, va_list args)
[mutableOptions setValue: [NSNumber numberWithUnsignedInt: self.timeout] forKey: @"timeout"];
}
options = [NSDictionary dictionaryWithDictionary: mutableOptions];
[mutableOptions release];
return [MMHTTPRequest requestWithOptions: options callback: callback];
}
- (void) dealloc
{
[super dealloc];
}
@end

View file

@ -28,7 +28,7 @@ typedef void (^MMHTTPImageCallback)(NSInteger status, UIImage *image);
@property (nonatomic, retain) NSMutableDictionary *headers;
@property (nonatomic, retain) NSData *data;
@property (nonatomic, retain) NSString *type;
@property (nonatomic, copy) MMHTTPCallback callback;
@property (nonatomic, assign) MMHTTPCallback callback;
@property NSUInteger timeout;
@property (readonly) NSInteger statusCode;
@property (strong, readonly) NSDictionary *responseHeaders;

View file

@ -29,14 +29,14 @@
+ (id) requestWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback
{
return [[self alloc] initWithOptions: options callback: callback];
return [[[self alloc] initWithOptions: options callback: callback] autorelease];
}
- (id) initWithOptions: (NSDictionary *)options callback: (MMHTTPCallback)callback
{
self = [super init];
if (self) {
self.callback = [callback copy];
self.callback = Block_copy(callback);
self.timeout = MMHTTPRequestDefaultTimeout;
self.method = [options objectForKey: @"method"];
self.url = [options objectForKey: @"url"];
@ -51,11 +51,9 @@
- (void) cancel
{
Block_release(self.callback);
_callback = nil;
[_connection cancel];
if (self.callback) {
Block_release(self.callback);
self.callback = nil;
}
}
- (NSData *) responseData
@ -65,9 +63,9 @@
- (NSString *) responseText
{
return [[NSString alloc] initWithBytes: _responseData.bytes
length: _responseData.length
encoding: NSUTF8StringEncoding];
return [[[NSString alloc] initWithBytes: _responseData.bytes
length: _responseData.length
encoding: NSUTF8StringEncoding] autorelease];
}
- (UIImage *) responseImage
@ -100,14 +98,12 @@
- (void) connection: (NSURLConnection *)connection didReceiveResponse: (NSURLResponse *)response
{
// NSLog(@"didReceiveResponse: %@",response);
if ([response respondsToSelector: @selector(statusCode)])
{
_statusCode = [(NSHTTPURLResponse *)response statusCode];
_responseHeaders = [(NSHTTPURLResponse *)response allHeaderFields];
_responseHeaders = [[(NSHTTPURLResponse *)response allHeaderFields] retain];
}
else {
NSLog(@"Not an HTTP response? connection: %@ response: %@", connection, response);
_statusCode = 500;
_responseHeaders = [[NSDictionary alloc] init];
}
@ -117,13 +113,12 @@
- (void) connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
{
// NSLog(@"didReceiveData: %@", data);
[_responseData appendData: data];
}
- (void) connection: (NSURLConnection *)connection didFailWithError: (NSError *)error
{
NSLog(@"didFailWithError: %@", error);
[_responseData release];
_responseData = nil;
_statusCode = MMHTTPRequestStatusError;
if (self.callback) {
@ -133,9 +128,8 @@
- (void) connectionDidFinishLoading: (NSURLConnection *)connection
{
// NSLog(@"didFinishLoading: %d", self.statusCode);
id data = nil;
if (self.statusCode == 200) {
if (self.callback && self.statusCode == 200) {
if ([self.type isEqualToString: @"text"]) {
data = self.responseText;
}
@ -152,4 +146,20 @@
}
}
- (void) dealloc
{
[_responseData release];
[_responseHeaders release];
[_connection release];
[_request release];
[_method release];
[_url release];
[_headers release];
[_data release];
[_type release];
Block_release(_callback);
_callback = nil;
[super dealloc];
}
@end