HTTPServer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #import <Foundation/Foundation.h>
  2. @class GCDAsyncSocket;
  3. @class WebSocket;
  4. #if TARGET_OS_IPHONE
  5. #define IMPLEMENTED_PROTOCOLS
  6. #else
  7. #define IMPLEMENTED_PROTOCOLS
  8. #endif
  9. @interface HTTPServer : NSObject IMPLEMENTED_PROTOCOLS
  10. {
  11. // Underlying asynchronous TCP/IP socket
  12. GCDAsyncSocket *asyncSocket;
  13. // Dispatch queues
  14. dispatch_queue_t serverQueue;
  15. dispatch_queue_t connectionQueue;
  16. void *IsOnServerQueueKey;
  17. void *IsOnConnectionQueueKey;
  18. // HTTP server configuration
  19. NSString *documentRoot;
  20. Class connectionClass;
  21. NSString *interface;
  22. UInt16 port;
  23. // Connection management
  24. NSMutableArray *connections;
  25. NSLock *connectionsLock;
  26. BOOL isRunning;
  27. }
  28. /**
  29. * Specifies the document root to serve files from.
  30. * For example, if you set this to "/Users/<your_username>/Sites",
  31. * then it will serve files out of the local Sites directory (including subdirectories).
  32. *
  33. * The default value is nil.
  34. * The default server configuration will not serve any files until this is set.
  35. *
  36. * If you change the documentRoot while the server is running,
  37. * the change will affect future incoming http connections.
  38. **/
  39. - (NSString *)documentRoot;
  40. - (void)setDocumentRoot:(NSString *)value;
  41. /**
  42. * The connection class is the class used to handle incoming HTTP connections.
  43. *
  44. * The default value is [HTTPConnection class].
  45. * You can override HTTPConnection, and then set this to [MyHTTPConnection class].
  46. *
  47. * If you change the connectionClass while the server is running,
  48. * the change will affect future incoming http connections.
  49. **/
  50. - (Class)connectionClass;
  51. - (void)setConnectionClass:(Class)value;
  52. /**
  53. * Set what interface you'd like the server to listen on.
  54. * By default this is nil, which causes the server to listen on all available interfaces like en1, wifi etc.
  55. *
  56. * The interface may be specified by name (e.g. "en1" or "lo0") or by IP address (e.g. "192.168.4.34").
  57. * You may also use the special strings "localhost" or "loopback" to specify that
  58. * the socket only accept connections from the local machine.
  59. **/
  60. - (NSString *)interface;
  61. - (void)setInterface:(NSString *)value;
  62. /**
  63. * The port number to run the HTTP server on.
  64. *
  65. * The default port number is zero, meaning the server will automatically use any available port.
  66. * This is the recommended port value, as it avoids possible port conflicts with other applications.
  67. * Technologies such as Bonjour can be used to allow other applications to automatically discover the port number.
  68. *
  69. * Note: As is common on most OS's, you need root privledges to bind to port numbers below 1024.
  70. *
  71. * You can change the port property while the server is running, but it won't affect the running server.
  72. * To actually change the port the server is listening for connections on you'll need to restart the server.
  73. *
  74. * The listeningPort method will always return the port number the running server is listening for connections on.
  75. * If the server is not running this method returns 0.
  76. **/
  77. - (UInt16)port;
  78. - (UInt16)listeningPort;
  79. - (void)setPort:(UInt16)value;
  80. /**
  81. * Attempts to starts the server on the configured port, interface, etc.
  82. *
  83. * If an error occurs, this method returns NO and sets the errPtr (if given).
  84. * Otherwise returns YES on success.
  85. *
  86. * Some examples of errors that might occur:
  87. * - You specified the server listen on a port which is already in use by another application.
  88. * - You specified the server listen on a port number below 1024, which requires root priviledges.
  89. *
  90. * Code Example:
  91. *
  92. * NSError *err = nil;
  93. * if (![httpServer start:&err])
  94. * {
  95. * NSLog(@"Error starting http server: %@", err);
  96. * }
  97. **/
  98. - (BOOL)start:(NSError **)errPtr;
  99. /**
  100. * Stops the server, preventing it from accepting any new connections.
  101. * You may specify whether or not you want to close the existing client connections.
  102. *
  103. * The default stop method (with no arguments) will close any existing connections. (It invokes [self stop:NO])
  104. **/
  105. - (void)stop;
  106. - (void)stop:(BOOL)keepExistingConnections;
  107. - (BOOL)isRunning;
  108. - (NSUInteger)numberOfHTTPConnections;
  109. @end