@@ -41,10 +41,11 @@ #import "OFInvalidFormatException.h" #import "OFNotImplementedException.h" #import "OFOpenFileFailedException.h" #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" +#import "OFTruncatedDataException.h" #import "autorelease.h" #import "macros.h" #import "of_asprintf.h" #import "unicode.h" @@ -899,11 +900,13 @@ { void *pool; OFHTTPClient *client; OFHTTPRequest *request; OFHTTPRequestReply *reply; - OFString *contentType; + OFDictionary *headers; + OFString *contentType, *contentLength; + OFDataArray *data; Class c; c = [self class]; [self release]; @@ -926,13 +929,15 @@ if ([reply statusCode] != 200) @throw [OFHTTPRequestFailedException exceptionWithClass: [request class] request: request reply: reply]; + + headers = [reply headers]; if (encoding == OF_STRING_ENCODING_AUTODETECT && - (contentType = [[reply headers] objectForKey: @"Content-Type"])) { + (contentType = [headers objectForKey: @"Content-Type"]) != nil) { contentType = [contentType lowercaseString]; if ([contentType hasSuffix: @"charset=utf-8"]) encoding = OF_STRING_ENCODING_UTF_8; if ([contentType hasSuffix: @"charset=iso-8859-1"]) @@ -944,15 +949,23 @@ } if (encoding == OF_STRING_ENCODING_AUTODETECT) encoding = OF_STRING_ENCODING_UTF_8; - self = [[c alloc] initWithCString: (char*)[[reply data] items] + data = [reply readDataArrayTillEndOfStream]; + + if ((contentLength = [headers objectForKey: @"Content-Length"]) != nil) + if ([data count] != (size_t)[contentLength decimalValue]) + @throw [OFTruncatedDataException + exceptionWithClass: [self class]]; + + self = [[c alloc] initWithCString: (char*)[data items] encoding: encoding - length: [[reply data] count]]; + length: [data count]]; objc_autoreleasePoolPop(pool); + return self; } - initWithSerialization: (OFXMLElement*)element {