From 398528173149b2abdc879ece5ac36934717d523c Mon Sep 17 00:00:00 2001 From: rfm Date: Sat, 29 Aug 2015 21:13:14 +0000 Subject: [PATCH] tweak logging in NSURLProtocol to cope with nul bytes git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38950 72102866-910b-0410-8b05-ffd578937521 --- Source/NSURLProtocol.m | 66 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/Source/NSURLProtocol.m b/Source/NSURLProtocol.m index 107fbb216..ff120362a 100644 --- a/Source/NSURLProtocol.m +++ b/Source/NSURLProtocol.m @@ -36,6 +36,7 @@ #import "GSTLS.h" #import "GSURLPrivate.h" #import "GNUstepBase/GSMime.h" +#import "GNUstepBase/NSData+GNUstepBase.h" #import "GNUstepBase/NSString+GNUstepBase.h" #import "GNUstepBase/NSURL+GNUstepBase.h" @@ -46,6 +47,7 @@ #endif #define USE_ZLIB 0 + #if USE_ZLIB #if defined(HAVE_ZLIB_H) #include @@ -66,6 +68,61 @@ zfree(void *opaque, void *mem) #endif #endif +static void +debugRead(id handle, unsigned len, const unsigned char *ptr) +{ + int pos; + NSData *data; + + data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr + length: len + freeWhenDone: NO]; + + for (pos = 0; pos < len; pos++) + { + if (0 == ptr[pos]) + { + char *esc = [data escapedRepresentation: 0]; + + NSLog(@"Read for %p of %u bytes (escaped) - '%s'\n%@", + handle, len, esc, data); + free(esc); + RELEASE(data); + return; + } + } + NSLog(@"Read for %p of %d bytes - '%*.*s'\n%@", + handle, len, len, len, ptr, data); + RELEASE(data); +} +static void +debugWrite(id handle, unsigned len, const unsigned char *ptr) +{ + int pos; + NSData *data; + + data = [[NSData alloc] initWithBytesNoCopy: (void*)ptr + length: len + freeWhenDone: NO]; + + for (pos = 0; pos < len; pos++) + { + if (0 == ptr[pos]) + { + char *esc = [data escapedRepresentation: 0]; + + NSLog(@"Write for %p of %u bytes (escaped) - '%s'\n%@", + handle, len, esc, data); + free(esc); + RELEASE(data); + return; + } + } + NSLog(@"Write for %p of %d bytes - '%*.*s'\n%@", + handle, len, len, len, ptr, data); + RELEASE(data); +} + @interface GSSocketStreamPair : NSObject { NSInputStream *ip; @@ -887,8 +944,7 @@ static NSURLProtocol *placeholder = nil; } if (_debug) { - NSLog(@"%@ read %d bytes: '%*.*s'", - self, readCount, readCount, readCount, buffer); + debugRead(self, readCount, buffer); } if (_parser == nil) @@ -1508,8 +1564,7 @@ static NSURLProtocol *placeholder = nil; { if (_debug == YES) { - NSLog(@"%@ wrote %d bytes: '%*.*s'", self, written, - written, written, bytes + _writeOffset); + debugWrite(self, written, bytes + _writeOffset); } _writeOffset += written; if (_writeOffset >= len) @@ -1566,8 +1621,7 @@ static NSURLProtocol *placeholder = nil; { if (_debug == YES) { - NSLog(@"%@ wrote %d bytes: '%*.*s'", self, - written, written, written, buffer); + debugWrite(self, written, buffer); } len -= written; if (len > 0)