2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2005, 2006 Michael Emmel mike.emmel@gmail.com
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "ResourceHandle.h"
31 #include "CachedResourceLoader.h"
32 #include "NotImplemented.h"
33 #include "ResourceHandleInternal.h"
34 #include "ResourceHandleManager.h"
35 #include "SharedBuffer.h"
37 #if PLATFORM(WIN) && USE(CF)
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RetainPtr.h>
44 class WebCoreSynchronousLoader : public ResourceHandleClient {
46 WebCoreSynchronousLoader();
48 virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
49 virtual void didReceiveData(ResourceHandle*, const char*, int, int encodedDataLength);
50 virtual void didFinishLoading(ResourceHandle*, double /*finishTime*/);
51 virtual void didFail(ResourceHandle*, const ResourceError&);
53 ResourceResponse resourceResponse() const { return m_response; }
54 ResourceError resourceError() const { return m_error; }
55 Vector<char> data() const { return m_data; }
58 ResourceResponse m_response;
59 ResourceError m_error;
63 WebCoreSynchronousLoader::WebCoreSynchronousLoader()
67 void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
69 m_response = response;
72 void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
74 m_data.append(data, length);
77 void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*, double)
81 void WebCoreSynchronousLoader::didFail(ResourceHandle*, const ResourceError& error)
86 ResourceHandleInternal::~ResourceHandleInternal()
90 curl_slist_free_all(m_customHeaders);
93 ResourceHandle::~ResourceHandle()
98 bool ResourceHandle::start(NetworkingContext* context)
100 // The frame could be null if the ResourceHandle is not associated to any
101 // Frame, e.g. if we are downloading a file.
102 // If the frame is not null but the page is null this must be an attempted
103 // load from an unload handler, so let's just block it.
104 // If both the frame and the page are not null the context is valid.
105 if (context && !context->isValid())
108 ResourceHandleManager::sharedInstance()->add(this);
112 void ResourceHandle::cancel()
114 ResourceHandleManager::sharedInstance()->cancel(this);
117 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
122 bool ResourceHandle::supportsBufferedData()
127 #if PLATFORM(WIN) && USE(CF)
128 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
130 static HashSet<String> hosts;
135 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
137 allowsAnyHTTPSCertificateHosts().add(host.lower());
141 #if PLATFORM(WIN) && USE(CF)
142 // FIXME: The CFDataRef will need to be something else when
144 static HashMap<String, RetainPtr<CFDataRef> >& clientCerts()
146 static HashMap<String, RetainPtr<CFDataRef> > certs;
150 void ResourceHandle::setClientCertificate(const String& host, CFDataRef cert)
152 clientCerts().set(host.lower(), cert);
156 void ResourceHandle::platformSetDefersLoading(bool defers)
158 #if LIBCURL_VERSION_NUM > 0x071200
163 CURLcode error = curl_easy_pause(d->m_handle, CURLPAUSE_ALL);
164 // If we could not defer the handle, so don't do it.
165 if (error != CURLE_OK)
168 CURLcode error = curl_easy_pause(d->m_handle, CURLPAUSE_CONT);
169 if (error != CURLE_OK)
170 // Restarting the handle has failed so just cancel it.
174 LOG_ERROR("Deferred loading is implemented if libcURL version is above 7.18.0");
178 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
184 bool ResourceHandle::loadsBlocked()
190 void ResourceHandle::loadResourceSynchronously(NetworkingContext*, const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
192 WebCoreSynchronousLoader syncLoader;
193 RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(request, &syncLoader, true, false));
195 ResourceHandleManager* manager = ResourceHandleManager::sharedInstance();
197 manager->dispatchSynchronousJob(handle.get());
199 error = syncLoader.resourceError();
200 data = syncLoader.data();
201 response = syncLoader.resourceResponse();
204 //stubs needed for windows version
205 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge&)
210 void ResourceHandle::receivedCredential(const AuthenticationChallenge&, const Credential&)
215 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&)
220 void ResourceHandle::receivedCancellation(const AuthenticationChallenge&)
225 } // namespace WebCore