mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 00:30:53 +00:00
Safer debug logging for http request/response
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38946 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
7b9aa12650
commit
6b1fb1d5a1
4 changed files with 130 additions and 57 deletions
|
@ -1,6 +1,9 @@
|
||||||
2015-08-29 Richard Frith-Macdonald <rfm@gnu.org>
|
2015-08-29 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
* Source/Additions/GSMime.m: Improve descriptions for debug.
|
* Source/Additions/GSMime.m: Improve descriptions for debug.
|
||||||
|
* Source/Additions/NSData+GNUstepBase.m: Add escaped representation.
|
||||||
|
* Source/GSHTTPURLHandle.m: Use escaped text format for debug log
|
||||||
|
(and plist armored format) to ensure that we safely log binary data
|
||||||
|
|
||||||
2015-08-24 Richard Frith-Macdonald <rfm@gnu.org>
|
2015-08-24 Richard Frith-Macdonald <rfm@gnu.org>
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,18 @@ extern "C" {
|
||||||
*/
|
*/
|
||||||
+ (id) dataWithRandomBytesOfLength: (NSUInteger)length;
|
+ (id) dataWithRandomBytesOfLength: (NSUInteger)length;
|
||||||
|
|
||||||
|
/** Returns an NSString object containing a backslash escaped representation
|
||||||
|
* of the receiver.
|
||||||
|
*/
|
||||||
|
- (NSString*) escapedRepresentation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a buffer containing an ASCII backslash escaped representation
|
||||||
|
* of the receiver, (and optionally the size of the buffer excluding
|
||||||
|
* the trailing nul terminator).
|
||||||
|
*/
|
||||||
|
- (char*) escapedRepresentation: (NSUInteger*)length;
|
||||||
|
|
||||||
/** Returns data formed by gunzipping the contents of the receiver.<br />
|
/** Returns data formed by gunzipping the contents of the receiver.<br />
|
||||||
* If the receiver did not contain data produced by gzip, this method
|
* If the receiver did not contain data produced by gzip, this method
|
||||||
* simply returns the receiver.<br />
|
* simply returns the receiver.<br />
|
||||||
|
@ -70,23 +82,22 @@ extern "C" {
|
||||||
*/
|
*/
|
||||||
- (BOOL) isGzipped;
|
- (BOOL) isGzipped;
|
||||||
|
|
||||||
/**
|
/** Returns an NSString object containing an ASCII hexadecimal representation
|
||||||
* Returns an NSString object containing an ASCII hexadecimal representation
|
|
||||||
* of the receiver. This means that the returned object will contain
|
* of the receiver. This means that the returned object will contain
|
||||||
* exactly twice as many characters as there are bytes as the receiver,
|
* exactly twice as many characters as there are bytes as the receiver,
|
||||||
* as each byte in the receiver is represented by two hexadecimal digits.<br />
|
* as each byte in the receiver is represented by two hexadecimal digits.<br />
|
||||||
* The high order four bits of each byte is encoded before the low
|
* The high order four bits of each byte is encoded before the low
|
||||||
* order four bits. Capital letters 'A' to 'F' are used to represent
|
* order four bits. Capital letters 'A' to 'F' are used to represent
|
||||||
* values from 10 to 15.<br />
|
* values from 10 to 15.
|
||||||
* If you need the hexadecimal representation as raw byte data, use code
|
|
||||||
* like -
|
|
||||||
* <example>
|
|
||||||
* hexData = [[sourceData hexadecimalRepresentation]
|
|
||||||
* dataUsingEncoding: NSASCIIStringEncoding];
|
|
||||||
* </example>
|
|
||||||
*/
|
*/
|
||||||
- (NSString*) hexadecimalRepresentation;
|
- (NSString*) hexadecimalRepresentation;
|
||||||
|
|
||||||
|
/** Returns a buffer containing an ASCII string with a nul terminated
|
||||||
|
* hexadecimal representation of the receiver, (and optionally the size
|
||||||
|
* of the buffer excluding the trailing nul terminator).
|
||||||
|
*/
|
||||||
|
- (char*) hexadecimalRepresentation: (NSUInteger*)length;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialises the receiver with the supplied string data which contains
|
* Initialises the receiver with the supplied string data which contains
|
||||||
* a hexadecimal coding of the bytes. The parsing of the string is
|
* a hexadecimal coding of the bytes. The parsing of the string is
|
||||||
|
|
|
@ -116,33 +116,107 @@ randombytes(uint8_t *buf, unsigned len)
|
||||||
return AUTORELEASE(d);
|
return AUTORELEASE(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
- (NSString*) escapedRepresentation
|
||||||
* Returns an NSString object containing an ASCII hexadecimal representation
|
{
|
||||||
* of the receiver. This means that the returned object will contain
|
char *buf;
|
||||||
* exactly twice as many characters as there are bytes as the receiver,
|
NSUInteger len;
|
||||||
* as each byte in the receiver is represented by two hexadecimal digits.<br />
|
NSString *string;
|
||||||
* The high order four bits of each byte is encoded before the low
|
|
||||||
* order four bits. Capital letters 'A' to 'F' are used to represent
|
buf = [self escapedRepresentation: &len];
|
||||||
* values from 10 to 15.<br />
|
string = [[NSString alloc] initWithBytesNoCopy: buf
|
||||||
* If you need the hexadecimal representation as raw byte data, use code
|
length: len
|
||||||
* like -
|
encoding: NSASCIIStringEncoding
|
||||||
* <example>
|
freeWhenDone: YES];
|
||||||
* hexData = [[sourceData hexadecimalRepresentation]
|
return AUTORELEASE(string);
|
||||||
* dataUsingEncoding: NSASCIIStringEncoding];
|
}
|
||||||
* </example>
|
|
||||||
*/
|
- (char*) escapedRepresentation: (NSUInteger*)length
|
||||||
|
{
|
||||||
|
const uint8_t *bytes = (const uint8_t*)[self bytes];
|
||||||
|
uint8_t *buf;
|
||||||
|
NSUInteger count = [self length];
|
||||||
|
NSUInteger size = count + 1;
|
||||||
|
NSUInteger index;
|
||||||
|
NSUInteger pos;
|
||||||
|
|
||||||
|
for (index = 0; index < count; index++)
|
||||||
|
{
|
||||||
|
uint8_t b = bytes[index];
|
||||||
|
|
||||||
|
if ('\n' == b) size++;
|
||||||
|
else if ('\r' == b) size++;
|
||||||
|
else if ('\t' == b) size++;
|
||||||
|
else if ('\\' == b) size++;
|
||||||
|
else if (!isprint(b)) size += 3;
|
||||||
|
}
|
||||||
|
buf = (uint8_t*)malloc(size);
|
||||||
|
for (pos = index = 0; index < count; index++)
|
||||||
|
{
|
||||||
|
uint8_t b = bytes[index];
|
||||||
|
|
||||||
|
if ('\n' == b)
|
||||||
|
{
|
||||||
|
buf[pos++] = '\\';
|
||||||
|
buf[pos++] = 'n';
|
||||||
|
}
|
||||||
|
else if ('\r' == b)
|
||||||
|
{
|
||||||
|
buf[pos++] = '\\';
|
||||||
|
buf[pos++] = 'r';
|
||||||
|
}
|
||||||
|
else if ('\t' == b)
|
||||||
|
{
|
||||||
|
buf[pos++] = '\\';
|
||||||
|
buf[pos++] = 't';
|
||||||
|
}
|
||||||
|
else if ('\\' == b)
|
||||||
|
{
|
||||||
|
buf[pos++] = '\\';
|
||||||
|
buf[pos++] = '\\';
|
||||||
|
}
|
||||||
|
else if (!isprint(b))
|
||||||
|
{
|
||||||
|
sprintf((char*)&buf[pos], "\\x%02x", b);
|
||||||
|
pos += 4;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buf[pos++] = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buf[pos] = '\0';
|
||||||
|
if (0 != length)
|
||||||
|
{
|
||||||
|
*length = pos;
|
||||||
|
}
|
||||||
|
return (char*)buf;
|
||||||
|
}
|
||||||
|
|
||||||
- (NSString*) hexadecimalRepresentation
|
- (NSString*) hexadecimalRepresentation
|
||||||
|
{
|
||||||
|
char *buf;
|
||||||
|
NSUInteger len;
|
||||||
|
NSString *string;
|
||||||
|
|
||||||
|
buf = [self hexadecimalRepresentation: &len];
|
||||||
|
string = [[NSString alloc] initWithBytesNoCopy: buf
|
||||||
|
length: len
|
||||||
|
encoding: NSASCIIStringEncoding
|
||||||
|
freeWhenDone: YES];
|
||||||
|
return AUTORELEASE(string);
|
||||||
|
}
|
||||||
|
|
||||||
|
- (char*) hexadecimalRepresentation: (NSUInteger*)length
|
||||||
{
|
{
|
||||||
static const char *hexChars = "0123456789ABCDEF";
|
static const char *hexChars = "0123456789ABCDEF";
|
||||||
unsigned slen = [self length];
|
unsigned slen = [self length];
|
||||||
unsigned dlen = slen * 2;
|
unsigned dlen = slen * 2;
|
||||||
const unsigned char *src = (const unsigned char *)[self bytes];
|
const unsigned char *src = (const unsigned char *)[self bytes];
|
||||||
char *dst = (char*)NSZoneMalloc(NSDefaultMallocZone(), dlen);
|
char *dst;
|
||||||
unsigned spos = 0;
|
unsigned spos = 0;
|
||||||
unsigned dpos = 0;
|
unsigned dpos = 0;
|
||||||
NSData *data;
|
|
||||||
NSString *string;
|
|
||||||
|
|
||||||
|
dst = (char*)malloc(dlen + 1);
|
||||||
while (spos < slen)
|
while (spos < slen)
|
||||||
{
|
{
|
||||||
unsigned char c = src[spos++];
|
unsigned char c = src[spos++];
|
||||||
|
@ -150,12 +224,12 @@ randombytes(uint8_t *buf, unsigned len)
|
||||||
dst[dpos++] = hexChars[(c >> 4) & 0x0f];
|
dst[dpos++] = hexChars[(c >> 4) & 0x0f];
|
||||||
dst[dpos++] = hexChars[c & 0x0f];
|
dst[dpos++] = hexChars[c & 0x0f];
|
||||||
}
|
}
|
||||||
data = [NSData allocWithZone: NSDefaultMallocZone()];
|
dst[dpos] = '\0';
|
||||||
data = [data initWithBytesNoCopy: dst length: dlen];
|
if (0 != length)
|
||||||
string = [[NSString alloc] initWithData: data
|
{
|
||||||
encoding: NSASCIIStringEncoding];
|
*length = dpos;
|
||||||
RELEASE(data);
|
}
|
||||||
return AUTORELEASE(string);
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSData*) gunzipped
|
- (NSData*) gunzipped
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
#import "Foundation/NSValue.h"
|
#import "Foundation/NSValue.h"
|
||||||
#import "GNUstepBase/GSMime.h"
|
#import "GNUstepBase/GSMime.h"
|
||||||
#import "GNUstepBase/GSLock.h"
|
#import "GNUstepBase/GSLock.h"
|
||||||
|
#import "GNUstepBase/NSData+GNUstepBase.h"
|
||||||
#import "GNUstepBase/NSString+GNUstepBase.h"
|
#import "GNUstepBase/NSString+GNUstepBase.h"
|
||||||
#import "GNUstepBase/NSURL+GNUstepBase.h"
|
#import "GNUstepBase/NSURL+GNUstepBase.h"
|
||||||
#import "NSCallBacks.h"
|
#import "NSCallBacks.h"
|
||||||
|
@ -230,36 +231,20 @@ static Class sslClass = 0;
|
||||||
static void
|
static void
|
||||||
debugRead(GSHTTPURLHandle *handle, NSData *data)
|
debugRead(GSHTTPURLHandle *handle, NSData *data)
|
||||||
{
|
{
|
||||||
int len = (int)[data length];
|
unsigned len = (unsigned)[data length];
|
||||||
const char *ptr = (const char*)[data bytes];
|
char *esc = [data escapedRepresentation: 0];
|
||||||
int pos;
|
|
||||||
|
|
||||||
for (pos = 0; pos < len; pos++)
|
NSLog(@"Read for %p of %u bytes - '%s'\n%@", handle, len, esc, data);
|
||||||
{
|
free(esc);
|
||||||
if (0 == ptr[pos])
|
|
||||||
{
|
|
||||||
NSLog(@"Read for %p of %d bytes - %@", handle, len, data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NSLog(@"Read for %p of %d bytes - '%*.*s'", handle, len, len, len, ptr);
|
|
||||||
}
|
}
|
||||||
static void
|
static void
|
||||||
debugWrite(GSHTTPURLHandle *handle, NSData *data)
|
debugWrite(GSHTTPURLHandle *handle, NSData *data)
|
||||||
{
|
{
|
||||||
int len = (int)[data length];
|
unsigned len = (unsigned)[data length];
|
||||||
const char *ptr = (const char*)[data bytes];
|
char *esc = [data escapedRepresentation: 0];
|
||||||
int pos = len;
|
|
||||||
|
|
||||||
for (pos = 0; pos < len; pos++)
|
NSLog(@"Write for %p of %u bytes - '%s'\n%@", handle, len, esc, data);
|
||||||
{
|
free(esc);
|
||||||
if (0 == ptr[pos])
|
|
||||||
{
|
|
||||||
NSLog(@"Write for %p of %d bytes - %@", handle, len, data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NSLog(@"Write for %p of %d bytes -'%*.*s'", handle, len, len, len, ptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl
|
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue