diff --git a/CGDWebServer/GCDWebServer.h b/CGDWebServer/GCDWebServer.h index 2b21ecd..3f9f075 100644 --- a/CGDWebServer/GCDWebServer.h +++ b/CGDWebServer/GCDWebServer.h @@ -42,26 +42,26 @@ typedef GCDWebServerResponse* (^GCDWebServerProcessBlock)(GCDWebServerRequest* r } @property(nonatomic, readonly, getter=isRunning) BOOL running; @property(nonatomic, readonly) NSUInteger port; -- (void) addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock; -- (void) removeAllHandlers; +- (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock; +- (void)removeAllHandlers; -- (BOOL) start; // Default is main runloop, 8080 port and computer name -- (BOOL) startWithRunloop:(NSRunLoop*)runloop port:(NSUInteger)port bonjourName:(NSString*)name; // Pass nil name to disable Bonjour or empty string to use computer name -- (void) stop; +- (BOOL)start; // Default is main runloop, 8080 port and computer name +- (BOOL)startWithRunloop:(NSRunLoop*)runloop port:(NSUInteger)port bonjourName:(NSString*)name; // Pass nil name to disable Bonjour or empty string to use computer name +- (void)stop; @end @interface GCDWebServer (Subclassing) -+ (Class) connectionClass; -+ (NSString*) serverName; // Default is class name ++ (Class)connectionClass; ++ (NSString*)serverName; // Default is class name @end @interface GCDWebServer (Extensions) -- (BOOL) runWithPort:(NSUInteger)port; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only) +- (BOOL)runWithPort:(NSUInteger)port; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only) @end @interface GCDWebServer (Handlers) -- (void) addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block; -- (void) addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge; // Base path is recursive and case-sensitive -- (void) addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block; // Path is case-insensitive -- (void) addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block; // Regular expression is case-insensitive +- (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block; +- (void)addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge; // Base path is recursive and case-sensitive +- (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block; // Path is case-insensitive +- (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block; // Regular expression is case-insensitive @end diff --git a/CGDWebServer/GCDWebServer.m b/CGDWebServer/GCDWebServer.m index 30c9856..5f9d16e 100644 --- a/CGDWebServer/GCDWebServer.m +++ b/CGDWebServer/GCDWebServer.m @@ -101,7 +101,7 @@ static void _SignalHandler(int signal) { @synthesize matchBlock=_matchBlock, processBlock=_processBlock; -- (id) initWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock { +- (id)initWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock { if ((self = [super init])) { _matchBlock = Block_copy(matchBlock); _processBlock = Block_copy(processBlock); @@ -109,7 +109,7 @@ static void _SignalHandler(int signal) { return self; } -- (void) dealloc { +- (void)dealloc { Block_release(_matchBlock); Block_release(_processBlock); @@ -122,18 +122,18 @@ static void _SignalHandler(int signal) { @synthesize handlers=_handlers, port=_port; -+ (void) initialize { ++ (void)initialize { [GCDWebServerConnection class]; // Initialize class immediately to make sure it happens on the main thread } -- (id) init { +- (id)init { if ((self = [super init])) { _handlers = [[NSMutableArray alloc] init]; } return self; } -- (void) dealloc { +- (void)dealloc { if (_runLoop) { [self stop]; } @@ -143,19 +143,19 @@ static void _SignalHandler(int signal) { [super dealloc]; } -- (void) addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)handlerBlock { +- (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)handlerBlock { DCHECK(_runLoop == nil); GCDWebServerHandler* handler = [[GCDWebServerHandler alloc] initWithMatchBlock:matchBlock processBlock:handlerBlock]; [_handlers insertObject:handler atIndex:0]; [handler release]; } -- (void) removeAllHandlers { +- (void)removeAllHandlers { DCHECK(_runLoop == nil); [_handlers removeAllObjects]; } -- (BOOL) start { +- (BOOL)start { return [self startWithRunloop:[NSRunLoop mainRunLoop] port:8080 bonjourName:@""]; } @@ -184,7 +184,7 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat } } -- (BOOL) startWithRunloop:(NSRunLoop*)runloop port:(NSUInteger)port bonjourName:(NSString*)name { +- (BOOL)startWithRunloop:(NSRunLoop*)runloop port:(NSUInteger)port bonjourName:(NSString*)name { DCHECK(runloop); DCHECK(port); DCHECK(_runLoop == nil); @@ -232,11 +232,11 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat return (_runLoop != nil ? YES : NO); } -- (BOOL) isRunning { +- (BOOL)isRunning { return (_runLoop != nil ? YES : NO); } -- (void) stop { +- (void)stop { DCHECK(_runLoop != nil); if (_socket) { if (_service) { @@ -259,11 +259,11 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat @implementation GCDWebServer (Subclassing) -+ (Class) connectionClass { ++ (Class)connectionClass { return [GCDWebServerConnection class]; } -+ (NSString*) serverName { ++ (NSString*)serverName { return NSStringFromClass(self); } @@ -271,7 +271,7 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat @implementation GCDWebServer (Extensions) -- (BOOL) runWithPort:(NSUInteger)port { +- (BOOL)runWithPort:(NSUInteger)port { BOOL success = NO; _run = YES; void* handler = signal(SIGINT, _SignalHandler); @@ -292,7 +292,7 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat @implementation GCDWebServer (Handlers) -- (void) addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block { +- (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block { [self addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) { return [[[class alloc] initWithMethod:requestMethod url:requestURL headers:requestHeaders path:urlPath query:urlQuery] autorelease]; @@ -300,11 +300,11 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat } processBlock:block]; } -- (GCDWebServerResponse*) _responseWithContentsOfFile:(NSString*)path { +- (GCDWebServerResponse*)_responseWithContentsOfFile:(NSString*)path { return [GCDWebServerFileResponse responseWithFile:path]; } -- (GCDWebServerResponse*) _responseWithContentsOfDirectory:(NSString*)path { +- (GCDWebServerResponse*)_responseWithContentsOfDirectory:(NSString*)path { NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path]; if (enumerator == nil) { return nil; @@ -330,7 +330,7 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat return [GCDWebServerDataResponse responseWithHTML:html]; } -- (void) addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge { +- (void)addHandlerForBasePath:(NSString*)basePath localPath:(NSString*)localPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge { if ([basePath hasPrefix:@"/"] && [basePath hasSuffix:@"/"]) { [self addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) { @@ -373,7 +373,7 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat } } -- (void) addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block { +- (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block { if ([path hasPrefix:@"/"] && [class isSubclassOfClass:[GCDWebServerRequest class]]) { [self addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) { @@ -391,7 +391,7 @@ static void _SocketCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDat } } -- (void) addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block { +- (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)class processBlock:(GCDWebServerProcessBlock)block { NSRegularExpression* expression = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionCaseInsensitive error:NULL]; if (expression && [class isSubclassOfClass:[GCDWebServerRequest class]]) { [self addHandlerWithMatchBlock:^GCDWebServerRequest *(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery) { diff --git a/CGDWebServer/GCDWebServerConnection.h b/CGDWebServer/GCDWebServerConnection.h index db79e68..5e14367 100644 --- a/CGDWebServer/GCDWebServerConnection.h +++ b/CGDWebServer/GCDWebServerConnection.h @@ -50,7 +50,7 @@ @end @interface GCDWebServerConnection (Subclassing) -- (void) open; -- (GCDWebServerResponse*) processRequest:(GCDWebServerRequest*)request withBlock:(GCDWebServerProcessBlock)block; -- (void) close; +- (void)open; +- (GCDWebServerResponse*)processRequest:(GCDWebServerRequest*)request withBlock:(GCDWebServerProcessBlock)block; +- (void)close; @end diff --git a/CGDWebServer/GCDWebServerConnection.m b/CGDWebServer/GCDWebServerConnection.m index 87ed6fc..50759ed 100644 --- a/CGDWebServer/GCDWebServerConnection.m +++ b/CGDWebServer/GCDWebServerConnection.m @@ -48,7 +48,7 @@ static dispatch_queue_t _formatterQueue = NULL; @implementation GCDWebServerConnection (Read) -- (void) _readBufferWithLength:(NSUInteger)length completionBlock:(ReadBufferCompletionBlock)block { +- (void)_readBufferWithLength:(NSUInteger)length completionBlock:(ReadBufferCompletionBlock)block { dispatch_read(_socket, length, kReadWriteQueue, ^(dispatch_data_t buffer, int error) { @autoreleasepool { @@ -75,7 +75,7 @@ static dispatch_queue_t _formatterQueue = NULL; }); } -- (void) _readDataWithCompletionBlock:(ReadDataCompletionBlock)block { +- (void)_readDataWithCompletionBlock:(ReadDataCompletionBlock)block { [self _readBufferWithLength:SIZE_T_MAX completionBlock:^(dispatch_data_t buffer) { if (buffer) { @@ -93,7 +93,7 @@ static dispatch_queue_t _formatterQueue = NULL; }]; } -- (void) _readHeadersWithCompletionBlock:(ReadHeadersCompletionBlock)block { +- (void)_readHeadersWithCompletionBlock:(ReadHeadersCompletionBlock)block { DCHECK(_requestMessage); NSMutableData* data = [NSMutableData dataWithCapacity:kHeadersReadBuffer]; [self _readBufferWithLength:SIZE_T_MAX completionBlock:^(dispatch_data_t buffer) { @@ -127,7 +127,7 @@ static dispatch_queue_t _formatterQueue = NULL; }]; } -- (void) _readBodyWithRemainingLength:(NSUInteger)length completionBlock:(ReadBodyCompletionBlock)block { +- (void)_readBodyWithRemainingLength:(NSUInteger)length completionBlock:(ReadBodyCompletionBlock)block { DCHECK([_request hasBody]); [self _readBufferWithLength:length completionBlock:^(dispatch_data_t buffer) { @@ -166,7 +166,7 @@ static dispatch_queue_t _formatterQueue = NULL; @implementation GCDWebServerConnection (Write) -- (void) _writeBuffer:(dispatch_data_t)buffer withCompletionBlock:(WriteBufferCompletionBlock)block { +- (void)_writeBuffer:(dispatch_data_t)buffer withCompletionBlock:(WriteBufferCompletionBlock)block { size_t size = dispatch_data_get_size(buffer); dispatch_write(_socket, buffer, kReadWriteQueue, ^(dispatch_data_t data, int error) { @@ -185,7 +185,7 @@ static dispatch_queue_t _formatterQueue = NULL; }); } -- (void) _writeData:(NSData*)data withCompletionBlock:(WriteDataCompletionBlock)block { +- (void)_writeData:(NSData*)data withCompletionBlock:(WriteDataCompletionBlock)block { [data retain]; dispatch_data_t buffer = dispatch_data_create(data.bytes, data.length, dispatch_get_current_queue(), ^{ [data release]; @@ -194,14 +194,14 @@ static dispatch_queue_t _formatterQueue = NULL; dispatch_release(buffer); } -- (void) _writeHeadersWithCompletionBlock:(WriteHeadersCompletionBlock)block { +- (void)_writeHeadersWithCompletionBlock:(WriteHeadersCompletionBlock)block { DCHECK(_responseMessage); CFDataRef message = CFHTTPMessageCopySerializedMessage(_responseMessage); [self _writeData:(NSData*)message withCompletionBlock:block]; CFRelease(message); } -- (void) _writeBodyWithCompletionBlock:(WriteBodyCompletionBlock)block { +- (void)_writeBodyWithCompletionBlock:(WriteBodyCompletionBlock)block { DCHECK([_response hasBody]); void* buffer = malloc(kBodyWriteBufferSize); NSInteger result = [_response read:buffer maxLength:kBodyWriteBufferSize]; @@ -233,7 +233,7 @@ static dispatch_queue_t _formatterQueue = NULL; @synthesize server=_server, address=_address, totalBytesRead=_bytesRead, totalBytesWritten=_bytesWritten; -+ (void) initialize { ++ (void)initialize { DCHECK([NSThread isMainThread]); // NSDateFormatter should be initialized on main thread if (_separatorData == nil) { _separatorData = [[NSData alloc] initWithBytes:"\r\n\r\n" length:4]; @@ -258,7 +258,7 @@ static dispatch_queue_t _formatterQueue = NULL; } } -- (void) _initializeResponseHeadersWithStatusCode:(NSInteger)statusCode { +- (void)_initializeResponseHeadersWithStatusCode:(NSInteger)statusCode { _responseMessage = CFHTTPMessageCreateResponse(kCFAllocatorDefault, statusCode, NULL, kCFHTTPVersion1_1); CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Connection"), CFSTR("Close")); CFHTTPMessageSetHeaderFieldValue(_responseMessage, CFSTR("Server"), (CFStringRef)[[_server class] serverName]); @@ -268,7 +268,7 @@ static dispatch_queue_t _formatterQueue = NULL; }); } -- (void) _abortWithStatusCode:(NSUInteger)statusCode { +- (void)_abortWithStatusCode:(NSUInteger)statusCode { DCHECK(_responseMessage == NULL); DCHECK((statusCode >= 400) && (statusCode < 600)); [self _initializeResponseHeadersWithStatusCode:statusCode]; @@ -279,7 +279,7 @@ static dispatch_queue_t _formatterQueue = NULL; } // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html -- (void) _processRequest { +- (void)_processRequest { DCHECK(_responseMessage == NULL); GCDWebServerResponse* response = [self processRequest:_request withBlock:_handler.processBlock]; @@ -323,7 +323,7 @@ static dispatch_queue_t _formatterQueue = NULL; } -- (void) _readRequestBody:(NSData*)initialData { +- (void)_readRequestBody:(NSData*)initialData { if ([_request open]) { NSInteger length = _request.contentLength; if (initialData.length) { @@ -364,7 +364,7 @@ static dispatch_queue_t _formatterQueue = NULL; } } -- (void) _readRequestHeaders { +- (void)_readRequestHeaders { _requestMessage = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, true); [self _readHeadersWithCompletionBlock:^(NSData* extraData) { @@ -426,7 +426,7 @@ static dispatch_queue_t _formatterQueue = NULL; }]; } -- (id) initWithServer:(GCDWebServer*)server address:(NSData*)address socket:(CFSocketNativeHandle)socket { +- (id)initWithServer:(GCDWebServer*)server address:(NSData*)address socket:(CFSocketNativeHandle)socket { if ((self = [super init])) { _server = [server retain]; _address = [address retain]; @@ -437,7 +437,7 @@ static dispatch_queue_t _formatterQueue = NULL; return self; } -- (void) dealloc { +- (void)dealloc { [self close]; [_server release]; @@ -460,12 +460,12 @@ static dispatch_queue_t _formatterQueue = NULL; @implementation GCDWebServerConnection (Subclassing) -- (void) open { +- (void)open { LOG_DEBUG(@"Did open connection on socket %i", _socket); [self _readRequestHeaders]; } -- (GCDWebServerResponse*) processRequest:(GCDWebServerRequest*)request withBlock:(GCDWebServerProcessBlock)block { +- (GCDWebServerResponse*)processRequest:(GCDWebServerRequest*)request withBlock:(GCDWebServerProcessBlock)block { LOG_DEBUG(@"Connection on socket %i processing %@ request for \"%@\" (%i bytes body)", _socket, _request.method, _request.path, _request.contentLength); GCDWebServerResponse* response = nil; @try { @@ -477,7 +477,7 @@ static dispatch_queue_t _formatterQueue = NULL; return response; } -- (void) close { +- (void)close { close(_socket); LOG_DEBUG(@"Did close connection on socket %i", _socket); } diff --git a/CGDWebServer/GCDWebServerPrivate.h b/CGDWebServer/GCDWebServerPrivate.h index d537bd0..ef8ba82 100644 --- a/CGDWebServer/GCDWebServerPrivate.h +++ b/CGDWebServer/GCDWebServerPrivate.h @@ -93,7 +93,7 @@ NSDictionary* GCDWebServerParseURLEncodedForm(NSString* form); #endif @interface GCDWebServerConnection () -- (id) initWithServer:(GCDWebServer*)server address:(NSData*)address socket:(CFSocketNativeHandle)socket; +- (id)initWithServer:(GCDWebServer*)server address:(NSData*)address socket:(CFSocketNativeHandle)socket; @end @interface GCDWebServer () @@ -107,5 +107,5 @@ NSDictionary* GCDWebServerParseURLEncodedForm(NSString* form); } @property(nonatomic, readonly) GCDWebServerMatchBlock matchBlock; @property(nonatomic, readonly) GCDWebServerProcessBlock processBlock; -- (id) initWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock; +- (id)initWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock; @end diff --git a/CGDWebServer/GCDWebServerRequest.h b/CGDWebServer/GCDWebServerRequest.h index c28e236..5b9c405 100644 --- a/CGDWebServer/GCDWebServerRequest.h +++ b/CGDWebServer/GCDWebServerRequest.h @@ -44,14 +44,14 @@ @property(nonatomic, readonly) NSDictionary* query; // May be nil @property(nonatomic, readonly) NSString* contentType; // Automatically parsed from headers (nil if request has no body) @property(nonatomic, readonly) NSUInteger contentLength; // Automatically parsed from headers -- (id) initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query; -- (BOOL) hasBody; // Convenience method +- (id)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query; +- (BOOL)hasBody; // Convenience method @end @interface GCDWebServerRequest (Subclassing) -- (BOOL) open; // Implementation required -- (NSInteger) write:(const void*)buffer maxLength:(NSUInteger)length; // Implementation required -- (BOOL) close; // Implementation required +- (BOOL)open; // Implementation required +- (NSInteger)write:(const void*)buffer maxLength:(NSUInteger)length; // Implementation required +- (BOOL)close; // Implementation required @end @interface GCDWebServerDataRequest : GCDWebServerRequest { @@ -74,7 +74,7 @@ NSDictionary* _arguments; } @property(nonatomic, readonly) NSDictionary* arguments; // Only valid after open / write / close sequence -+ (NSString*) mimeType; ++ (NSString*)mimeType; @end @interface GCDWebServerMultiPart : NSObject { @@ -121,5 +121,5 @@ } @property(nonatomic, readonly) NSDictionary* arguments; // Only valid after open / write / close sequence @property(nonatomic, readonly) NSDictionary* files; // Only valid after open / write / close sequence -+ (NSString*) mimeType; ++ (NSString*)mimeType; @end diff --git a/CGDWebServer/GCDWebServerRequest.m b/CGDWebServer/GCDWebServerRequest.m index cdc5bf0..d0386b5 100644 --- a/CGDWebServer/GCDWebServerRequest.m +++ b/CGDWebServer/GCDWebServerRequest.m @@ -72,7 +72,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize method=_method, URL=_url, headers=_headers, path=_path, query=_query, contentType=_type, contentLength=_length; -- (id) initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query { +- (id)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query { if ((self = [super init])) { _method = [method copy]; _url = [url retain]; @@ -96,7 +96,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return self; } -- (void) dealloc { +- (void)dealloc { [_method release]; [_url release]; [_headers release]; @@ -107,7 +107,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { [super dealloc]; } -- (BOOL) hasBody { +- (BOOL)hasBody { return _type ? YES : NO; } @@ -115,17 +115,17 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @implementation GCDWebServerRequest (Subclassing) -- (BOOL) open { +- (BOOL)open { [self doesNotRecognizeSelector:_cmd]; return NO; } -- (NSInteger) write:(const void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)write:(const void*)buffer maxLength:(NSUInteger)length { [self doesNotRecognizeSelector:_cmd]; return -1; } -- (BOOL) close { +- (BOOL)close { [self doesNotRecognizeSelector:_cmd]; return NO; } @@ -136,26 +136,26 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize data=_data; -- (void) dealloc { +- (void)dealloc { DCHECK(_data != nil); [_data release]; [super dealloc]; } -- (BOOL) open { +- (BOOL)open { DCHECK(_data == nil); _data = [[NSMutableData alloc] initWithCapacity:self.contentLength]; return _data ? YES : NO; } -- (NSInteger) write:(const void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)write:(const void*)buffer maxLength:(NSUInteger)length { DCHECK(_data != nil); [_data appendBytes:buffer length:length]; return length; } -- (BOOL) close { +- (BOOL)close { DCHECK(_data != nil); return YES; } @@ -166,14 +166,14 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize filePath=_filePath; -- (id) initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query { +- (id)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query { if ((self = [super initWithMethod:method url:url headers:headers path:path query:query])) { _filePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:[[NSProcessInfo processInfo] globallyUniqueString]] retain]; } return self; } -- (void) dealloc { +- (void)dealloc { DCHECK(_file < 0); unlink([_filePath fileSystemRepresentation]); [_filePath release]; @@ -181,18 +181,18 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { [super dealloc]; } -- (BOOL) open { +- (BOOL)open { DCHECK(_file == 0); _file = open([_filePath fileSystemRepresentation], O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); return (_file > 0 ? YES : NO); } -- (NSInteger) write:(const void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)write:(const void*)buffer maxLength:(NSUInteger)length { DCHECK(_file > 0); return write(_file, buffer, length); } -- (BOOL) close { +- (BOOL)close { DCHECK(_file > 0); int result = close(_file); _file = -1; @@ -205,17 +205,17 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize arguments=_arguments; -+ (NSString*) mimeType { ++ (NSString*)mimeType { return @"application/x-www-form-urlencoded"; } -- (void) dealloc { +- (void)dealloc { [_arguments release]; [super dealloc]; } -- (BOOL) close { +- (BOOL)close { if (![super close]) { return NO; } @@ -234,7 +234,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize contentType=_contentType, mimeType=_mimeType; -- (id) initWithContentType:(NSString*)contentType { +- (id)initWithContentType:(NSString*)contentType { if ((self = [super init])) { _contentType = [contentType copy]; NSArray* components = [_contentType componentsSeparatedByString:@";"]; @@ -248,7 +248,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return self; } -- (void) dealloc { +- (void)dealloc { [_contentType release]; [_mimeType release]; @@ -261,7 +261,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize data=_data, string=_string; -- (id) initWithContentType:(NSString*)contentType data:(NSData*)data { +- (id)initWithContentType:(NSString*)contentType data:(NSData*)data { if ((self = [super initWithContentType:contentType])) { _data = [data retain]; @@ -273,14 +273,14 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return self; } -- (void) dealloc { +- (void)dealloc { [_data release]; [_string release]; [super dealloc]; } -- (NSString*) description { +- (NSString*)description { return [NSString stringWithFormat:@"<%@ | '%@' | %i bytes>", [self class], self.mimeType, (int)_data.length]; } @@ -290,7 +290,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize fileName=_fileName, temporaryPath=_temporaryPath; -- (id) initWithContentType:(NSString*)contentType fileName:(NSString*)fileName temporaryPath:(NSString*)temporaryPath { +- (id)initWithContentType:(NSString*)contentType fileName:(NSString*)fileName temporaryPath:(NSString*)temporaryPath { if ((self = [super initWithContentType:contentType])) { _fileName = [fileName copy]; _temporaryPath = [temporaryPath copy]; @@ -298,7 +298,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return self; } -- (void) dealloc { +- (void)dealloc { unlink([_temporaryPath fileSystemRepresentation]); [_fileName release]; @@ -307,7 +307,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { [super dealloc]; } -- (NSString*) description { +- (NSString*)description { return [NSString stringWithFormat:@"<%@ | '%@' | '%@>'", [self class], self.mimeType, _fileName]; } @@ -317,7 +317,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { @synthesize arguments=_arguments, files=_files; -+ (void) initialize { ++ (void)initialize { if (_newlineData == nil) { _newlineData = [[NSData alloc] initWithBytes:"\r\n" length:2]; DCHECK(_newlineData); @@ -332,11 +332,11 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { } } -+ (NSString*) mimeType { ++ (NSString*)mimeType { return @"multipart/form-data"; } -- (id) initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query { +- (id)initWithMethod:(NSString*)method url:(NSURL*)url headers:(NSDictionary*)headers path:(NSString*)path query:(NSDictionary*)query { if ((self = [super initWithMethod:method url:url headers:headers path:path query:query])) { NSString* boundary = _ExtractHeaderParameter(self.contentType, @"boundary"); if (boundary) { @@ -354,7 +354,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return self; } -- (BOOL) open { +- (BOOL)open { DCHECK(_parserData == nil); _parserData = [[NSMutableData alloc] initWithCapacity:kMultiPartBufferSize]; _parserState = kParserState_Start; @@ -362,7 +362,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { } // http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 -- (BOOL) _parseData { +- (BOOL)_parseData { BOOL success = YES; if (_parserState == kParserState_Headers) { @@ -479,13 +479,13 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return success; } -- (NSInteger) write:(const void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)write:(const void*)buffer maxLength:(NSUInteger)length { DCHECK(_parserData != nil); [_parserData appendBytes:buffer length:length]; return ([self _parseData] ? length : -1); } -- (BOOL) close { +- (BOOL)close { DCHECK(_parserData != nil); [_parserData release]; _parserData = nil; @@ -500,7 +500,7 @@ static NSStringEncoding _StringEncodingFromCharset(NSString* charset) { return (_parserState == kParserState_End ? YES : NO); } -- (void) dealloc { +- (void)dealloc { DCHECK(_parserData == nil); [_arguments release]; [_files release]; diff --git a/CGDWebServer/GCDWebServerResponse.h b/CGDWebServer/GCDWebServerResponse.h index 8df9a3a..1b28cd0 100644 --- a/CGDWebServer/GCDWebServerResponse.h +++ b/CGDWebServer/GCDWebServerResponse.h @@ -41,23 +41,23 @@ @property(nonatomic) NSUInteger cacheControlMaxAge; // Default is 0 seconds i.e. "no-cache" @property(nonatomic, readonly) NSDictionary* additionalHeaders; + (GCDWebServerResponse*) response; -- (id) init; -- (id) initWithContentType:(NSString*)type contentLength:(NSUInteger)length; // Pass nil contentType to indicate empty body -- (void) setValue:(NSString*)value forAdditionalHeader:(NSString*)header; -- (BOOL) hasBody; // Convenience method +- (id)init; +- (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length; // Pass nil contentType to indicate empty body +- (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header; +- (BOOL)hasBody; // Convenience method @end @interface GCDWebServerResponse (Subclassing) -- (BOOL) open; // Implementation required -- (NSInteger) read:(void*)buffer maxLength:(NSUInteger)length; // Implementation required -- (BOOL) close; // Implementation required +- (BOOL)open; // Implementation required +- (NSInteger)read:(void*)buffer maxLength:(NSUInteger)length; // Implementation required +- (BOOL)close; // Implementation required @end @interface GCDWebServerResponse (Extensions) -+ (GCDWebServerResponse*) responseWithStatusCode:(NSInteger)statusCode; -+ (GCDWebServerResponse*) responseWithRedirect:(NSURL*)location permanent:(BOOL)permanent; -- (id) initWithStatusCode:(NSInteger)statusCode; -- (id) initWithRedirect:(NSURL*)location permanent:(BOOL)permanent; ++ (GCDWebServerResponse*)responseWithStatusCode:(NSInteger)statusCode; ++ (GCDWebServerResponse*)responseWithRedirect:(NSURL*)location permanent:(BOOL)permanent; +- (id)initWithStatusCode:(NSInteger)statusCode; +- (id)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent; @end @interface GCDWebServerDataResponse : GCDWebServerResponse { @@ -65,17 +65,17 @@ NSData* _data; NSInteger _offset; } -+ (GCDWebServerDataResponse*) responseWithData:(NSData*)data contentType:(NSString*)type; -- (id) initWithData:(NSData*)data contentType:(NSString*)type; ++ (GCDWebServerDataResponse*)responseWithData:(NSData*)data contentType:(NSString*)type; +- (id)initWithData:(NSData*)data contentType:(NSString*)type; @end @interface GCDWebServerDataResponse (Extensions) -+ (GCDWebServerDataResponse*) responseWithText:(NSString*)text; -+ (GCDWebServerDataResponse*) responseWithHTML:(NSString*)html; -+ (GCDWebServerDataResponse*) responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables; -- (id) initWithText:(NSString*)text; // Encodes using UTF-8 -- (id) initWithHTML:(NSString*)html; // Encodes using UTF-8 -- (id) initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables; // Simple template system that replaces all occurences of "%variable%" with corresponding value (encodes using UTF-8) ++ (GCDWebServerDataResponse*)responseWithText:(NSString*)text; ++ (GCDWebServerDataResponse*)responseWithHTML:(NSString*)html; ++ (GCDWebServerDataResponse*)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables; +- (id)initWithText:(NSString*)text; // Encodes using UTF-8 +- (id)initWithHTML:(NSString*)html; // Encodes using UTF-8 +- (id)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables; // Simple template system that replaces all occurences of "%variable%" with corresponding value (encodes using UTF-8) @end @interface GCDWebServerFileResponse : GCDWebServerResponse { @@ -83,8 +83,8 @@ NSString* _path; int _file; } -+ (GCDWebServerFileResponse*) responseWithFile:(NSString*)path; -+ (GCDWebServerFileResponse*) responseWithFile:(NSString*)path isAttachment:(BOOL)attachment; -- (id) initWithFile:(NSString*)path; -- (id) initWithFile:(NSString*)path isAttachment:(BOOL)attachment; ++ (GCDWebServerFileResponse*)responseWithFile:(NSString*)path; ++ (GCDWebServerFileResponse*)responseWithFile:(NSString*)path isAttachment:(BOOL)attachment; +- (id)initWithFile:(NSString*)path; +- (id)initWithFile:(NSString*)path isAttachment:(BOOL)attachment; @end diff --git a/CGDWebServer/GCDWebServerResponse.m b/CGDWebServer/GCDWebServerResponse.m index 2860a73..ed74ea2 100644 --- a/CGDWebServer/GCDWebServerResponse.m +++ b/CGDWebServer/GCDWebServerResponse.m @@ -33,15 +33,15 @@ @synthesize contentType=_type, contentLength=_length, statusCode=_status, cacheControlMaxAge=_maxAge, additionalHeaders=_headers; -+ (GCDWebServerResponse*) response { ++(GCDWebServerResponse*) response { return [[[[self class] alloc] init] autorelease]; } -- (id) init { +- (id)init { return [self initWithContentType:nil contentLength:0]; } -- (id) initWithContentType:(NSString*)type contentLength:(NSUInteger)length { +- (id)initWithContentType:(NSString*)type contentLength:(NSUInteger)length { if ((self = [super init])) { _type = [type copy]; _length = length; @@ -56,18 +56,18 @@ return self; } -- (void) dealloc { +- (void)dealloc { [_type release]; [_headers release]; [super dealloc]; } -- (void) setValue:(NSString*)value forAdditionalHeader:(NSString*)header { +- (void)setValue:(NSString*)value forAdditionalHeader:(NSString*)header { [_headers setValue:value forKey:header]; } -- (BOOL) hasBody { +- (BOOL)hasBody { return _type ? YES : NO; } @@ -75,17 +75,17 @@ @implementation GCDWebServerResponse (Subclassing) -- (BOOL) open { +- (BOOL)open { [self doesNotRecognizeSelector:_cmd]; return NO; } -- (NSInteger) read:(void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)read:(void*)buffer maxLength:(NSUInteger)length { [self doesNotRecognizeSelector:_cmd]; return -1; } -- (BOOL) close { +- (BOOL)close { [self doesNotRecognizeSelector:_cmd]; return NO; } @@ -94,22 +94,22 @@ @implementation GCDWebServerResponse (Extensions) -+ (GCDWebServerResponse*) responseWithStatusCode:(NSInteger)statusCode { ++ (GCDWebServerResponse*)responseWithStatusCode:(NSInteger)statusCode { return [[[self alloc] initWithStatusCode:statusCode] autorelease]; } -+ (GCDWebServerResponse*) responseWithRedirect:(NSURL*)location permanent:(BOOL)permanent { ++ (GCDWebServerResponse*)responseWithRedirect:(NSURL*)location permanent:(BOOL)permanent { return [[[self alloc] initWithRedirect:location permanent:permanent] autorelease]; } -- (id) initWithStatusCode:(NSInteger)statusCode { +- (id)initWithStatusCode:(NSInteger)statusCode { if ((self = [self initWithContentType:nil contentLength:0])) { self.statusCode = statusCode; } return self; } -- (id) initWithRedirect:(NSURL*)location permanent:(BOOL)permanent { +- (id)initWithRedirect:(NSURL*)location permanent:(BOOL)permanent { if ((self = [self initWithContentType:nil contentLength:0])) { self.statusCode = permanent ? 301 : 307; [self setValue:[location absoluteString] forAdditionalHeader:@"Location"]; @@ -121,11 +121,11 @@ @implementation GCDWebServerDataResponse -+ (GCDWebServerDataResponse*) responseWithData:(NSData*)data contentType:(NSString*)type { ++ (GCDWebServerDataResponse*)responseWithData:(NSData*)data contentType:(NSString*)type { return [[[[self class] alloc] initWithData:data contentType:type] autorelease]; } -- (id) initWithData:(NSData*)data contentType:(NSString*)type { +- (id)initWithData:(NSData*)data contentType:(NSString*)type { if (data == nil) { DNOT_REACHED(); [self release]; @@ -139,20 +139,20 @@ return self; } -- (void) dealloc { +- (void)dealloc { DCHECK(_offset < 0); [_data release]; [super dealloc]; } -- (BOOL) open { +- (BOOL)open { DCHECK(_offset < 0); _offset = 0; return YES; } -- (NSInteger) read:(void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)read:(void*)buffer maxLength:(NSUInteger)length { DCHECK(_offset >= 0); NSInteger size = 0; if (_offset < _data.length) { @@ -163,7 +163,7 @@ return size; } -- (BOOL) close { +- (BOOL)close { DCHECK(_offset >= 0); _offset = -1; return YES; @@ -173,19 +173,19 @@ @implementation GCDWebServerDataResponse (Extensions) -+ (GCDWebServerDataResponse*) responseWithText:(NSString*)text { ++ (GCDWebServerDataResponse*)responseWithText:(NSString*)text { return [[[self alloc] initWithText:text] autorelease]; } -+ (GCDWebServerDataResponse*) responseWithHTML:(NSString*)html { ++ (GCDWebServerDataResponse*)responseWithHTML:(NSString*)html { return [[[self alloc] initWithHTML:html] autorelease]; } -+ (GCDWebServerDataResponse*) responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables { ++ (GCDWebServerDataResponse*)responseWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables { return [[[self alloc] initWithHTMLTemplate:path variables:variables] autorelease]; } -- (id) initWithText:(NSString*)text { +- (id)initWithText:(NSString*)text { NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding]; if (data == nil) { DNOT_REACHED(); @@ -195,7 +195,7 @@ return [self initWithData:data contentType:@"text/plain; charset=utf-8"]; } -- (id) initWithHTML:(NSString*)html { +- (id)initWithHTML:(NSString*)html { NSData* data = [html dataUsingEncoding:NSUTF8StringEncoding]; if (data == nil) { DNOT_REACHED(); @@ -205,7 +205,7 @@ return [self initWithData:data contentType:@"text/html; charset=utf-8"]; } -- (id) initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables { +- (id)initWithHTMLTemplate:(NSString*)path variables:(NSDictionary*)variables { NSMutableString* html = [[NSMutableString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; [variables enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSString* value, BOOL* stop) { [html replaceOccurrencesOfString:[NSString stringWithFormat:@"%%%@%%", key] withString:value options:0 range:NSMakeRange(0, html.length)]; @@ -219,19 +219,19 @@ @implementation GCDWebServerFileResponse -+ (GCDWebServerFileResponse*) responseWithFile:(NSString*)path { ++ (GCDWebServerFileResponse*)responseWithFile:(NSString*)path { return [[[[self class] alloc] initWithFile:path] autorelease]; } -+ (GCDWebServerFileResponse*) responseWithFile:(NSString*)path isAttachment:(BOOL)attachment { ++ (GCDWebServerFileResponse*)responseWithFile:(NSString*)path isAttachment:(BOOL)attachment { return [[[[self class] alloc] initWithFile:path isAttachment:attachment] autorelease]; } -- (id) initWithFile:(NSString*)path { +- (id)initWithFile:(NSString*)path { return [self initWithFile:path isAttachment:NO]; } -- (id) initWithFile:(NSString*)path isAttachment:(BOOL)attachment { +- (id)initWithFile:(NSString*)path isAttachment:(BOOL)attachment { struct stat info; if (lstat([path fileSystemRepresentation], &info) || !(info.st_mode & S_IFREG)) { DNOT_REACHED(); @@ -259,25 +259,25 @@ return self; } -- (void) dealloc { +- (void)dealloc { DCHECK(_file <= 0); [_path release]; [super dealloc]; } -- (BOOL) open { +- (BOOL)open { DCHECK(_file <= 0); _file = open([_path fileSystemRepresentation], O_NOFOLLOW | O_RDONLY); return (_file > 0 ? YES : NO); } -- (NSInteger) read:(void*)buffer maxLength:(NSUInteger)length { +- (NSInteger)read:(void*)buffer maxLength:(NSUInteger)length { DCHECK(_file > 0); return read(_file, buffer, length); } -- (BOOL) close { +- (BOOL)close { DCHECK(_file > 0); int result = close(_file); _file = 0;