From 25188c661c80a34dcef0e1c51ee9cc2de77ce875 Mon Sep 17 00:00:00 2001 From: rfm Date: Tue, 15 May 2007 05:55:02 +0000 Subject: [PATCH] Simplify and save a little memory git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25156 72102866-910b-0410-8b05-ffd578937521 --- Source/NSURLConnection.m | 69 ++++++++++++++++++++-------------------- Source/NSURLProtocol.m | 48 +++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 36 deletions(-) diff --git a/Source/NSURLConnection.m b/Source/NSURLConnection.m index 796dbb5e9..f095278ef 100644 --- a/Source/NSURLConnection.m +++ b/Source/NSURLConnection.m @@ -137,21 +137,18 @@ redirectResponse: (NSURLResponse*)redirectResponse @end -@interface GSURLConnection : NSObject +typedef struct { -@public - NSURLConnection *_parent; // Not retained NSURLRequest *_request; NSURLProtocol *_protocol; id _delegate; // Not retained -} -@end +} Internal; typedef struct { @defs(NSURLConnection) } priv; -#define this ((GSURLConnection*)(((priv*)self)->_NSURLConnectionInternal)) -#define inst ((GSURLConnection*)(((priv*)o)->_NSURLConnectionInternal)) +#define this ((Internal*)(((priv*)self)->_NSURLConnectionInternal)) +#define inst ((Internal*)(((priv*)o)->_NSURLConnectionInternal)) @implementation NSURLConnection @@ -161,9 +158,8 @@ typedef struct { if (o != nil) { - o->_NSURLConnectionInternal - = NSAllocateObject([GSURLConnection class], 0, z); - inst->_parent = o; + o->_NSURLConnectionInternal = NSZoneCalloc(GSObjCZone(self), + 1, sizeof(Internal)); } return o; } @@ -184,7 +180,13 @@ typedef struct { - (void) dealloc { - RELEASE(this); + if (this != 0) + { + RELEASE(this->_protocol); + RELEASE(this->_request); + NSZoneFree([self zone], this); + _NSURLConnectionInternal = 0; + } [super dealloc]; } @@ -200,9 +202,10 @@ typedef struct { { this->_request = [request copy]; this->_delegate = delegate; - this->_protocol = [[NSURLProtocol alloc] initWithRequest: this->_request - cachedResponse: nil - client: this]; + this->_protocol = [[NSURLProtocol alloc] + initWithRequest: this->_request + cachedResponse: nil + client: (id)self]; [this->_protocol startLoading]; } return self; @@ -299,14 +302,7 @@ typedef struct { @end -@implementation GSURLConnection - -- (void) dealloc -{ - RELEASE(_protocol); - RELEASE(_request); - [super dealloc]; -} +@implementation NSURLConnection (URLProtocolClient) - (void) URLProtocol: (NSURLProtocol *)protocol cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse @@ -317,26 +313,27 @@ typedef struct { - (void) URLProtocol: (NSURLProtocol *)protocol didFailWithError: (NSError *)error { - [_delegate connection: _parent didFailWithError: error]; + [this->_delegate connection: self didFailWithError: error]; } - (void) URLProtocol: (NSURLProtocol *)protocol didLoadData: (NSData *)data { - [_delegate connection: _parent didReceiveData: data]; + [this->_delegate connection: self didReceiveData: data]; } - (void) URLProtocol: (NSURLProtocol *)protocol didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge { - [_delegate connection: _parent didReceiveAuthenticationChallenge: challenge]; + [this->_delegate connection: self + didReceiveAuthenticationChallenge: challenge]; } - (void) URLProtocol: (NSURLProtocol *)protocol didReceiveResponse: (NSURLResponse *)response cacheStoragePolicy: (NSURLCacheStoragePolicy)policy { - [_delegate connection: _parent didReceiveResponse: response]; + [this->_delegate connection: self didReceiveResponse: response]; if (policy == NSURLCacheStorageAllowed || policy == NSURLCacheStorageAllowedInMemoryOnly) { @@ -348,9 +345,9 @@ typedef struct { wasRedirectedToRequest: (NSURLRequest *)request redirectResponse: (NSURLResponse *)redirectResponse { - request = [_delegate connection: _parent - willSendRequest: request - redirectResponse: redirectResponse]; + request = [this->_delegate connection: self + willSendRequest: request + redirectResponse: redirectResponse]; if (this->_protocol == nil) { /* Our protocol is nil, so we have been cancelled by the delegate. @@ -361,25 +358,27 @@ typedef struct { { /* Follow the redirect ... stop the old load and start a new one. */ - [_protocol stopLoading]; + [this->_protocol stopLoading]; DESTROY(this->_protocol); ASSIGNCOPY(this->_request, request); - this->_protocol = [[NSURLProtocol alloc] initWithRequest: this->_request - cachedResponse: nil - client: this]; + this->_protocol = [[NSURLProtocol alloc] + initWithRequest: this->_request + cachedResponse: nil + client: (id)self]; [this->_protocol startLoading]; } } - (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol { - [_delegate connectionDidFinishLoading: _parent]; + [this->_delegate connectionDidFinishLoading: self]; } - (void) URLProtocol: (NSURLProtocol *)protocol didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge { - [_delegate connection: _parent didCancelAuthenticationChallenge: challenge]; + [this->_delegate connection: self + didCancelAuthenticationChallenge: challenge]; } @end diff --git a/Source/NSURLProtocol.m b/Source/NSURLProtocol.m index 49340cc19..ec01656c8 100644 --- a/Source/NSURLProtocol.m +++ b/Source/NSURLProtocol.m @@ -345,9 +345,55 @@ static NSURLProtocol *placeholder = nil; NSLog(@"startLoading when load in progress"); return; } + _isLoading = YES; _complete = NO; - _debug = NO; + _debug = YES; + + /* Perform a redirect if the path is empty. + * As per MacOs-X documentation. + */ + if ([[[this->request URL] path] length] == 0) + { + NSURLRequest *request; + NSString *s = [[this->request URL] absoluteString]; + NSURL *url; + + if ([s rangeOfString: @"?"].length > 0) + { + s = [s stringByReplacingString: @"?" withString: @"/?"]; + } + else if ([s rangeOfString: @"#"].length > 0) + { + s = [s stringByReplacingString: @"#" withString: @"/#"]; + } + else + { + s = [s stringByAppendingString: @"/"]; + } + url = [NSURL URLWithString: s]; + request = [NSURLRequest requestWithURL: url]; + if (request == nil) + { + NSError *e; + + e = [NSError errorWithDomain: @"Invalid redirect request" + code: 0 + userInfo: nil]; + [this->client URLProtocol: self + didFailWithError: e]; + } + else + { + [this->client URLProtocol: self + wasRedirectedToRequest: request + redirectResponse: nil]; + } + if (_isLoading == NO) + { + return; + } + } if (0 && this->cachedResponse) {