From 3633b11e59c0a52596f1ff8a047effebed395c3a Mon Sep 17 00:00:00 2001 From: rfm Date: Wed, 16 May 2007 16:02:34 +0000 Subject: [PATCH] Minor cleanups git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25175 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 +++++ Source/NSURLConnection.m | 2 +- Source/NSURLProtocol.m | 53 +++++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9a99adb16..3ae77797e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2007-05-16 Richard Frith-Macdonald + + * Source/NSURLConnection.m: Cancel when deallocating. + * Source/NSURLProtocol.m: Make sure load is stopped if an error + occurs. + 2007-05-15 Richard Frith-Macdonald * Source/NSException.m: Permit some recursion in handling uncaught diff --git a/Source/NSURLConnection.m b/Source/NSURLConnection.m index f095278ef..c32b55fda 100644 --- a/Source/NSURLConnection.m +++ b/Source/NSURLConnection.m @@ -182,7 +182,7 @@ typedef struct { { if (this != 0) { - RELEASE(this->_protocol); + [self cancel]; RELEASE(this->_request); NSZoneFree([self zone], this); _NSURLConnectionInternal = 0; diff --git a/Source/NSURLProtocol.m b/Source/NSURLProtocol.m index 4297b5119..bf42fa4fa 100644 --- a/Source/NSURLProtocol.m +++ b/Source/NSURLProtocol.m @@ -334,6 +334,7 @@ static NSURLProtocol *placeholder = nil; if ([methods objectForKey: [this->request HTTPMethod]] == nil) { NSLog(@"Invalid HTTP Method: %@", this->request); + [self stopLoading]; [this->client URLProtocol: self didFailWithError: [NSError errorWithDomain: @"Invalid HTTP Method" code: 0 @@ -380,6 +381,7 @@ static NSURLProtocol *placeholder = nil; e = [NSError errorWithDomain: @"Invalid redirect request" code: 0 userInfo: nil]; + [self stopLoading]; [this->client URLProtocol: self didFailWithError: e]; } @@ -431,6 +433,7 @@ static NSURLProtocol *placeholder = nil; { NSLog(@"did not create streams for %@:%@", host, [url port]); } + [self stopLoading]; [this->client URLProtocol: self didFailWithError: [NSError errorWithDomain: @"can't connect" code: 0 userInfo: [NSDictionary dictionaryWithObjectsAndKeys: @@ -497,7 +500,7 @@ static NSURLProtocol *placeholder = nil; { NSLog(@"receive error %@", e); } - [self _unschedule]; + [self stopLoading]; [this->client URLProtocol: self didFailWithError: e]; } return; @@ -523,7 +526,7 @@ static NSURLProtocol *placeholder = nil; e = [NSError errorWithDomain: @"parse error" code: 0 userInfo: nil]; - [self _unschedule]; + [self stopLoading]; [this->client URLProtocol: self didFailWithError: e]; return; } @@ -612,6 +615,7 @@ static NSURLProtocol *placeholder = nil; e = [NSError errorWithDomain: @"Invalid redirect request" code: 0 userInfo: nil]; + [self stopLoading]; [this->client URLProtocol: self didFailWithError: e]; } @@ -747,26 +751,35 @@ static NSURLProtocol *placeholder = nil; #endif /* - * Tell superclass that we have successfully loaded the data. + * Tell superclass that we have successfully loaded the data + * (as long as we haven't had the load terminated by the client). */ - d = [_parser data]; - bodyLength = [d length]; - if (bodyLength > _parseOffset) + if (_isLoading == YES) { - if (_parseOffset > 0) - { - d = [d subdataWithRange: - NSMakeRange(_parseOffset, bodyLength - _parseOffset)]; - } - _parseOffset = bodyLength; - if (_isLoading == YES) + d = [_parser data]; + bodyLength = [d length]; + if (bodyLength > _parseOffset) { + if (_parseOffset > 0) + { + d = [d subdataWithRange: + NSMakeRange(_parseOffset, bodyLength - _parseOffset)]; + } + _parseOffset = bodyLength; [this->client URLProtocol: self didLoadData: d]; } + + /* Check again in case the client cancelled the load inside + * the URLProtocol:didLoadData: callback. + */ + if (_isLoading == YES) + { + _isLoading = NO; + [this->client URLProtocolDidFinishLoading: self]; + } } - [this->client URLProtocolDidFinishLoading: self]; } - else + else if (_isLoading == YES) { /* * Report partial data if possible. @@ -783,15 +796,12 @@ static NSURLProtocol *placeholder = nil; NSMakeRange(_parseOffset, [d length] - _parseOffset)]; } _parseOffset = bodyLength; - if (_isLoading == YES) - { - [this->client URLProtocol: self didLoadData: d]; - } + [this->client URLProtocol: self didLoadData: d]; } } } - if (_complete == NO && readCount == 0) + if (_complete == NO && readCount == 0 && _isLoading == YES) { /* The read failed ... dropped, but parsing is not complete. * The request was sent, so we can't know whether it was @@ -802,6 +812,7 @@ static NSURLProtocol *placeholder = nil; { NSLog(@"HTTP response not received - %@", _parser); } + [self stopLoading]; [this->client URLProtocol: self didFailWithError: [NSError errorWithDomain: @"receive incomplete" code: 0 @@ -986,6 +997,7 @@ static NSURLProtocol *placeholder = nil; NSLog(@"error reading from HTTPBody stream %@", [NSError _last]); } + [self stopLoading]; [this->client URLProtocol: self didFailWithError: [NSError errorWithDomain: @"can't read body" code: 0 @@ -1050,6 +1062,7 @@ static NSURLProtocol *placeholder = nil; } } NSLog(@"An error %@ occurred on the event %08x of stream %@ of %@", [stream streamError], event, stream, self); + [self stopLoading]; [this->client URLProtocol: self didFailWithError: [stream streamError]]; }