Simplified internal checks for requests and responses

This commit is contained in:
Pierre-Olivier Latour
2014-04-08 23:00:21 -07:00
parent 289059c875
commit c454dc4e8e
6 changed files with 7 additions and 20 deletions
-3
View File
@@ -49,7 +49,6 @@
} }
- (BOOL)open:(NSError**)error { - (BOOL)open:(NSError**)error {
DCHECK(_data == nil);
if (self.contentLength != NSNotFound) { if (self.contentLength != NSNotFound) {
_data = [[NSMutableData alloc] initWithCapacity:self.contentLength]; _data = [[NSMutableData alloc] initWithCapacity:self.contentLength];
} else { } else {
@@ -63,13 +62,11 @@
} }
- (BOOL)writeData:(NSData*)data error:(NSError**)error { - (BOOL)writeData:(NSData*)data error:(NSError**)error {
DCHECK(_data != nil);
[_data appendData:data]; [_data appendData:data];
return YES; return YES;
} }
- (BOOL)close:(NSError**)error { - (BOOL)close:(NSError**)error {
DCHECK(_data != nil);
return YES; return YES;
} }
+1 -7
View File
@@ -50,7 +50,6 @@ static inline NSError* _MakePosixError(int code) {
} }
- (void)dealloc { - (void)dealloc {
DCHECK(_file < 0);
unlink([_filePath fileSystemRepresentation]); unlink([_filePath fileSystemRepresentation]);
ARC_RELEASE(_filePath); ARC_RELEASE(_filePath);
@@ -58,7 +57,6 @@ static inline NSError* _MakePosixError(int code) {
} }
- (BOOL)open:(NSError**)error { - (BOOL)open:(NSError**)error {
DCHECK(_file == 0);
_file = open([_filePath fileSystemRepresentation], O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); _file = open([_filePath fileSystemRepresentation], O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (_file <= 0) { if (_file <= 0) {
*error = _MakePosixError(errno); *error = _MakePosixError(errno);
@@ -68,7 +66,6 @@ static inline NSError* _MakePosixError(int code) {
} }
- (BOOL)writeData:(NSData*)data error:(NSError**)error { - (BOOL)writeData:(NSData*)data error:(NSError**)error {
DCHECK(_file > 0);
if (write(_file, data.bytes, data.length) != (ssize_t)data.length) { if (write(_file, data.bytes, data.length) != (ssize_t)data.length) {
*error = _MakePosixError(errno); *error = _MakePosixError(errno);
return NO; return NO;
@@ -77,10 +74,7 @@ static inline NSError* _MakePosixError(int code) {
} }
- (BOOL)close:(NSError**)error { - (BOOL)close:(NSError**)error {
DCHECK(_file > 0); if (close(_file) < 0) {
int result = close(_file);
_file = -1;
if (result < 0) {
*error = _MakePosixError(errno); *error = _MakePosixError(errno);
return NO; return NO;
} }
-6
View File
@@ -131,14 +131,12 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
} }
- (void)dealloc { - (void)dealloc {
DCHECK(_file <= 0);
ARC_RELEASE(_path); ARC_RELEASE(_path);
ARC_DEALLOC(super); ARC_DEALLOC(super);
} }
- (BOOL)open:(NSError**)error { - (BOOL)open:(NSError**)error {
DCHECK(_file <= 0);
_file = open([_path fileSystemRepresentation], O_NOFOLLOW | O_RDONLY); _file = open([_path fileSystemRepresentation], O_NOFOLLOW | O_RDONLY);
if (_file <= 0) { if (_file <= 0) {
*error = _MakePosixError(errno); *error = _MakePosixError(errno);
@@ -147,14 +145,12 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
if (lseek(_file, _offset, SEEK_SET) != (off_t)_offset) { if (lseek(_file, _offset, SEEK_SET) != (off_t)_offset) {
*error = _MakePosixError(errno); *error = _MakePosixError(errno);
close(_file); close(_file);
_file = 0;
return NO; return NO;
} }
return YES; return YES;
} }
- (NSData*)readData:(NSError**)error { - (NSData*)readData:(NSError**)error {
DCHECK(_file > 0);
size_t length = MIN((NSUInteger)kFileReadBufferSize, _size); size_t length = MIN((NSUInteger)kFileReadBufferSize, _size);
NSMutableData* data = [[NSMutableData alloc] initWithLength:length]; NSMutableData* data = [[NSMutableData alloc] initWithLength:length];
ssize_t result = read(_file, data.mutableBytes, length); ssize_t result = read(_file, data.mutableBytes, length);
@@ -170,9 +166,7 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
} }
- (void)close { - (void)close {
DCHECK(_file > 0);
close(_file); close(_file);
_file = 0;
} }
@end @end
@@ -205,7 +205,6 @@ static NSData* _dashNewlineData = nil;
} }
- (BOOL)open:(NSError**)error { - (BOOL)open:(NSError**)error {
DCHECK(_parserData == nil);
_parserData = [[NSMutableData alloc] initWithCapacity:kMultiPartBufferSize]; _parserData = [[NSMutableData alloc] initWithCapacity:kMultiPartBufferSize];
_parserState = kParserState_Start; _parserState = kParserState_Start;
return YES; return YES;
@@ -330,7 +329,6 @@ static NSData* _dashNewlineData = nil;
} }
- (BOOL)writeData:(NSData*)data error:(NSError**)error { - (BOOL)writeData:(NSData*)data error:(NSError**)error {
DCHECK(_parserData != nil);
[_parserData appendBytes:data.bytes length:data.length]; [_parserData appendBytes:data.bytes length:data.length];
if (![self _parseData]) { if (![self _parseData]) {
*error = [NSError errorWithDomain:kGCDWebServerErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Failed parsing multipart form data"}]; *error = [NSError errorWithDomain:kGCDWebServerErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey: @"Failed parsing multipart form data"}];
@@ -340,7 +338,6 @@ static NSData* _dashNewlineData = nil;
} }
- (BOOL)close:(NSError**)error { - (BOOL)close:(NSError**)error {
DCHECK(_parserData != nil);
ARC_RELEASE(_parserData); ARC_RELEASE(_parserData);
_parserData = nil; _parserData = nil;
ARC_RELEASE(_controlName); ARC_RELEASE(_controlName);
@@ -364,7 +361,6 @@ static NSData* _dashNewlineData = nil;
} }
- (void)dealloc { - (void)dealloc {
DCHECK(_parserData == nil);
ARC_RELEASE(_arguments); ARC_RELEASE(_arguments);
ARC_RELEASE(_files); ARC_RELEASE(_files);
ARC_RELEASE(_boundary); ARC_RELEASE(_boundary);
+3
View File
@@ -269,6 +269,7 @@
} }
- (BOOL)performOpen:(NSError**)error { - (BOOL)performOpen:(NSError**)error {
DCHECK(_type);
if (_opened) { if (_opened) {
DNOT_REACHED(); DNOT_REACHED();
return NO; return NO;
@@ -286,10 +287,12 @@
} }
- (BOOL)performWriteData:(NSData*)data error:(NSError**)error { - (BOOL)performWriteData:(NSData*)data error:(NSError**)error {
DCHECK(_opened);
return [_writer writeData:data error:error]; return [_writer writeData:data error:error];
} }
- (BOOL)performClose:(NSError**)error { - (BOOL)performClose:(NSError**)error {
DCHECK(_opened);
return [_writer close:error]; return [_writer close:error];
} }
+3
View File
@@ -222,6 +222,7 @@
} }
- (BOOL)performOpen:(NSError**)error { - (BOOL)performOpen:(NSError**)error {
DCHECK(_type);
if (_opened) { if (_opened) {
DNOT_REACHED(); DNOT_REACHED();
return NO; return NO;
@@ -239,10 +240,12 @@
} }
- (NSData*)performReadData:(NSError**)error { - (NSData*)performReadData:(NSError**)error {
DCHECK(_opened);
return [_reader readData:error]; return [_reader readData:error];
} }
- (void)performClose { - (void)performClose {
DCHECK(_opened);
[_reader close]; [_reader close];
} }