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