GCDWebServer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright (c) 2012-2014, Pierre-Olivier Latour
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * The name of Pierre-Olivier Latour may not be used to endorse
  12. or promote products derived from this software without specific
  13. prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL PIERRE-OLIVIER LATOUR BE LIABLE FOR ANY
  18. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import <TargetConditionals.h>
  26. #import "GCDWebServerRequest.h"
  27. #import "GCDWebServerResponse.h"
  28. typedef NS_ENUM(int, GCDWebServerLogLevel) {
  29. kGCDWebServerLogLevel_Debug = 0, // Only available if "NDEBUG" is not defined when building
  30. kGCDWebServerLogLevel_Verbose,
  31. kGCDWebServerLogLevel_Info,
  32. kGCDWebServerLogLevel_Warning,
  33. kGCDWebServerLogLevel_Error,
  34. kGCDWebServerLogLevel_Exception,
  35. };
  36. typedef GCDWebServerRequest* (^GCDWebServerMatchBlock)(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery);
  37. typedef GCDWebServerResponse* (^GCDWebServerProcessBlock)(GCDWebServerRequest* request);
  38. @interface GCDWebServer : NSObject
  39. @property(nonatomic, readonly, getter=isRunning) BOOL running;
  40. @property(nonatomic, readonly) NSUInteger port;
  41. @property(nonatomic, readonly) NSString* bonjourName; // Only non-nil if Bonjour registration is active
  42. - (instancetype)init;
  43. - (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;
  44. - (void)removeAllHandlers;
  45. - (BOOL)start; // Default is port 8080 (OS X & iOS Simulator) or 80 (iOS) and computer name
  46. - (BOOL)startWithPort:(NSUInteger)port bonjourName:(NSString*)name; // Pass nil name to disable Bonjour or empty string to use computer name
  47. - (void)stop;
  48. @end
  49. @interface GCDWebServer (Subclassing)
  50. + (Class)connectionClass;
  51. + (NSString*)serverName; // Default is class name
  52. + (BOOL)shouldAutomaticallyMapHEADToGET; // Default is YES which means HEAD requests are mapped to GET requests with the response body being discarded
  53. @end
  54. @interface GCDWebServer (Extensions)
  55. @property(nonatomic, readonly) NSURL* serverURL; // Only non-nil if server is running
  56. @property(nonatomic, readonly) NSURL* bonjourServerURL; // Only non-nil if server is running and Bonjour registration is active
  57. #if !TARGET_OS_IPHONE
  58. @property(nonatomic, getter=isRecordingEnabled) BOOL recordingEnabled; // Creates files in the current directory containing the raw data for all requests and responses (directory most NOT contain prior recordings)
  59. - (BOOL)runWithPort:(NSUInteger)port; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only)
  60. #endif
  61. #ifdef __GCDWEBSERVER_ENABLE_TESTING__
  62. - (NSInteger)runTestsInDirectory:(NSString*)path withPort:(NSUInteger)port; // Returns number of failed tests or -1 if server failed to start
  63. #endif
  64. @end
  65. @interface GCDWebServer (Handlers)
  66. - (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
  67. - (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Path is case-insensitive
  68. - (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Regular expression is case-insensitive
  69. @end
  70. @interface GCDWebServer (GETHandlers)
  71. - (void)addGETHandlerForPath:(NSString*)path staticData:(NSData*)staticData contentType:(NSString*)contentType cacheAge:(NSUInteger)cacheAge; // Path is case-insensitive
  72. - (void)addGETHandlerForPath:(NSString*)path filePath:(NSString*)filePath isAttachment:(BOOL)isAttachment cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests; // Path is case-insensitive
  73. - (void)addGETHandlerForBasePath:(NSString*)basePath directoryPath:(NSString*)directoryPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests; // Base path is recursive and case-sensitive
  74. @end
  75. @interface GCDWebServer (Logging)
  76. #ifndef __GCDWEBSERVER_LOGGING_HEADER__
  77. + (void)setLogLevel:(GCDWebServerLogLevel)level; // Default level is DEBUG or INFO if "NDEBUG" is defined when building (it can also be set at runtime with the "logLevel" environment variable)
  78. #endif
  79. - (void)logVerbose:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  80. - (void)logInfo:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  81. - (void)logWarning:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  82. - (void)logError:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  83. @end