GCDWebServer.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. Copyright (c) 2012-2015, 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. /**
  29. * The GCDWebServerMatchBlock is called for every handler added to the
  30. * GCDWebServer whenever a new HTTP request has started (i.e. HTTP headers have
  31. * been received). The block is passed the basic info for the request (HTTP method,
  32. * URL, headers...) and must decide if it wants to handle it or not.
  33. *
  34. * If the handler can handle the request, the block must return a new
  35. * GCDWebServerRequest instance created with the same basic info.
  36. * Otherwise, it simply returns nil.
  37. */
  38. typedef GCDWebServerRequest* (^GCDWebServerMatchBlock)(NSString* requestMethod, NSURL* requestURL, NSDictionary* requestHeaders, NSString* urlPath, NSDictionary* urlQuery);
  39. /**
  40. * The GCDWebServerProcessBlock is called after the HTTP request has been fully
  41. * received (i.e. the entire HTTP body has been read). The block is passed the
  42. * GCDWebServerRequest created at the previous step by the GCDWebServerMatchBlock.
  43. *
  44. * The block must return a GCDWebServerResponse or nil on error, which will
  45. * result in a 500 HTTP status code returned to the client. It's however
  46. * recommended to return a GCDWebServerErrorResponse on error so more useful
  47. * information can be returned to the client.
  48. */
  49. typedef GCDWebServerResponse* (^GCDWebServerProcessBlock)(__kindof GCDWebServerRequest* request);
  50. /**
  51. * The GCDWebServerAsynchronousProcessBlock works like the GCDWebServerProcessBlock
  52. * except the GCDWebServerResponse can be returned to the server at a later time
  53. * allowing for asynchronous generation of the response.
  54. *
  55. * The block must eventually call "completionBlock" passing a GCDWebServerResponse
  56. * or nil on error, which will result in a 500 HTTP status code returned to the client.
  57. * It's however recommended to return a GCDWebServerErrorResponse on error so more
  58. * useful information can be returned to the client.
  59. */
  60. typedef void (^GCDWebServerCompletionBlock)(GCDWebServerResponse* response);
  61. typedef void (^GCDWebServerAsyncProcessBlock)(__kindof GCDWebServerRequest* request, GCDWebServerCompletionBlock completionBlock);
  62. /**
  63. * The port used by the GCDWebServer (NSNumber / NSUInteger).
  64. *
  65. * The default value is 0 i.e. let the OS pick a random port.
  66. */
  67. extern NSString* const GCDWebServerOption_Port;
  68. /**
  69. * The Bonjour name used by the GCDWebServer (NSString). If set to an empty string,
  70. * the name will automatically take the value of the GCDWebServerOption_ServerName
  71. * option. If this option is set to nil, Bonjour will be disabled.
  72. *
  73. * The default value is nil.
  74. */
  75. extern NSString* const GCDWebServerOption_BonjourName;
  76. /**
  77. * The Bonjour service type used by the GCDWebServer (NSString).
  78. *
  79. * The default value is "_http._tcp", the service type for HTTP web servers.
  80. */
  81. extern NSString* const GCDWebServerOption_BonjourType;
  82. /**
  83. * Request a port mapping in the NAT gateway (NSNumber / BOOL).
  84. *
  85. * This uses the DNSService API under the hood which supports IPv4 mappings only.
  86. *
  87. * The default value is NO.
  88. *
  89. * @warning The external port set up by the NAT gateway may be different than
  90. * the one used by the GCDWebServer.
  91. */
  92. extern NSString* const GCDWebServerOption_RequestNATPortMapping;
  93. /**
  94. * Only accept HTTP requests coming from localhost i.e. not from the outside
  95. * network (NSNumber / BOOL).
  96. *
  97. * The default value is NO.
  98. *
  99. * @warning Bonjour and NAT port mapping should be disabled if using this option
  100. * since the server will not be reachable from the outside network anyway.
  101. */
  102. extern NSString* const GCDWebServerOption_BindToLocalhost;
  103. /**
  104. * The maximum number of incoming HTTP requests that can be queued waiting to
  105. * be handled before new ones are dropped (NSNumber / NSUInteger).
  106. *
  107. * The default value is 16.
  108. */
  109. extern NSString* const GCDWebServerOption_MaxPendingConnections;
  110. /**
  111. * The value for "Server" HTTP header used by the GCDWebServer (NSString).
  112. *
  113. * The default value is the GCDWebServer class name.
  114. */
  115. extern NSString* const GCDWebServerOption_ServerName;
  116. /**
  117. * The authentication method used by the GCDWebServer
  118. * (one of "GCDWebServerAuthenticationMethod_...").
  119. *
  120. * The default value is nil i.e. authentication is disabled.
  121. */
  122. extern NSString* const GCDWebServerOption_AuthenticationMethod;
  123. /**
  124. * The authentication realm used by the GCDWebServer (NSString).
  125. *
  126. * The default value is the same as the GCDWebServerOption_ServerName option.
  127. */
  128. extern NSString* const GCDWebServerOption_AuthenticationRealm;
  129. /**
  130. * The authentication accounts used by the GCDWebServer
  131. * (NSDictionary of username / password pairs).
  132. *
  133. * The default value is nil i.e. no accounts.
  134. */
  135. extern NSString* const GCDWebServerOption_AuthenticationAccounts;
  136. /**
  137. * The class used by the GCDWebServer when instantiating GCDWebServerConnection
  138. * (subclass of GCDWebServerConnection).
  139. *
  140. * The default value is the GCDWebServerConnection class.
  141. */
  142. extern NSString* const GCDWebServerOption_ConnectionClass;
  143. /**
  144. * Allow the GCDWebServer to pretend "HEAD" requests are actually "GET" ones
  145. * and automatically discard the HTTP body of the response (NSNumber / BOOL).
  146. *
  147. * The default value is YES.
  148. */
  149. extern NSString* const GCDWebServerOption_AutomaticallyMapHEADToGET;
  150. /**
  151. * The interval expressed in seconds used by the GCDWebServer to decide how to
  152. * coalesce calls to -webServerDidConnect: and -webServerDidDisconnect:
  153. * (NSNumber / double). Coalescing will be disabled if the interval is <= 0.0.
  154. *
  155. * The default value is 1.0 second.
  156. */
  157. extern NSString* const GCDWebServerOption_ConnectedStateCoalescingInterval;
  158. /**
  159. * Set the dispatch queue priority on which server connection will be
  160. * run (NSNumber / long).
  161. *
  162. *
  163. * The default value is DISPATCH_QUEUE_PRIORITY_DEFAULT.
  164. */
  165. extern NSString* const GCDWebServerOption_DispatchQueuePriority;
  166. #if TARGET_OS_IPHONE
  167. /**
  168. * Enables the GCDWebServer to automatically suspend itself (as if -stop was
  169. * called) when the iOS app goes into the background and the last
  170. * GCDWebServerConnection is closed, then resume itself (as if -start was called)
  171. * when the iOS app comes back to the foreground (NSNumber / BOOL).
  172. *
  173. * See the README.md file for more information about this option.
  174. *
  175. * The default value is YES.
  176. *
  177. * @warning The running property will be NO while the GCDWebServer is suspended.
  178. */
  179. extern NSString* const GCDWebServerOption_AutomaticallySuspendInBackground;
  180. #endif
  181. /**
  182. * HTTP Basic Authentication scheme (see https://tools.ietf.org/html/rfc2617).
  183. *
  184. * @warning Use of this authentication scheme is not recommended as the
  185. * passwords are sent in clear.
  186. */
  187. extern NSString* const GCDWebServerAuthenticationMethod_Basic;
  188. /**
  189. * HTTP Digest Access Authentication scheme (see https://tools.ietf.org/html/rfc2617).
  190. */
  191. extern NSString* const GCDWebServerAuthenticationMethod_DigestAccess;
  192. @class GCDWebServer;
  193. /**
  194. * Delegate methods for GCDWebServer.
  195. *
  196. * @warning These methods are always called on the main thread in a serialized way.
  197. */
  198. @protocol GCDWebServerDelegate <NSObject>
  199. @optional
  200. /**
  201. * This method is called after the server has successfully started.
  202. */
  203. - (void)webServerDidStart:(GCDWebServer*)server;
  204. /**
  205. * This method is called after the Bonjour registration for the server has
  206. * successfully completed.
  207. *
  208. * Use the "bonjourServerURL" property to retrieve the Bonjour address of the
  209. * server.
  210. */
  211. - (void)webServerDidCompleteBonjourRegistration:(GCDWebServer*)server;
  212. /**
  213. * This method is called after the NAT port mapping for the server has been
  214. * updated.
  215. *
  216. * Use the "publicServerURL" property to retrieve the public address of the
  217. * server.
  218. */
  219. - (void)webServerDidUpdateNATPortMapping:(GCDWebServer*)server;
  220. /**
  221. * This method is called when the first GCDWebServerConnection is opened by the
  222. * server to serve a series of HTTP requests.
  223. *
  224. * A series of HTTP requests is considered ongoing as long as new HTTP requests
  225. * keep coming (and new GCDWebServerConnection instances keep being opened),
  226. * until before the last HTTP request has been responded to (and the
  227. * corresponding last GCDWebServerConnection closed).
  228. */
  229. - (void)webServerDidConnect:(GCDWebServer*)server;
  230. /**
  231. * This method is called when the last GCDWebServerConnection is closed after
  232. * the server has served a series of HTTP requests.
  233. *
  234. * The GCDWebServerOption_ConnectedStateCoalescingInterval option can be used
  235. * to have the server wait some extra delay before considering that the series
  236. * of HTTP requests has ended (in case there some latency between consecutive
  237. * requests). This effectively coalesces the calls to -webServerDidConnect:
  238. * and -webServerDidDisconnect:.
  239. */
  240. - (void)webServerDidDisconnect:(GCDWebServer*)server;
  241. /**
  242. * This method is called after the server has stopped.
  243. */
  244. - (void)webServerDidStop:(GCDWebServer*)server;
  245. @end
  246. /**
  247. * The GCDWebServer class listens for incoming HTTP requests on a given port,
  248. * then passes each one to a "handler" capable of generating an HTTP response
  249. * for it, which is then sent back to the client.
  250. *
  251. * GCDWebServer instances can be created and used from any thread but it's
  252. * recommended to have the main thread's runloop be running so internal callbacks
  253. * can be handled e.g. for Bonjour registration.
  254. *
  255. * See the README.md file for more information about the architecture of GCDWebServer.
  256. */
  257. @interface GCDWebServer : NSObject
  258. /**
  259. * Sets the delegate for the server.
  260. */
  261. @property(nonatomic, assign) id<GCDWebServerDelegate> delegate;
  262. /**
  263. * Returns YES if the server is currently running.
  264. */
  265. @property(nonatomic, readonly, getter=isRunning) BOOL running;
  266. /**
  267. * Returns the port used by the server.
  268. *
  269. * @warning This property is only valid if the server is running.
  270. */
  271. @property(nonatomic, readonly) NSUInteger port;
  272. /**
  273. * Returns the Bonjour name used by the server.
  274. *
  275. * @warning This property is only valid if the server is running and Bonjour
  276. * registration has successfully completed, which can take up to a few seconds.
  277. */
  278. @property(nonatomic, readonly) NSString* bonjourName;
  279. /**
  280. * Returns the Bonjour service type used by the server.
  281. *
  282. * @warning This property is only valid if the server is running and Bonjour
  283. * registration has successfully completed, which can take up to a few seconds.
  284. */
  285. @property(nonatomic, readonly) NSString* bonjourType;
  286. /**
  287. * This method is the designated initializer for the class.
  288. */
  289. - (instancetype)init;
  290. /**
  291. * Adds to the server a handler that generates responses synchronously when handling incoming HTTP requests.
  292. *
  293. * Handlers are called in a LIFO queue, so if multiple handlers can potentially
  294. * respond to a given request, the latest added one wins.
  295. *
  296. * @warning Addling handlers while the server is running is not allowed.
  297. */
  298. - (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;
  299. /**
  300. * Adds to the server a handler that generates responses asynchronously when handling incoming HTTP requests.
  301. *
  302. * Handlers are called in a LIFO queue, so if multiple handlers can potentially
  303. * respond to a given request, the latest added one wins.
  304. *
  305. * @warning Addling handlers while the server is running is not allowed.
  306. */
  307. - (void)addHandlerWithMatchBlock:(GCDWebServerMatchBlock)matchBlock asyncProcessBlock:(GCDWebServerAsyncProcessBlock)processBlock;
  308. /**
  309. * Removes all handlers previously added to the server.
  310. *
  311. * @warning Removing handlers while the server is running is not allowed.
  312. */
  313. - (void)removeAllHandlers;
  314. /**
  315. * Starts the server with explicit options. This method is the designated way
  316. * to start the server.
  317. *
  318. * Returns NO if the server failed to start and sets "error" argument if not NULL.
  319. */
  320. - (BOOL)startWithOptions:(NSDictionary*)options error:(NSError**)error;
  321. /**
  322. * Stops the server and prevents it to accepts new HTTP requests.
  323. *
  324. * @warning Stopping the server does not abort GCDWebServerConnection instances
  325. * currently handling already received HTTP requests. These connections will
  326. * continue to execute normally until completion.
  327. */
  328. - (void)stop;
  329. @end
  330. @interface GCDWebServer (Extensions)
  331. /**
  332. * Returns the server's URL.
  333. *
  334. * @warning This property is only valid if the server is running.
  335. */
  336. @property(nonatomic, readonly) NSURL* serverURL;
  337. /**
  338. * Returns the server's Bonjour URL.
  339. *
  340. * @warning This property is only valid if the server is running and Bonjour
  341. * registration has successfully completed, which can take up to a few seconds.
  342. * Also be aware this property will not automatically update if the Bonjour hostname
  343. * has been dynamically changed after the server started running (this should be rare).
  344. */
  345. @property(nonatomic, readonly) NSURL* bonjourServerURL;
  346. /**
  347. * Returns the server's public URL.
  348. *
  349. * @warning This property is only valid if the server is running and NAT port
  350. * mapping is active.
  351. */
  352. @property(nonatomic, readonly) NSURL* publicServerURL;
  353. /**
  354. * Starts the server on port 8080 (OS X & iOS Simulator) or port 80 (iOS)
  355. * using the default Bonjour name.
  356. *
  357. * Returns NO if the server failed to start.
  358. */
  359. - (BOOL)start;
  360. /**
  361. * Starts the server on a given port and with a specific Bonjour name.
  362. * Pass a nil Bonjour name to disable Bonjour entirely or an empty string to
  363. * use the default name.
  364. *
  365. * Returns NO if the server failed to start.
  366. */
  367. - (BOOL)startWithPort:(NSUInteger)port bonjourName:(NSString*)name;
  368. #if !TARGET_OS_IPHONE
  369. /**
  370. * Runs the server synchronously using -startWithPort:bonjourName: until a
  371. * SIGINT signal is received i.e. Ctrl-C. This method is intended to be used
  372. * by command line tools.
  373. *
  374. * Returns NO if the server failed to start.
  375. *
  376. * @warning This method must be used from the main thread only.
  377. */
  378. - (BOOL)runWithPort:(NSUInteger)port bonjourName:(NSString*)name;
  379. /**
  380. * Runs the server synchronously using -startWithOptions: until a SIGTERM or
  381. * SIGINT signal is received i.e. Ctrl-C in Terminal. This method is intended to
  382. * be used by command line tools.
  383. *
  384. * Returns NO if the server failed to start and sets "error" argument if not NULL.
  385. *
  386. * @warning This method must be used from the main thread only.
  387. */
  388. - (BOOL)runWithOptions:(NSDictionary*)options error:(NSError**)error;
  389. #endif
  390. @end
  391. @interface GCDWebServer (Handlers)
  392. /**
  393. * Adds a default handler to the server to handle all incoming HTTP requests
  394. * with a given HTTP method and generate responses synchronously.
  395. */
  396. - (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
  397. /**
  398. * Adds a default handler to the server to handle all incoming HTTP requests
  399. * with a given HTTP method and generate responses asynchronously.
  400. */
  401. - (void)addDefaultHandlerForMethod:(NSString*)method requestClass:(Class)aClass asyncProcessBlock:(GCDWebServerAsyncProcessBlock)block;
  402. /**
  403. * Adds a handler to the server to handle incoming HTTP requests with a given
  404. * HTTP method and a specific case-insensitive path and generate responses
  405. * synchronously.
  406. */
  407. - (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
  408. /**
  409. * Adds a handler to the server to handle incoming HTTP requests with a given
  410. * HTTP method and a specific case-insensitive path and generate responses
  411. * asynchronously.
  412. */
  413. - (void)addHandlerForMethod:(NSString*)method path:(NSString*)path requestClass:(Class)aClass asyncProcessBlock:(GCDWebServerAsyncProcessBlock)block;
  414. /**
  415. * Adds a handler to the server to handle incoming HTTP requests with a given
  416. * HTTP method and a path matching a case-insensitive regular expression and
  417. * generate responses synchronously.
  418. */
  419. - (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass processBlock:(GCDWebServerProcessBlock)block;
  420. /**
  421. * Adds a handler to the server to handle incoming HTTP requests with a given
  422. * HTTP method and a path matching a case-insensitive regular expression and
  423. * generate responses asynchronously.
  424. */
  425. - (void)addHandlerForMethod:(NSString*)method pathRegex:(NSString*)regex requestClass:(Class)aClass asyncProcessBlock:(GCDWebServerAsyncProcessBlock)block;
  426. @end
  427. @interface GCDWebServer (GETHandlers)
  428. /**
  429. * Adds a handler to the server to respond to incoming "GET" HTTP requests
  430. * with a specific case-insensitive path with in-memory data.
  431. */
  432. - (void)addGETHandlerForPath:(NSString*)path staticData:(NSData*)staticData contentType:(NSString*)contentType cacheAge:(NSUInteger)cacheAge;
  433. /**
  434. * Adds a handler to the server to respond to incoming "GET" HTTP requests
  435. * with a specific case-insensitive path with a file.
  436. */
  437. - (void)addGETHandlerForPath:(NSString*)path filePath:(NSString*)filePath isAttachment:(BOOL)isAttachment cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests;
  438. /**
  439. * Adds a handler to the server to respond to incoming "GET" HTTP requests
  440. * with a case-insensitive path inside a base path with the corresponding file
  441. * inside a local directory. If no local file matches the request path, a 401
  442. * HTTP status code is returned to the client.
  443. *
  444. * The "indexFilename" argument allows to specify an "index" file name to use
  445. * when the request path corresponds to a directory.
  446. */
  447. - (void)addGETHandlerForBasePath:(NSString*)basePath directoryPath:(NSString*)directoryPath indexFilename:(NSString*)indexFilename cacheAge:(NSUInteger)cacheAge allowRangeRequests:(BOOL)allowRangeRequests;
  448. @end
  449. /**
  450. * GCDWebServer provides its own built-in logging facility which is used by
  451. * default. It simply sends log messages to stderr assuming it is connected
  452. * to a terminal type device.
  453. *
  454. * GCDWebServer is also compatible with a limited set of third-party logging
  455. * facilities. If one of them is available at compile time, GCDWebServer will
  456. * automatically use it in place of the built-in one.
  457. *
  458. * Currently supported third-party logging facilities are:
  459. * - XLFacility (by the same author as GCDWebServer): https://github.com/swisspol/XLFacility
  460. * - CocoaLumberjack: https://github.com/CocoaLumberjack/CocoaLumberjack
  461. *
  462. * For both the built-in logging facility and CocoaLumberjack, the default
  463. * logging level is INFO (or DEBUG if the preprocessor constant "DEBUG"
  464. * evaluates to non-zero at compile time).
  465. *
  466. * It's possible to have GCDWebServer use a custom logging facility by defining
  467. * the "__GCDWEBSERVER_LOGGING_HEADER__" preprocessor constant in Xcode build
  468. * settings to the name of a custom header file (escaped like \"MyLogging.h\").
  469. * This header file must define the following set of macros:
  470. *
  471. * GWS_LOG_DEBUG(...)
  472. * GWS_LOG_VERBOSE(...)
  473. * GWS_LOG_INFO(...)
  474. * GWS_LOG_WARNING(...)
  475. * GWS_LOG_ERROR(...)
  476. * GWS_LOG_EXCEPTION(__EXCEPTION__)
  477. *
  478. * IMPORTANT: Except for GWS_LOG_EXCEPTION() which gets passed an NSException,
  479. * these macros must behave like NSLog(). Furthermore the GWS_LOG_DEBUG() macro
  480. * should not do anything unless the preprocessor constant "DEBUG" evaluates to
  481. * non-zero.
  482. *
  483. * The logging methods below send log messages to the same logging facility
  484. * used by GCDWebServer. They can be used for consistency wherever you interact
  485. * with GCDWebServer in your code (e.g. in the implementation of handlers).
  486. */
  487. @interface GCDWebServer (Logging)
  488. /**
  489. * Sets the log level of the logging facility below which log messages are discarded.
  490. *
  491. * @warning The interpretation of the "level" argument depends on the logging
  492. * facility used at compile time.
  493. *
  494. * If using the built-in logging facility, the log levels are as follow:
  495. * DEBUG = 0
  496. * VERBOSE = 1
  497. * INFO = 2
  498. * WARNING = 3
  499. * ERROR = 4
  500. * EXCEPTION = 5
  501. */
  502. + (void)setLogLevel:(int)level;
  503. /**
  504. * Logs a message to the logging facility at the VERBOSE level.
  505. */
  506. - (void)logVerbose:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  507. /**
  508. * Logs a message to the logging facility at the INFO level.
  509. */
  510. - (void)logInfo:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  511. /**
  512. * Logs a message to the logging facility at the WARNING level.
  513. */
  514. - (void)logWarning:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  515. /**
  516. * Logs a message to the logging facility at the ERROR level.
  517. */
  518. - (void)logError:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);
  519. /**
  520. * Logs an exception to the logging facility at the EXCEPTION level.
  521. */
  522. - (void)logException:(NSException*)exception;
  523. @end
  524. #ifdef __GCDWEBSERVER_ENABLE_TESTING__
  525. @interface GCDWebServer (Testing)
  526. /**
  527. * Activates recording of HTTP requests and responses which create files in the
  528. * current directory containing the raw data for all requests and responses.
  529. *
  530. * @warning The current directory must not contain any prior recording files.
  531. */
  532. @property(nonatomic, getter=isRecordingEnabled) BOOL recordingEnabled;
  533. /**
  534. * Runs tests by playing back pre-recorded HTTP requests in the given directory
  535. * and comparing the generated responses with the pre-recorded ones.
  536. *
  537. * Returns the number of failed tests or -1 if server failed to start.
  538. */
  539. - (NSInteger)runTestsWithOptions:(NSDictionary*)options inDirectory:(NSString*)path;
  540. @end
  541. #endif