Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Commit

Permalink
[Dox] Clean up TTURLRequest.
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Apr 6, 2010
1 parent d456049 commit f762dc7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/TTRequestLoader.m
Expand Up @@ -44,7 +44,7 @@ @implementation TTRequestLoader
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initForRequest:(TTURLRequest*)request queue:(TTURLRequestQueue*)queue { - (id)initForRequest:(TTURLRequest*)request queue:(TTURLRequestQueue*)queue {
if (self = [super init]) { if (self = [super init]) {
_urlPath = [request.URL copy]; _urlPath = [request.urlPath copy];
_queue = queue; _queue = queue;
_cacheKey = [request.cacheKey retain]; _cacheKey = [request.cacheKey retain];
_cachePolicy = request.cachePolicy; _cachePolicy = request.cachePolicy;
Expand Down
4 changes: 2 additions & 2 deletions src/TTURLImageResponse.m
Expand Up @@ -59,7 +59,7 @@ - (NSError*)request:(TTURLRequest*)request processResponse:(NSHTTPURLResponse*)r
// TODO(jverkoey Feb 10, 2010): This logic doesn't entirely make sense. Why don't we just store // TODO(jverkoey Feb 10, 2010): This logic doesn't entirely make sense. Why don't we just store
// the data in the cache if there was a cache miss, and then just retain the image data we // the data in the cache if there was a cache miss, and then just retain the image data we
// downloaded? This needs to be tested in production. // downloaded? This needs to be tested in production.
UIImage* image = [[TTURLCache sharedCache] imageForURL:request.URL fromDisk:NO]; UIImage* image = [[TTURLCache sharedCache] imageForURL:request.urlPath fromDisk:NO];


if (nil == image) { if (nil == image) {
image = [UIImage imageWithData:data]; image = [UIImage imageWithData:data];
Expand All @@ -74,7 +74,7 @@ - (NSError*)request:(TTURLRequest*)request processResponse:(NSHTTPURLResponse*)r
// NSData* data = UIImagePNGRepresentation(image); // NSData* data = UIImagePNGRepresentation(image);
// [[TTURLCache sharedCache] storeData:data forURL:request.URL]; // [[TTURLCache sharedCache] storeData:data forURL:request.URL];
// } // }
[[TTURLCache sharedCache] storeImage:image forURL:request.URL]; [[TTURLCache sharedCache] storeImage:image forURL:request.urlPath];
} }


_image = [image retain]; _image = [image retain];
Expand Down
50 changes: 33 additions & 17 deletions src/TTURLRequest.m
Expand Up @@ -32,7 +32,7 @@
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTURLRequest @implementation TTURLRequest


@synthesize URL = _URL; @synthesize urlPath = _urlPath;
@synthesize httpMethod = _httpMethod; @synthesize httpMethod = _httpMethod;
@synthesize httpBody = _httpBody; @synthesize httpBody = _httpBody;
@synthesize parameters = _parameters; @synthesize parameters = _parameters;
Expand Down Expand Up @@ -77,7 +77,7 @@ + (TTURLRequest*)requestWithURL:(NSString*)URL delegate:(id /*<TTURLRequestDeleg
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithURL:(NSString*)URL delegate:(id /*<TTURLRequestDelegate>*/)delegate { - (id)initWithURL:(NSString*)URL delegate:(id /*<TTURLRequestDelegate>*/)delegate {
if (self = [self init]) { if (self = [self init]) {
_URL = [URL retain]; _urlPath = [URL retain];
if (nil != delegate) { if (nil != delegate) {
[_delegates addObject:delegate]; [_delegates addObject:delegate];
} }
Expand All @@ -92,12 +92,7 @@ - (id)init {
_delegates = TTCreateNonRetainingArray(); _delegates = TTCreateNonRetainingArray();
_cachePolicy = TTURLRequestCachePolicyDefault; _cachePolicy = TTURLRequestCachePolicyDefault;
_cacheExpirationAge = TT_DEFAULT_CACHE_EXPIRATION_AGE; _cacheExpirationAge = TT_DEFAULT_CACHE_EXPIRATION_AGE;
_isLoading = NO;
_shouldHandleCookies = YES; _shouldHandleCookies = YES;
_totalBytesLoaded = 0;
_totalBytesExpected = 0;
_respondedFromCache = NO;
_filterPasswordLogging = NO;
_charsetForMultipart = NSUTF8StringEncoding; _charsetForMultipart = NSUTF8StringEncoding;
} }
return self; return self;
Expand All @@ -106,7 +101,7 @@ - (id)init {


/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc { - (void)dealloc {
TT_RELEASE_SAFELY(_URL); TT_RELEASE_SAFELY(_urlPath);
TT_RELEASE_SAFELY(_httpMethod); TT_RELEASE_SAFELY(_httpMethod);
TT_RELEASE_SAFELY(_response); TT_RELEASE_SAFELY(_response);
TT_RELEASE_SAFELY(_httpBody); TT_RELEASE_SAFELY(_httpBody);
Expand All @@ -118,18 +113,19 @@ - (void)dealloc {
TT_RELEASE_SAFELY(_timestamp); TT_RELEASE_SAFELY(_timestamp);
TT_RELEASE_SAFELY(_files); TT_RELEASE_SAFELY(_files);
TT_RELEASE_SAFELY(_delegates); TT_RELEASE_SAFELY(_delegates);

[super dealloc]; [super dealloc];
} }




/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString*)description { - (NSString*)description {
return [NSString stringWithFormat:@"<TTURLRequest %@>", _URL]; return [NSString stringWithFormat:@"<TTURLRequest %@>", _urlPath];
} }




/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSString *)md5HexDigest:(NSString*)input { - (NSString*)md5HexDigest:(NSString*)input {
const char* str = [input UTF8String]; const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH]; unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, strlen(str), result); CC_MD5(str, strlen(str), result);
Expand All @@ -146,7 +142,7 @@ - (NSString *)md5HexDigest:(NSString*)input {
- (NSString*)generateCacheKey { - (NSString*)generateCacheKey {
if ([_httpMethod isEqualToString:@"POST"] if ([_httpMethod isEqualToString:@"POST"]
|| [_httpMethod isEqualToString:@"PUT"]) { || [_httpMethod isEqualToString:@"PUT"]) {
NSMutableString* joined = [[[NSMutableString alloc] initWithString:self.URL] autorelease]; NSMutableString* joined = [[[NSMutableString alloc] initWithString:self.urlPath] autorelease];
NSEnumerator* e = [_parameters keyEnumerator]; NSEnumerator* e = [_parameters keyEnumerator];
for (id key; key = [e nextObject]; ) { for (id key; key = [e nextObject]; ) {
[joined appendString:key]; [joined appendString:key];
Expand All @@ -159,15 +155,15 @@ - (NSString*)generateCacheKey {


return [self md5HexDigest:joined]; return [self md5HexDigest:joined];
} else { } else {
return [self md5HexDigest:self.URL]; return [self md5HexDigest:self.urlPath];
} }
} }




/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSData*)generatePostBody { - (NSData*)generatePostBody {
NSMutableData *body = [NSMutableData data]; NSMutableData* body = [NSMutableData data];
NSString *beginLine = [NSString stringWithFormat:@"\r\n--%@\r\n", kStringBoundary]; NSString* beginLine = [NSString stringWithFormat:@"\r\n--%@\r\n", kStringBoundary];


[body appendData:[[NSString stringWithFormat:@"--%@\r\n", kStringBoundary] [body appendData:[[NSString stringWithFormat:@"--%@\r\n", kStringBoundary]
dataUsingEncoding:NSUTF8StringEncoding]]; dataUsingEncoding:NSUTF8StringEncoding]];
Expand Down Expand Up @@ -282,7 +278,7 @@ - (NSString*)cacheKey {




/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field { - (void)setValue:(NSString*)value forHTTPHeaderField:(NSString*)field {
if (!_headers) { if (!_headers) {
_headers = [[NSMutableDictionary alloc] init]; _headers = [[NSMutableDictionary alloc] init];
} }
Expand All @@ -306,12 +302,12 @@ - (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)f
- (BOOL)send { - (BOOL)send {
if (_parameters) { if (_parameters) {
// Don't log passwords. Save now, restore after logging // Don't log passwords. Save now, restore after logging
NSString *password = [_parameters objectForKey:@"password"]; NSString* password = [_parameters objectForKey:@"password"];
if (_filterPasswordLogging && password) { if (_filterPasswordLogging && password) {
[_parameters setObject:@"[FILTERED]" forKey:@"password"]; [_parameters setObject:@"[FILTERED]" forKey:@"password"];
} }


TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"SEND %@ %@", self.URL, self.parameters); TTDCONDITIONLOG(TTDFLAG_URLREQUEST, @"SEND %@ %@", self.urlPath, self.parameters);


if (password) { if (password) {
[_parameters setObject:password forKey:@"password"]; [_parameters setObject:password forKey:@"password"];
Expand Down Expand Up @@ -339,4 +335,24 @@ - (NSURLRequest*)createNSURLRequest {
} }




///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Properties


///////////////////////////////////////////////////////////////////////////////////////////////////
// Deprecated
- (void)setURL:(NSString*)urlPath {
NSString* aUrlPath = [urlPath copy];
[_urlPath release];
_urlPath = aUrlPath;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// Deprecated
- (NSString*)URL {
return _urlPath;
}

@end @end
14 changes: 7 additions & 7 deletions src/TTURLRequestQueue.m
Expand Up @@ -153,15 +153,15 @@ - (BOOL)loadFromCache:(NSString*)URL cacheKey:(NSString*)cacheKey
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)loadRequestFromCache:(TTURLRequest*)request { - (BOOL)loadRequestFromCache:(TTURLRequest*)request {
if (!request.cacheKey) { if (!request.cacheKey) {
request.cacheKey = [[TTURLCache sharedCache] keyForURL:request.URL]; request.cacheKey = [[TTURLCache sharedCache] keyForURL:request.urlPath];
} }


if (request.cachePolicy & (TTURLRequestCachePolicyDisk|TTURLRequestCachePolicyMemory)) { if (request.cachePolicy & (TTURLRequestCachePolicyDisk|TTURLRequestCachePolicyMemory)) {
id data = nil; id data = nil;
NSDate* timestamp = nil; NSDate* timestamp = nil;
NSError* error = nil; NSError* error = nil;


if ([self loadFromCache:request.URL cacheKey:request.cacheKey if ([self loadFromCache:request.urlPath cacheKey:request.cacheKey
expires:request.cacheExpirationAge expires:request.cacheExpirationAge
fromDisk:!_suspended && request.cachePolicy & TTURLRequestCachePolicyDisk fromDisk:!_suspended && request.cachePolicy & TTURLRequestCachePolicyDisk
data:&data error:&error timestamp:&timestamp]) { data:&data error:&error timestamp:&timestamp]) {
Expand Down Expand Up @@ -333,7 +333,7 @@ - (BOOL)sendRequest:(TTURLRequest*)request {
} }
} }


if (!request.URL.length) { if (!request.urlPath.length) {
NSError* error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil]; NSError* error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil];
for (id<TTURLRequestDelegate> delegate in request.delegates) { for (id<TTURLRequestDelegate> delegate in request.delegates) {
if ([delegate respondsToSelector:@selector(request:didFailLoadWithError:)]) { if ([delegate respondsToSelector:@selector(request:didFailLoadWithError:)]) {
Expand Down Expand Up @@ -365,7 +365,7 @@ - (BOOL)sendRequest:(TTURLRequest*)request {
[_loaderQueue addObject:loader]; [_loaderQueue addObject:loader];
} else { } else {
++_totalLoading; ++_totalLoading;
[loader load:[NSURL URLWithString:request.URL]]; [loader load:[NSURL URLWithString:request.urlPath]];
} }
[loader release]; [loader release];


Expand All @@ -385,7 +385,7 @@ - (BOOL)sendSynchronousRequest:(TTURLRequest*)request {
} }
} }


if (!request.URL.length) { if (!request.urlPath.length) {
NSError* error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil]; NSError* error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil];
for (id<TTURLRequestDelegate> delegate in request.delegates) { for (id<TTURLRequestDelegate> delegate in request.delegates) {
if ([delegate respondsToSelector:@selector(request:didFailLoadWithError:)]) { if ([delegate respondsToSelector:@selector(request:didFailLoadWithError:)]) {
Expand All @@ -403,7 +403,7 @@ - (BOOL)sendSynchronousRequest:(TTURLRequest*)request {
// Should be decremented eventually by loadSynchronously // Should be decremented eventually by loadSynchronously
++_totalLoading; ++_totalLoading;


[loader loadSynchronously:[NSURL URLWithString:request.URL]]; [loader loadSynchronously:[NSURL URLWithString:request.urlPath]];
TT_RELEASE_SAFELY(loader); TT_RELEASE_SAFELY(loader);


return NO; return NO;
Expand Down Expand Up @@ -470,7 +470,7 @@ - (void)cancelAllRequests {
/////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////
- (NSURLRequest*)createNSURLRequest:(TTURLRequest*)request URL:(NSURL*)URL { - (NSURLRequest*)createNSURLRequest:(TTURLRequest*)request URL:(NSURL*)URL {
if (!URL) { if (!URL) {
URL = [NSURL URLWithString:request.URL]; URL = [NSURL URLWithString:request.urlPath];
} }


NSMutableURLRequest* URLRequest = [NSMutableURLRequest requestWithURL:URL NSMutableURLRequest* URLRequest = [NSMutableURLRequest requestWithURL:URL
Expand Down
16 changes: 13 additions & 3 deletions src/Three20/TTURLRequest.h
Expand Up @@ -16,6 +16,8 @@


#import "Three20/TTGlobalNetwork.h" #import "Three20/TTGlobalNetwork.h"


#import "Three20/TTCorePreprocessorMacros.h"

@protocol TTURLRequestDelegate; @protocol TTURLRequestDelegate;
@protocol TTURLResponse; @protocol TTURLResponse;


Expand All @@ -26,7 +28,7 @@
* processing using TTURLResponse objects. * processing using TTURLResponse objects.
*/ */
@interface TTURLRequest : NSObject { @interface TTURLRequest : NSObject {
NSString* _URL; NSString* _urlPath;


NSString* _httpMethod; NSString* _httpMethod;
NSData* _httpBody; NSData* _httpBody;
Expand Down Expand Up @@ -60,9 +62,17 @@
} }


/** /**
* The URL to be loaded by the request. * The URL path to be loaded by the request.
*/
@property (nonatomic, copy) NSString* urlPath;

/**
* The URL path to be loaded by the request.
*
* Deprecated due to name ambiguity. Use urlPath instead.
* Remove after May 6, 2010.
*/ */
@property (nonatomic, copy) NSString* URL; @property (nonatomic, copy) NSString* URL __TTDEPRECATED_METHOD;


/** /**
* The HTTP method to send with the request. * The HTTP method to send with the request.
Expand Down

0 comments on commit f762dc7

Please sign in to comment.