GCDWebServer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension);
  42. NSString* GCDWebServerEscapeURLString(NSString* string);
  43. NSString* GCDWebServerUnescapeURLString(NSString* string);
  44. NSDictionary* GCDWebServerParseURLEncodedForm(NSString* form);
  45. NSString* GCDWebServerGetPrimaryIPv4Address(); // Returns IPv4 address of primary connected service on OS X or of WiFi interface on iOS if connected
  46. #ifdef __cplusplus
  47. }
  48. #endif
  49. @interface GCDWebServer : NSObject
  50. @property(nonatomic, readonly, getter=isRunning) BOOL running;
  51. @property(nonatomic, readonly) NSUInteger port;
  52. @property(nonatomic, readonly) NSString* bonjourName; // Only non-nil if Bonjour registration is active
  53. - (instancetype)init;
  54. - (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;
  55. - (void)removeAllHandlers;
  56. - (BOOL)start; // Default is port 8080 (OS X & iOS Simulator) or 80 (iOS) and computer name
  57. - (BOOL)startWithPort:(NSUInteger)port bonjourName:(NSString*)name; // Pass nil name to disable Bonjour or empty string to use computer name
  58. - (void)stop;
  59. @end
  60. @interface GCDWebServer (Subclassing)
  61. + (Class)connectionClass;
  62. + (NSString*)serverName; // Default is class name
  63. + (BOOL)shouldAutomaticallyMapHEADToGET; // Default is YES which means HEAD requests are mapped to GET requests with the response body being discarded
  64. @end
  65. @interface GCDWebServer (Extensions)
  66. @property(nonatomic, readonly) NSURL* serverURL; // Only non-nil if server is running
  67. @property(nonatomic, readonly) NSURL* bonjourServerURL; // Only non-nil if server is running and Bonjour registration is active
  68. #if !TARGET_OS_IPHONE
  69. @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)
  70. - (BOOL)runWithPort:(NSUInteger)port; // Starts then automatically stops on SIGINT i.e. Ctrl-C (use on main thread only)
  71. - (NSInteger)runTestsInDirectory:(NSString*)path withPort:(NSUInteger)port; // Returns number of failed tests or -1 if server failed to start
  72. #endif
  73. @end
  74. @interface GCDWebServer (Handlers)
  75. - (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
  76. - (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Path is case-insensitive
  77. - (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block; // Regular expression is case-insensitive
  78. @end
  79. @interface GCDWebServer (GETHandlers)
  80. - (void)addGETHandlerForPath:(NSString*)path staticData:(NSData*)staticData contentType:(NSString*)contentType cacheAge:(NSUInteger)cacheAge; // Path is case-insensitive
  81. - (void)addGETHandlerForPath:(NSString*)path filePath:(NSString*)filePath isAttachment:(BOOL)isAttachment cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests; // Path is case-insensitive
  82. - (void)addGETHandlerForBasePath:(NSString*)basePath directoryPath:(NSString*)directoryPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests; // Base path is recursive and case-sensitive
  83. @end
  84. @interface GCDWebServer (Logging)
  85. #ifndef __GCDWEBSERVER_LOGGING_HEADER__
  86. + (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)
  87. #endif
  88. - (void)logVerbose:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  89. - (void)logInfo:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  90. - (void)logWarning:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  91. - (void)logError:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  92. @end