From 05a94eb676a35f091098b2c60ffcbfda096d56a9 Mon Sep 17 00:00:00 2001 From: CaS Date: Mon, 31 Oct 2005 14:01:18 +0000 Subject: [PATCH] Make URL clients a bit safer. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@21929 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++++++ Source/GSHTTPURLHandle.m | 19 +++++++++++++++++-- Source/NSURLHandle.m | 2 ++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 232f8e706..eea229c44 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2005-10-31 Richard Frith-Macdonald + + * Source/GSHTTPURLHandle.m: retain self for the duration of I/O + handler methods where we may make callbacks to clients. In case + the client owns the handle, and decides to release it. + 2005-10-30 Richard Frith-Macdonald * Source/NSLog.m: Apply patch #5399 from Jeremy Bettis. diff --git a/Source/GSHTTPURLHandle.m b/Source/GSHTTPURLHandle.m index 5cc7f7fbd..d1bc039bc 100644 --- a/Source/GSHTTPURLHandle.m +++ b/Source/GSHTTPURLHandle.m @@ -368,6 +368,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) NSString *version; NSMapEnumerator enumerator; + RETAIN(self); if (debug) NSLog(@"%@ %s", NSStringFromSelector(_cmd), keepalive?"K":""); s = [basic mutableCopy]; @@ -457,6 +458,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) [sock writeInBackgroundAndNotify: buf]; RELEASE(buf); RELEASE(s); + RELEASE(self); } - (void) bgdRead: (NSNotification*) not @@ -466,6 +468,8 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) NSData *d; NSRange r; + RETAIN(self); + if (debug) NSLog(@"%@ %s", NSStringFromSelector(_cmd), keepalive?"K":""); d = [dict objectForKey: NSFileHandleNotificationDataItem]; if (debug == YES) debugRead(self, d); @@ -596,11 +600,13 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) loadComplete: NO]; } } - if (sock != nil) + if (sock != nil + && (connectionState == reading || connectionState == idle)) { [sock readInBackgroundAndNotify]; } } + RELEASE(self); } - (void) bgdTunnelRead: (NSNotification*) not @@ -610,6 +616,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) NSData *d; GSMimeParser *p = [GSMimeParser new]; + RETAIN(self); if (debug) NSLog(@"%@ %s", NSStringFromSelector(_cmd), keepalive?"K":""); d = [dict objectForKey: NSFileHandleNotificationDataItem]; if (debug == YES) debugRead(self, d); @@ -646,6 +653,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) [sock readInBackgroundAndNotify]; } RELEASE(p); + RELEASE(self); } - (void) loadInBackground @@ -680,13 +688,13 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) - (void) bgdConnect: (NSNotification*)notification { NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; - NSDictionary *userInfo = [notification userInfo]; NSMutableString *s; NSString *e; NSString *method; NSString *path; + RETAIN(self); if (debug) NSLog(@"%@ %s", NSStringFromSelector(_cmd), keepalive?"K":""); path = [[u path] stringByTrimmingSpaces]; @@ -709,6 +717,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) [self endLoadInBackground]; [self backgroundLoadDidFailWithReason: [NSString stringWithFormat: @"Failed to connect: %@", e]]; + RELEASE(self); return; } @@ -789,6 +798,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) { [self endLoadInBackground]; [self backgroundLoadDidFailWithReason: @"Failed proxy tunneling"]; + RELEASE(self); return; } } @@ -802,6 +812,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) [self endLoadInBackground]; [self backgroundLoadDidFailWithReason: @"Failed to make ssl connect"]; + RELEASE(self); return; } } @@ -843,6 +854,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) [self bgdApply: s]; RELEASE(s); + RELEASE(self); } - (void) bgdWrite: (NSNotification*)notification @@ -851,6 +863,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) NSDictionary *userInfo = [notification userInfo]; NSString *e; + RETAIN(self); if (debug) NSLog(@"%@ %s", NSStringFromSelector(_cmd), keepalive?"K":""); e = [userInfo objectForKey: GSFileHandleNotificationError]; if (e != nil) @@ -880,6 +893,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) [self endLoadInBackground]; [self backgroundLoadDidFailWithReason: [NSString stringWithFormat: @"Failed to write request: %@", e]]; + RELEASE(self); return; } else @@ -916,6 +930,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) } connectionState = reading; } + RELEASE(self); } /** diff --git a/Source/NSURLHandle.m b/Source/NSURLHandle.m index 23d4dbeb8..a656a4652 100644 --- a/Source/NSURLHandle.m +++ b/Source/NSURLHandle.m @@ -317,10 +317,12 @@ static Class NSURLHandleClass = 0; */ - (void) cancelLoadInBackground { + RETAIN(self); [_clients makeObjectsPerformSelector: @selector(URLHandleResourceDidCancelLoading:) withObject: self]; [self endLoadInBackground]; + RELEASE(self); } - (void) dealloc