GCDWebServerPrivate.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2012-2013, 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. #if __has_feature(objc_arc)
  26. #define ARC_BRIDGE __bridge
  27. #define ARC_BRIDGE_RELEASE(__OBJECT__) CFBridgingRelease(__OBJECT__)
  28. #define ARC_RETAIN(__OBJECT__) __OBJECT__
  29. #define ARC_RELEASE(__OBJECT__)
  30. #define ARC_AUTORELEASE(__OBJECT__) __OBJECT__
  31. #define ARC_DEALLOC(__OBJECT__)
  32. #else
  33. #define ARC_BRIDGE
  34. #define ARC_BRIDGE_RELEASE(__OBJECT__) [(id)__OBJECT__ autorelease]
  35. #define ARC_RETAIN(__OBJECT__) [__OBJECT__ retain]
  36. #define ARC_RELEASE(__OBJECT__) [__OBJECT__ release]
  37. #define ARC_AUTORELEASE(__OBJECT__) [__OBJECT__ autorelease]
  38. #define ARC_DEALLOC(__OBJECT__) [__OBJECT__ dealloc]
  39. #endif
  40. #import "GCDWebServerConnection.h"
  41. #ifdef __GCDWEBSERVER_LOGGING_HEADER__
  42. // Define __GCDWEBSERVER_LOGGING_HEADER__ as a preprocessor constant to redirect GCDWebServer logging to your own system
  43. #import __GCDWEBSERVER_LOGGING_HEADER__
  44. #else
  45. static inline void __LogMessage(long level, NSString* format, ...) {
  46. static const char* levelNames[] = {"DEBUG", "VERBOSE", "INFO", "WARNING", "ERROR", "EXCEPTION"};
  47. static long minLevel = -1;
  48. if (minLevel < 0) {
  49. const char* logLevel = getenv("logLevel");
  50. minLevel = logLevel ? atoi(logLevel) : 0;
  51. }
  52. if (level >= minLevel) {
  53. va_list arguments;
  54. va_start(arguments, format);
  55. NSString* message = [[NSString alloc] initWithFormat:format arguments:arguments];
  56. va_end(arguments);
  57. printf("[%s] %s\n", levelNames[level], [message UTF8String]);
  58. ARC_RELEASE(message);
  59. }
  60. }
  61. #define LOG_VERBOSE(...) __LogMessage(1, __VA_ARGS__)
  62. #define LOG_INFO(...) __LogMessage(2, __VA_ARGS__)
  63. #define LOG_WARNING(...) __LogMessage(3, __VA_ARGS__)
  64. #define LOG_ERROR(...) __LogMessage(4, __VA_ARGS__)
  65. #define LOG_EXCEPTION(__EXCEPTION__) __LogMessage(5, @"%@", __EXCEPTION__)
  66. #ifdef NDEBUG
  67. #define DCHECK(__CONDITION__)
  68. #define DNOT_REACHED()
  69. #define LOG_DEBUG(...)
  70. #else
  71. #define DCHECK(__CONDITION__) \
  72. do { \
  73. if (!(__CONDITION__)) { \
  74. abort(); \
  75. } \
  76. } while (0)
  77. #define DNOT_REACHED() abort()
  78. #define LOG_DEBUG(...) __LogMessage(0, __VA_ARGS__)
  79. #endif
  80. #endif
  81. #define kGCDWebServerDefaultMimeType @"application/octet-stream"
  82. #define kGCDWebServerGCDQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension);
  87. NSString* GCDWebServerUnescapeURLString(NSString* string);
  88. NSDictionary* GCDWebServerParseURLEncodedForm(NSString* form);
  89. #ifdef __cplusplus
  90. }
  91. #endif
  92. @interface GCDWebServerConnection ()
  93. - (id)initWithServer:(GCDWebServer*)server address:(NSData*)address socket:(CFSocketNativeHandle)socket;
  94. @end
  95. @interface GCDWebServer ()
  96. @property(nonatomic, readonly) NSArray* handlers;
  97. @end
  98. @interface GCDWebServerHandler : NSObject {
  99. @private
  100. GCDWebServerMatchBlock _matchBlock;
  101. GCDWebServerProcessBlock _processBlock;
  102. }
  103. @property(nonatomic, readonly) GCDWebServerMatchBlock matchBlock;
  104. @property(nonatomic, readonly) GCDWebServerProcessBlock processBlock;
  105. - (id)initWithMatchBlock:(GCDWebServerMatchBlock)matchBlock processBlock:(GCDWebServerProcessBlock)processBlock;
  106. @end