Browse Source

Simplified internal checks for requests and responses

Pierre-Olivier Latour 11 years ago
parent
commit
c454dc4e8e

+ 0 - 3
CGDWebServer/GCDWebServerDataRequest.m

@@ -49,7 +49,6 @@
 }
 
 - (BOOL)open:(NSError**)error {
-  DCHECK(_data == nil);
   if (self.contentLength != NSNotFound) {
     _data = [[NSMutableData alloc] initWithCapacity:self.contentLength];
   } else {
@@ -63,13 +62,11 @@
 }
 
 - (BOOL)writeData:(NSData*)data error:(NSError**)error {
-  DCHECK(_data != nil);
   [_data appendData:data];
   return YES;
 }
 
 - (BOOL)close:(NSError**)error {
-  DCHECK(_data != nil);
   return YES;
 }
 

+ 1 - 7
CGDWebServer/GCDWebServerFileRequest.m

@@ -50,7 +50,6 @@ static inline NSError* _MakePosixError(int code) {
 }
 
 - (void)dealloc {
-  DCHECK(_file < 0);
   unlink([_filePath fileSystemRepresentation]);
   ARC_RELEASE(_filePath);
   
@@ -58,7 +57,6 @@ static inline NSError* _MakePosixError(int code) {
 }
 
 - (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);
   if (_file <= 0) {
     *error = _MakePosixError(errno);
@@ -68,7 +66,6 @@ static inline NSError* _MakePosixError(int code) {
 }
 
 - (BOOL)writeData:(NSData*)data error:(NSError**)error {
-  DCHECK(_file > 0);
   if (write(_file, data.bytes, data.length) != (ssize_t)data.length) {
     *error = _MakePosixError(errno);
     return NO;
@@ -77,10 +74,7 @@ static inline NSError* _MakePosixError(int code) {
 }
 
 - (BOOL)close:(NSError**)error {
-  DCHECK(_file > 0);
-  int result = close(_file);
-  _file = -1;
-  if (result < 0) {
+  if (close(_file) < 0) {
     *error = _MakePosixError(errno);
     return NO;
   }

+ 0 - 6
CGDWebServer/GCDWebServerFileResponse.m

@@ -131,14 +131,12 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
 }
 
 - (void)dealloc {
-  DCHECK(_file <= 0);
   ARC_RELEASE(_path);
   
   ARC_DEALLOC(super);
 }
 
 - (BOOL)open:(NSError**)error {
-  DCHECK(_file <= 0);
   _file = open([_path fileSystemRepresentation], O_NOFOLLOW | O_RDONLY);
   if (_file <= 0) {
     *error = _MakePosixError(errno);
@@ -147,14 +145,12 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
   if (lseek(_file, _offset, SEEK_SET) != (off_t)_offset) {
     *error = _MakePosixError(errno);
     close(_file);
-    _file = 0;
     return NO;
   }
   return YES;
 }
 
 - (NSData*)readData:(NSError**)error {
-  DCHECK(_file > 0);
   size_t length = MIN((NSUInteger)kFileReadBufferSize, _size);
   NSMutableData* data = [[NSMutableData alloc] initWithLength:length];
   ssize_t result = read(_file, data.mutableBytes, length);
@@ -170,9 +166,7 @@ static inline NSDate* _NSDateFromTimeSpec(const struct timespec* t) {
 }
 
 - (void)close {
-  DCHECK(_file > 0);
   close(_file);
-  _file = 0;
 }
 
 @end

+ 0 - 4
CGDWebServer/GCDWebServerMultiPartFormRequest.m

@@ -205,7 +205,6 @@ static NSData* _dashNewlineData = nil;
 }
 
 - (BOOL)open:(NSError**)error {
-  DCHECK(_parserData == nil);
   _parserData = [[NSMutableData alloc] initWithCapacity:kMultiPartBufferSize];
   _parserState = kParserState_Start;
   return YES;
@@ -330,7 +329,6 @@ static NSData* _dashNewlineData = nil;
 }
 
 - (BOOL)writeData:(NSData*)data error:(NSError**)error {
-  DCHECK(_parserData != nil);
   [_parserData appendBytes:data.bytes length:data.length];
   if (![self _parseData]) {
     *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 {
-  DCHECK(_parserData != nil);
   ARC_RELEASE(_parserData);
   _parserData = nil;
   ARC_RELEASE(_controlName);
@@ -364,7 +361,6 @@ static NSData* _dashNewlineData = nil;
 }
 
 - (void)dealloc {
-  DCHECK(_parserData == nil);
   ARC_RELEASE(_arguments);
   ARC_RELEASE(_files);
   ARC_RELEASE(_boundary);

+ 3 - 0
CGDWebServer/GCDWebServerRequest.m

@@ -269,6 +269,7 @@
 }
 
 - (BOOL)performOpen:(NSError**)error {
+  DCHECK(_type);
   if (_opened) {
     DNOT_REACHED();
     return NO;
@@ -286,10 +287,12 @@
 }
 
 - (BOOL)performWriteData:(NSData*)data error:(NSError**)error {
+  DCHECK(_opened);
   return [_writer writeData:data error:error];
 }
 
 - (BOOL)performClose:(NSError**)error {
+  DCHECK(_opened);
   return [_writer close:error];
 }
 

+ 3 - 0
CGDWebServer/GCDWebServerResponse.m

@@ -222,6 +222,7 @@
 }
 
 - (BOOL)performOpen:(NSError**)error {
+  DCHECK(_type);
   if (_opened) {
     DNOT_REACHED();
     return NO;
@@ -239,10 +240,12 @@
 }
 
 - (NSData*)performReadData:(NSError**)error {
+  DCHECK(_opened);
   return [_reader readData:error];
 }
 
 - (void)performClose {
+  DCHECK(_opened);
   [_reader close];
 }