GCDWebServer.h 23 KB

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