ResourceLoadDelegate.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2007, 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #import "config.h"
  29. #import "ResourceLoadDelegate.h"
  30. #import "DumpRenderTree.h"
  31. #import "TestRunner.h"
  32. #import <WebKit/WebKit.h>
  33. #import <WebKit/WebTypesInternal.h>
  34. #import <WebKit/WebDataSourcePrivate.h>
  35. #import <wtf/Assertions.h>
  36. using namespace std;
  37. @interface NSURL (DRTExtras)
  38. - (NSString *)_drt_descriptionSuitableForTestResult;
  39. @end
  40. @interface NSError (DRTExtras)
  41. - (NSString *)_drt_descriptionSuitableForTestResult;
  42. @end
  43. @interface NSURLResponse (DRTExtras)
  44. - (NSString *)_drt_descriptionSuitableForTestResult;
  45. @end
  46. @interface NSURLRequest (DRTExtras)
  47. - (NSString *)_drt_descriptionSuitableForTestResult;
  48. @end
  49. @implementation NSError (DRTExtras)
  50. - (NSString *)_drt_descriptionSuitableForTestResult
  51. {
  52. NSString *str = [NSString stringWithFormat:@"<NSError domain %@, code %ld", [self domain], static_cast<long>([self code])];
  53. NSURL *failingURL;
  54. if ((failingURL = [[self userInfo] objectForKey:@"NSErrorFailingURLKey"]))
  55. str = [str stringByAppendingFormat:@", failing URL \"%@\"", [failingURL _drt_descriptionSuitableForTestResult]];
  56. str = [str stringByAppendingFormat:@">"];
  57. return str;
  58. }
  59. @end
  60. @implementation NSURL (DRTExtras)
  61. - (NSString *)_drt_descriptionSuitableForTestResult
  62. {
  63. if (![self isFileURL])
  64. return [self absoluteString];
  65. WebDataSource *dataSource = [mainFrame dataSource];
  66. if (!dataSource)
  67. dataSource = [mainFrame provisionalDataSource];
  68. NSString *basePath = [[[[dataSource request] URL] path] stringByDeletingLastPathComponent];
  69. basePath = [basePath stringByAppendingString:@"/"];
  70. if ([[self path] hasPrefix:basePath])
  71. return [[self path] substringFromIndex:[basePath length]];
  72. return [self absoluteString];
  73. }
  74. @end
  75. @implementation NSURLResponse (DRTExtras)
  76. - (NSString *)_drt_descriptionSuitableForTestResult
  77. {
  78. int statusCode = 0;
  79. if ([self isKindOfClass:[NSHTTPURLResponse class]])
  80. statusCode = [(NSHTTPURLResponse *)self statusCode];
  81. return [NSString stringWithFormat:@"<NSURLResponse %@, http status code %i>", [[self URL] _drt_descriptionSuitableForTestResult], statusCode];
  82. }
  83. @end
  84. @implementation NSURLRequest (DRTExtras)
  85. - (NSString *)_drt_descriptionSuitableForTestResult
  86. {
  87. NSString *httpMethod = [self HTTPMethod];
  88. if (!httpMethod)
  89. httpMethod = @"(none)";
  90. return [NSString stringWithFormat:@"<NSURLRequest URL %@, main document URL %@, http method %@>", [[self URL] _drt_descriptionSuitableForTestResult], [[self mainDocumentURL] _drt_descriptionSuitableForTestResult], httpMethod];
  91. }
  92. @end
  93. @implementation ResourceLoadDelegate
  94. - (id)webView: (WebView *)wv identifierForInitialRequest: (NSURLRequest *)request fromDataSource: (WebDataSource *)dataSource
  95. {
  96. ASSERT([[dataSource webFrame] dataSource] || [[dataSource webFrame] provisionalDataSource]);
  97. if (!done && gTestRunner->dumpResourceLoadCallbacks())
  98. return [[request URL] _drt_descriptionSuitableForTestResult];
  99. return @"<unknown>";
  100. }
  101. BOOL isLocalhost(NSString *host)
  102. {
  103. // FIXME: Support IPv6 loopbacks.
  104. return NSOrderedSame == [host compare:@"127.0.0.1"] || NSOrderedSame == [host caseInsensitiveCompare:@"localhost"];
  105. }
  106. BOOL hostIsUsedBySomeTestsToGenerateError(NSString *host)
  107. {
  108. return NSOrderedSame == [host compare:@"255.255.255.255"];
  109. }
  110. -(NSURLRequest *)webView: (WebView *)wv resource:identifier willSendRequest: (NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
  111. {
  112. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  113. NSString *string = [NSString stringWithFormat:@"%@ - willSendRequest %@ redirectResponse %@", identifier, [request _drt_descriptionSuitableForTestResult],
  114. [redirectResponse _drt_descriptionSuitableForTestResult]];
  115. printf("%s\n", [string UTF8String]);
  116. }
  117. if (!done && !gTestRunner->deferMainResourceDataLoad()) {
  118. [dataSource _setDeferMainResourceDataLoad:false];
  119. }
  120. if (!done && gTestRunner->willSendRequestReturnsNull())
  121. return nil;
  122. if (!done && gTestRunner->willSendRequestReturnsNullOnRedirect() && redirectResponse) {
  123. printf("Returning null for this redirect\n");
  124. return nil;
  125. }
  126. NSURL *url = [request URL];
  127. NSString *host = [url host];
  128. if (host && (NSOrderedSame == [[url scheme] caseInsensitiveCompare:@"http"] || NSOrderedSame == [[url scheme] caseInsensitiveCompare:@"https"])) {
  129. NSString *testPathOrURL = [NSString stringWithUTF8String:gTestRunner->testPathOrURL().c_str()];
  130. NSString *lowercaseTestPathOrURL = [testPathOrURL lowercaseString];
  131. NSString *testHost = 0;
  132. if ([lowercaseTestPathOrURL hasPrefix:@"http:"] || [lowercaseTestPathOrURL hasPrefix:@"https:"])
  133. testHost = [[NSURL URLWithString:testPathOrURL] host];
  134. if (!isLocalhost(host) && !hostIsUsedBySomeTestsToGenerateError(host) && (!testHost || isLocalhost(testHost))) {
  135. printf("Blocked access to external URL %s\n", [[url absoluteString] cStringUsingEncoding:NSUTF8StringEncoding]);
  136. return nil;
  137. }
  138. }
  139. if (disallowedURLs && CFSetContainsValue(disallowedURLs, url))
  140. return nil;
  141. NSMutableURLRequest *newRequest = [request mutableCopy];
  142. const set<string>& clearHeaders = gTestRunner->willSendRequestClearHeaders();
  143. for (set<string>::const_iterator header = clearHeaders.begin(); header != clearHeaders.end(); ++header) {
  144. NSString *nsHeader = [[NSString alloc] initWithUTF8String:header->c_str()];
  145. [newRequest setValue:nil forHTTPHeaderField:nsHeader];
  146. [nsHeader release];
  147. }
  148. const std::string& destination = gTestRunner->redirectionDestinationForURL([[url absoluteString] UTF8String]);
  149. if (destination.length())
  150. [newRequest setURL:[NSURL URLWithString:[NSString stringWithUTF8String:destination.data()]]];
  151. return [newRequest autorelease];
  152. }
  153. - (void)webView:(WebView *)wv resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
  154. {
  155. if (!gTestRunner->handlesAuthenticationChallenges()) {
  156. NSString *string = [NSString stringWithFormat:@"%@ - didReceiveAuthenticationChallenge - Simulating cancelled authentication sheet", identifier];
  157. printf("%s\n", [string UTF8String]);
  158. [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
  159. return;
  160. }
  161. const char* user = gTestRunner->authenticationUsername().c_str();
  162. NSString *nsUser = [NSString stringWithFormat:@"%s", user ? user : ""];
  163. const char* password = gTestRunner->authenticationPassword().c_str();
  164. NSString *nsPassword = [NSString stringWithFormat:@"%s", password ? password : ""];
  165. NSString *string = [NSString stringWithFormat:@"%@ - didReceiveAuthenticationChallenge - Responding with %@:%@", identifier, nsUser, nsPassword];
  166. printf("%s\n", [string UTF8String]);
  167. [[challenge sender] useCredential:[NSURLCredential credentialWithUser:nsUser password:nsPassword persistence:NSURLCredentialPersistenceForSession]
  168. forAuthenticationChallenge:challenge];
  169. }
  170. - (void)webView:(WebView *)wv resource:(id)identifier didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
  171. {
  172. }
  173. -(void)webView: (WebView *)wv resource:identifier didReceiveResponse: (NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource
  174. {
  175. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  176. NSString *string = [NSString stringWithFormat:@"%@ - didReceiveResponse %@", identifier, [response _drt_descriptionSuitableForTestResult]];
  177. printf("%s\n", [string UTF8String]);
  178. }
  179. if (!done && gTestRunner->dumpResourceResponseMIMETypes())
  180. printf("%s has MIME type %s\n", [[[[response URL] relativePath] lastPathComponent] UTF8String], [[response MIMEType] UTF8String]);
  181. }
  182. -(void)webView: (WebView *)wv resource:identifier didReceiveContentLength: (NSInteger)length fromDataSource:(WebDataSource *)dataSource
  183. {
  184. }
  185. -(void)webView: (WebView *)wv resource:identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
  186. {
  187. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  188. NSString *string = [NSString stringWithFormat:@"%@ - didFinishLoading", identifier];
  189. printf("%s\n", [string UTF8String]);
  190. }
  191. }
  192. -(void)webView: (WebView *)wv resource:identifier didFailLoadingWithError:(NSError *)error fromDataSource:(WebDataSource *)dataSource
  193. {
  194. if (!done && gTestRunner->dumpResourceLoadCallbacks()) {
  195. NSString *string = [NSString stringWithFormat:@"%@ - didFailLoadingWithError: %@", identifier, [error _drt_descriptionSuitableForTestResult]];
  196. printf("%s\n", [string UTF8String]);
  197. }
  198. }
  199. - (void)webView: (WebView *)wv plugInFailedWithError:(NSError *)error dataSource:(WebDataSource *)dataSource
  200. {
  201. // The call to -display here simulates the "Plug-in not found" sheet that Safari shows.
  202. // It is used for platform/mac/plugins/update-widget-from-style-recalc.html
  203. [wv display];
  204. }
  205. -(NSCachedURLResponse *) webView: (WebView *)wv resource:(id)identifier willCacheResponse:(NSCachedURLResponse *)response fromDataSource:(WebDataSource *)dataSource
  206. {
  207. if (!done && gTestRunner->dumpWillCacheResponse()) {
  208. NSString *string = [NSString stringWithFormat:@"%@ - willCacheResponse: called", identifier];
  209. printf("%s\n", [string UTF8String]);
  210. }
  211. return response;
  212. }
  213. -(BOOL)webView: (WebView*)webView shouldPaintBrokenImageForURL:(NSURL*)imageURL
  214. {
  215. // Only log the message when shouldPaintBrokenImage() returns NO; this avoids changing results of layout tests with failed
  216. // images, e.g., security/block-test-no-port.html.
  217. if (!done && gTestRunner->dumpResourceLoadCallbacks() && !gTestRunner->shouldPaintBrokenImage()) {
  218. NSString *string = [NSString stringWithFormat:@"%@ - shouldPaintBrokenImage: NO", [imageURL _drt_descriptionSuitableForTestResult]];
  219. printf("%s\n", [string UTF8String]);
  220. }
  221. return gTestRunner->shouldPaintBrokenImage();
  222. }
  223. @end