GCDWebServerPrivate.h 4.9 KB

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