2 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "ResourceHandle.h"
29 #include "ResourceHandleClient.h"
30 #include "ResourceHandleInternal.h"
32 #include "AuthenticationCF.h"
33 #include "AuthenticationChallenge.h"
34 #include "DocLoader.h"
36 #include "FrameLoader.h"
38 #include "NotImplemented.h"
39 #include "ResourceError.h"
40 #include "ResourceResponse.h"
42 #include <WTF/HashMap.h>
44 #include <sys/types.h>
46 #include <process.h> // for _beginthread()
48 #include <CFNetwork/CFNetwork.h>
49 #include <WebKitSystemInterface/WebKitSystemInterface.h>
53 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
55 static HashSet<String> hosts;
60 CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo)
62 ResourceHandle* handle = (ResourceHandle*)clientInfo;
64 LOG(Network, "CFNet - willSendRequest(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().url().ascii());
66 ResourceRequest request(cfRequest);
68 handle->client()->willSendRequest(handle, request, cfRedirectResponse);
70 cfRequest = request.cfURLRequest();
76 void didReceiveResponse(CFURLConnectionRef conn, CFURLResponseRef cfResponse, const void* clientInfo)
78 ResourceHandle* handle = (ResourceHandle*)clientInfo;
80 LOG(Network, "CFNet - didReceiveResponse(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().url().ascii());
83 handle->client()->didReceiveResponse(handle, cfResponse);
86 void didReceiveData(CFURLConnectionRef conn, CFDataRef data, CFIndex originalLength, const void* clientInfo)
88 ResourceHandle* handle = (ResourceHandle*)clientInfo;
89 const UInt8* bytes = CFDataGetBytePtr(data);
90 CFIndex length = CFDataGetLength(data);
92 LOG(Network, "CFNet - didReceiveData(conn=%p, handle=%p, bytes=%d) (%s)", conn, handle, length, handle->request().url().url().ascii());
95 handle->client()->didReceiveData(handle, (const char*)bytes, length, originalLength);
98 void didFinishLoading(CFURLConnectionRef conn, const void* clientInfo)
100 ResourceHandle* handle = (ResourceHandle*)clientInfo;
102 LOG(Network, "CFNet - didFinishLoading(conn=%p, handle=%p) (%s)", conn, handle, handle->request().url().url().ascii());
104 if (handle->client())
105 handle->client()->didFinishLoading(handle);
108 void didFail(CFURLConnectionRef conn, CFErrorRef error, const void* clientInfo)
110 ResourceHandle* handle = (ResourceHandle*)clientInfo;
112 LOG(Network, "CFNet - didFail(conn=%p, handle=%p, error = %p) (%s)", conn, handle, error, handle->request().url().url().ascii());
114 if (handle->client())
115 handle->client()->didFail(handle, ResourceError(error));
118 CFCachedURLResponseRef willCacheResponse(CFURLConnectionRef conn, CFCachedURLResponseRef cachedResponse, const void* clientInfo)
120 ResourceHandle* handle = (ResourceHandle*)clientInfo;
122 CacheStoragePolicy policy = static_cast<CacheStoragePolicy>(CFCachedURLResponseGetStoragePolicy(cachedResponse));
124 if (handle->client())
125 handle->client()->willCacheResponse(handle, policy);
127 if (static_cast<CFURLCacheStoragePolicy>(policy) != CFCachedURLResponseGetStoragePolicy(cachedResponse))
128 cachedResponse = CFCachedURLResponseCreateWithUserInfo(kCFAllocatorDefault,
129 CFCachedURLResponseGetWrappedResponse(cachedResponse),
130 CFCachedURLResponseGetReceiverData(cachedResponse),
131 CFCachedURLResponseGetUserInfo(cachedResponse),
132 static_cast<CFURLCacheStoragePolicy>(policy));
133 CFRetain(cachedResponse);
135 return cachedResponse;
138 void didReceiveChallenge(CFURLConnectionRef conn, CFURLAuthChallengeRef challenge, const void* clientInfo)
140 ResourceHandle* handle = (ResourceHandle*)clientInfo;
142 LOG(Network, "CFNet - didReceiveChallenge(conn=%p, handle=%p (%s)", conn, handle, handle->request().url().url().ascii());
144 handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(challenge, handle));
147 void addHeadersFromHashMap(CFMutableURLRequestRef request, const HTTPHeaderMap& requestHeaders)
149 if (!requestHeaders.size())
152 HTTPHeaderMap::const_iterator end = requestHeaders.end();
153 for (HTTPHeaderMap::const_iterator it = requestHeaders.begin(); it != end; ++it) {
154 CFStringRef key = it->first.createCFString();
155 CFStringRef value = it->second.createCFString();
156 CFURLRequestSetHTTPHeaderFieldValue(request, key, value);
162 ResourceHandleInternal::~ResourceHandleInternal()
165 LOG(Network, "CFNet - Cancelling connection %p (%s)", m_connection, m_request.url().url().ascii());
166 CFURLConnectionCancel(m_connection.get());
170 ResourceHandle::~ResourceHandle()
172 LOG(Network, "CFNet - Destroying job %p (%s)", this, d->m_request.url().url().ascii());
175 CFArrayRef arrayFromFormData(const FormData& d)
177 size_t size = d.elements().size();
178 CFMutableArrayRef a = CFArrayCreateMutable(0, d.elements().size(), &kCFTypeArrayCallBacks);
179 for (size_t i = 0; i < size; ++i) {
180 const FormDataElement& e = d.elements()[i];
181 if (e.m_type == FormDataElement::data) {
182 CFDataRef data = CFDataCreate(0, (const UInt8*)e.m_data.data(), e.m_data.size());
183 CFArrayAppendValue(a, data);
186 ASSERT(e.m_type == FormDataElement::encodedFile);
187 CFStringRef filename = e.m_filename.createCFString();
188 CFArrayAppendValue(a, filename);
195 void emptyPerform(void* unused)
199 static CFRunLoopRef loaderRL = 0;
200 void runLoaderThread(void *unused)
202 loaderRL = CFRunLoopGetCurrent();
204 // Must add a source to the run loop to prevent CFRunLoopRun() from exiting
205 CFRunLoopSourceContext ctxt = {0, (void *)1 /*must be non-NULL*/, 0, 0, 0, 0, 0, 0, 0, emptyPerform};
206 CFRunLoopSourceRef bogusSource = CFRunLoopSourceCreate(0, 0, &ctxt);
207 CFRunLoopAddSource(loaderRL, bogusSource,kCFRunLoopDefaultMode);
212 CFRunLoopRef ResourceHandle::loaderRunLoop()
215 _beginthread(runLoaderThread, 0, 0);
216 while (loaderRL == 0) {
217 // FIXME: sleep 10? that can't be right...
224 static CFURLRequestRef makeFinalRequest(const ResourceRequest& request)
226 CFMutableURLRequestRef newRequest = CFURLRequestCreateMutableCopy(kCFAllocatorDefault, request.cfURLRequest());
228 if (allowsAnyHTTPSCertificateHosts().contains(request.url().host().lower())) {
229 CFTypeRef keys[] = { kCFStreamSSLAllowsAnyRoot, kCFStreamSSLAllowsExpiredRoots };
230 CFTypeRef values[] = { kCFBooleanTrue, kCFBooleanTrue };
231 static CFDictionaryRef sslProps = CFDictionaryCreate(kCFAllocatorDefault, keys, values, sizeof(keys) / sizeof(keys[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
232 CFURLRequestSetSSLProperties(newRequest, sslProps);
235 if (CFHTTPCookieStorageRef defaultCookieStorage = wkGetDefaultHTTPCookieStorage())
236 CFURLRequestSetHTTPCookieStorageAcceptPolicy(newRequest, CFHTTPCookieStorageGetCookieAcceptPolicy(defaultCookieStorage));
241 bool ResourceHandle::start(Frame* frame)
243 // If we are no longer attached to a Page, this must be an attempted load from an
244 // onUnload handler, so let's just block it.
248 RetainPtr<CFURLRequestRef> request(AdoptCF, makeFinalRequest(d->m_request));
250 // CFURLConnection Callback API currently at version 1
251 const int CFURLConnectionClientVersion = 1;
252 CFURLConnectionClient client = {CFURLConnectionClientVersion, this, 0, 0, 0, willSendRequest, didReceiveResponse, didReceiveData, NULL, didFinishLoading, didFail, willCacheResponse, didReceiveChallenge};
254 d->m_connection.adoptCF(CFURLConnectionCreate(0, request.get(), &client));
256 CFURLConnectionScheduleWithCurrentMessageQueue(d->m_connection.get());
257 CFURLConnectionScheduleDownloadWithRunLoop(d->m_connection.get(), loaderRunLoop(), kCFRunLoopDefaultMode);
258 CFURLConnectionStart(d->m_connection.get());
260 LOG(Network, "CFNet - Starting URL %s (handle=%p, conn=%p)", d->m_request.url().url().ascii(), this, d->m_connection);
265 void ResourceHandle::cancel()
267 if (d->m_connection) {
268 CFURLConnectionCancel(d->m_connection.get());
273 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
275 ASSERT_NOT_REACHED();
279 bool ResourceHandle::supportsBufferedData()
284 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
286 LOG(Network, "CFNet - didReceiveAuthenticationChallenge()");
287 ASSERT(!d->m_currentCFChallenge);
288 ASSERT(d->m_currentWebChallenge.isNull());
289 // Since CFURLConnection networking relies on keeping a reference to the original CFURLAuthChallengeRef,
290 // we make sure that is actually present
291 ASSERT(challenge.cfURLAuthChallengeRef());
293 d->m_currentCFChallenge = challenge.cfURLAuthChallengeRef();
294 d->m_currentWebChallenge = AuthenticationChallenge(d->m_currentCFChallenge, this);
297 client()->didReceiveAuthenticationChallenge(this, d->m_currentWebChallenge);
300 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
302 LOG(Network, "CFNet - receivedCredential()");
303 ASSERT(!challenge.isNull());
304 ASSERT(challenge.cfURLAuthChallengeRef());
305 if (challenge != d->m_currentWebChallenge)
308 CFURLCredentialRef cfCredential = createCF(credential);
309 CFURLConnectionUseCredential(d->m_connection.get(), cfCredential, challenge.cfURLAuthChallengeRef());
310 CFRelease(cfCredential);
312 clearAuthentication();
315 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
317 LOG(Network, "CFNet - receivedRequestToContinueWithoutCredential()");
318 ASSERT(!challenge.isNull());
319 ASSERT(challenge.cfURLAuthChallengeRef());
320 if (challenge != d->m_currentWebChallenge)
323 CFURLConnectionUseCredential(d->m_connection.get(), 0, challenge.cfURLAuthChallengeRef());
325 clearAuthentication();
328 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
330 LOG(Network, "CFNet - receivedCancellation()");
331 if (challenge != d->m_currentWebChallenge)
335 client()->receivedCancellation(this, challenge);
338 CFURLConnectionRef ResourceHandle::connection() const
340 return d->m_connection.get();
343 CFURLConnectionRef ResourceHandle::releaseConnectionForDownload()
345 LOG(Network, "CFNet - Job %p releasing connection %p for download", this, d->m_connection.get());
346 return d->m_connection.releaseRef();
349 void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& vector)
351 ASSERT(!request.isEmpty());
352 CFURLResponseRef cfResponse = 0;
353 CFErrorRef cfError = 0;
354 RetainPtr<CFURLRequestRef> cfRequest(AdoptCF, makeFinalRequest(request));
356 CFDataRef data = CFURLConnectionSendSynchronousRequest(cfRequest.get(), &cfResponse, &cfError, request.timeoutInterval());
358 response = cfResponse;
360 CFRelease(cfResponse);
367 ASSERT(vector.isEmpty());
368 vector.append(CFDataGetBytePtr(data), CFDataGetLength(data));
373 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
375 allowsAnyHTTPSCertificateHosts().add(host.lower());
378 void ResourceHandle::setDefersLoading(bool defers)
381 CFURLConnectionHalt(d->m_connection.get());
383 CFURLConnectionResume(d->m_connection.get());
386 } // namespace WebCore