Changed -[GCDWebServerConnection open] to return a BOOL

This commit is contained in:
Pierre-Olivier Latour
2014-04-15 16:20:39 -03:00
parent 4fb5d67e9b
commit a55781e2c1
2 changed files with 23 additions and 14 deletions
+1 -1
View File
@@ -40,7 +40,7 @@
@end @end
@interface GCDWebServerConnection (Subclassing) @interface GCDWebServerConnection (Subclassing)
- (void)open; - (BOOL)open; // Return NO to reject connection e.g. after validating local or remote addresses
- (void)didUpdateBytesRead; // Called from arbitrary thread after @totalBytesRead is updated - Default implementation does nothing - (void)didUpdateBytesRead; // Called from arbitrary thread after @totalBytesRead is updated - Default implementation does nothing
- (void)didUpdateBytesWritten; // Called from arbitrary thread after @totalBytesWritten is updated - Default implementation does nothing - (void)didUpdateBytesWritten; // Called from arbitrary thread after @totalBytesWritten is updated - Default implementation does nothing
- (GCDWebServerResponse*)processRequest:(GCDWebServerRequest*)request withBlock:(GCDWebServerProcessBlock)block; // Only called if the request can be processed - (GCDWebServerResponse*)processRequest:(GCDWebServerRequest*)request withBlock:(GCDWebServerProcessBlock)block; // Only called if the request can be processed
+22 -13
View File
@@ -70,6 +70,7 @@ static int32_t _connectionCounter = 0;
GCDWebServerResponse* _response; GCDWebServerResponse* _response;
NSInteger _statusCode; NSInteger _statusCode;
BOOL _opened;
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
NSUInteger _connectionIndex; NSUInteger _connectionIndex;
NSString* _requestPath; NSString* _requestPath;
@@ -654,7 +655,15 @@ static inline NSUInteger _ScanHexNumber(const void* bytes, NSUInteger size) {
_remoteAddress = ARC_RETAIN(remoteAddress); _remoteAddress = ARC_RETAIN(remoteAddress);
_socket = socket; _socket = socket;
[self open]; if (![self open]) {
close(_socket);
ARC_RELEASE(self);
return nil;
}
_opened = YES;
LOG_DEBUG(@"Did open connection on socket %i", _socket);
[self _readRequestHeaders];
} }
return self; return self;
} }
@@ -681,7 +690,16 @@ static NSString* _StringFromAddressData(NSData* data) {
} }
- (void)dealloc { - (void)dealloc {
[self close]; if (_opened) {
[self close];
}
int result = close(_socket);
if (result != 0) {
LOG_ERROR(@"Failed closing socket %i for connection (%i): %s", _socket, errno, strerror(errno));
} else {
LOG_DEBUG(@"Did close connection on socket %i", _socket);
}
ARC_RELEASE(_server); ARC_RELEASE(_server);
ARC_RELEASE(_localAddress); ARC_RELEASE(_localAddress);
@@ -709,9 +727,7 @@ static NSString* _StringFromAddressData(NSData* data) {
@implementation GCDWebServerConnection (Subclassing) @implementation GCDWebServerConnection (Subclassing)
- (void)open { - (BOOL)open {
LOG_DEBUG(@"Did open connection on socket %i", _socket);
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
if (_server.recordingEnabled) { if (_server.recordingEnabled) {
_connectionIndex = OSAtomicIncrement32(&_connectionCounter); _connectionIndex = OSAtomicIncrement32(&_connectionCounter);
@@ -726,7 +742,7 @@ static NSString* _StringFromAddressData(NSData* data) {
} }
#endif #endif
[self _readRequestHeaders]; return YES;
} }
- (void)didUpdateBytesRead { - (void)didUpdateBytesRead {
@@ -788,13 +804,6 @@ static inline BOOL _CompareResources(NSString* responseETag, NSString* requestET
} }
- (void)close { - (void)close {
int result = close(_socket);
if (result != 0) {
LOG_ERROR(@"Failed closing socket %i for connection (%i): %s", _socket, errno, strerror(errno));
} else {
LOG_DEBUG(@"Did close connection on socket %i", _socket);
}
#if !TARGET_OS_IPHONE #if !TARGET_OS_IPHONE
if (_requestPath) { if (_requestPath) {
BOOL success = NO; BOOL success = NO;