WebNSURLExtras.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2005, 2007, 2008, 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com)
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #import "WebNSURLExtras.h"
  30. #import "WebKitNSStringExtras.h"
  31. #import "WebLocalizableStrings.h"
  32. #import "WebNSDataExtras.h"
  33. #import "WebNSObjectExtras.h"
  34. #import "WebSystemInterface.h"
  35. #import <Foundation/NSURLRequest.h>
  36. #import <WebCore/KURL.h>
  37. #import <WebCore/LoaderNSURLExtras.h>
  38. #import <WebCore/WebCoreNSURLExtras.h>
  39. #import <WebKitSystemInterface.h>
  40. #import <wtf/Assertions.h>
  41. #import <unicode/uchar.h>
  42. #import <unicode/uscript.h>
  43. using namespace WebCore;
  44. using namespace WTF;
  45. #define URL_BYTES_BUFFER_LENGTH 2048
  46. @implementation NSURL (WebNSURLExtras)
  47. + (NSURL *)_web_URLWithUserTypedString:(NSString *)string relativeToURL:(NSURL *)URL
  48. {
  49. return URLWithUserTypedString(string, URL);
  50. }
  51. + (NSURL *)_web_URLWithUserTypedString:(NSString *)string
  52. {
  53. return URLWithUserTypedString(string, nil);
  54. }
  55. + (NSURL *)_web_URLWithDataAsString:(NSString *)string
  56. {
  57. if (string == nil) {
  58. return nil;
  59. }
  60. return [self _web_URLWithDataAsString:string relativeToURL:nil];
  61. }
  62. + (NSURL *)_web_URLWithDataAsString:(NSString *)string relativeToURL:(NSURL *)baseURL
  63. {
  64. if (string == nil) {
  65. return nil;
  66. }
  67. string = [string _webkit_stringByTrimmingWhitespace];
  68. NSData *data = [string dataUsingEncoding:NSISOLatin1StringEncoding];
  69. return URLWithData(data, baseURL);
  70. }
  71. + (NSURL *)_web_URLWithData:(NSData *)data
  72. {
  73. return URLWithData(data, nil);
  74. }
  75. + (NSURL *)_web_URLWithData:(NSData *)data relativeToURL:(NSURL *)baseURL
  76. {
  77. return URLWithData(data, baseURL);
  78. }
  79. - (NSData *)_web_originalData
  80. {
  81. return originalURLData(self);
  82. }
  83. - (NSString *)_web_originalDataAsString
  84. {
  85. return [[[NSString alloc] initWithData:originalURLData(self) encoding:NSISOLatin1StringEncoding] autorelease];
  86. }
  87. - (NSString *)_web_userVisibleString
  88. {
  89. return userVisibleString(self);
  90. }
  91. - (BOOL)_web_isEmpty
  92. {
  93. if (!CFURLGetBaseURL((CFURLRef)self))
  94. return CFURLGetBytes((CFURLRef)self, NULL, 0) == 0;
  95. return [originalURLData(self) length] == 0;
  96. }
  97. - (const char *)_web_URLCString
  98. {
  99. NSMutableData *data = [NSMutableData data];
  100. [data appendData:originalURLData(self)];
  101. [data appendBytes:"\0" length:1];
  102. return (const char *)[data bytes];
  103. }
  104. - (NSURL *)_webkit_canonicalize
  105. {
  106. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:self];
  107. Class concreteClass = WKNSURLProtocolClassForRequest(request);
  108. if (!concreteClass) {
  109. [request release];
  110. return self;
  111. }
  112. // This applies NSURL's concept of canonicalization, but not KURL's concept. It would
  113. // make sense to apply both, but when we tried that it caused a performance degradation
  114. // (see 5315926). It might make sense to apply only the KURL concept and not the NSURL
  115. // concept, but it's too risky to make that change for WebKit 3.0.
  116. NSURLRequest *newRequest = [concreteClass canonicalRequestForRequest:request];
  117. NSURL *newURL = [newRequest URL];
  118. NSURL *result = [[newURL retain] autorelease];
  119. [request release];
  120. return result;
  121. }
  122. - (NSURL *)_web_URLByTruncatingOneCharacterBeforeComponent:(CFURLComponentType)component
  123. {
  124. return URLByTruncatingOneCharacterBeforeComponent(self, component);
  125. }
  126. - (NSURL *)_webkit_URLByRemovingFragment
  127. {
  128. return URLByTruncatingOneCharacterBeforeComponent(self, kCFURLComponentFragment);
  129. }
  130. - (NSURL *)_webkit_URLByRemovingResourceSpecifier
  131. {
  132. return URLByTruncatingOneCharacterBeforeComponent(self, kCFURLComponentResourceSpecifier);
  133. }
  134. - (NSURL *)_web_URLByRemovingUserInfo
  135. {
  136. return URLByRemovingUserInfo(self);
  137. }
  138. - (BOOL)_webkit_isJavaScriptURL
  139. {
  140. return [[self _web_originalDataAsString] _webkit_isJavaScriptURL];
  141. }
  142. - (NSString *)_webkit_scriptIfJavaScriptURL
  143. {
  144. return [[self absoluteString] _webkit_scriptIfJavaScriptURL];
  145. }
  146. - (BOOL)_webkit_isFileURL
  147. {
  148. return [[self _web_originalDataAsString] _webkit_isFileURL];
  149. }
  150. - (BOOL)_webkit_isFTPDirectoryURL
  151. {
  152. return [[self _web_originalDataAsString] _webkit_isFTPDirectoryURL];
  153. }
  154. - (BOOL)_webkit_shouldLoadAsEmptyDocument
  155. {
  156. return [[self _web_originalDataAsString] _webkit_hasCaseInsensitivePrefix:@"about:"] || [self _web_isEmpty];
  157. }
  158. - (NSURL *)_web_URLWithLowercasedScheme
  159. {
  160. CFRange range;
  161. CFURLGetByteRangeForComponent((CFURLRef)self, kCFURLComponentScheme, &range);
  162. if (range.location == kCFNotFound) {
  163. return self;
  164. }
  165. UInt8 static_buffer[URL_BYTES_BUFFER_LENGTH];
  166. UInt8 *buffer = static_buffer;
  167. CFIndex bytesFilled = CFURLGetBytes((CFURLRef)self, buffer, URL_BYTES_BUFFER_LENGTH);
  168. if (bytesFilled == -1) {
  169. CFIndex bytesToAllocate = CFURLGetBytes((CFURLRef)self, NULL, 0);
  170. buffer = static_cast<UInt8 *>(malloc(bytesToAllocate));
  171. bytesFilled = CFURLGetBytes((CFURLRef)self, buffer, bytesToAllocate);
  172. ASSERT(bytesFilled == bytesToAllocate);
  173. }
  174. int i;
  175. BOOL changed = NO;
  176. for (i = 0; i < range.length; ++i) {
  177. char c = buffer[range.location + i];
  178. char lower = toASCIILower(c);
  179. if (c != lower) {
  180. buffer[range.location + i] = lower;
  181. changed = YES;
  182. }
  183. }
  184. NSURL *result = changed
  185. ? (NSURL *)WebCFAutorelease(CFURLCreateAbsoluteURLWithBytes(NULL, buffer, bytesFilled, kCFStringEncodingUTF8, nil, YES))
  186. : (NSURL *)self;
  187. if (buffer != static_buffer) {
  188. free(buffer);
  189. }
  190. return result;
  191. }
  192. -(NSData *)_web_schemeSeparatorWithoutColon
  193. {
  194. NSData *result = nil;
  195. CFRange rangeWithSeparators;
  196. CFRange range = CFURLGetByteRangeForComponent((CFURLRef)self, kCFURLComponentScheme, &rangeWithSeparators);
  197. if (rangeWithSeparators.location != kCFNotFound) {
  198. NSString *absoluteString = [self absoluteString];
  199. NSRange separatorsRange = NSMakeRange(range.location + range.length + 1, rangeWithSeparators.length - range.length - 1);
  200. if (separatorsRange.location + separatorsRange.length <= [absoluteString length]) {
  201. NSString *slashes = [absoluteString substringWithRange:separatorsRange];
  202. result = [slashes dataUsingEncoding:NSISOLatin1StringEncoding];
  203. }
  204. }
  205. return result;
  206. }
  207. -(NSData *)_web_dataForURLComponentType:(CFURLComponentType)componentType
  208. {
  209. return dataForURLComponentType(self, componentType);
  210. }
  211. -(NSData *)_web_schemeData
  212. {
  213. return dataForURLComponentType(self, kCFURLComponentScheme);
  214. }
  215. -(NSData *)_web_hostData
  216. {
  217. NSData *result = dataForURLComponentType(self, kCFURLComponentHost);
  218. NSData *scheme = [self _web_schemeData];
  219. // Take off localhost for file
  220. if ([scheme _web_isCaseInsensitiveEqualToCString:"file"]) {
  221. return ([result _web_isCaseInsensitiveEqualToCString:"localhost"]) ? nil : result;
  222. }
  223. return result;
  224. }
  225. - (NSString *)_web_hostString
  226. {
  227. NSData *data = [self _web_hostData];
  228. if (!data) {
  229. data = [NSData data];
  230. }
  231. return [[[NSString alloc] initWithData:[self _web_hostData] encoding:NSUTF8StringEncoding] autorelease];
  232. }
  233. - (NSString *)_webkit_suggestedFilenameWithMIMEType:(NSString *)MIMEType
  234. {
  235. return suggestedFilenameWithMIMEType(self, MIMEType);
  236. }
  237. - (NSURL *)_webkit_URLFromURLOrSchemelessFileURL
  238. {
  239. if ([self scheme])
  240. return self;
  241. return [NSURL URLWithString:[@"file:" stringByAppendingString:[self absoluteString]]];
  242. }
  243. @end
  244. @implementation NSString (WebNSURLExtras)
  245. - (BOOL)_web_isUserVisibleURL
  246. {
  247. return isUserVisibleURL(self);
  248. }
  249. - (BOOL)_webkit_isJavaScriptURL
  250. {
  251. return [self _webkit_hasCaseInsensitivePrefix:@"javascript:"];
  252. }
  253. - (BOOL)_webkit_isFileURL
  254. {
  255. return [self rangeOfString:@"file:" options:(NSCaseInsensitiveSearch | NSAnchoredSearch)].location != NSNotFound;
  256. }
  257. - (NSString *)_webkit_stringByReplacingValidPercentEscapes
  258. {
  259. return decodeURLEscapeSequences(self);
  260. }
  261. - (NSString *)_webkit_scriptIfJavaScriptURL
  262. {
  263. if (![self _webkit_isJavaScriptURL]) {
  264. return nil;
  265. }
  266. return [[self substringFromIndex:11] _webkit_stringByReplacingValidPercentEscapes];
  267. }
  268. - (BOOL)_webkit_isFTPDirectoryURL
  269. {
  270. int length = [self length];
  271. if (length < 5) { // 5 is length of "ftp:/"
  272. return NO;
  273. }
  274. unichar lastChar = [self characterAtIndex:length - 1];
  275. return lastChar == '/' && [self _webkit_hasCaseInsensitivePrefix:@"ftp:"];
  276. }
  277. - (BOOL)_web_hostNameNeedsDecodingWithRange:(NSRange)range
  278. {
  279. return hostNameNeedsDecodingWithRange(self, range);
  280. }
  281. - (BOOL)_web_hostNameNeedsEncodingWithRange:(NSRange)range
  282. {
  283. return hostNameNeedsEncodingWithRange(self, range);
  284. }
  285. - (NSString *)_web_decodeHostNameWithRange:(NSRange)range
  286. {
  287. return decodeHostNameWithRange(self, range);
  288. }
  289. - (NSString *)_web_encodeHostNameWithRange:(NSRange)range
  290. {
  291. return encodeHostNameWithRange(self, range);
  292. }
  293. - (NSString *)_web_decodeHostName
  294. {
  295. return decodeHostName(self);
  296. }
  297. - (NSString *)_web_encodeHostName
  298. {
  299. return encodeHostName(self);
  300. }
  301. -(NSRange)_webkit_rangeOfURLScheme
  302. {
  303. NSRange colon = [self rangeOfString:@":"];
  304. if (colon.location != NSNotFound && colon.location > 0) {
  305. NSRange scheme = {0, colon.location};
  306. static NSCharacterSet *InverseSchemeCharacterSet = nil;
  307. if (!InverseSchemeCharacterSet) {
  308. /*
  309. This stuff is very expensive. 10-15 msec on a 2x1.2GHz. If not cached it swamps
  310. everything else when adding items to the autocomplete DB. Makes me wonder if we
  311. even need to enforce the character set here.
  312. */
  313. NSString *acceptableCharacters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+.-";
  314. InverseSchemeCharacterSet = [[[NSCharacterSet characterSetWithCharactersInString:acceptableCharacters] invertedSet] retain];
  315. }
  316. NSRange illegals = [self rangeOfCharacterFromSet:InverseSchemeCharacterSet options:0 range:scheme];
  317. if (illegals.location == NSNotFound)
  318. return scheme;
  319. }
  320. return NSMakeRange(NSNotFound, 0);
  321. }
  322. -(BOOL)_webkit_looksLikeAbsoluteURL
  323. {
  324. // Trim whitespace because _web_URLWithString allows whitespace.
  325. return [[self _webkit_stringByTrimmingWhitespace] _webkit_rangeOfURLScheme].location != NSNotFound;
  326. }
  327. - (NSString *)_webkit_URLFragment
  328. {
  329. NSRange fragmentRange;
  330. fragmentRange = [self rangeOfString:@"#" options:NSLiteralSearch];
  331. if (fragmentRange.location == NSNotFound)
  332. return nil;
  333. return [self substringFromIndex:fragmentRange.location + 1];
  334. }
  335. @end