From 6908b4724d9a009244b05c56b1143b5cf1ad2b96 Mon Sep 17 00:00:00 2001
From: Richard Frith-MacDonald
You never instantiate NSURLProtocol yourself ... it should only - * ever be done by other classes within mthe URL loading system. + * ever be done by other classes within the URL loading system. *
*/ @interface NSURLProtocol : NSObject diff --git a/Source/GSHTTPURLHandle.m b/Source/GSHTTPURLHandle.m index 43bcdeb19..a14bae765 100644 --- a/Source/GSHTTPURLHandle.m +++ b/Source/GSHTTPURLHandle.m @@ -228,56 +228,21 @@ static NSLock *urlLock = nil; static Class sslClass = 0; -static NSLock *debugLock = nil; -static NSString *debugFile; - -static void debugRead(GSHTTPURLHandle *handle, NSData *data) +static void +debugRead(GSHTTPURLHandle *handle, NSData *data) { - NSString *s; - int d; + int len = (int)[data length]; + const char *ptr = (const char*)[data bytes]; - [debugLock lock]; -#if defined(__MINGW__) - d = _wopen((const unichar*)[debugFile fileSystemRepresentation], - O_WRONLY|O_CREAT|O_APPEND, 0644); -#else - d = open([debugFile fileSystemRepresentation], - O_WRONLY|O_CREAT|O_APPEND, 0644); -#endif - if (d >= 0) - { - s = [NSString stringWithFormat: @"\nRead for %p at %@ %u bytes - '", - handle, [NSDate date], [data length]]; - write(d, [s cString], [s cStringLength]); - write(d, [data bytes], [data length]); - write(d, "'", 1); - close(d); - } - [debugLock unlock]; + NSLog(@"Read for %p of %d bytes -'%*.*s'", handle, len, len, len, ptr); } -static void debugWrite(GSHTTPURLHandle *handle, NSData *data) +static void +debugWrite(GSHTTPURLHandle *handle, NSData *data) { - NSString *s; - int d; + int len = (int)[data length]; + const char *ptr = (const char*)[data bytes]; - [debugLock lock]; -#if defined(__MINGW__) - d = _wopen((const unichar*)[debugFile fileSystemRepresentation], - O_WRONLY|O_CREAT|O_APPEND, 0644); -#else - d = open([debugFile fileSystemRepresentation], - O_WRONLY|O_CREAT|O_APPEND, 0644); -#endif - if (d >= 0) - { - s = [NSString stringWithFormat: @"\nWrite for %p at %@ %u bytes - '", - handle, [NSDate date], [data length]]; - write(d, [s cString], [s cStringLength]); - write(d, [data bytes], [data length]); - write(d, "'", 1); - close(d); - } - [debugLock unlock]; + NSLog(@"Write for %p of %d bytes -'%*.*s'", handle, len, len, len, ptr); } + (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl @@ -310,12 +275,6 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) urlCache = [NSMutableDictionary new]; urlOrder = [NSMutableArray new]; urlLock = [GSLazyLock new]; - debugLock = [GSLazyLock new]; - debugFile = [NSString stringWithFormat: @"%@/GSHTTP.%d", - NSTemporaryDirectory(), - [[NSProcessInfo processInfo] processIdentifier]]; - IF_NO_GC([debugFile retain];) - #if !defined(__MINGW__) sslClass = [NSFileHandle sslClass]; #endif @@ -352,6 +311,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) { if ((self = [super initWithURL: newUrl cached: cached]) != nil) { + debug = GSDebugSet(@"NSURLHandle"); dat = [NSMutableData new]; pageInfo = [NSMutableDictionary new]; wProperties = NSCreateMapTable(writeKeyCallBacks, @@ -554,7 +514,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) /* * Send request to server. */ - if (debug == YES) debugWrite(self, buf); + if (YES == debug) debugWrite(self, buf); [sock writeInBackgroundAndNotify: buf]; RELEASE(buf); RELEASE(s); @@ -574,7 +534,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) if (debug) NSLog(@"%@ %p %s", NSStringFromSelector(_cmd), self, keepalive?"K":""); d = [dict objectForKey: NSFileHandleNotificationDataItem]; - if (debug == YES) debugRead(self, d); + if (YES == debug) debugRead(self, d); readCount = [d length]; if (connectionState == idle) @@ -584,7 +544,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) * it should just be the connection being closed by the other * end because of a timeout etc. */ - if (debug == YES && [d length] != 0) + if (YES == debug && [d length] != 0) { NSLog(@"%@ %p %s Unexpected data (%*.*s) from remote!", NSStringFromSelector(_cmd), self, keepalive?"K":"", @@ -596,7 +556,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) } else if ([parser parse: d] == NO && [parser isComplete] == NO) { - if (debug == YES) + if (YES == debug) { NSLog(@"HTTP parse failure - %@", parser); } @@ -826,7 +786,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) * lost in the network or the remote end received it and * the response was lost. */ - if (debug == YES) + if (YES == debug) { NSLog(@"HTTP response not received - %@", parser); } @@ -856,7 +816,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) if (debug) NSLog(@"%@ %p %s", NSStringFromSelector(_cmd), self, keepalive?"K":""); d = [dict objectForKey: NSFileHandleNotificationDataItem]; - if (debug == YES) debugRead(self, d); + if (YES == debug) debugRead(self, d); if ([d length] > 0) { @@ -1012,7 +972,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data) object: sock]; buf = [cmd dataUsingEncoding: NSASCIIStringEncoding]; - if (debug == YES) debugWrite(self, buf); + if (YES == debug) debugWrite(self, buf); [sock writeInBackgroundAndNotify: buf]; when = [NSDate alloc]; diff --git a/Source/NSURLHandle.m b/Source/NSURLHandle.m index 0741fb733..f36f070d3 100644 --- a/Source/NSURLHandle.m +++ b/Source/NSURLHandle.m @@ -548,6 +548,14 @@ static Class NSURLHandleClass = 0; return [self availableResourceData]; } +/* Private method ... subclasses override this to enable debug to be + * turned off and on. + */ +- (void) setDebug: (BOOL)aFlag +{ + return; +} + /** * Returns the current status of the handle. */ diff --git a/Source/NSURLProtocol.m b/Source/NSURLProtocol.m index f4bd8de8e..b310fa335 100644 --- a/Source/NSURLProtocol.m +++ b/Source/NSURLProtocol.m @@ -536,6 +536,14 @@ static NSURLProtocol *placeholder = nil; return this->request; } +/* This method is here so that it's safe to set debug on any NSURLProtocol + * even if the concrete subclass doesn't actually support debug logging. + */ +- (void) setDebug: (BOOL)flag +{ + return; +} + @end @@ -615,6 +623,15 @@ static NSURLProtocol *placeholder = nil; [super dealloc]; } +- (id) init +{ + if (nil != (self = [super init])) + { + _debug = GSDebugSet(@"NSURLProtocol"); + } + return self; +} + - (void) setDebug: (BOOL)flag { _debug = flag; @@ -656,7 +673,6 @@ static NSURLProtocol *placeholder = nil; _statusCode = 0; /* No status returned yet. */ _isLoading = YES; _complete = NO; - _debug = GSDebugSet(@"NSHTTPURLProtocol"); /* Perform a redirect if the path is empty. * As per MacOs-X documentation.