Added -description methods

This commit is contained in:
Pierre-Olivier Latour
2014-04-09 13:44:53 -07:00
parent 4008b5b476
commit 97929f7d89
10 changed files with 112 additions and 7 deletions

View File

@ -151,6 +151,17 @@ NSDate* GCDWebServerParseHTTPDate(NSString* string) {
return date; return date;
} }
NSString* GCDWebServerDescribeData(NSData* data, NSString* contentType) {
if ([contentType hasPrefix:@"text/"] || [contentType isEqualToString:@"application/json"] || [contentType isEqualToString:@"application/xml"]) {
NSString* charset = GCDWebServerExtractHeaderValueParameter(contentType, @"charset");
NSString* string = [[NSString alloc] initWithData:data encoding:GCDWebServerStringEncodingFromCharset(charset)];
if (string) {
return ARC_AUTORELEASE(string);
}
}
return [NSString stringWithFormat:@"<%lu bytes>", (unsigned long)data.length];
}
NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension) { NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension) {
static NSDictionary* _overrides = nil; static NSDictionary* _overrides = nil;
if (_overrides == nil) { if (_overrides == nil) {

View File

@ -70,6 +70,15 @@
return YES; return YES;
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithString:[super description]];
if (_data) {
[description appendString:@"\n\n"];
[description appendString:GCDWebServerDescribeData(_data, self.contentType)];
}
return description;
}
@end @end
@implementation GCDWebServerDataRequest (Extensions) @implementation GCDWebServerDataRequest (Extensions)

View File

@ -73,6 +73,13 @@
return data; return data;
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithString:[super description]];
[description appendString:@"\n\n"];
[description appendString:GCDWebServerDescribeData(_data, self.contentType)];
return description;
}
@end @end
@implementation GCDWebServerDataResponse (Extensions) @implementation GCDWebServerDataResponse (Extensions)

View File

@ -170,4 +170,10 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
close(_file); close(_file);
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithString:[super description]];
[description appendFormat:@"\n\n{%@}", _path];
return description;
}
@end @end

View File

@ -100,7 +100,7 @@ static NSData* _dashNewlineData = nil;
} }
- (NSString*)description { - (NSString*)description {
return [NSString stringWithFormat:@"<%@ | '%@' | %i bytes>", [self class], self.mimeType, (int)_data.length]; return [NSString stringWithFormat:@"<%@ | '%@' | %lu bytes>", [self class], self.mimeType, (unsigned long)_data.length];
} }
@end @end
@ -198,6 +198,14 @@ static NSData* _dashNewlineData = nil;
return self; return self;
} }
- (void)dealloc {
ARC_RELEASE(_arguments);
ARC_RELEASE(_files);
ARC_RELEASE(_boundary);
ARC_DEALLOC(super);
}
- (BOOL)open:(NSError**)error { - (BOOL)open:(NSError**)error {
_parserData = [[NSMutableData alloc] initWithCapacity:kMultiPartBufferSize]; _parserData = [[NSMutableData alloc] initWithCapacity:kMultiPartBufferSize];
_parserState = kParserState_Start; _parserState = kParserState_Start;
@ -357,12 +365,24 @@ static NSData* _dashNewlineData = nil;
return YES; return YES;
} }
- (void)dealloc { - (NSString*)description {
ARC_RELEASE(_arguments); NSMutableString* description = [NSMutableString stringWithString:[super description]];
ARC_RELEASE(_files); if (_arguments.count) {
ARC_RELEASE(_boundary); [description appendString:@"\n"];
for (NSString* key in [[_arguments allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
ARC_DEALLOC(super); GCDWebServerMultiPartArgument* argument = [_arguments objectForKey:key];
[description appendFormat:@"\n%@ (%@)\n", key, argument.contentType];
[description appendString:GCDWebServerDescribeData(argument.data, argument.contentType)];
}
}
if (_files.count) {
[description appendString:@"\n"];
for (NSString* key in [[_files allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
GCDWebServerMultiPartFile* file = [_files objectForKey:key];
[description appendFormat:@"\n%@ (%@): %@\n{%@}", key, file.contentType, file.fileName, file.temporaryPath];
}
}
return description;
} }
@end @end

View File

@ -114,6 +114,7 @@ extern NSString* GCDWebServerExtractHeaderValueParameter(NSString* header, NSStr
extern NSStringEncoding GCDWebServerStringEncodingFromCharset(NSString* charset); extern NSStringEncoding GCDWebServerStringEncodingFromCharset(NSString* charset);
extern NSString* GCDWebServerFormatHTTPDate(NSDate* date); extern NSString* GCDWebServerFormatHTTPDate(NSDate* date);
extern NSDate* GCDWebServerParseHTTPDate(NSString* string); extern NSDate* GCDWebServerParseHTTPDate(NSString* string);
extern NSString* GCDWebServerDescribeData(NSData* data, NSString* contentType);
@interface GCDWebServerConnection () @interface GCDWebServerConnection ()
- (id)initWithServer:(GCDWebServer*)server localAddress:(NSData*)localAddress remoteAddress:(NSData*)remoteAddress socket:(CFSocketNativeHandle)socket; - (id)initWithServer:(GCDWebServer*)server localAddress:(NSData*)localAddress remoteAddress:(NSData*)remoteAddress socket:(CFSocketNativeHandle)socket;

View File

@ -307,4 +307,16 @@
return [_writer close:error]; return [_writer close:error];
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithFormat:@"%@ %@", _method, _path];
for (NSString* argument in [[_query allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
[description appendFormat:@"\n %@ = %@", argument, [_query objectForKey:argument]];
}
[description appendString:@"\n"];
for (NSString* header in [[_headers allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
[description appendFormat:@"\n%@: %@", header, [_headers objectForKey:header]];
}
return description;
}
@end @end

View File

@ -254,6 +254,30 @@
[_reader close]; [_reader close];
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithFormat:@"Status Code = %i", (int)_status];
if (_type) {
[description appendFormat:@"\nContent Type = %@", _type];
}
if (_length != NSNotFound) {
[description appendFormat:@"\nContent Length = %lu", (unsigned long)_length];
}
[description appendFormat:@"\nCache Control Max Age = %lu", (unsigned long)_maxAge];
if (_lastModified) {
[description appendFormat:@"\nLast Modified Date = %@", _lastModified];
}
if (_eTag) {
[description appendFormat:@"\nETag = %@", _eTag];
}
if (_headers.count) {
[description appendString:@"\n"];
for (NSString* header in [[_headers allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
[description appendFormat:@"\n%@: %@", header, [_headers objectForKey:header]];
}
}
return description;
}
@end @end
@implementation GCDWebServerResponse (Extensions) @implementation GCDWebServerResponse (Extensions)

View File

@ -58,4 +58,10 @@
return _block(error); return _block(error);
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithString:[super description]];
[description appendString:@"\n\n<STREAM>"];
return description;
}
@end @end

View File

@ -61,4 +61,13 @@
return YES; return YES;
} }
- (NSString*)description {
NSMutableString* description = [NSMutableString stringWithString:[super description]];
[description appendString:@"\n"];
for (NSString* argument in [[_arguments allKeys] sortedArrayUsingSelector:@selector(compare:)]) {
[description appendFormat:@"\n%@ = %@", argument, [_arguments objectForKey:argument]];
}
return description;
}
@end @end