Skip to content
Browse files

Reformatting and documenting AFHTTPRequestOperation -connection:didRe…

…ceiveResponse:
  • Loading branch information...
1 parent 384df96 commit d863759f529f60bc937ff137fbdd1010f1d2bb05 @mattt mattt committed Aug 22, 2012
Showing with 14 additions and 12 deletions.
  1. +14 −12 AFNetworking/AFHTTPRequestOperation.m
View
26 AFNetworking/AFHTTPRequestOperation.m
@@ -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];
}

0 comments on commit d863759

Please sign in to comment.
Something went wrong with that request. Please try again.