Skip to content

Commit

Permalink
Adding a new "- (BOOL)shouldDie" method to HTTPConnection. This allow…
Browse files Browse the repository at this point in the history
…s subclasses to (easily/optionally) force close the connection after the response has been fully sent.
  • Loading branch information
robbiehanson committed Apr 21, 2010
1 parent 0a0b744 commit a8445ba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
12 changes: 8 additions & 4 deletions HTTPAsyncFileResponse.m
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,19 @@ - (void)connectionDidClose

- (void)readDataInBackground:(NSNumber *)lengthNumber
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSUInteger length = [lengthNumber unsignedIntegerValue];

NSData *readData = [fileHandle readDataOfLength:length];

[self performSelector:@selector(fileHandleDidReadData:)
onThread:connectionThread
withObject:readData
waitUntilDone:NO
modes:connectionRunLoopModes];
onThread:connectionThread
withObject:readData
waitUntilDone:NO
modes:connectionRunLoopModes];

[pool release];
}

- (void)fileHandleDidReadData:(NSData *)readData
Expand Down
1 change: 1 addition & 0 deletions HTTPConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
- (NSData *)preprocessResponse:(CFHTTPMessageRef)response;
- (NSData *)preprocessErrorResponse:(CFHTTPMessageRef)response;

- (BOOL)shouldDie;
- (void)die;

@end
Expand Down
44 changes: 33 additions & 11 deletions HTTPConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@ - (BOOL)parseRangeRequest:(NSString *)rangeHeader withContentLength:(UInt64)cont
**/
- (void)replyToHTTPRequest
{
// NSData *tempData = (NSData *)CFHTTPMessageCopySerializedMessage(request);
// NSString *tempStr = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
//
// NSLog(@"HTTPConnection:%p replyToHTTPRequest:\n%@", self, tempStr);
//
// [tempStr release];
// [tempData release];

// Check the HTTP version - if it's anything but HTTP version 1.1, we don't support it
NSString *version = [NSMakeCollectable(CFHTTPMessageCopyVersion(request)) autorelease];
if(!version || ![version isEqualToString:(NSString *)kCFHTTPVersion1_1])
Expand Down Expand Up @@ -1885,17 +1893,24 @@ - (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag
ranges_headers = nil;
ranges_boundry = nil;

// Release the old request, and create a new one
if(request) CFRelease(request);
request = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, YES);

numHeaderLines = 0;

// And start listening for more requests
[asyncSocket readDataToData:[AsyncSocket CRLFData]
withTimeout:READ_TIMEOUT
maxLength:LIMIT_MAX_HEADER_LINE_LENGTH
tag:HTTP_REQUEST_HEADER];
if([self shouldDie])
{
[self die];
}
else
{
// Release the old request, and create a new one
if(request) CFRelease(request);
request = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, YES);

numHeaderLines = 0;

// And start listening for more requests
[asyncSocket readDataToData:[AsyncSocket CRLFData]
withTimeout:READ_TIMEOUT
maxLength:LIMIT_MAX_HEADER_LINE_LENGTH
tag:HTTP_REQUEST_HEADER];
}
}
}
}
Expand Down Expand Up @@ -1948,6 +1963,13 @@ - (void)responseHasAvailableData
#pragma mark Closing
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

- (BOOL)shouldDie
{
// Override me if you want to force close the connection after the response has been fully sent.

return NO;
}

- (void)die
{
// Override me if you want to perform any custom actions when a connection is closed.
Expand Down
1 change: 1 addition & 0 deletions HTTPResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,4 @@
// If you don't know the content-length in advanced, you should also implement the isChunked method.
// This means the response will not include a Content-Length header, and will instead use "Transfer-Encoding: chunked".
// There's a good chance that if your response is asynchronous, it's also chunked.
// If your response is chunked, you don't need to worry about range requests.

0 comments on commit a8445ba

Please sign in to comment.