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