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
This commit is contained in:
rfm 2007-05-15 05:55:02 +00:00
parent 2b31ab954f
commit 25188c661c
2 changed files with 81 additions and 36 deletions

View file

@ -137,21 +137,18 @@ redirectResponse: (NSURLResponse*)redirectResponse
@end @end
@interface GSURLConnection : NSObject <NSURLProtocolClient> typedef struct
{ {
@public
NSURLConnection *_parent; // Not retained
NSURLRequest *_request; NSURLRequest *_request;
NSURLProtocol *_protocol; NSURLProtocol *_protocol;
id _delegate; // Not retained id _delegate; // Not retained
} } Internal;
@end
typedef struct { typedef struct {
@defs(NSURLConnection) @defs(NSURLConnection)
} priv; } priv;
#define this ((GSURLConnection*)(((priv*)self)->_NSURLConnectionInternal)) #define this ((Internal*)(((priv*)self)->_NSURLConnectionInternal))
#define inst ((GSURLConnection*)(((priv*)o)->_NSURLConnectionInternal)) #define inst ((Internal*)(((priv*)o)->_NSURLConnectionInternal))
@implementation NSURLConnection @implementation NSURLConnection
@ -161,9 +158,8 @@ typedef struct {
if (o != nil) if (o != nil)
{ {
o->_NSURLConnectionInternal o->_NSURLConnectionInternal = NSZoneCalloc(GSObjCZone(self),
= NSAllocateObject([GSURLConnection class], 0, z); 1, sizeof(Internal));
inst->_parent = o;
} }
return o; return o;
} }
@ -184,7 +180,13 @@ typedef struct {
- (void) dealloc - (void) dealloc
{ {
RELEASE(this); if (this != 0)
{
RELEASE(this->_protocol);
RELEASE(this->_request);
NSZoneFree([self zone], this);
_NSURLConnectionInternal = 0;
}
[super dealloc]; [super dealloc];
} }
@ -200,9 +202,10 @@ typedef struct {
{ {
this->_request = [request copy]; this->_request = [request copy];
this->_delegate = delegate; this->_delegate = delegate;
this->_protocol = [[NSURLProtocol alloc] initWithRequest: this->_request this->_protocol = [[NSURLProtocol alloc]
cachedResponse: nil initWithRequest: this->_request
client: this]; cachedResponse: nil
client: (id<NSURLProtocolClient>)self];
[this->_protocol startLoading]; [this->_protocol startLoading];
} }
return self; return self;
@ -299,14 +302,7 @@ typedef struct {
@end @end
@implementation GSURLConnection @implementation NSURLConnection (URLProtocolClient)
- (void) dealloc
{
RELEASE(_protocol);
RELEASE(_request);
[super dealloc];
}
- (void) URLProtocol: (NSURLProtocol *)protocol - (void) URLProtocol: (NSURLProtocol *)protocol
cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse cachedResponseIsValid: (NSCachedURLResponse *)cachedResponse
@ -317,26 +313,27 @@ typedef struct {
- (void) URLProtocol: (NSURLProtocol *)protocol - (void) URLProtocol: (NSURLProtocol *)protocol
didFailWithError: (NSError *)error didFailWithError: (NSError *)error
{ {
[_delegate connection: _parent didFailWithError: error]; [this->_delegate connection: self didFailWithError: error];
} }
- (void) URLProtocol: (NSURLProtocol *)protocol - (void) URLProtocol: (NSURLProtocol *)protocol
didLoadData: (NSData *)data didLoadData: (NSData *)data
{ {
[_delegate connection: _parent didReceiveData: data]; [this->_delegate connection: self didReceiveData: data];
} }
- (void) URLProtocol: (NSURLProtocol *)protocol - (void) URLProtocol: (NSURLProtocol *)protocol
didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge didReceiveAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{ {
[_delegate connection: _parent didReceiveAuthenticationChallenge: challenge]; [this->_delegate connection: self
didReceiveAuthenticationChallenge: challenge];
} }
- (void) URLProtocol: (NSURLProtocol *)protocol - (void) URLProtocol: (NSURLProtocol *)protocol
didReceiveResponse: (NSURLResponse *)response didReceiveResponse: (NSURLResponse *)response
cacheStoragePolicy: (NSURLCacheStoragePolicy)policy cacheStoragePolicy: (NSURLCacheStoragePolicy)policy
{ {
[_delegate connection: _parent didReceiveResponse: response]; [this->_delegate connection: self didReceiveResponse: response];
if (policy == NSURLCacheStorageAllowed if (policy == NSURLCacheStorageAllowed
|| policy == NSURLCacheStorageAllowedInMemoryOnly) || policy == NSURLCacheStorageAllowedInMemoryOnly)
{ {
@ -348,9 +345,9 @@ typedef struct {
wasRedirectedToRequest: (NSURLRequest *)request wasRedirectedToRequest: (NSURLRequest *)request
redirectResponse: (NSURLResponse *)redirectResponse redirectResponse: (NSURLResponse *)redirectResponse
{ {
request = [_delegate connection: _parent request = [this->_delegate connection: self
willSendRequest: request willSendRequest: request
redirectResponse: redirectResponse]; redirectResponse: redirectResponse];
if (this->_protocol == nil) if (this->_protocol == nil)
{ {
/* Our protocol is nil, so we have been cancelled by the delegate. /* 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. /* Follow the redirect ... stop the old load and start a new one.
*/ */
[_protocol stopLoading]; [this->_protocol stopLoading];
DESTROY(this->_protocol); DESTROY(this->_protocol);
ASSIGNCOPY(this->_request, request); ASSIGNCOPY(this->_request, request);
this->_protocol = [[NSURLProtocol alloc] initWithRequest: this->_request this->_protocol = [[NSURLProtocol alloc]
cachedResponse: nil initWithRequest: this->_request
client: this]; cachedResponse: nil
client: (id<NSURLProtocolClient>)self];
[this->_protocol startLoading]; [this->_protocol startLoading];
} }
} }
- (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol - (void) URLProtocolDidFinishLoading: (NSURLProtocol *)protocol
{ {
[_delegate connectionDidFinishLoading: _parent]; [this->_delegate connectionDidFinishLoading: self];
} }
- (void) URLProtocol: (NSURLProtocol *)protocol - (void) URLProtocol: (NSURLProtocol *)protocol
didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge didCancelAuthenticationChallenge: (NSURLAuthenticationChallenge *)challenge
{ {
[_delegate connection: _parent didCancelAuthenticationChallenge: challenge]; [this->_delegate connection: self
didCancelAuthenticationChallenge: challenge];
} }
@end @end

View file

@ -345,9 +345,55 @@ static NSURLProtocol *placeholder = nil;
NSLog(@"startLoading when load in progress"); NSLog(@"startLoading when load in progress");
return; return;
} }
_isLoading = YES; _isLoading = YES;
_complete = NO; _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) if (0 && this->cachedResponse)
{ {