Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validating response #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions AFDownloadRequestOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ @interface AFDownloadRequestOperation() {
@property (nonatomic, assign) long long totalBytesReadPerDownload;
@property (assign) long long offsetContentLength;
@property (nonatomic, copy) AFURLConnectionProgressiveOperationProgressBlock progressiveDownloadProgress;
@property (nonatomic, strong) NSError *responseSerializationError;
@end

@implementation AFDownloadRequestOperation
Expand Down Expand Up @@ -183,15 +184,6 @@ - (unsigned long long)fileSizeForPath:(NSString *)path {
return fileSize;
}

#pragma mark - AFHTTPRequestOperation

+ (NSIndexSet *)acceptableStatusCodes {
NSMutableIndexSet *acceptableStatusCodes = [NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];
[acceptableStatusCodes addIndex:416];

return acceptableStatusCodes;
}

#pragma mark - AFURLRequestOperation

- (void)pause {
Expand All @@ -201,30 +193,37 @@ - (void)pause {

- (id)responseObject {
@synchronized(self) {
if (!_responseObject && [self isFinished] && !self.error) {
NSError *localError = nil;
if ([self isCancelled]) {
// should we clean up? most likely we don't.
if (self.isDeletingTempFileOnCancel) {
[self deleteTempFileWithError:&localError];
if (!_responseObject && [self isFinished]) {
NSError *error = nil;
if (! [self.responseSerializer validateResponse:self.response data:self.responseData error:&error]) {
self.responseSerializationError = error;
}

if (!self.error) {
NSError *localError = nil;
if ([self isCancelled]) {
// should we clean up? most likely we don't.
if (self.isDeletingTempFileOnCancel) {
[self deleteTempFileWithError:&localError];
if (localError) {
_fileError = localError;
}
}

// loss of network connections = error set, but not cancel
}else if(!self.error) {
// move file to final position and capture error
NSFileManager *fileManager = [NSFileManager new];
if (self.shouldOverwrite) {
[fileManager removeItemAtPath:_targetPath error:NULL]; // avoid "File exists" error
}
[fileManager moveItemAtPath:[self tempPath] toPath:_targetPath error:&localError];
if (localError) {
_fileError = localError;
} else {
_responseObject = _targetPath;
}
}

// loss of network connections = error set, but not cancel
}else if(!self.error) {
// move file to final position and capture error
NSFileManager *fileManager = [NSFileManager new];
if (self.shouldOverwrite) {
[fileManager removeItemAtPath:_targetPath error:NULL]; // avoid "File exists" error
}
[fileManager moveItemAtPath:[self tempPath] toPath:_targetPath error:&localError];
if (localError) {
_fileError = localError;
} else {
_responseObject = _targetPath;
}
}
}
}
Expand All @@ -233,7 +232,7 @@ - (id)responseObject {
}

- (NSError *)error {
return _fileError ?: [super error];
return self.responseSerializationError ? : (_fileError ?: [super error]);
}

#pragma mark - NSURLConnectionDelegate
Expand Down Expand Up @@ -326,4 +325,4 @@ + (NSString *)md5StringForString:(NSString *)string {
r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
}

@end
@end