HTTPServer.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #import <Foundation/Foundation.h>
  2. @class GCDAsyncSocket;
  3. @class WebSocket;
  4. #if TARGET_OS_IPHONE
  5. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 40000 // iPhone 4.0
  6. #define IMPLEMENTED_PROTOCOLS <NSNetServiceDelegate>
  7. #else
  8. #define IMPLEMENTED_PROTOCOLS
  9. #endif
  10. #else
  11. #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 // Mac OS X 10.6
  12. #define IMPLEMENTED_PROTOCOLS <NSNetServiceDelegate>
  13. #else
  14. #define IMPLEMENTED_PROTOCOLS
  15. #endif
  16. #endif
  17. @interface HTTPServer : NSObject IMPLEMENTED_PROTOCOLS
  18. {
  19. // Underlying asynchronous TCP/IP socket
  20. GCDAsyncSocket *asyncSocket;
  21. // Dispatch queues
  22. dispatch_queue_t serverQueue;
  23. dispatch_queue_t connectionQueue;
  24. void *IsOnServerQueueKey;
  25. void *IsOnConnectionQueueKey;
  26. // HTTP server configuration
  27. NSString *documentRoot;
  28. Class connectionClass;
  29. NSString *interface;
  30. UInt16 port;
  31. // NSNetService and related variables
  32. NSNetService *netService;
  33. NSString *domain;
  34. NSString *type;
  35. NSString *name;
  36. NSString *publishedName;
  37. NSDictionary *txtRecordDictionary;
  38. // Connection management
  39. NSMutableArray *connections;
  40. NSMutableArray *webSockets;
  41. NSLock *connectionsLock;
  42. NSLock *webSocketsLock;
  43. BOOL isRunning;
  44. }
  45. /**
  46. * Specifies the document root to serve files from.
  47. * For example, if you set this to "/Users/<your_username>/Sites",
  48. * then it will serve files out of the local Sites directory (including subdirectories).
  49. *
  50. * The default value is nil.
  51. * The default server configuration will not serve any files until this is set.
  52. *
  53. * If you change the documentRoot while the server is running,
  54. * the change will affect future incoming http connections.
  55. **/
  56. - (NSString *)documentRoot;
  57. - (void)setDocumentRoot:(NSString *)value;
  58. /**
  59. * The connection class is the class used to handle incoming HTTP connections.
  60. *
  61. * The default value is [HTTPConnection class].
  62. * You can override HTTPConnection, and then set this to [MyHTTPConnection class].
  63. *
  64. * If you change the connectionClass while the server is running,
  65. * the change will affect future incoming http connections.
  66. **/
  67. - (Class)connectionClass;
  68. - (void)setConnectionClass:(Class)value;
  69. /**
  70. * Set what interface you'd like the server to listen on.
  71. * By default this is nil, which causes the server to listen on all available interfaces like en1, wifi etc.
  72. *
  73. * The interface may be specified by name (e.g. "en1" or "lo0") or by IP address (e.g. "192.168.4.34").
  74. * You may also use the special strings "localhost" or "loopback" to specify that
  75. * the socket only accept connections from the local machine.
  76. **/
  77. - (NSString *)interface;
  78. - (void)setInterface:(NSString *)value;
  79. /**
  80. * The port number to run the HTTP server on.
  81. *
  82. * The default port number is zero, meaning the server will automatically use any available port.
  83. * This is the recommended port value, as it avoids possible port conflicts with other applications.
  84. * Technologies such as Bonjour can be used to allow other applications to automatically discover the port number.
  85. *
  86. * Note: As is common on most OS's, you need root privledges to bind to port numbers below 1024.
  87. *
  88. * You can change the port property while the server is running, but it won't affect the running server.
  89. * To actually change the port the server is listening for connections on you'll need to restart the server.
  90. *
  91. * The listeningPort method will always return the port number the running server is listening for connections on.
  92. * If the server is not running this method returns 0.
  93. **/
  94. - (UInt16)port;
  95. - (UInt16)listeningPort;
  96. - (void)setPort:(UInt16)value;
  97. /**
  98. * Bonjour domain for publishing the service.
  99. * The default value is "local.".
  100. *
  101. * Note: Bonjour publishing requires you set a type.
  102. *
  103. * If you change the domain property after the bonjour service has already been published (server already started),
  104. * you'll need to invoke the republishBonjour method to update the broadcasted bonjour service.
  105. **/
  106. - (NSString *)domain;
  107. - (void)setDomain:(NSString *)value;
  108. /**
  109. * Bonjour name for publishing the service.
  110. * The default value is "".
  111. *
  112. * If using an empty string ("") for the service name when registering,
  113. * the system will automatically use the "Computer Name".
  114. * Using an empty string will also handle name conflicts
  115. * by automatically appending a digit to the end of the name.
  116. *
  117. * Note: Bonjour publishing requires you set a type.
  118. *
  119. * If you change the name after the bonjour service has already been published (server already started),
  120. * you'll need to invoke the republishBonjour method to update the broadcasted bonjour service.
  121. *
  122. * The publishedName method will always return the actual name that was published via the bonjour service.
  123. * If the service is not running this method returns nil.
  124. **/
  125. - (NSString *)name;
  126. - (NSString *)publishedName;
  127. - (void)setName:(NSString *)value;
  128. /**
  129. * Bonjour type for publishing the service.
  130. * The default value is nil.
  131. * The service will not be published via bonjour unless the type is set.
  132. *
  133. * If you wish to publish the service as a traditional HTTP server, you should set the type to be "_http._tcp.".
  134. *
  135. * If you change the type after the bonjour service has already been published (server already started),
  136. * you'll need to invoke the republishBonjour method to update the broadcasted bonjour service.
  137. **/
  138. - (NSString *)type;
  139. - (void)setType:(NSString *)value;
  140. /**
  141. * Republishes the service via bonjour if the server is running.
  142. * If the service was not previously published, this method will publish it (if the server is running).
  143. **/
  144. - (void)republishBonjour;
  145. /**
  146. *
  147. **/
  148. - (NSDictionary *)TXTRecordDictionary;
  149. - (void)setTXTRecordDictionary:(NSDictionary *)dict;
  150. /**
  151. * Attempts to starts the server on the configured port, interface, etc.
  152. *
  153. * If an error occurs, this method returns NO and sets the errPtr (if given).
  154. * Otherwise returns YES on success.
  155. *
  156. * Some examples of errors that might occur:
  157. * - You specified the server listen on a port which is already in use by another application.
  158. * - You specified the server listen on a port number below 1024, which requires root priviledges.
  159. *
  160. * Code Example:
  161. *
  162. * NSError *err = nil;
  163. * if (![httpServer start:&err])
  164. * {
  165. * NSLog(@"Error starting http server: %@", err);
  166. * }
  167. **/
  168. - (BOOL)start:(NSError **)errPtr;
  169. /**
  170. * Stops the server, preventing it from accepting any new connections.
  171. * You may specify whether or not you want to close the existing client connections.
  172. *
  173. * The default stop method (with no arguments) will close any existing connections. (It invokes [self stop:NO])
  174. **/
  175. - (void)stop;
  176. - (void)stop:(BOOL)keepExistingConnections;
  177. - (BOOL)isRunning;
  178. - (void)addWebSocket:(WebSocket *)ws;
  179. - (NSUInteger)numberOfHTTPConnections;
  180. - (NSUInteger)numberOfWebSocketConnections;
  181. @end