Skip to content

Commit

Permalink
Reformatting and documenting AFHTTPRequestOperation -connection:didRe…
Browse files Browse the repository at this point in the history
…ceiveResponse:
  • Loading branch information
mattt committed Aug 22, 2012
1 parent 384df96 commit d863759
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions AFNetworking/AFHTTPRequestOperation.m
Expand Up @@ -275,31 +275,33 @@ - (void)connection:(NSURLConnection *)connection
{
self.response = (NSHTTPURLResponse *)response;

// 206 = Partial Content.
// Set Content-Range header if status code of response is 206 (Partial Content)
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.7
long long totalContentLength = self.response.expectedContentLength;
long long fileOffset = 0;
NSUInteger statusCode = ([self.response isKindOfClass:[NSHTTPURLResponse class]]) ? (NSUInteger)[self.response statusCode] : 200;
if (statusCode != 206) {
if (statusCode == 206) {
NSString *contentRange = [self.response.allHeaderFields valueForKey:@"Content-Range"];
if ([contentRange hasPrefix:@"bytes"]) {
NSArray *byteRanges = [contentRange componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" -/"]];
if ([byteRanges count] == 4) {
fileOffset = [[byteRanges objectAtIndex:1] longLongValue];
totalContentLength = [[byteRanges objectAtIndex:2] longLongValue] ?: -1; // if this is "*", it's converted to 0, but -1 is default.
}
}
} else {
if ([self.outputStream propertyForKey:NSStreamFileCurrentOffsetKey]) {
[self.outputStream setProperty:[NSNumber numberWithInteger:0] forKey:NSStreamFileCurrentOffsetKey];
} else {
if ([[self.outputStream propertyForKey:NSStreamDataWrittenToMemoryStreamKey] length] > 0) {
self.outputStream = [NSOutputStream outputStreamToMemory];
}
}
} else {
NSString *contentRange = [self.response.allHeaderFields valueForKey:@"Content-Range"];
if ([contentRange hasPrefix:@"bytes"]) {
NSArray *bytes = [contentRange componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" -/"]];
if ([bytes count] == 4) {
fileOffset = [[bytes objectAtIndex:1] longLongValue];
totalContentLength = [[bytes objectAtIndex:2] longLongValue] ?: -1; // if this is *, it's converted to 0, but -1 is default.
}
}

}

self.offsetContentLength = MAX(fileOffset, 0);
self.totalContentLength = totalContentLength;

[self.outputStream open];
}

Expand Down

0 comments on commit d863759

Please sign in to comment.