initial import
[vuplus_webkit] / Source / WebCore / platform / network / curl / ResourceHandleCurl.cpp
1 /*
2  * Copyright (C) 2004, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2005, 2006 Michael Emmel mike.emmel@gmail.com
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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.
26  */
27
28 #include "config.h"
29 #include "ResourceHandle.h"
30
31 #include "CachedResourceLoader.h"
32 #include "NotImplemented.h"
33 #include "ResourceHandleInternal.h"
34 #include "ResourceHandleManager.h"
35 #include "SharedBuffer.h"
36
37 #if PLATFORM(WIN) && USE(CF)
38 #include <wtf/PassRefPtr.h>
39 #include <wtf/RetainPtr.h>
40 #endif
41
42 namespace WebCore {
43
44 class WebCoreSynchronousLoader : public ResourceHandleClient {
45 public:
46     WebCoreSynchronousLoader();
47
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&);
52
53     ResourceResponse resourceResponse() const { return m_response; }
54     ResourceError resourceError() const { return m_error; }
55     Vector<char> data() const { return m_data; }
56
57 private:
58     ResourceResponse m_response;
59     ResourceError m_error;
60     Vector<char> m_data;
61 };
62
63 WebCoreSynchronousLoader::WebCoreSynchronousLoader()
64 {
65 }
66
67 void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
68 {
69     m_response = response;
70 }
71
72 void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
73 {
74     m_data.append(data, length);
75 }
76
77 void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*, double)
78 {
79 }
80
81 void WebCoreSynchronousLoader::didFail(ResourceHandle*, const ResourceError& error)
82 {
83     m_error = error;
84 }
85
86 ResourceHandleInternal::~ResourceHandleInternal()
87 {
88     fastFree(m_url);
89     if (m_customHeaders)
90         curl_slist_free_all(m_customHeaders);
91 }
92
93 ResourceHandle::~ResourceHandle()
94 {
95     cancel();
96 }
97
98 bool ResourceHandle::start(NetworkingContext* context)
99 {
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())
106         return false;
107
108     ResourceHandleManager::sharedInstance()->add(this);
109     return true;
110 }
111
112 void ResourceHandle::cancel()
113 {
114     ResourceHandleManager::sharedInstance()->cancel(this);
115 }
116
117 #if PLATFORM(WIN) && USE(CF)
118 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
119 {
120     static HashSet<String> hosts;
121
122     return hosts;
123 }
124
125 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
126 {
127     allowsAnyHTTPSCertificateHosts().add(host.lower());
128 }
129 #endif
130
131 #if PLATFORM(WIN) && USE(CF)
132 // FIXME:  The CFDataRef will need to be something else when
133 // building without 
134 static HashMap<String, RetainPtr<CFDataRef> >& clientCerts()
135 {
136     static HashMap<String, RetainPtr<CFDataRef> > certs;
137     return certs;
138 }
139
140 void ResourceHandle::setClientCertificate(const String& host, CFDataRef cert)
141 {
142     clientCerts().set(host.lower(), cert);
143 }
144 #endif
145
146 void ResourceHandle::platformSetDefersLoading(bool defers)
147 {
148 #if LIBCURL_VERSION_NUM > 0x071200
149     if (!d->m_handle)
150         return;
151
152     if (defers) {
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)
156             return;
157     } else {
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.
161             cancel();
162     }
163 #else
164     LOG_ERROR("Deferred loading is implemented if libcURL version is above 7.18.0");
165 #endif
166 }
167
168 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
169 {
170     notImplemented();
171     return false;
172 }
173
174 bool ResourceHandle::loadsBlocked()
175 {
176     notImplemented();
177     return false;
178 }
179
180 void ResourceHandle::loadResourceSynchronously(NetworkingContext*, const ResourceRequest& request, StoredCredentials storedCredentials, ResourceError& error, ResourceResponse& response, Vector<char>& data)
181 {
182     WebCoreSynchronousLoader syncLoader;
183     RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(request, &syncLoader, true, false));
184
185     ResourceHandleManager* manager = ResourceHandleManager::sharedInstance();
186
187     manager->dispatchSynchronousJob(handle.get());
188
189     error = syncLoader.resourceError();
190     data = syncLoader.data();
191     response = syncLoader.resourceResponse();
192 }
193
194 //stubs needed for windows version
195 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge&) 
196 {
197     notImplemented();
198 }
199
200 void ResourceHandle::receivedCredential(const AuthenticationChallenge&, const Credential&) 
201 {
202     notImplemented();
203 }
204
205 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&) 
206 {
207     notImplemented();
208 }
209
210 void ResourceHandle::receivedCancellation(const AuthenticationChallenge&)
211 {
212     notImplemented();
213 }
214
215 } // namespace WebCore