GCDWebServerPrivate.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <os/object.h>
  26. #import <sys/socket.h>
  27. /**
  28. * All GCDWebServer headers.
  29. */
  30. #import "GCDWebServerHTTPStatusCodes.h"
  31. #import "GCDWebServerFunctions.h"
  32. #import "GCDWebServer.h"
  33. #import "GCDWebServerConnection.h"
  34. #import "GCDWebServerDataRequest.h"
  35. #import "GCDWebServerFileRequest.h"
  36. #import "GCDWebServerMultiPartFormRequest.h"
  37. #import "GCDWebServerURLEncodedFormRequest.h"
  38. #import "GCDWebServerDataResponse.h"
  39. #import "GCDWebServerErrorResponse.h"
  40. #import "GCDWebServerFileResponse.h"
  41. #import "GCDWebServerStreamedResponse.h"
  42. /**
  43. * Check if a custom logging facility should be used instead.
  44. */
  45. #if defined(__GCDWEBSERVER_LOGGING_HEADER__)
  46. #define __GCDWEBSERVER_LOGGING_FACILITY_CUSTOM__
  47. #import __GCDWEBSERVER_LOGGING_HEADER__
  48. /**
  49. * Automatically detect if XLFacility is available and if so use it as a
  50. * logging facility.
  51. */
  52. #elif defined(__has_include) && __has_include("XLFacilityMacros.h")
  53. #define __GCDWEBSERVER_LOGGING_FACILITY_XLFACILITY__
  54. #undef XLOG_TAG
  55. #define XLOG_TAG @"gcdwebserver.internal"
  56. #import "XLFacilityMacros.h"
  57. #define GWS_LOG_DEBUG(...) XLOG_DEBUG(__VA_ARGS__)
  58. #define GWS_LOG_VERBOSE(...) XLOG_VERBOSE(__VA_ARGS__)
  59. #define GWS_LOG_INFO(...) XLOG_INFO(__VA_ARGS__)
  60. #define GWS_LOG_WARNING(...) XLOG_WARNING(__VA_ARGS__)
  61. #define GWS_LOG_ERROR(...) XLOG_ERROR(__VA_ARGS__)
  62. #define GWS_DCHECK(__CONDITION__) XLOG_DEBUG_CHECK(__CONDITION__)
  63. #define GWS_DNOT_REACHED() XLOG_DEBUG_UNREACHABLE()
  64. /**
  65. * If all of the above fail, then use GCDWebServer built-in
  66. * logging facility.
  67. */
  68. #else
  69. #define __GCDWEBSERVER_LOGGING_FACILITY_BUILTIN__
  70. typedef NS_ENUM(int, GCDWebServerLoggingLevel) {
  71. kGCDWebServerLoggingLevel_Debug = 0,
  72. kGCDWebServerLoggingLevel_Verbose,
  73. kGCDWebServerLoggingLevel_Info,
  74. kGCDWebServerLoggingLevel_Warning,
  75. kGCDWebServerLoggingLevel_Error
  76. };
  77. extern GCDWebServerLoggingLevel GCDWebServerLogLevel;
  78. extern void GCDWebServerLogMessage(GCDWebServerLoggingLevel level, NSString* _Nonnull format, ...) NS_FORMAT_FUNCTION(2, 3);
  79. #if DEBUG
  80. #define GWS_LOG_DEBUG(...) \
  81. do { \
  82. if (GCDWebServerLogLevel <= kGCDWebServerLoggingLevel_Debug) GCDWebServerLogMessage(kGCDWebServerLoggingLevel_Debug, __VA_ARGS__); \
  83. } while (0)
  84. #else
  85. #define GWS_LOG_DEBUG(...)
  86. #endif
  87. #define GWS_LOG_VERBOSE(...) \
  88. do { \
  89. if (GCDWebServerLogLevel <= kGCDWebServerLoggingLevel_Verbose) GCDWebServerLogMessage(kGCDWebServerLoggingLevel_Verbose, __VA_ARGS__); \
  90. } while (0)
  91. #define GWS_LOG_INFO(...) \
  92. do { \
  93. if (GCDWebServerLogLevel <= kGCDWebServerLoggingLevel_Info) GCDWebServerLogMessage(kGCDWebServerLoggingLevel_Info, __VA_ARGS__); \
  94. } while (0)
  95. #define GWS_LOG_WARNING(...) \
  96. do { \
  97. if (GCDWebServerLogLevel <= kGCDWebServerLoggingLevel_Warning) GCDWebServerLogMessage(kGCDWebServerLoggingLevel_Warning, __VA_ARGS__); \
  98. } while (0)
  99. #define GWS_LOG_ERROR(...) \
  100. do { \
  101. if (GCDWebServerLogLevel <= kGCDWebServerLoggingLevel_Error) GCDWebServerLogMessage(kGCDWebServerLoggingLevel_Error, __VA_ARGS__); \
  102. } while (0)
  103. #endif
  104. /**
  105. * Consistency check macros used when building Debug only.
  106. */
  107. #if !defined(GWS_DCHECK) || !defined(GWS_DNOT_REACHED)
  108. #if DEBUG
  109. #define GWS_DCHECK(__CONDITION__) \
  110. do { \
  111. if (!(__CONDITION__)) { \
  112. abort(); \
  113. } \
  114. } while (0)
  115. #define GWS_DNOT_REACHED() abort()
  116. #else
  117. #define GWS_DCHECK(__CONDITION__)
  118. #define GWS_DNOT_REACHED()
  119. #endif
  120. #endif
  121. NS_ASSUME_NONNULL_BEGIN
  122. /**
  123. * GCDWebServer internal constants and APIs.
  124. */
  125. #define kGCDWebServerDefaultMimeType @"application/octet-stream"
  126. #define kGCDWebServerErrorDomain @"GCDWebServerErrorDomain"
  127. static inline BOOL GCDWebServerIsValidByteRange(NSRange range) {
  128. return ((range.location != NSUIntegerMax) || (range.length > 0));
  129. }
  130. static inline NSError* GCDWebServerMakePosixError(int code) {
  131. return [NSError errorWithDomain:NSPOSIXErrorDomain code:code userInfo:@{NSLocalizedDescriptionKey : (NSString*)[NSString stringWithUTF8String:strerror(code)]}];
  132. }
  133. extern void GCDWebServerInitializeFunctions(void);
  134. extern NSString* _Nullable GCDWebServerNormalizeHeaderValue(NSString* _Nullable value);
  135. extern NSString* _Nullable GCDWebServerTruncateHeaderValue(NSString* _Nullable value);
  136. extern NSString* _Nullable GCDWebServerExtractHeaderValueParameter(NSString* _Nullable value, NSString* attribute);
  137. extern NSStringEncoding GCDWebServerStringEncodingFromCharset(NSString* charset);
  138. extern BOOL GCDWebServerIsTextContentType(NSString* type);
  139. extern NSString* GCDWebServerDescribeData(NSData* data, NSString* contentType);
  140. extern NSString* GCDWebServerComputeMD5Digest(NSString* format, ...) NS_FORMAT_FUNCTION(1, 2);
  141. extern NSString* GCDWebServerStringFromSockAddr(const struct sockaddr* addr, BOOL includeService);
  142. @interface GCDWebServerConnection ()
  143. - (instancetype)initWithServer:(GCDWebServer*)server localAddress:(NSData*)localAddress remoteAddress:(NSData*)remoteAddress socket:(CFSocketNativeHandle)socket;
  144. @end
  145. @interface GCDWebServer ()
  146. @property(nonatomic, readonly) NSMutableArray<GCDWebServerHandler*>* handlers;
  147. @property(nonatomic, readonly, nullable) NSString* serverName;
  148. @property(nonatomic, readonly, nullable) NSString* authenticationRealm;
  149. @property(nonatomic, readonly, nullable) NSMutableDictionary<NSString*, NSString*>* authenticationBasicAccounts;
  150. @property(nonatomic, readonly, nullable) NSMutableDictionary<NSString*, NSString*>* authenticationDigestAccounts;
  151. @property(nonatomic, readonly) BOOL shouldAutomaticallyMapHEADToGET;
  152. @property(nonatomic, readonly) dispatch_queue_priority_t dispatchQueuePriority;
  153. - (void)willStartConnection:(GCDWebServerConnection*)connection;
  154. - (void)didEndConnection:(GCDWebServerConnection*)connection;
  155. @end
  156. @interface GCDWebServerHandler : NSObject
  157. @property(nonatomic, readonly) GCDWebServerMatchBlock matchBlock;
  158. @property(nonatomic, readonly) GCDWebServerAsyncProcessBlock asyncProcessBlock;
  159. @end
  160. @interface GCDWebServerRequest ()
  161. @property(nonatomic, readonly) BOOL usesChunkedTransferEncoding;
  162. @property(nonatomic) NSData* localAddressData;
  163. @property(nonatomic) NSData* remoteAddressData;
  164. - (void)prepareForWriting;
  165. - (BOOL)performOpen:(NSError**)error;
  166. - (BOOL)performWriteData:(NSData*)data error:(NSError**)error;
  167. - (BOOL)performClose:(NSError**)error;
  168. - (void)setAttribute:(nullable id)attribute forKey:(NSString*)key;
  169. @end
  170. @interface GCDWebServerResponse ()
  171. @property(nonatomic, readonly) NSDictionary<NSString*, NSString*>* additionalHeaders;
  172. @property(nonatomic, readonly) BOOL usesChunkedTransferEncoding;
  173. - (void)prepareForReading;
  174. - (BOOL)performOpen:(NSError**)error;
  175. - (void)performReadDataWithCompletion:(GCDWebServerBodyReaderCompletionBlock)block;
  176. - (void)performClose;
  177. @end
  178. NS_ASSUME_NONNULL_END