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