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 #if PLATFORM(WIN) && USE(CF)
118 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
120 static HashSet<String> hosts;
125 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
127 allowsAnyHTTPSCertificateHosts().add(host.lower());
131 #if PLATFORM(WIN) && USE(CF)
132 // FIXME: The CFDataRef will need to be something else when
134 static HashMap<String, RetainPtr<CFDataRef> >& clientCerts()
136 static HashMap<String, RetainPtr<CFDataRef> > certs;
140 void ResourceHandle::setClientCertificate(const String& host, CFDataRef cert)
142 clientCerts().set(host.lower(), cert);
146 void ResourceHandle::platformSetDefersLoading(bool defers)
148 #if LIBCURL_VERSION_NUM > 0x071200
153 CURLcode error = curl_easy_pause(d->m_handle, CURLPAUSE_ALL);
154 // If we could not defer the handle, so don't do it.
155 if (error != CURLE_OK)
158 CURLcode error = curl_easy_pause(d->m_handle, CURLPAUSE_CONT);
159 if (error != CURLE_OK)
160 // Restarting the handle has failed so just cancel it.
164 LOG_ERROR("Deferred loading is implemented if libcURL version is above 7.18.0");
168 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
174 bool ResourceHandle::loadsBlocked()
180 void ResourceHandle::loadResourceSynchronously(NetworkingContext*, const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
182 WebCoreSynchronousLoader syncLoader;
183 RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(request, &syncLoader, true, false));
185 ResourceHandleManager* manager = ResourceHandleManager::sharedInstance();
187 manager->dispatchSynchronousJob(handle.get());
189 error = syncLoader.resourceError();
190 data = syncLoader.data();
191 response = syncLoader.resourceResponse();
194 //stubs needed for windows version
195 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge&)
200 void ResourceHandle::receivedCredential(const AuthenticationChallenge&, const Credential&)
205 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&)
210 void ResourceHandle::receivedCancellation(const AuthenticationChallenge&)
215 } // namespace WebCore