Index: src/OFHTTPRequest.h ================================================================== --- src/OFHTTPRequest.h +++ src/OFHTTPRequest.h @@ -93,19 +93,21 @@ of_http_request_type_t requestType; OFString *queryString; OFDictionary *headers; BOOL redirectsFromHTTPSToHTTPAllowed; id delegate; + BOOL storesData; } #ifdef OF_HAVE_PROPERTIES @property (copy) OFURL *URL; @property (assign) of_http_request_type_t requestType; @property (copy) OFString *queryString; @property (copy) OFDictionary *headers; @property (assign) BOOL redirectsFromHTTPSToHTTPAllowed; @property (retain) id delegate; +@property (assign) BOOL storesData; #endif /** * \return A new, autoreleased OFHTTPRequest */ @@ -195,10 +197,25 @@ /** * \return The delegate for the HTTP request. */ - (id )delegate; +/** + * Sets whether an OFDataArray with the data is created. + * + * Setting this to NO is only useful if you are using the delegate to handle the + * data. + * + * \param enabled Whether to store the data in an OFDataArray + */ +- (void)setStoresData: (BOOL)enabled; + +/** + * \return Whether an OFDataArray with the data is created + */ +- (BOOL)storesData; + /** * Performs the HTTP request and returns an OFHTTPRequestResult. * * \return An OFHTTPRequestResult with the result of the HTTP request */ Index: src/OFHTTPRequest.m ================================================================== --- src/OFHTTPRequest.m +++ src/OFHTTPRequest.m @@ -56,10 +56,11 @@ requestType = OF_HTTP_REQUEST_TYPE_GET; headers = [[OFDictionary alloc] initWithObject: @"Something using ObjFW " @"" forKey: @"User-Agent"]; + storesData = YES; return self; } - initWithURL: (OFURL*)url @@ -142,10 +143,20 @@ - (id )delegate { OF_GETTER(delegate, YES) } + +- (void)setStoresData: (BOOL)enabled +{ + storesData = enabled; +} + +- (BOOL)storesData +{ + return storesData; +} - (OFHTTPRequestResult*)perform { return [self performWithRedirects: 10]; } @@ -180,10 +191,11 @@ OFEnumerator *enumerator; OFString *key; int status; const char *t = NULL; char *buf; + size_t bytesReceived; [sock connectToHost: [URL host] onPort: [URL port]]; /* @@ -325,22 +337,29 @@ [delegate request: self didReceiveHeaders: s_headers withStatusCode: status]; - data = [OFDataArray dataArrayWithItemSize: 1]; + if (storesData) + data = [OFDataArray dataArrayWithItemSize: 1]; + else + data = nil; + buf = [self allocMemoryWithSize: of_pagesize]; + bytesReceived = 0; @try { size_t len; while ((len = [sock readNBytes: of_pagesize intoBuffer: buf]) > 0) { - [data addNItems: len - fromCArray: buf]; [delegate request: self didReceiveData: buf withLength: len]; + + bytesReceived += len; + [data addNItems: len + fromCArray: buf]; } } @finally { [self freeMemory: buf]; } @@ -352,11 +371,11 @@ if (cl > SIZE_MAX) @throw [OFOutOfRangeException newWithClass: isa]; - if (cl != [data count]) + if (cl != bytesReceived) @throw [OFTruncatedDataException newWithClass: isa]; } /*