REGRESSION(r140338): WebKitGtk+ trying to allocate a buffer too big
[WebKit-https.git] / Source / WebCore / platform / network / soup / ResourceHandleSoup.cpp
1 /*
2  * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Alp Toker <alp@atoker.com>
4  * Copyright (C) 2008 Xan Lopez <xan@gnome.org>
5  * Copyright (C) 2008, 2010 Collabora Ltd.
6  * Copyright (C) 2009 Holger Hans Peter Freyther
7  * Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>
8  * Copyright (C) 2009 Christian Dywan <christian@imendio.com>
9  * Copyright (C) 2009, 2010, 2011, 2012 Igalia S.L.
10  * Copyright (C) 2009 John Kjellberg <john.kjellberg@power.alstom.com>
11  * Copyright (C) 2012 Intel Corporation
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Library General Public
15  * License as published by the Free Software Foundation; either
16  * version 2 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  * Library General Public License for more details.
22  *
23  * You should have received a copy of the GNU Library General Public License
24  * along with this library; see the file COPYING.LIB.  If not, write to
25  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26  * Boston, MA 02110-1301, USA.
27  */
28
29 #include "config.h"
30 #include "ResourceHandle.h"
31
32 #include "CachedResourceLoader.h"
33 #include "ChromeClient.h"
34 #include "CookieJarSoup.h"
35 #include "CredentialStorage.h"
36 #include "FileSystem.h"
37 #include "Frame.h"
38 #include "GOwnPtrSoup.h"
39 #include "HTTPParsers.h"
40 #include "LocalizedStrings.h"
41 #include "Logging.h"
42 #include "MIMETypeRegistry.h"
43 #include "NetworkingContext.h"
44 #include "NotImplemented.h"
45 #include "Page.h"
46 #include "ResourceError.h"
47 #include "ResourceHandleClient.h"
48 #include "ResourceHandleInternal.h"
49 #include "ResourceResponse.h"
50 #include "SharedBuffer.h"
51 #include "SoupURIUtils.h"
52 #include "TextEncoding.h"
53 #include <errno.h>
54 #include <fcntl.h>
55 #include <gio/gio.h>
56 #include <glib.h>
57 #define LIBSOUP_USE_UNSTABLE_REQUEST_API
58 #include <libsoup/soup-multipart-input-stream.h>
59 #include <libsoup/soup-request-http.h>
60 #include <libsoup/soup-requester.h>
61 #include <libsoup/soup.h>
62 #include <sys/stat.h>
63 #include <sys/types.h>
64 #include <unistd.h>
65 #include <wtf/SHA1.h>
66 #include <wtf/gobject/GRefPtr.h>
67 #include <wtf/text/Base64.h>
68 #include <wtf/text/CString.h>
69
70 #if ENABLE(BLOB)
71 #include "BlobData.h"
72 #include "BlobRegistryImpl.h"
73 #include "BlobStorageData.h"
74 #endif
75
76 #if PLATFORM(GTK)
77 #include "CredentialBackingStore.h"
78 #endif
79
80 namespace WebCore {
81
82 #define READ_BUFFER_SIZE 8192
83
84 inline static void soupLogPrinter(SoupLogger*, SoupLoggerLogLevel, char direction, const char* data, gpointer)
85 {
86 #if LOG_DISABLED
87     UNUSED_PARAM(direction);
88     UNUSED_PARAM(data);
89 #endif
90     LOG(Network, "%c %s", direction, data);
91 }
92
93 static bool loadingSynchronousRequest = false;
94
95 class WebCoreSynchronousLoader : public ResourceHandleClient {
96     WTF_MAKE_NONCOPYABLE(WebCoreSynchronousLoader);
97 public:
98
99     WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, SoupSession* session, Vector<char>& data)
100         : m_error(error)
101         , m_response(response)
102         , m_session(session)
103         , m_data(data)
104         , m_finished(false)
105     {
106         // We don't want any timers to fire while we are doing our synchronous load
107         // so we replace the thread default main context. The main loop iterations
108         // will only process GSources associated with this inner context.
109         loadingSynchronousRequest = true;
110         GRefPtr<GMainContext> innerMainContext = adoptGRef(g_main_context_new());
111         g_main_context_push_thread_default(innerMainContext.get());
112         m_mainLoop = adoptGRef(g_main_loop_new(innerMainContext.get(), false));
113
114         adjustMaxConnections(1);
115     }
116
117     ~WebCoreSynchronousLoader()
118     {
119         adjustMaxConnections(-1);
120
121         GMainContext* context = g_main_context_get_thread_default();
122         while (g_main_context_pending(context))
123             g_main_context_iteration(context, FALSE);
124
125         g_main_context_pop_thread_default(context);
126         loadingSynchronousRequest = false;
127     }
128
129     void adjustMaxConnections(int adjustment)
130     {
131         int maxConnections, maxConnectionsPerHost;
132         g_object_get(m_session,
133                      SOUP_SESSION_MAX_CONNS, &maxConnections,
134                      SOUP_SESSION_MAX_CONNS_PER_HOST, &maxConnectionsPerHost,
135                      NULL);
136         maxConnections += adjustment;
137         maxConnectionsPerHost += adjustment;
138         g_object_set(m_session,
139                      SOUP_SESSION_MAX_CONNS, maxConnections,
140                      SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
141                      NULL);
142
143     }
144
145     virtual bool isSynchronousClient()
146     {
147         return true;
148     }
149
150     virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
151     {
152         m_response = response;
153     }
154
155     virtual void didReceiveData(ResourceHandle*, const char* data, int length, int)
156     {
157         m_data.append(data, length);
158     }
159
160     virtual void didFinishLoading(ResourceHandle*, double)
161     {
162         if (g_main_loop_is_running(m_mainLoop.get()))
163             g_main_loop_quit(m_mainLoop.get());
164         m_finished = true;
165     }
166
167     virtual void didFail(ResourceHandle* handle, const ResourceError& error)
168     {
169         m_error = error;
170         didFinishLoading(handle, 0);
171     }
172
173     virtual void didReceiveAuthenticationChallenge(ResourceHandle*, const AuthenticationChallenge& challenge)
174     {
175         // We do not handle authentication for synchronous XMLHttpRequests.
176         challenge.authenticationClient()->receivedRequestToContinueWithoutCredential(challenge);
177     }
178
179     void run()
180     {
181         if (!m_finished)
182             g_main_loop_run(m_mainLoop.get());
183     }
184
185 private:
186     ResourceError& m_error;
187     ResourceResponse& m_response;
188     SoupSession* m_session;
189     Vector<char>& m_data;
190     bool m_finished;
191     GRefPtr<GMainLoop> m_mainLoop;
192 };
193
194 class HostTLSCertificateSet {
195 public:
196     void add(GTlsCertificate* certificate)
197     {
198         String certificateHash = computeCertificateHash(certificate);
199         if (!certificateHash.isEmpty())
200             m_certificates.add(certificateHash);
201     }
202
203     bool contains(GTlsCertificate* certificate)
204     {
205         return m_certificates.contains(computeCertificateHash(certificate));
206     }
207
208 private:
209     static String computeCertificateHash(GTlsCertificate* certificate)
210     {
211         GByteArray* data = 0;
212         g_object_get(G_OBJECT(certificate), "certificate", &data, NULL);
213         if (!data)
214             return String();
215
216         static const size_t sha1HashSize = 20;
217         GRefPtr<GByteArray> certificateData = adoptGRef(data);
218         SHA1 sha1;
219         sha1.addBytes(certificateData->data, certificateData->len);
220
221         Vector<uint8_t, sha1HashSize> digest;
222         sha1.computeHash(digest);
223
224         return base64Encode(reinterpret_cast<const char*>(digest.data()), sha1HashSize);
225     }
226
227     HashSet<String> m_certificates;
228 };
229
230 static bool createSoupRequestAndMessageForHandle(ResourceHandle*, const ResourceRequest&, bool isHTTPFamilyRequest);
231 static void cleanupSoupRequestOperation(ResourceHandle*, bool isDestroying = false);
232 static void sendRequestCallback(GObject*, GAsyncResult*, gpointer);
233 static void readCallback(GObject*, GAsyncResult*, gpointer);
234 static void closeCallback(GObject*, GAsyncResult*, gpointer);
235 static gboolean requestTimeoutCallback(void*);
236 #if ENABLE(WEB_TIMING)
237 static int  milisecondsSinceRequest(double requestTime);
238 #endif
239
240 static bool gIgnoreSSLErrors = false;
241
242 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
243 {
244     DEFINE_STATIC_LOCAL(HashSet<String>, hosts, ());
245     return hosts;
246 }
247
248 typedef HashMap<String, HostTLSCertificateSet> CertificatesMap;
249 static CertificatesMap& clientCertificates()
250 {
251     DEFINE_STATIC_LOCAL(CertificatesMap, certificates, ());
252     return certificates;
253 }
254
255 ResourceHandleInternal::~ResourceHandleInternal()
256 {
257 }
258
259 static SoupSession* sessionFromContext(NetworkingContext* context)
260 {
261     return (context && context->isValid()) ? context->soupSession() : ResourceHandle::defaultSession();
262 }
263
264 ResourceHandle::~ResourceHandle()
265 {
266     cleanupSoupRequestOperation(this, true);
267 }
268
269 static void ensureSessionIsInitialized(SoupSession* session)
270 {
271     if (g_object_get_data(G_OBJECT(session), "webkit-init"))
272         return;
273
274     if (session == ResourceHandle::defaultSession()) {
275         SoupCookieJar* jar = SOUP_COOKIE_JAR(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
276         if (!jar)
277             soup_session_add_feature(session, SOUP_SESSION_FEATURE(soupCookieJar()));
278         else
279             setSoupCookieJar(jar);
280     }
281
282 #if !LOG_DISABLED
283     if (!soup_session_get_feature(session, SOUP_TYPE_LOGGER) && LogNetwork.state == WTFLogChannelOn) {
284         SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(SOUP_LOGGER_LOG_BODY), -1);
285         soup_session_add_feature(session, SOUP_SESSION_FEATURE(logger));
286         soup_logger_set_printer(logger, soupLogPrinter, 0, 0);
287         g_object_unref(logger);
288     }
289 #endif // !LOG_DISABLED
290
291     if (!soup_session_get_feature(session, SOUP_TYPE_REQUESTER)) {
292         SoupRequester* requester = soup_requester_new();
293         soup_session_add_feature(session, SOUP_SESSION_FEATURE(requester));
294         g_object_unref(requester);
295     }
296
297     g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
298 }
299
300 SoupSession* ResourceHandleInternal::soupSession()
301 {
302     SoupSession* session = sessionFromContext(m_context.get());
303     ensureSessionIsInitialized(session);
304     return session;
305 }
306
307 static bool isAuthenticationFailureStatusCode(int httpStatusCode)
308 {
309     return httpStatusCode == SOUP_STATUS_PROXY_AUTHENTICATION_REQUIRED || httpStatusCode == SOUP_STATUS_UNAUTHORIZED;
310 }
311
312 static void gotHeadersCallback(SoupMessage* message, gpointer data)
313 {
314     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
315     if (!handle)
316         return;
317     ResourceHandleInternal* d = handle->getInternal();
318     if (d->m_cancelled)
319         return;
320
321 #if ENABLE(WEB_TIMING)
322     if (d->m_response.resourceLoadTiming())
323         d->m_response.resourceLoadTiming()->receiveHeadersEnd = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
324 #endif
325
326 #if PLATFORM(GTK)
327     // We are a bit more conservative with the persistent credential storage than the session store,
328     // since we are waiting until we know that this authentication succeeded before actually storing.
329     // This is because we want to avoid hitting the disk twice (once to add and once to remove) for
330     // incorrect credentials or polluting the keychain with invalid credentials.
331     if (!isAuthenticationFailureStatusCode(message->status_code) && message->status_code < 500 && !d->m_credentialDataToSaveInPersistentStore.credential.isEmpty()) {
332         credentialBackingStore().storeCredentialsForChallenge(
333             d->m_credentialDataToSaveInPersistentStore.challenge,
334             d->m_credentialDataToSaveInPersistentStore.credential);
335     }
336 #endif
337
338     // The original response will be needed later to feed to willSendRequest in
339     // doRedirect() in case we are redirected. For this reason, we store it here.
340     ResourceResponse response;
341     response.updateFromSoupMessage(message);
342     d->m_response = response;
343 }
344
345 static void applyAuthenticationToRequest(ResourceHandle* handle, ResourceRequest& request, bool redirect)
346 {
347     // m_user/m_pass are credentials given manually, for instance, by the arguments passed to XMLHttpRequest.open().
348     ResourceHandleInternal* d = handle->getInternal();
349
350     if (handle->shouldUseCredentialStorage()) {
351         if (d->m_user.isEmpty() && d->m_pass.isEmpty())
352             d->m_initialCredential = CredentialStorage::get(request.url());
353         else if (!redirect) {
354             // If there is already a protection space known for the URL, update stored credentials
355             // before sending a request. This makes it possible to implement logout by sending an
356             // XMLHttpRequest with known incorrect credentials, and aborting it immediately (so that
357             // an authentication dialog doesn't pop up).
358             CredentialStorage::set(Credential(d->m_user, d->m_pass, CredentialPersistenceNone), request.url());
359         }
360     }
361
362     String user = d->m_user;
363     String password = d->m_pass;
364     if (!d->m_initialCredential.isEmpty()) {
365         user = d->m_initialCredential.user();
366         password = d->m_initialCredential.password();
367     }
368
369     if (user.isEmpty() && password.isEmpty())
370         return;
371
372     // We always put the credentials into the URL. In the CFNetwork-port HTTP family credentials are applied in
373     // the didReceiveAuthenticationChallenge callback, but libsoup requires us to use this method to override
374     // any previously remembered credentials. It has its own per-session credential storage.
375     KURL urlWithCredentials(request.url());
376     urlWithCredentials.setUser(user);
377     urlWithCredentials.setPass(password);
378     request.setURL(urlWithCredentials);
379 }
380
381 // Called each time the message is going to be sent again except the first time.
382 // This happens when libsoup handles HTTP authentication.
383 static void restartedCallback(SoupMessage*, gpointer data)
384 {
385     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
386     if (!handle)
387         return;
388     ResourceHandleInternal* d = handle->getInternal();
389     if (d->m_cancelled)
390         return;
391
392 #if ENABLE(WEB_TIMING)
393     ResourceResponse& redirectResponse = d->m_response;
394     redirectResponse.setResourceLoadTiming(ResourceLoadTiming::create());
395     redirectResponse.resourceLoadTiming()->requestTime = monotonicallyIncreasingTime();
396 #endif
397 }
398
399 static bool shouldRedirect(ResourceHandle* handle)
400 {
401     ResourceHandleInternal* d = handle->getInternal();
402     SoupMessage* message = d->m_soupMessage.get();
403
404     // Some 3xx status codes aren't actually redirects.
405     if (message->status_code == 300 || message->status_code == 304 || message->status_code == 305 || message->status_code == 306)
406         return false;
407
408     if (!soup_message_headers_get_one(message->response_headers, "Location"))
409         return false;
410
411     return true;
412 }
413
414 static bool shouldRedirectAsGET(SoupMessage* message, KURL& newURL, bool crossOrigin)
415 {
416     if (message->method == SOUP_METHOD_GET)
417         return false;
418
419     if (!newURL.protocolIsInHTTPFamily())
420         return true;
421
422     switch (message->status_code) {
423     case SOUP_STATUS_SEE_OTHER:
424         return true;
425     case SOUP_STATUS_FOUND:
426     case SOUP_STATUS_MOVED_PERMANENTLY:
427         if (message->method == SOUP_METHOD_POST)
428             return true;
429         break;
430     }
431
432     if (crossOrigin && message->method == SOUP_METHOD_DELETE)
433         return true;
434
435     return false;
436 }
437
438 static void doRedirect(ResourceHandle* handle)
439 {
440     ResourceHandleInternal* d = handle->getInternal();
441     static const int maxRedirects = 20;
442
443     if (d->m_redirectCount++ > maxRedirects) {
444         d->client()->didFail(handle, ResourceError::transportError(d->m_soupRequest.get(), SOUP_STATUS_TOO_MANY_REDIRECTS, "Too many redirects"));
445         cleanupSoupRequestOperation(handle);
446         return;
447     }
448
449     ResourceRequest request = handle->firstRequest();
450     SoupMessage* message = d->m_soupMessage.get();
451     const char* location = soup_message_headers_get_one(message->response_headers, "Location");
452     KURL newURL = KURL(soupURIToKURL(soup_message_get_uri(message)), location);
453     bool crossOrigin = !protocolHostAndPortAreEqual(request.url(), newURL);
454     request.setURL(newURL);
455
456     if (shouldRedirectAsGET(message, newURL, crossOrigin)) {
457         request.setHTTPMethod("GET");
458         request.setHTTPBody(0);
459         request.clearHTTPContentType();
460     }
461
462     // Should not set Referer after a redirect from a secure resource to non-secure one.
463     if (!newURL.protocolIs("https") && protocolIs(request.httpReferrer(), "https"))
464         request.clearHTTPReferrer();
465
466     d->m_user = newURL.user();
467     d->m_pass = newURL.pass();
468     request.removeCredentials();
469
470     if (crossOrigin) {
471         // If the network layer carries over authentication headers from the original request
472         // in a cross-origin redirect, we want to clear those headers here. 
473         request.clearHTTPAuthorization();
474
475         // TODO: We are losing any username and password specified in the redirect URL, as this is the 
476         // same behavior as the CFNet port. We should investigate if this is really what we want.
477     } else
478         applyAuthenticationToRequest(handle, request, true);
479
480     cleanupSoupRequestOperation(handle);
481     if (!createSoupRequestAndMessageForHandle(handle, request, true)) {
482         d->client()->cannotShowURL(handle);
483         return;
484     }
485
486     // If we sent credentials with this request's URL, we don't want the response to carry them to
487     // the WebKit layer. They were only placed in the URL for the benefit of libsoup.
488     request.removeCredentials();
489
490     d->client()->willSendRequest(handle, request, d->m_response);
491     handle->sendPendingRequest();
492 }
493
494 static void redirectCloseCallback(GObject*, GAsyncResult* result, gpointer data)
495 {
496     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
497     ResourceHandleInternal* d = handle->getInternal();
498
499     if (d->m_cancelled || !handle->client()) {
500         cleanupSoupRequestOperation(handle.get());
501         return;
502     }
503
504     g_input_stream_close_finish(d->m_inputStream.get(), result, 0);
505     doRedirect(handle.get());
506 }
507
508 static void redirectSkipCallback(GObject*, GAsyncResult* asyncResult, gpointer data)
509 {
510     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
511
512     ResourceHandleInternal* d = handle->getInternal();
513     ResourceHandleClient* client = handle->client();
514
515     if (d->m_cancelled || !client) {
516         cleanupSoupRequestOperation(handle.get());
517         return;
518     }
519
520     GOwnPtr<GError> error;
521     gssize bytesSkipped = g_input_stream_read_finish(d->m_inputStream.get(), asyncResult, &error.outPtr());
522     if (error) {
523         client->didFail(handle.get(), ResourceError::genericIOError(error.get(), d->m_soupRequest.get()));
524         cleanupSoupRequestOperation(handle.get());
525         return;
526     }
527
528     if (bytesSkipped > 0) {
529         g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
530             d->m_cancellable.get(), redirectSkipCallback, handle.get());
531         return;
532     }
533
534     g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT, 0, redirectCloseCallback, handle.get());
535 }
536
537 static void wroteBodyDataCallback(SoupMessage*, SoupBuffer* buffer, gpointer data)
538 {
539     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
540     if (!handle)
541         return;
542
543     ASSERT(buffer);
544     ResourceHandleInternal* internal = handle->getInternal();
545     internal->m_bodyDataSent += buffer->length;
546
547     if (internal->m_cancelled)
548         return;
549     ResourceHandleClient* client = handle->client();
550     if (!client)
551         return;
552
553     client->didSendData(handle.get(), internal->m_bodyDataSent, internal->m_bodySize);
554 }
555
556 static void cleanupSoupRequestOperation(ResourceHandle* handle, bool isDestroying)
557 {
558     ResourceHandleInternal* d = handle->getInternal();
559
560     d->m_soupRequest.clear();
561     d->m_inputStream.clear();
562     d->m_multipartInputStream.clear();
563     d->m_cancellable.clear();
564
565     if (d->m_soupMessage) {
566         g_signal_handlers_disconnect_matched(d->m_soupMessage.get(), G_SIGNAL_MATCH_DATA,
567                                              0, 0, 0, 0, handle);
568         g_object_set_data(G_OBJECT(d->m_soupMessage.get()), "handle", 0);
569         d->m_soupMessage.clear();
570     }
571
572     if (d->m_buffer) {
573         g_slice_free1(READ_BUFFER_SIZE, d->m_buffer);
574         d->m_buffer = 0;
575     }
576
577     if (d->m_timeoutSource) {
578         g_source_destroy(d->m_timeoutSource.get());
579         d->m_timeoutSource.clear();
580     }
581
582     if (!isDestroying)
583         handle->deref();
584 }
585
586 static bool handleUnignoredTLSErrors(ResourceHandle* handle)
587 {
588     ResourceHandleInternal* d = handle->getInternal();
589     const ResourceResponse& response = d->m_response;
590
591     if (!response.soupMessageTLSErrors() || gIgnoreSSLErrors)
592         return false;
593
594     String lowercaseHostURL = handle->firstRequest().url().host().lower();
595     if (allowsAnyHTTPSCertificateHosts().contains(lowercaseHostURL))
596         return false;
597
598     // We aren't ignoring errors globally, but the user may have already decided to accept this certificate.
599     CertificatesMap::iterator i = clientCertificates().find(lowercaseHostURL);
600     if (i != clientCertificates().end() && i->value.contains(response.soupMessageCertificate()))
601         return false;
602
603     handle->client()->didFail(handle, ResourceError::tlsError(d->m_soupRequest.get(), response.soupMessageTLSErrors(), response.soupMessageCertificate()));
604     return true;
605 }
606
607 static void nextMultipartResponsePartCallback(GObject* /*source*/, GAsyncResult* result, gpointer data)
608 {
609     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
610
611     ResourceHandleInternal* d = handle->getInternal();
612     ResourceHandleClient* client = handle->client();
613
614     if (d->m_cancelled || !client) {
615         cleanupSoupRequestOperation(handle.get());
616         return;
617     }
618
619     ASSERT(!d->m_inputStream);
620
621     GOwnPtr<GError> error;
622     d->m_inputStream = adoptGRef(soup_multipart_input_stream_next_part_finish(d->m_multipartInputStream.get(), result, &error.outPtr()));
623     if (error) {
624         client->didFail(handle.get(), ResourceError::httpError(d->m_soupMessage.get(), error.get(), d->m_soupRequest.get()));
625         cleanupSoupRequestOperation(handle.get());
626         return;
627     }
628
629     if (!d->m_inputStream) {
630         client->didFinishLoading(handle.get(), 0);
631         cleanupSoupRequestOperation(handle.get());
632         return;
633     }
634
635     d->m_response = ResourceResponse();
636     d->m_response.setURL(handle->firstRequest().url());
637     d->m_response.updateFromSoupMessageHeaders(soup_multipart_input_stream_get_headers(d->m_multipartInputStream.get()));
638
639     client->didReceiveResponse(handle.get(), d->m_response);
640
641     if (d->m_cancelled || !client) {
642         cleanupSoupRequestOperation(handle.get());
643         return;
644     }
645
646     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE,
647         G_PRIORITY_DEFAULT, d->m_cancellable.get(), readCallback, handle.get());
648 }
649
650 static void sendRequestCallback(GObject*, GAsyncResult* result, gpointer data)
651 {
652     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
653
654     ResourceHandleInternal* d = handle->getInternal();
655     ResourceHandleClient* client = handle->client();
656     SoupMessage* soupMessage = d->m_soupMessage.get();
657
658     if (d->m_cancelled || !client) {
659         cleanupSoupRequestOperation(handle.get());
660         return;
661     }
662
663     if (d->m_defersLoading) {
664         d->m_deferredResult = result;
665         return;
666     }
667
668     GOwnPtr<GError> error;
669     GRefPtr<GInputStream> inputStream = adoptGRef(soup_request_send_finish(d->m_soupRequest.get(), result, &error.outPtr()));
670     if (error) {
671         client->didFail(handle.get(), ResourceError::httpError(soupMessage, error.get(), d->m_soupRequest.get()));
672         cleanupSoupRequestOperation(handle.get());
673         return;
674     }
675
676     d->m_buffer = static_cast<char*>(g_slice_alloc(READ_BUFFER_SIZE));
677
678     if (soupMessage) {
679         if (SOUP_STATUS_IS_REDIRECTION(soupMessage->status_code) && shouldRedirect(handle.get())) {
680             d->m_inputStream = inputStream;
681             // We use read_async() rather than skip_async() to work around
682             // https://bugzilla.gnome.org/show_bug.cgi?id=691489 until we can
683             // depend on glib > 2.35.4
684             g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
685                 d->m_cancellable.get(), redirectSkipCallback, handle.get());
686             return;
687         }
688
689         if (handle->shouldContentSniff() && soupMessage->status_code != SOUP_STATUS_NOT_MODIFIED) {
690             const char* sniffedType = soup_request_get_content_type(d->m_soupRequest.get());
691             d->m_response.setSniffedContentType(sniffedType);
692         }
693         d->m_response.updateFromSoupMessage(soupMessage);
694
695         if (handleUnignoredTLSErrors(handle.get())) {
696             cleanupSoupRequestOperation(handle.get());
697             return;
698         }
699
700     } else {
701         d->m_response.setURL(handle->firstRequest().url());
702         const gchar* contentType = soup_request_get_content_type(d->m_soupRequest.get());
703         d->m_response.setMimeType(extractMIMETypeFromMediaType(contentType));
704         d->m_response.setTextEncodingName(extractCharsetFromMediaType(contentType));
705         d->m_response.setExpectedContentLength(soup_request_get_content_length(d->m_soupRequest.get()));
706     }
707
708     client->didReceiveResponse(handle.get(), d->m_response);
709
710     if (d->m_cancelled) {
711         cleanupSoupRequestOperation(handle.get());
712         return;
713     }
714
715     if (soupMessage && d->m_response.isMultipart()) {
716         d->m_multipartInputStream = adoptGRef(soup_multipart_input_stream_new(soupMessage, inputStream.get()));
717         soup_multipart_input_stream_next_part_async(d->m_multipartInputStream.get(), G_PRIORITY_DEFAULT,
718             d->m_cancellable.get(), nextMultipartResponsePartCallback, handle.get());
719         return;
720     }
721
722     d->m_inputStream = inputStream;
723     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE,
724                               G_PRIORITY_DEFAULT, d->m_cancellable.get(), readCallback, handle.get());
725 }
726
727 static bool addFileToSoupMessageBody(SoupMessage* message, const String& fileNameString, size_t offset, size_t lengthToSend, unsigned long& totalBodySize)
728 {
729     GOwnPtr<GError> error;
730     CString fileName = fileSystemRepresentation(fileNameString);
731     GMappedFile* fileMapping = g_mapped_file_new(fileName.data(), false, &error.outPtr());
732     if (error)
733         return false;
734
735     gsize bufferLength = lengthToSend;
736     if (!lengthToSend)
737         bufferLength = g_mapped_file_get_length(fileMapping);
738     totalBodySize += bufferLength;
739
740     SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping) + offset,
741                                                         bufferLength,
742                                                         fileMapping,
743                                                         reinterpret_cast<GDestroyNotify>(g_mapped_file_unref));
744     soup_message_body_append_buffer(message->request_body, soupBuffer);
745     soup_buffer_free(soupBuffer);
746     return true;
747 }
748
749 #if ENABLE(BLOB)
750 static bool blobIsOutOfDate(const BlobDataItem& blobItem)
751 {
752     ASSERT(blobItem.type == BlobDataItem::File);
753     if (!isValidFileTime(blobItem.expectedModificationTime))
754         return false;
755
756     time_t fileModificationTime;
757     if (!getFileModificationTime(blobItem.path, fileModificationTime))
758         return true;
759
760     return fileModificationTime != static_cast<time_t>(blobItem.expectedModificationTime);
761 }
762
763 static void addEncodedBlobItemToSoupMessageBody(SoupMessage* message, const BlobDataItem& blobItem, unsigned long& totalBodySize)
764 {
765     if (blobItem.type == BlobDataItem::Data) {
766         totalBodySize += blobItem.length;
767         soup_message_body_append(message->request_body, SOUP_MEMORY_TEMPORARY,
768                                  blobItem.data->data() + blobItem.offset, blobItem.length);
769         return;
770     }
771
772     ASSERT(blobItem.type == BlobDataItem::File);
773     if (blobIsOutOfDate(blobItem))
774         return;
775
776     addFileToSoupMessageBody(message,
777                              blobItem.path,
778                              blobItem.offset,
779                              blobItem.length == BlobDataItem::toEndOfFile ? 0 : blobItem.length,
780                              totalBodySize);
781 }
782
783 static void addEncodedBlobToSoupMessageBody(SoupMessage* message, const FormDataElement& element, unsigned long& totalBodySize)
784 {
785     RefPtr<BlobStorageData> blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(KURL(ParsedURLString, element.m_url));
786     if (!blobData)
787         return;
788
789     for (size_t i = 0; i < blobData->items().size(); ++i)
790         addEncodedBlobItemToSoupMessageBody(message, blobData->items()[i], totalBodySize);
791 }
792 #endif // ENABLE(BLOB)
793
794 static bool addFormElementsToSoupMessage(SoupMessage* message, const char*, FormData* httpBody, unsigned long& totalBodySize)
795 {
796     soup_message_body_set_accumulate(message->request_body, FALSE);
797     size_t numElements = httpBody->elements().size();
798     for (size_t i = 0; i < numElements; i++) {
799         const FormDataElement& element = httpBody->elements()[i];
800
801         if (element.m_type == FormDataElement::data) {
802             totalBodySize += element.m_data.size();
803             soup_message_body_append(message->request_body, SOUP_MEMORY_TEMPORARY,
804                                      element.m_data.data(), element.m_data.size());
805             continue;
806         }
807
808         if (element.m_type == FormDataElement::encodedFile) {
809             if (!addFileToSoupMessageBody(message ,
810                                          element.m_filename,
811                                          0 /* offset */,
812                                          0 /* lengthToSend */,
813                                          totalBodySize))
814                 return false;
815             continue;
816         }
817
818 #if ENABLE(BLOB)
819         ASSERT(element.m_type == FormDataElement::encodedBlob);
820         addEncodedBlobToSoupMessageBody(message, element, totalBodySize);
821 #endif
822     }
823     return true;
824 }
825
826 #if ENABLE(WEB_TIMING)
827 static int milisecondsSinceRequest(double requestTime)
828 {
829     return static_cast<int>((monotonicallyIncreasingTime() - requestTime) * 1000.0);
830 }
831
832 static void wroteBodyCallback(SoupMessage*, gpointer data)
833 {
834     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
835     if (!handle)
836         return;
837
838     ResourceHandleInternal* d = handle->getInternal();
839     if (!d->m_response.resourceLoadTiming())
840         return;
841
842     d->m_response.resourceLoadTiming()->sendEnd = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
843 }
844
845 static void requestStartedCallback(SoupSession*, SoupMessage* soupMessage, SoupSocket*, gpointer)
846 {
847     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
848     if (!handle)
849         return;
850
851     ResourceHandleInternal* d = handle->getInternal();
852     if (!d->m_response.resourceLoadTiming())
853         return;
854
855     d->m_response.resourceLoadTiming()->sendStart = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
856     if (d->m_response.resourceLoadTiming()->sslStart != -1) {
857         // WebCore/inspector/front-end/RequestTimingView.js assumes
858         // that SSL time is included in connection time so must
859         // substract here the SSL delta that will be added later (see
860         // WebInspector.RequestTimingView.createTimingTable in the
861         // file above for more details).
862         d->m_response.resourceLoadTiming()->sendStart -=
863             d->m_response.resourceLoadTiming()->sslEnd - d->m_response.resourceLoadTiming()->sslStart;
864     }
865 }
866
867 static void networkEventCallback(SoupMessage*, GSocketClientEvent event, GIOStream*, gpointer data)
868 {
869     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
870     if (!handle)
871         return;
872     ResourceHandleInternal* d = handle->getInternal();
873     if (d->m_cancelled)
874         return;
875
876     int deltaTime = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
877     switch (event) {
878     case G_SOCKET_CLIENT_RESOLVING:
879         d->m_response.resourceLoadTiming()->dnsStart = deltaTime;
880         break;
881     case G_SOCKET_CLIENT_RESOLVED:
882         d->m_response.resourceLoadTiming()->dnsEnd = deltaTime;
883         break;
884     case G_SOCKET_CLIENT_CONNECTING:
885         d->m_response.resourceLoadTiming()->connectStart = deltaTime;
886         if (d->m_response.resourceLoadTiming()->dnsStart != -1)
887             // WebCore/inspector/front-end/RequestTimingView.js assumes
888             // that DNS time is included in connection time so must
889             // substract here the DNS delta that will be added later (see
890             // WebInspector.RequestTimingView.createTimingTable in the
891             // file above for more details).
892             d->m_response.resourceLoadTiming()->connectStart -=
893                 d->m_response.resourceLoadTiming()->dnsEnd - d->m_response.resourceLoadTiming()->dnsStart;
894         break;
895     case G_SOCKET_CLIENT_CONNECTED:
896         // Web Timing considers that connection time involves dns, proxy & TLS negotiation...
897         // so we better pick G_SOCKET_CLIENT_COMPLETE for connectEnd
898         break;
899     case G_SOCKET_CLIENT_PROXY_NEGOTIATING:
900         d->m_response.resourceLoadTiming()->proxyStart = deltaTime;
901         break;
902     case G_SOCKET_CLIENT_PROXY_NEGOTIATED:
903         d->m_response.resourceLoadTiming()->proxyEnd = deltaTime;
904         break;
905     case G_SOCKET_CLIENT_TLS_HANDSHAKING:
906         d->m_response.resourceLoadTiming()->sslStart = deltaTime;
907         break;
908     case G_SOCKET_CLIENT_TLS_HANDSHAKED:
909         d->m_response.resourceLoadTiming()->sslEnd = deltaTime;
910         break;
911     case G_SOCKET_CLIENT_COMPLETE:
912         d->m_response.resourceLoadTiming()->connectEnd = deltaTime;
913         break;
914     default:
915         ASSERT_NOT_REACHED();
916         break;
917     }
918 }
919 #endif
920
921 static const char* gSoupRequestInitiatingPageIDKey = "wk-soup-request-initiating-page-id";
922
923 static void setSoupRequestInitiatingPageIDFromNetworkingContext(SoupRequest* request, NetworkingContext* context)
924 {
925     if (!context || !context->isValid())
926         return;
927
928     uint64_t* initiatingPageIDPtr = static_cast<uint64_t*>(fastMalloc(sizeof(uint64_t)));
929     *initiatingPageIDPtr = context->initiatingPageID();
930     g_object_set_data_full(G_OBJECT(request), g_intern_static_string(gSoupRequestInitiatingPageIDKey), initiatingPageIDPtr, fastFree);
931 }
932
933 static bool createSoupMessageForHandleAndRequest(ResourceHandle* handle, const ResourceRequest& request)
934 {
935     ASSERT(handle);
936
937     ResourceHandleInternal* d = handle->getInternal();
938     ASSERT(d->m_soupRequest);
939
940     d->m_soupMessage = adoptGRef(soup_request_http_get_message(SOUP_REQUEST_HTTP(d->m_soupRequest.get())));
941     if (!d->m_soupMessage)
942         return false;
943
944     SoupMessage* soupMessage = d->m_soupMessage.get();
945     request.updateSoupMessage(soupMessage);
946
947     g_object_set_data(G_OBJECT(soupMessage), "handle", handle);
948     if (!handle->shouldContentSniff())
949         soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_SNIFFER);
950
951     String firstPartyString = request.firstPartyForCookies().string();
952     if (!firstPartyString.isEmpty()) {
953         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
954         soup_message_set_first_party(soupMessage, firstParty.get());
955     }
956
957     FormData* httpBody = request.httpBody();
958     CString contentType = request.httpContentType().utf8().data();
959     if (httpBody && !httpBody->isEmpty() && !addFormElementsToSoupMessage(soupMessage, contentType.data(), httpBody, d->m_bodySize)) {
960         // We failed to prepare the body data, so just fail this load.
961         d->m_soupMessage.clear();
962         return false;
963     }
964
965     // Make sure we have an Accept header for subresources; some sites
966     // want this to serve some of their subresources
967     if (!soup_message_headers_get_one(soupMessage->request_headers, "Accept"))
968         soup_message_headers_append(soupMessage->request_headers, "Accept", "*/*");
969
970     // In the case of XHR .send() and .send("") explicitly tell libsoup to send a zero content-lenght header
971     // for consistency with other backends (e.g. Chromium's) and other UA implementations like FF. It's done
972     // in the backend here instead of in XHR code since in XHR CORS checking prevents us from this kind of
973     // late header manipulation.
974     if ((request.httpMethod() == "POST" || request.httpMethod() == "PUT")
975         && (!request.httpBody() || request.httpBody()->isEmpty()))
976         soup_message_headers_set_content_length(soupMessage->request_headers, 0);
977
978     g_signal_connect(d->m_soupMessage.get(), "got-headers", G_CALLBACK(gotHeadersCallback), handle);
979     g_signal_connect(d->m_soupMessage.get(), "restarted", G_CALLBACK(restartedCallback), handle);
980     g_signal_connect(d->m_soupMessage.get(), "wrote-body-data", G_CALLBACK(wroteBodyDataCallback), handle);
981
982     soup_message_set_flags(d->m_soupMessage.get(), static_cast<SoupMessageFlags>(soup_message_get_flags(d->m_soupMessage.get()) | SOUP_MESSAGE_NO_REDIRECT));
983
984 #if ENABLE(WEB_TIMING)
985     d->m_response.setResourceLoadTiming(ResourceLoadTiming::create());
986     g_signal_connect(d->m_soupMessage.get(), "network-event", G_CALLBACK(networkEventCallback), handle);
987     g_signal_connect(d->m_soupMessage.get(), "wrote-body", G_CALLBACK(wroteBodyCallback), handle);
988 #endif
989
990     return true;
991 }
992
993 static bool createSoupRequestAndMessageForHandle(ResourceHandle* handle, const ResourceRequest& request, bool isHTTPFamilyRequest)
994 {
995     ResourceHandleInternal* d = handle->getInternal();
996     SoupRequester* requester = SOUP_REQUESTER(soup_session_get_feature(d->soupSession(), SOUP_TYPE_REQUESTER));
997
998     GOwnPtr<GError> error;
999
1000     GOwnPtr<SoupURI> soupURI(request.soupURI());
1001     if (!soupURI)
1002         return false;
1003
1004     d->m_soupRequest = adoptGRef(soup_requester_request_uri(requester, soupURI.get(), &error.outPtr()));
1005     if (error) {
1006         d->m_soupRequest.clear();
1007         return false;
1008     }
1009
1010     // SoupMessages are only applicable to HTTP-family requests.
1011     if (isHTTPFamilyRequest && !createSoupMessageForHandleAndRequest(handle, request)) {
1012         d->m_soupRequest.clear();
1013         return false;
1014     }
1015
1016     return true;
1017 }
1018
1019 bool ResourceHandle::start(NetworkingContext* context)
1020 {
1021     ASSERT(!d->m_soupMessage);
1022
1023     // The frame could be null if the ResourceHandle is not associated to any
1024     // Frame, e.g. if we are downloading a file.
1025     // If the frame is not null but the page is null this must be an attempted
1026     // load from an unload handler, so let's just block it.
1027     // If both the frame and the page are not null the context is valid.
1028     if (context && !context->isValid())
1029         return false;
1030
1031     // Used to set the keep track of custom SoupSessions for ports that support it (EFL).
1032     d->m_context = context;
1033
1034     // Only allow the POST and GET methods for non-HTTP requests.
1035     const ResourceRequest& request = firstRequest();
1036     bool isHTTPFamilyRequest = request.url().protocolIsInHTTPFamily();
1037     if (!isHTTPFamilyRequest && request.httpMethod() != "GET" && request.httpMethod() != "POST") {
1038         this->scheduleFailure(InvalidURLFailure); // Error must not be reported immediately
1039         return true;
1040     }
1041
1042     applyAuthenticationToRequest(this, firstRequest(), false);
1043
1044     if (!createSoupRequestAndMessageForHandle(this, request, isHTTPFamilyRequest)) {
1045         this->scheduleFailure(InvalidURLFailure); // Error must not be reported immediately
1046         return true;
1047     }
1048
1049     setSoupRequestInitiatingPageIDFromNetworkingContext(d->m_soupRequest.get(), context);
1050
1051     // Send the request only if it's not been explicitly deferred.
1052     if (!d->m_defersLoading)
1053         sendPendingRequest();
1054
1055     return true;
1056 }
1057
1058 void ResourceHandle::sendPendingRequest()
1059 {
1060 #if ENABLE(WEB_TIMING)
1061     if (d->m_response.resourceLoadTiming())
1062         d->m_response.resourceLoadTiming()->requestTime = monotonicallyIncreasingTime();
1063 #endif
1064
1065     if (d->m_firstRequest.timeoutInterval() > 0) {
1066         // soup_add_timeout returns a GSource* whose only reference is owned by
1067         // the context. We need to have our own reference to it, hence not using adoptRef.
1068         d->m_timeoutSource = soup_add_timeout(g_main_context_get_thread_default(),
1069             d->m_firstRequest.timeoutInterval() * 1000, requestTimeoutCallback, this);
1070     }
1071
1072     // Balanced by a deref() in cleanupSoupRequestOperation, which should always run.
1073     ref();
1074
1075     d->m_cancellable = adoptGRef(g_cancellable_new());
1076     soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, this);
1077 }
1078
1079 void ResourceHandle::cancel()
1080 {
1081     d->m_cancelled = true;
1082     if (d->m_soupMessage)
1083         soup_session_cancel_message(d->soupSession(), d->m_soupMessage.get(), SOUP_STATUS_CANCELLED);
1084     else if (d->m_cancellable)
1085         g_cancellable_cancel(d->m_cancellable.get());
1086 }
1087
1088 bool ResourceHandle::shouldUseCredentialStorage()
1089 {
1090     return (!client() || client()->shouldUseCredentialStorage(this)) && firstRequest().url().protocolIsInHTTPFamily();
1091 }
1092
1093 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
1094 {
1095     allowsAnyHTTPSCertificateHosts().add(host.lower());
1096 }
1097
1098 void ResourceHandle::setClientCertificate(const String& host, GTlsCertificate* certificate)
1099 {
1100     clientCertificates().add(host.lower(), HostTLSCertificateSet()).iterator->value.add(certificate);
1101 }
1102
1103 void ResourceHandle::setIgnoreSSLErrors(bool ignoreSSLErrors)
1104 {
1105     gIgnoreSSLErrors = ignoreSSLErrors;
1106 }
1107
1108 #if PLATFORM(GTK)
1109 void getCredentialFromPersistentStoreCallback(const Credential& credential, void* data)
1110 {
1111     static_cast<ResourceHandle*>(data)->continueDidReceiveAuthenticationChallenge(credential);
1112 }
1113 #endif
1114
1115 void ResourceHandle::continueDidReceiveAuthenticationChallenge(const Credential& credentialFromPersistentStorage)
1116 {
1117     ASSERT(!d->m_currentWebChallenge.isNull());
1118     AuthenticationChallenge& challenge = d->m_currentWebChallenge;
1119
1120     ASSERT(challenge.soupSession());
1121     ASSERT(challenge.soupMessage());
1122     if (!credentialFromPersistentStorage.isEmpty())
1123         challenge.setProposedCredential(credentialFromPersistentStorage);
1124
1125     if (!client()) {
1126         soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());
1127         clearAuthentication();
1128         return;
1129     }
1130
1131     ASSERT(challenge.soupSession());
1132     ASSERT(challenge.soupMessage());
1133     client()->didReceiveAuthenticationChallenge(this, challenge);
1134 }
1135
1136 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
1137 {
1138     ASSERT(d->m_currentWebChallenge.isNull());
1139
1140     // FIXME: Per the specification, the user shouldn't be asked for credentials if there were incorrect ones provided explicitly.
1141     bool useCredentialStorage = shouldUseCredentialStorage();
1142     if (useCredentialStorage) {
1143         if (!d->m_initialCredential.isEmpty() || challenge.previousFailureCount()) {
1144             // The stored credential wasn't accepted, stop using it. There is a race condition
1145             // here, since a different credential might have already been stored by another
1146             // ResourceHandle, but the observable effect should be very minor, if any.
1147             CredentialStorage::remove(challenge.protectionSpace());
1148         }
1149
1150         if (!challenge.previousFailureCount()) {
1151             Credential credential = CredentialStorage::get(challenge.protectionSpace());
1152             if (!credential.isEmpty() && credential != d->m_initialCredential) {
1153                 ASSERT(credential.persistence() == CredentialPersistenceNone);
1154
1155                 // Store the credential back, possibly adding it as a default for this directory.
1156                 if (isAuthenticationFailureStatusCode(challenge.failureResponse().httpStatusCode()))
1157                     CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());
1158
1159                 soup_auth_authenticate(challenge.soupAuth(), credential.user().utf8().data(), credential.password().utf8().data());
1160                 return;
1161             }
1162         }
1163     }
1164
1165     d->m_currentWebChallenge = challenge;
1166     soup_session_pause_message(challenge.soupSession(), challenge.soupMessage());
1167
1168 #if PLATFORM(GTK)
1169     // We could also do this before we even start the request, but that would be at the expense
1170     // of all request latency, versus a one-time latency for the small subset of requests that
1171     // use HTTP authentication. In the end, this doesn't matter much, because persistent credentials
1172     // will become session credentials after the first use.
1173     if (useCredentialStorage) {
1174         credentialBackingStore().credentialForChallenge(challenge, getCredentialFromPersistentStoreCallback, this);
1175         return;
1176     }
1177 #endif
1178
1179     continueDidReceiveAuthenticationChallenge(Credential());
1180 }
1181
1182 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge)
1183 {
1184     ASSERT(!challenge.isNull());
1185     if (challenge != d->m_currentWebChallenge)
1186         return;
1187     soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());
1188
1189     clearAuthentication();
1190 }
1191
1192 void ResourceHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential)
1193 {
1194     ASSERT(!challenge.isNull());
1195     if (challenge != d->m_currentWebChallenge)
1196         return;
1197
1198     // FIXME: Support empty credentials. Currently, an empty credential cannot be stored in WebCore credential storage, as that's empty value for its map.
1199     if (credential.isEmpty()) {
1200         receivedRequestToContinueWithoutCredential(challenge);
1201         return;
1202     }
1203
1204     if (shouldUseCredentialStorage()) {
1205         // Eventually we will manage per-session credentials only internally or use some newly-exposed API from libsoup,
1206         // because once we authenticate via libsoup, there is no way to ignore it for a particular request. Right now,
1207         // we place the credentials in the store even though libsoup will never fire the authenticate signal again for
1208         // this protection space.
1209         if (credential.persistence() == CredentialPersistenceForSession || credential.persistence() == CredentialPersistencePermanent)
1210             CredentialStorage::set(credential, challenge.protectionSpace(), challenge.failureResponse().url());
1211
1212 #if PLATFORM(GTK)
1213         if (credential.persistence() == CredentialPersistencePermanent) {
1214             d->m_credentialDataToSaveInPersistentStore.credential = credential;
1215             d->m_credentialDataToSaveInPersistentStore.challenge = challenge;
1216         }
1217 #endif
1218     }
1219
1220     ASSERT(challenge.soupSession());
1221     ASSERT(challenge.soupMessage());
1222     soup_auth_authenticate(challenge.soupAuth(), credential.user().utf8().data(), credential.password().utf8().data());
1223     soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());
1224
1225     clearAuthentication();
1226 }
1227
1228 void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge)
1229 {
1230     ASSERT(!challenge.isNull());
1231     if (challenge != d->m_currentWebChallenge)
1232         return;
1233
1234     soup_session_unpause_message(challenge.soupSession(), challenge.soupMessage());
1235
1236     if (client())
1237         client()->receivedCancellation(this, challenge);
1238
1239     clearAuthentication();
1240 }
1241
1242 static bool waitingToSendRequest(ResourceHandle* handle)
1243 {
1244     // We need to check for d->m_soupRequest because the request may have raised a failure
1245     // (for example invalid URLs). We cannot  simply check for d->m_scheduledFailure because
1246     // it's cleared as soon as the failure event is fired.
1247     return handle->getInternal()->m_soupRequest && !handle->getInternal()->m_cancellable;
1248 }
1249
1250 void ResourceHandle::platformSetDefersLoading(bool defersLoading)
1251 {
1252     if (d->m_cancelled)
1253         return;
1254
1255     // Except when canceling a possible timeout timer, we only need to take action here to UN-defer loading.
1256     if (defersLoading) {
1257         if (d->m_timeoutSource) {
1258             g_source_destroy(d->m_timeoutSource.get());
1259             d->m_timeoutSource.clear();
1260         }
1261         return;
1262     }
1263
1264     if (waitingToSendRequest(this)) {
1265         sendPendingRequest();
1266         return;
1267     }
1268
1269     if (d->m_deferredResult) {
1270         GRefPtr<GAsyncResult> asyncResult = adoptGRef(d->m_deferredResult.leakRef());
1271
1272         if (d->m_inputStream)
1273             readCallback(G_OBJECT(d->m_inputStream.get()), asyncResult.get(), this);
1274         else
1275             sendRequestCallback(G_OBJECT(d->m_soupRequest.get()), asyncResult.get(), this);
1276     }
1277 }
1278
1279 bool ResourceHandle::loadsBlocked()
1280 {
1281     return false;
1282 }
1283
1284 void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data)
1285 {
1286 #if ENABLE(BLOB)
1287     if (request.url().protocolIs("blob")) {
1288         blobRegistry().loadResourceSynchronously(request, error, response, data);
1289         return;
1290     }
1291 #endif
1292  
1293     ASSERT(!loadingSynchronousRequest);
1294     if (loadingSynchronousRequest) // In practice this cannot happen, but if for some reason it does,
1295         return;                    // we want to avoid accidentally going into an infinite loop of requests.
1296
1297     WebCoreSynchronousLoader syncLoader(error, response, sessionFromContext(context), data);
1298     RefPtr<ResourceHandle> handle = create(context, request, &syncLoader, false /*defersLoading*/, false /*shouldContentSniff*/);
1299     if (!handle)
1300         return;
1301
1302     // If the request has already failed, do not run the main loop, or else we'll block indefinitely.
1303     if (handle->d->m_scheduledFailureType != NoFailure)
1304         return;
1305
1306     syncLoader.run();
1307 }
1308
1309 static void closeCallback(GObject*, GAsyncResult* res, gpointer data)
1310 {
1311     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
1312     ResourceHandleInternal* d = handle->getInternal();
1313
1314     g_input_stream_close_finish(d->m_inputStream.get(), res, 0);
1315
1316     ResourceHandleClient* client = handle->client();
1317     if (client && loadingSynchronousRequest)
1318         client->didFinishLoading(handle.get(), 0);
1319
1320     cleanupSoupRequestOperation(handle.get());
1321 }
1322
1323 static void readCallback(GObject*, GAsyncResult* asyncResult, gpointer data)
1324 {
1325     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
1326
1327     ResourceHandleInternal* d = handle->getInternal();
1328     ResourceHandleClient* client = handle->client();
1329
1330     if (d->m_cancelled || !client) {
1331         cleanupSoupRequestOperation(handle.get());
1332         return;
1333     }
1334
1335     if (d->m_defersLoading) {
1336         d->m_deferredResult = asyncResult;
1337         return;
1338     }
1339
1340     GOwnPtr<GError> error;
1341     gssize bytesRead = g_input_stream_read_finish(d->m_inputStream.get(), asyncResult, &error.outPtr());
1342     if (error) {
1343         client->didFail(handle.get(), ResourceError::genericIOError(error.get(), d->m_soupRequest.get()));
1344         cleanupSoupRequestOperation(handle.get());
1345         return;
1346     }
1347
1348     if (!bytesRead) {
1349         // If this is a multipart message, we'll look for another part.
1350         if (d->m_soupMessage && d->m_multipartInputStream) {
1351             d->m_inputStream.clear();
1352             soup_multipart_input_stream_next_part_async(d->m_multipartInputStream.get(), G_PRIORITY_DEFAULT,
1353                 d->m_cancellable.get(), nextMultipartResponsePartCallback, handle.get());
1354             return;
1355         }
1356
1357         // We inform WebCore of load completion now instead of waiting for the input
1358         // stream to close because the input stream is closed asynchronously. If this
1359         // is a synchronous request, we wait until the closeCallback, because we don't
1360         // want to halt the internal main loop before the input stream closes.
1361         if (client && !loadingSynchronousRequest) {
1362             client->didFinishLoading(handle.get(), 0);
1363             handle->setClient(0); // Unset the client so that we do not try to access th
1364                                   // client in the closeCallback.
1365         }
1366         g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT, 0, closeCallback, handle.get());
1367         return;
1368     }
1369
1370     // It's mandatory to have sent a response before sending data
1371     ASSERT(!d->m_response.isNull());
1372
1373     client->didReceiveData(handle.get(), d->m_buffer, bytesRead, bytesRead);
1374
1375     // didReceiveData may cancel the load, which may release the last reference.
1376     if (d->m_cancelled || !client) {
1377         cleanupSoupRequestOperation(handle.get());
1378         return;
1379     }
1380
1381     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
1382                               d->m_cancellable.get(), readCallback, handle.get());
1383 }
1384
1385 static gboolean requestTimeoutCallback(gpointer data)
1386 {
1387     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
1388     handle->client()->didFail(handle.get(), ResourceError::timeoutError(handle->getInternal()->m_firstRequest.url().string()));
1389     handle->cancel();
1390
1391     return FALSE;
1392 }
1393
1394 static void authenticateCallback(SoupSession* session, SoupMessage* soupMessage, SoupAuth* soupAuth, gboolean retrying)
1395 {
1396     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
1397     if (!handle)
1398         return;
1399     handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(session, soupMessage, soupAuth, retrying, handle.get()));
1400 }
1401
1402 SoupSession* ResourceHandle::defaultSession()
1403 {
1404     static SoupSession* session = 0;
1405     // Values taken from http://www.browserscope.org/  following
1406     // the rule "Do What Every Other Modern Browser Is Doing". They seem
1407     // to significantly improve page loading time compared to soup's
1408     // default values.
1409     static const int maxConnections = 35;
1410     static const int maxConnectionsPerHost = 6;
1411
1412     if (!session) {
1413         session = soup_session_async_new();
1414         g_object_set(session,
1415                      SOUP_SESSION_MAX_CONNS, maxConnections,
1416                      SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
1417                      SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
1418                      SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER,
1419                      SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
1420                      SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
1421                      NULL);
1422         g_signal_connect(session, "authenticate", G_CALLBACK(authenticateCallback), 0);
1423
1424 #if ENABLE(WEB_TIMING)
1425         g_signal_connect(session, "request-started", G_CALLBACK(requestStartedCallback), 0);
1426 #endif
1427     }
1428
1429     return session;
1430 }
1431
1432 uint64_t ResourceHandle::getSoupRequestInitiatingPageID(SoupRequest* request)
1433 {
1434     uint64_t* initiatingPageIDPtr = static_cast<uint64_t*>(g_object_get_data(G_OBJECT(request), gSoupRequestInitiatingPageIDKey));
1435     return initiatingPageIDPtr ? *initiatingPageIDPtr : 0;
1436 }
1437
1438 }