fca65f94f73a809adb58cff55c03e82436a762d6
[WebKit-https.git] / WebKitTools / DumpRenderTree / mac / ResourceLoadDelegate.mm
1 /*
2  * Copyright (C) 2007, 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
29 #import "config.h"
30 #import "ResourceLoadDelegate.h"
31
32 #import "DumpRenderTree.h"
33 #import "LayoutTestController.h"
34 #import <WebKit/WebKit.h>
35 #import <WebKit/WebTypesInternal.h>
36 #import <wtf/Assertions.h>
37
38 using namespace std;
39
40 @interface NSURL (DRTExtras)
41 - (NSString *)_drt_descriptionSuitableForTestResult;
42 @end
43
44 @interface NSError (DRTExtras)
45 - (NSString *)_drt_descriptionSuitableForTestResult;
46 @end
47
48 @interface NSURLResponse (DRTExtras)
49 - (NSString *)_drt_descriptionSuitableForTestResult;
50 @end
51
52 @interface NSURLRequest (DRTExtras)
53 - (NSString *)_drt_descriptionSuitableForTestResult;
54 @end
55
56 @implementation NSError (DRTExtras)
57 - (NSString *)_drt_descriptionSuitableForTestResult 
58 {
59     NSString *str = [NSString stringWithFormat:@"<NSError domain %@, code %d", [self domain], [self code]];
60     NSURL *failingURL;
61
62     if ((failingURL = [[self userInfo] objectForKey:@"NSErrorFailingURLKey"]))
63         str = [str stringByAppendingFormat:@", failing URL \"%@\"", [failingURL _drt_descriptionSuitableForTestResult]];
64
65     str = [str stringByAppendingFormat:@">"];
66
67     return str;
68 }
69
70 @end
71
72 @implementation NSURL (DRTExtras)
73
74 - (NSString *)_drt_descriptionSuitableForTestResult 
75 {
76     if (![self isFileURL])
77         return [self absoluteString];
78
79     WebDataSource *dataSource = [mainFrame dataSource];
80     if (!dataSource)
81         dataSource = [mainFrame provisionalDataSource];
82
83     NSString *basePath = [[[[dataSource request] URL] path] stringByDeletingLastPathComponent];
84
85     return [[self path] substringFromIndex:[basePath length] + 1];
86 }
87
88 @end
89
90 @implementation NSURLResponse (DRTExtras)
91
92 - (NSString *)_drt_descriptionSuitableForTestResult
93 {
94     int statusCode = 0;
95     if ([self isKindOfClass:[NSHTTPURLResponse class]])
96         statusCode = [(NSHTTPURLResponse *)self statusCode];
97     return [NSString stringWithFormat:@"<NSURLResponse %@, http status code %i>", [[self URL] _drt_descriptionSuitableForTestResult], statusCode];
98 }
99
100 @end
101
102 @implementation NSURLRequest (DRTExtras)
103
104 - (NSString *)_drt_descriptionSuitableForTestResult
105 {
106     NSString *httpMethod = [self HTTPMethod];
107     if (!httpMethod)
108         httpMethod = @"(none)";
109     return [NSString stringWithFormat:@"<NSURLRequest URL %@, main document URL %@, http method %@>", [[self URL] _drt_descriptionSuitableForTestResult], [[self mainDocumentURL] _drt_descriptionSuitableForTestResult], httpMethod];
110 }
111
112 @end
113
114 @implementation ResourceLoadDelegate
115
116 - webView: (WebView *)wv identifierForInitialRequest: (NSURLRequest *)request fromDataSource: (WebDataSource *)dataSource
117 {
118     ASSERT([[dataSource webFrame] dataSource] || [[dataSource webFrame] provisionalDataSource]);
119
120     if (!done && gLayoutTestController->dumpResourceLoadCallbacks())
121         return [[request URL] _drt_descriptionSuitableForTestResult];
122
123     return @"<unknown>";
124 }
125
126 -(NSURLRequest *)webView: (WebView *)wv resource:identifier willSendRequest: (NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
127 {
128     if (!done && gLayoutTestController->dumpResourceLoadCallbacks()) {
129         NSString *string = [NSString stringWithFormat:@"%@ - willSendRequest %@ redirectResponse %@", identifier, [request _drt_descriptionSuitableForTestResult],
130             [redirectResponse _drt_descriptionSuitableForTestResult]];
131         printf("%s\n", [string UTF8String]);
132     }
133
134     if (!done && gLayoutTestController->willSendRequestReturnsNull())
135         return nil;
136
137     if (!done && gLayoutTestController->willSendRequestReturnsNullOnRedirect() && redirectResponse) {
138         printf("Returning null for this redirect\n");
139         return nil;
140     }
141
142     NSURL *url = [request URL];
143     NSString *host = [url host];
144     if (host
145         && (NSOrderedSame == [[url scheme] caseInsensitiveCompare:@"http"] || NSOrderedSame == [[url scheme] caseInsensitiveCompare:@"https"])
146         && NSOrderedSame != [host compare:@"127.0.0.1"]
147         && NSOrderedSame != [host compare:@"255.255.255.255"] // used in some tests that expect to get back an error
148         && NSOrderedSame != [host caseInsensitiveCompare:@"localhost"]) {
149         printf("Blocked access to external URL %s\n", [[url absoluteString] cStringUsingEncoding:NSUTF8StringEncoding]);
150         return nil;
151     }
152
153     if (disallowedURLs && CFSetContainsValue(disallowedURLs, url))
154         return nil;
155
156     NSMutableURLRequest *newRequest = [request mutableCopy];
157     const set<string>& clearHeaders = gLayoutTestController->willSendRequestClearHeaders();
158     for (set<string>::const_iterator header = clearHeaders.begin(); header != clearHeaders.end(); ++header) {
159         NSString *nsHeader = [[NSString alloc] initWithUTF8String:header->c_str()];
160         [newRequest setValue:nil forHTTPHeaderField:nsHeader];
161         [nsHeader release];
162     }
163
164     return [newRequest autorelease];
165 }
166
167 - (void)webView:(WebView *)wv resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
168 {
169     if (!gLayoutTestController->handlesAuthenticationChallenges()) {
170         [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
171         return;
172     }
173     
174     const char* user = gLayoutTestController->authenticationUsername().c_str();
175     NSString *nsUser = [NSString stringWithFormat:@"%s", user ? user : ""];
176
177     const char* password = gLayoutTestController->authenticationPassword().c_str();
178     NSString *nsPassword = [NSString stringWithFormat:@"%s", password ? password : ""];
179
180     NSString *string = [NSString stringWithFormat:@"%@ - didReceiveAuthenticationChallenge - Responding with %@:%@", identifier, nsUser, nsPassword];
181     printf("%s\n", [string UTF8String]);
182     
183     [[challenge sender] useCredential:[NSURLCredential credentialWithUser:nsUser password:nsPassword persistence:NSURLCredentialPersistenceForSession]
184                               forAuthenticationChallenge:challenge];
185 }
186
187 - (void)webView:(WebView *)wv resource:(id)identifier didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
188 {
189 }
190
191 -(void)webView: (WebView *)wv resource:identifier didReceiveResponse: (NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource
192 {
193     if (!done && gLayoutTestController->dumpResourceLoadCallbacks()) {
194         NSString *string = [NSString stringWithFormat:@"%@ - didReceiveResponse %@", identifier, [response _drt_descriptionSuitableForTestResult]];
195         printf("%s\n", [string UTF8String]);
196     }
197     if (!done && gLayoutTestController->dumpResourceResponseMIMETypes())
198         printf("%s has MIME type %s\n", [[[[response URL] relativePath] lastPathComponent] UTF8String], [[response MIMEType] UTF8String]);
199 }
200
201 -(void)webView: (WebView *)wv resource:identifier didReceiveContentLength: (NSInteger)length fromDataSource:(WebDataSource *)dataSource
202 {
203 }
204
205 -(void)webView: (WebView *)wv resource:identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
206 {
207     if (!done && gLayoutTestController->dumpResourceLoadCallbacks()) {
208         NSString *string = [NSString stringWithFormat:@"%@ - didFinishLoading", identifier];
209         printf("%s\n", [string UTF8String]);
210     }
211 }
212
213 -(void)webView: (WebView *)wv resource:identifier didFailLoadingWithError:(NSError *)error fromDataSource:(WebDataSource *)dataSource
214 {
215     if (!done && gLayoutTestController->dumpResourceLoadCallbacks()) {
216         NSString *string = [NSString stringWithFormat:@"%@ - didFailLoadingWithError: %@", identifier, [error _drt_descriptionSuitableForTestResult]];
217         printf("%s\n", [string UTF8String]);
218     }
219 }
220
221 - (void)webView: (WebView *)wv plugInFailedWithError:(NSError *)error dataSource:(WebDataSource *)dataSource
222 {
223     // The call to -display here simulates the "Plug-in not found" sheet that Safari shows.
224     // It is used for platform/mac/plugins/update-widget-from-style-recalc.html
225     [wv display];
226 }
227
228 -(NSCachedURLResponse *) webView: (WebView *)wv resource:(id)identifier willCacheResponse:(NSCachedURLResponse *)response fromDataSource:(WebDataSource *)dataSource
229 {
230     if (!done && gLayoutTestController->dumpWillCacheResponse()) {
231         NSString *string = [NSString stringWithFormat:@"%@ - willCacheResponse: called", identifier];
232         printf("%s\n", [string UTF8String]);
233     }
234     return response;
235 }
236
237 @end