2012-12-30 14:23:49 +08:00
/*
2014-02-07 10:44:04 +08:00
Copyright ( c ) 2012 - 2014 , Pierre - Olivier Latour
2013-12-30 11:02:01 +08:00
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
* Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
* Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
documentation and / or other materials provided with the distribution .
* The name of Pierre - Olivier Latour may not be used to endorse
or promote products derived from this software without specific
prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS " AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL PIERRE - OLIVIER LATOUR BE LIABLE FOR ANY
DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
2012-12-30 14:23:49 +08:00
2014-03-30 08:41:36 +08:00
# import <TargetConditionals.h>
2012-12-30 14:23:49 +08:00
# import "GCDWebServerRequest.h"
# import "GCDWebServerResponse.h"
typedef GCDWebServerRequest * ( ^ GCDWebServerMatchBlock ) ( NSString * requestMethod , NSURL * requestURL , NSDictionary * requestHeaders , NSString * urlPath , NSDictionary * urlQuery ) ;
typedef GCDWebServerResponse * ( ^ GCDWebServerProcessBlock ) ( GCDWebServerRequest * request ) ;
2014-03-21 08:28:54 +08:00
# ifdef __cplusplus
extern " C " {
# endif
NSString * GCDWebServerGetMimeTypeForExtension ( NSString * extension ) ;
2014-03-30 04:55:22 +08:00
NSString * GCDWebServerEscapeURLString ( NSString * string ) ;
2014-03-21 08:28:54 +08:00
NSString * GCDWebServerUnescapeURLString ( NSString * string ) ;
NSDictionary * GCDWebServerParseURLEncodedForm ( NSString * form ) ;
2014-03-30 12:29:07 +08:00
NSString * GCDWebServerGetPrimaryIPv4Address ( ) ; // Returns IPv4 address of primary connected service on OS X or of WiFi interface on iOS if connected
2014-03-21 08:28:54 +08:00
# ifdef __cplusplus
}
# endif
2014-03-20 00:19:59 +08:00
@ interface GCDWebServer : NSObject
2012-12-30 14:23:49 +08:00
@ property ( nonatomic , readonly , getter = isRunning ) BOOL running ;
@ property ( nonatomic , readonly ) NSUInteger port ;
2014-01-24 06:17:44 +08:00
@ property ( nonatomic , readonly ) NSString * bonjourName ; // Only non-nil if Bonjour registration is active
2012-12-31 10:48:25 +08:00
- ( void ) addHandlerWithMatchBlock : ( GCDWebServerMatchBlock ) matchBlock processBlock : ( GCDWebServerProcessBlock ) processBlock ;
- ( void ) removeAllHandlers ;
2012-12-30 14:23:49 +08:00
2014-03-30 12:29:07 +08:00
- ( BOOL ) start ; // Default is port 8080 (OS X & iOS Simulator) or 80 (iOS) and computer name
2013-04-02 06:42:16 +08:00
- ( BOOL ) startWithPort : ( NSUInteger ) port bonjourName : ( NSString * ) name ; // Pass nil name to disable Bonjour or empty string to use computer name
2012-12-31 10:48:25 +08:00
- ( void ) stop ;
2012-12-30 14:23:49 +08:00
@ end
@ interface GCDWebServer ( Subclassing )
2012-12-31 10:48:25 +08:00
+ ( Class ) connectionClass ;
+ ( NSString * ) serverName ; // Default is class name
2012-12-30 14:23:49 +08:00
@ end
@ interface GCDWebServer ( Extensions )
2014-03-30 12:29:07 +08:00
@ property ( nonatomic , readonly ) NSURL * serverURL ; // Only non-nil if server is running
@ property ( nonatomic , readonly ) NSURL * bonjourServerURL ; // Only non-nil if server is running and Bonjour registration is active
# if !TARGET_OS_IPHONE
2012-12-31 10:48:25 +08:00
- ( BOOL ) runWithPort : ( NSUInteger ) port ; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only)
2014-03-23 11:11:52 +08:00
# endif
2014-03-30 12:29:07 +08:00
@ end
2014-03-23 11:11:52 +08:00
2012-12-30 14:23:49 +08:00
@ interface GCDWebServer ( Handlers )
2014-03-20 00:10:47 +08:00
- ( void ) addDefaultHandlerForMethod : ( NSString * ) method requestClass : ( Class ) aClass processBlock : ( GCDWebServerProcessBlock ) block ;
- ( void ) addHandlerForMethod : ( NSString * ) method path : ( NSString * ) path requestClass : ( Class ) aClass processBlock : ( GCDWebServerProcessBlock ) block ; // Path is case-insensitive
- ( void ) addHandlerForMethod : ( NSString * ) method pathRegex : ( NSString * ) regex requestClass : ( Class ) aClass processBlock : ( GCDWebServerProcessBlock ) block ; // Regular expression is case-insensitive
2012-12-30 14:23:49 +08:00
@ end
2014-03-21 03:55:09 +08:00
@ interface GCDWebServer ( GETHandlers )
- ( void ) addGETHandlerForPath : ( NSString * ) path staticData : ( NSData * ) staticData contentType : ( NSString * ) contentType cacheAge : ( NSUInteger ) cacheAge ; // Path is case-insensitive
- ( void ) addGETHandlerForPath : ( NSString * ) path filePath : ( NSString * ) filePath isAttachment : ( BOOL ) isAttachment cacheAge : ( NSUInteger ) cacheAge allowRangeRequests : ( BOOL ) allowRangeRequests ; // Path is case-insensitive
- ( void ) addGETHandlerForBasePath : ( NSString * ) basePath directoryPath : ( NSString * ) directoryPath indexFilename : ( NSString * ) indexFilename cacheAge : ( NSUInteger ) cacheAge allowRangeRequests : ( BOOL ) allowRangeRequests ; // Base path is recursive and case-sensitive
@ end