Reviewed by Dan Bernstein.
[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 "ResourceLoadDelegate.h"
30
31 #import "DumpRenderTree.h"
32 #import "LayoutTestController.h"
33 #import <WebKit/WebKit.h>
34 #import <WebKit/WebTypesInternal.h>
35 #import <wtf/Assertions.h>
36
37 @interface NSURL (DRTExtras)
38 - (NSString *)_drt_descriptionSuitableForTestResult;
39 @end
40
41 @interface NSError (DRTExtras)
42 - (NSString *)_drt_descriptionSuitableForTestResult;
43 @end
44
45 @interface NSURLResponse (DRTExtras)
46 - (NSString *)_drt_descriptionSuitableForTestResult;
47 @end
48
49 @interface NSURLRequest (DRTExtras)
50 - (NSString *)_drt_descriptionSuitableForTestResult;
51 @end
52
53 @implementation NSError (DRTExtras)
54 - (NSString *)_drt_descriptionSuitableForTestResult 
55 {
56     NSString *str = [NSString stringWithFormat:@"<NSError domain %@, code %d", [self domain], [self code]];
57     NSURL *failingURL;
58     
59     if ((failingURL = [[self userInfo] objectForKey:@"NSErrorFailingURLKey"]))
60         str = [str stringByAppendingFormat:@", failing URL \"%@\"", [failingURL _drt_descriptionSuitableForTestResult]];
61         
62     str = [str stringByAppendingFormat:@">"];
63     
64     return str;
65 }
66
67 @end
68
69 @implementation NSURL (DRTExtras)
70
71 - (NSString *)_drt_descriptionSuitableForTestResult 
72 {
73     if (![self isFileURL])
74         return [self absoluteString];
75
76     WebDataSource *dataSource = [mainFrame dataSource];
77     if (!dataSource)
78         dataSource = [mainFrame provisionalDataSource];
79     
80     NSString *basePath = [[[[dataSource request] URL] path] stringByDeletingLastPathComponent];
81     
82     return [[self path] substringFromIndex:[basePath length] + 1];
83 }
84
85 @end
86
87 @implementation NSURLResponse (DRTExtras)
88
89 - (NSString *)_drt_descriptionSuitableForTestResult
90 {
91     return [NSString stringWithFormat:@"<NSURLResponse %@>", [[self URL] _drt_descriptionSuitableForTestResult]];
92 }
93
94 @end
95
96 @implementation NSURLRequest (DRTExtras)
97
98 - (NSString *)_drt_descriptionSuitableForTestResult
99 {
100     return [NSString stringWithFormat:@"<NSURLRequest %@>", [[self URL] _drt_descriptionSuitableForTestResult]];
101 }
102
103 @end
104
105 @implementation ResourceLoadDelegate
106
107 - webView: (WebView *)wv identifierForInitialRequest: (NSURLRequest *)request fromDataSource: (WebDataSource *)dataSource
108 {
109     ASSERT([[dataSource webFrame] dataSource] || [[dataSource webFrame] provisionalDataSource]);
110     
111     if (!done && layoutTestController->dumpResourceLoadCallbacks())
112         return [[request URL] _drt_descriptionSuitableForTestResult];
113     
114     return @"<unknown>";
115 }
116
117 -(NSURLRequest *)webView: (WebView *)wv resource:identifier willSendRequest: (NSURLRequest *)newRequest redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)dataSource
118 {
119     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
120         NSString *string = [NSString stringWithFormat:@"%@ - willSendRequest %@ redirectResponse %@", identifier, [newRequest _drt_descriptionSuitableForTestResult],
121             [redirectResponse _drt_descriptionSuitableForTestResult]];
122         printf("%s\n", [string UTF8String]);
123     }    
124
125     NSURL *url = [newRequest URL];
126     NSString *host = [url host];
127     if (host
128         && (NSOrderedSame == [[url scheme] caseInsensitiveCompare:@"http"] || NSOrderedSame == [[url scheme] caseInsensitiveCompare:@"https"])
129         && NSOrderedSame != [host compare:@"127.0.0.1"]
130         && NSOrderedSame != [host compare:@"255.255.255.255"] // used in some tests that expect to get back an error
131         && NSOrderedSame != [host caseInsensitiveCompare:@"localhost"]) {
132         fprintf(stderr, "Blocked access to external URL %s\n", [[url absoluteString] cStringUsingEncoding:NSUTF8StringEncoding]);
133         return nil;
134     }
135
136     if (disallowedURLs && CFSetContainsValue(disallowedURLs, url))
137         return nil;
138     
139     return newRequest;
140 }
141
142 - (void)webView:(WebView *)wv resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
143 {
144 }
145
146 - (void)webView:(WebView *)wv resource:(id)identifier didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource
147 {
148 }
149
150 -(void)webView: (WebView *)wv resource:identifier didReceiveResponse: (NSURLResponse *)response fromDataSource:(WebDataSource *)dataSource
151 {
152     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
153         NSString *string = [NSString stringWithFormat:@"%@ - didReceiveResponse %@", identifier, [response _drt_descriptionSuitableForTestResult]];
154         printf ("%s\n", [string UTF8String]);
155     }    
156 }
157
158 -(void)webView: (WebView *)wv resource:identifier didReceiveContentLength: (NSInteger)length fromDataSource:(WebDataSource *)dataSource
159 {
160 }
161
162 -(void)webView: (WebView *)wv resource:identifier didFinishLoadingFromDataSource:(WebDataSource *)dataSource
163 {
164     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
165         NSString *string = [NSString stringWithFormat:@"%@ - didFinishLoading", identifier];
166         printf ("%s\n", [string UTF8String]);
167     }
168 }
169
170 -(void)webView: (WebView *)wv resource:identifier didFailLoadingWithError:(NSError *)error fromDataSource:(WebDataSource *)dataSource
171 {
172     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
173         NSString *string = [NSString stringWithFormat:@"%@ - didFailLoadingWithError: %@", identifier, [error _drt_descriptionSuitableForTestResult]];
174         printf ("%s\n", [string UTF8String]);
175     }
176 }
177
178 - (void)webView: (WebView *)wv plugInFailedWithError:(NSError *)error dataSource:(WebDataSource *)dataSource
179 {
180 }
181
182 -(NSCachedURLResponse *) webView: (WebView *)wv resource:(id)identifier willCacheResponse:(NSCachedURLResponse *)response fromDataSource:(WebDataSource *)dataSource
183 {
184     if (!done && layoutTestController->dumpResourceLoadCallbacks()) {
185         NSString *string = [NSString stringWithFormat:@"%@ - willCacheResponse: called", identifier];
186         printf ("%s\n", [string UTF8String]);
187     }
188     return response;
189 }
190
191 @end