GCDWebServerFunctions.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #if !__has_feature(objc_arc)
  26. #error GCDWebServer requires ARC
  27. #endif
  28. #import <TargetConditionals.h>
  29. #if TARGET_OS_IPHONE
  30. #import <MobileCoreServices/MobileCoreServices.h>
  31. #else
  32. #import <SystemConfiguration/SystemConfiguration.h>
  33. #endif
  34. #import <CommonCrypto/CommonDigest.h>
  35. #import <ifaddrs.h>
  36. #import <net/if.h>
  37. #import <netdb.h>
  38. #import "GCDWebServerPrivate.h"
  39. static NSDateFormatter* _dateFormatterRFC822 = nil;
  40. static NSDateFormatter* _dateFormatterISO8601 = nil;
  41. static dispatch_queue_t _dateFormatterQueue = NULL;
  42. // TODO: Handle RFC 850 and ANSI C's asctime() format
  43. void GCDWebServerInitializeFunctions() {
  44. GWS_DCHECK([NSThread isMainThread]); // NSDateFormatter should be initialized on main thread
  45. if (_dateFormatterRFC822 == nil) {
  46. _dateFormatterRFC822 = [[NSDateFormatter alloc] init];
  47. _dateFormatterRFC822.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  48. _dateFormatterRFC822.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
  49. _dateFormatterRFC822.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  50. GWS_DCHECK(_dateFormatterRFC822);
  51. }
  52. if (_dateFormatterISO8601 == nil) {
  53. _dateFormatterISO8601 = [[NSDateFormatter alloc] init];
  54. _dateFormatterISO8601.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
  55. _dateFormatterISO8601.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'+00:00'";
  56. _dateFormatterISO8601.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  57. GWS_DCHECK(_dateFormatterISO8601);
  58. }
  59. if (_dateFormatterQueue == NULL) {
  60. _dateFormatterQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
  61. GWS_DCHECK(_dateFormatterQueue);
  62. }
  63. }
  64. NSString* GCDWebServerNormalizeHeaderValue(NSString* value) {
  65. if (value) {
  66. NSRange range = [value rangeOfString:@";"]; // Assume part before ";" separator is case-insensitive
  67. if (range.location != NSNotFound) {
  68. value = [[[value substringToIndex:range.location] lowercaseString] stringByAppendingString:[value substringFromIndex:range.location]];
  69. } else {
  70. value = [value lowercaseString];
  71. }
  72. }
  73. return value;
  74. }
  75. NSString* GCDWebServerTruncateHeaderValue(NSString* value) {
  76. if (value) {
  77. NSRange range = [value rangeOfString:@";"];
  78. if (range.location != NSNotFound) {
  79. return [value substringToIndex:range.location];
  80. }
  81. }
  82. return value;
  83. }
  84. NSString* GCDWebServerExtractHeaderValueParameter(NSString* value, NSString* name) {
  85. NSString* parameter = nil;
  86. if (value) {
  87. NSScanner* scanner = [[NSScanner alloc] initWithString:value];
  88. [scanner setCaseSensitive:NO]; // Assume parameter names are case-insensitive
  89. NSString* string = [NSString stringWithFormat:@"%@=", name];
  90. if ([scanner scanUpToString:string intoString:NULL]) {
  91. [scanner scanString:string intoString:NULL];
  92. if ([scanner scanString:@"\"" intoString:NULL]) {
  93. [scanner scanUpToString:@"\"" intoString:&parameter];
  94. } else {
  95. [scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&parameter];
  96. }
  97. }
  98. }
  99. return parameter;
  100. }
  101. // http://www.w3schools.com/tags/ref_charactersets.asp
  102. NSStringEncoding GCDWebServerStringEncodingFromCharset(NSString* charset) {
  103. NSStringEncoding encoding = kCFStringEncodingInvalidId;
  104. if (charset) {
  105. encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)charset));
  106. }
  107. return (encoding != kCFStringEncodingInvalidId ? encoding : NSUTF8StringEncoding);
  108. }
  109. NSString* GCDWebServerFormatRFC822(NSDate* date) {
  110. __block NSString* string;
  111. dispatch_sync(_dateFormatterQueue, ^{
  112. string = [_dateFormatterRFC822 stringFromDate:date];
  113. });
  114. return string;
  115. }
  116. NSDate* GCDWebServerParseRFC822(NSString* string) {
  117. __block NSDate* date;
  118. dispatch_sync(_dateFormatterQueue, ^{
  119. date = [_dateFormatterRFC822 dateFromString:string];
  120. });
  121. return date;
  122. }
  123. NSString* GCDWebServerFormatISO8601(NSDate* date) {
  124. __block NSString* string;
  125. dispatch_sync(_dateFormatterQueue, ^{
  126. string = [_dateFormatterISO8601 stringFromDate:date];
  127. });
  128. return string;
  129. }
  130. NSDate* GCDWebServerParseISO8601(NSString* string) {
  131. __block NSDate* date;
  132. dispatch_sync(_dateFormatterQueue, ^{
  133. date = [_dateFormatterISO8601 dateFromString:string];
  134. });
  135. return date;
  136. }
  137. BOOL GCDWebServerIsTextContentType(NSString* type) {
  138. return ([type hasPrefix:@"text/"] || [type hasPrefix:@"application/json"] || [type hasPrefix:@"application/xml"]);
  139. }
  140. NSString* GCDWebServerDescribeData(NSData* data, NSString* type) {
  141. if (GCDWebServerIsTextContentType(type)) {
  142. NSString* charset = GCDWebServerExtractHeaderValueParameter(type, @"charset");
  143. NSString* string = [[NSString alloc] initWithData:data encoding:GCDWebServerStringEncodingFromCharset(charset)];
  144. if (string) {
  145. return string;
  146. }
  147. }
  148. return [NSString stringWithFormat:@"<%lu bytes>", (unsigned long)data.length];
  149. }
  150. NSString* GCDWebServerGetMimeTypeForExtension(NSString* extension, NSDictionary<NSString*, NSString*>* overrides) {
  151. NSDictionary* builtInOverrides = @{@"css" : @"text/css"};
  152. NSString* mimeType = nil;
  153. extension = [extension lowercaseString];
  154. if (extension.length) {
  155. mimeType = [overrides objectForKey:extension];
  156. if (mimeType == nil) {
  157. mimeType = [builtInOverrides objectForKey:extension];
  158. }
  159. if (mimeType == nil) {
  160. CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
  161. if (uti) {
  162. mimeType = CFBridgingRelease(UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType));
  163. CFRelease(uti);
  164. }
  165. }
  166. }
  167. return mimeType ? mimeType : kGCDWebServerDefaultMimeType;
  168. }
  169. NSString* GCDWebServerEscapeURLString(NSString* string) {
  170. #pragma clang diagnostic push
  171. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  172. return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)string, NULL, CFSTR(":@/?&=+"), kCFStringEncodingUTF8));
  173. #pragma clang diagnostic pop
  174. }
  175. NSString* GCDWebServerUnescapeURLString(NSString* string) {
  176. #pragma clang diagnostic push
  177. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  178. return CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault, (CFStringRef)string, CFSTR(""), kCFStringEncodingUTF8));
  179. #pragma clang diagnostic pop
  180. }
  181. NSDictionary<NSString*, NSString*>* GCDWebServerParseURLEncodedForm(NSString* form) {
  182. NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
  183. NSScanner* scanner = [[NSScanner alloc] initWithString:form];
  184. [scanner setCharactersToBeSkipped:nil];
  185. while (1) {
  186. NSString* key = nil;
  187. if (![scanner scanUpToString:@"=" intoString:&key] || [scanner isAtEnd]) {
  188. break;
  189. }
  190. [scanner setScanLocation:([scanner scanLocation] + 1)];
  191. NSString* value = nil;
  192. [scanner scanUpToString:@"&" intoString:&value];
  193. if (value == nil) {
  194. value = @"";
  195. }
  196. key = [key stringByReplacingOccurrencesOfString:@"+" withString:@" "];
  197. NSString* unescapedKey = key ? GCDWebServerUnescapeURLString(key) : nil;
  198. value = [value stringByReplacingOccurrencesOfString:@"+" withString:@" "];
  199. NSString* unescapedValue = value ? GCDWebServerUnescapeURLString(value) : nil;
  200. if (unescapedKey && unescapedValue) {
  201. [parameters setObject:unescapedValue forKey:unescapedKey];
  202. } else {
  203. GWS_LOG_WARNING(@"Failed parsing URL encoded form for key \"%@\" and value \"%@\"", key, value);
  204. GWS_DNOT_REACHED();
  205. }
  206. if ([scanner isAtEnd]) {
  207. break;
  208. }
  209. [scanner setScanLocation:([scanner scanLocation] + 1)];
  210. }
  211. return parameters;
  212. }
  213. NSString* GCDWebServerStringFromSockAddr(const struct sockaddr* addr, BOOL includeService) {
  214. char hostBuffer[NI_MAXHOST];
  215. char serviceBuffer[NI_MAXSERV];
  216. if (getnameinfo(addr, addr->sa_len, hostBuffer, sizeof(hostBuffer), serviceBuffer, sizeof(serviceBuffer), NI_NUMERICHOST | NI_NUMERICSERV | NI_NOFQDN) != 0) {
  217. #if DEBUG
  218. GWS_DNOT_REACHED();
  219. #else
  220. return @"";
  221. #endif
  222. }
  223. return includeService ? [NSString stringWithFormat:@"%s:%s", hostBuffer, serviceBuffer] : (NSString*)[NSString stringWithUTF8String:hostBuffer];
  224. }
  225. NSString* GCDWebServerGetPrimaryIPAddress(BOOL useIPv6) {
  226. NSString* address = nil;
  227. #if TARGET_OS_IPHONE
  228. #if !TARGET_IPHONE_SIMULATOR && !TARGET_OS_TV
  229. const char* primaryInterface = "en0"; // WiFi interface on iOS
  230. #endif
  231. #else
  232. const char* primaryInterface = NULL;
  233. SCDynamicStoreRef store = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("GCDWebServer"), NULL, NULL);
  234. if (store) {
  235. CFPropertyListRef info = SCDynamicStoreCopyValue(store, CFSTR("State:/Network/Global/IPv4")); // There is no equivalent for IPv6 but the primary interface should be the same
  236. if (info) {
  237. NSString* interface = [(__bridge NSDictionary*)info objectForKey:@"PrimaryInterface"];
  238. if (interface) {
  239. primaryInterface = [[NSString stringWithString:interface] UTF8String]; // Copy string to auto-release pool
  240. }
  241. CFRelease(info);
  242. }
  243. CFRelease(store);
  244. }
  245. if (primaryInterface == NULL) {
  246. primaryInterface = "lo0";
  247. }
  248. #endif
  249. struct ifaddrs* list;
  250. if (getifaddrs(&list) >= 0) {
  251. for (struct ifaddrs* ifap = list; ifap; ifap = ifap->ifa_next) {
  252. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_TV
  253. // Assume en0 is Ethernet and en1 is WiFi since there is no way to use SystemConfiguration framework in iOS Simulator
  254. // Assumption holds for Apple TV running tvOS
  255. if (strcmp(ifap->ifa_name, "en0") && strcmp(ifap->ifa_name, "en1"))
  256. #else
  257. if (strcmp(ifap->ifa_name, primaryInterface))
  258. #endif
  259. {
  260. continue;
  261. }
  262. if ((ifap->ifa_flags & IFF_UP) && ((!useIPv6 && (ifap->ifa_addr->sa_family == AF_INET)) || (useIPv6 && (ifap->ifa_addr->sa_family == AF_INET6)))) {
  263. address = GCDWebServerStringFromSockAddr(ifap->ifa_addr, NO);
  264. break;
  265. }
  266. }
  267. freeifaddrs(list);
  268. }
  269. return address;
  270. }
  271. NSString* GCDWebServerComputeMD5Digest(NSString* format, ...) {
  272. va_list arguments;
  273. va_start(arguments, format);
  274. const char* string = [[[NSString alloc] initWithFormat:format arguments:arguments] UTF8String];
  275. va_end(arguments);
  276. unsigned char md5[CC_MD5_DIGEST_LENGTH];
  277. CC_MD5(string, (CC_LONG)strlen(string), md5);
  278. char buffer[2 * CC_MD5_DIGEST_LENGTH + 1];
  279. for (int i = 0; i < CC_MD5_DIGEST_LENGTH; ++i) {
  280. unsigned char byte = md5[i];
  281. unsigned char byteHi = (byte & 0xF0) >> 4;
  282. buffer[2 * i + 0] = byteHi >= 10 ? 'a' + byteHi - 10 : '0' + byteHi;
  283. unsigned char byteLo = byte & 0x0F;
  284. buffer[2 * i + 1] = byteLo >= 10 ? 'a' + byteLo - 10 : '0' + byteLo;
  285. }
  286. buffer[2 * CC_MD5_DIGEST_LENGTH] = 0;
  287. return (NSString*)[NSString stringWithUTF8String:buffer];
  288. }
  289. NSString* GCDWebServerNormalizePath(NSString* path) {
  290. NSMutableArray* components = [[NSMutableArray alloc] init];
  291. for (NSString* component in [path componentsSeparatedByString:@"/"]) {
  292. if ([component isEqualToString:@".."]) {
  293. [components removeLastObject];
  294. } else if (component.length && ![component isEqualToString:@"."]) {
  295. [components addObject:component];
  296. }
  297. }
  298. if (path.length && ([path characterAtIndex:0] == '/')) {
  299. return [@"/" stringByAppendingString:[components componentsJoinedByString:@"/"]]; // Preserve initial slash
  300. }
  301. return [components componentsJoinedByString:@"/"];
  302. }