|
12 | 12 | #import "MCSUtils.h" |
13 | 13 | #import "MCSLogger.h" |
14 | 14 | #import "NSURLRequest+MCS.h" |
| 15 | +#import "MCSConsts.h" |
15 | 16 |
|
16 | 17 | @interface NSURLSessionTask (MCSDownloadExtended)<MCSDownloadTask> |
17 | 18 |
|
@@ -58,15 +59,26 @@ - (instancetype)init { |
58 | 59 | mDefaultResponseHandler = ^id<MCSDownloadResponse> _Nullable (NSURLSessionTask *task, NSURLResponse *response) { |
59 | 60 | if ( ![response isKindOfClass:NSHTTPURLResponse.class] ) return nil; |
60 | 61 | NSHTTPURLResponse *res = (NSHTTPURLResponse *)response; |
61 | | - MCSResponseContentRange contentRange = MCSResponseGetContentRange(res); |
62 | | - if ( MCSResponseRangeIsUndefined(contentRange) ) return nil; |
63 | | - NSRange range = MCSResponseRange(contentRange); |
64 | | - NSUInteger totalLength = contentRange.totalLength; |
65 | | - |
66 | 62 | NSInteger statusCode = res.statusCode; |
67 | 63 | NSString *_Nullable contentType = MCSResponseGetContentType(res); |
68 | 64 | NSString *_Nullable pathExtension = MCSSuggestedPathExtension(res); |
69 | | - return [MCSDownloadResponse.alloc initWithOriginalRequest:task.originalRequest currentRequest:task.currentRequest statusCode:statusCode pathExtension:pathExtension totalLength:totalLength range:range contentType:contentType]; |
| 65 | + switch (statusCode) { |
| 66 | + case 206: { |
| 67 | + MCSResponseContentRange contentRange = MCSResponseGetContentRange(res); |
| 68 | + if ( MCSResponseRangeIsUndefined(contentRange) ) return nil; |
| 69 | + NSRange range = MCSResponseRange(contentRange); |
| 70 | + NSUInteger totalLength = contentRange.totalLength; |
| 71 | + return [MCSDownloadResponse.alloc initWithOriginalRequest:task.originalRequest currentRequest:task.currentRequest statusCode:statusCode pathExtension:pathExtension totalLength:totalLength range:range contentType:contentType]; |
| 72 | + } |
| 73 | + break; |
| 74 | + case 200: { |
| 75 | + NSUInteger totalLength = MCSResponseGetContentLength(res) ?: NSUIntegerMax; |
| 76 | + NSRange range = NSMakeRange(0, totalLength); |
| 77 | + return [MCSDownloadResponse.alloc initWithOriginalRequest:task.originalRequest currentRequest:task.currentRequest statusCode:statusCode pathExtension:pathExtension totalLength:totalLength range:range contentType:contentType]; |
| 78 | + } |
| 79 | + break; |
| 80 | + default: return nil; |
| 81 | + } |
70 | 82 | }; |
71 | 83 | _responseHandler = mDefaultResponseHandler; |
72 | 84 | } |
|
0 commit comments