[GTK][Soup] Implement the default authentication dialog via WebCoreSupport
[WebKit-https.git] / Source / WebCore / platform / network / soup / ResourceHandleSoup.cpp
1 /*
2  * Copyright (C) 2008 Alp Toker <alp@atoker.com>
3  * Copyright (C) 2008 Xan Lopez <xan@gnome.org>
4  * Copyright (C) 2008, 2010 Collabora Ltd.
5  * Copyright (C) 2009 Holger Hans Peter Freyther
6  * Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>
7  * Copyright (C) 2009 Christian Dywan <christian@imendio.com>
8  * Copyright (C) 2009, 2010, 2011 Igalia S.L.
9  * Copyright (C) 2009 John Kjellberg <john.kjellberg@power.alstom.com>
10  * Copyright (C) 2012 Intel Corporation
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public
14  * License as published by the Free Software Foundation; either
15  * version 2 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public License
23  * along with this library; see the file COPYING.LIB.  If not, write to
24  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  * Boston, MA 02110-1301, USA.
26  */
27
28 #include "config.h"
29 #include "ResourceHandle.h"
30
31 #include "CachedResourceLoader.h"
32 #include "ChromeClient.h"
33 #include "CookieJarSoup.h"
34 #include "FileSystem.h"
35 #include "Frame.h"
36 #include "GOwnPtrSoup.h"
37 #include "HTTPParsers.h"
38 #include "LocalizedStrings.h"
39 #include "Logging.h"
40 #include "MIMETypeRegistry.h"
41 #include "NotImplemented.h"
42 #include "Page.h"
43 #include "ResourceError.h"
44 #include "ResourceHandleClient.h"
45 #include "ResourceHandleInternal.h"
46 #include "ResourceResponse.h"
47 #include "SharedBuffer.h"
48 #include "TextEncoding.h"
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <gio/gio.h>
52 #include <glib.h>
53 #define LIBSOUP_USE_UNSTABLE_REQUEST_API
54 #include <libsoup/soup-request-http.h>
55 #include <libsoup/soup-requester.h>
56 #include <libsoup/soup.h>
57 #include <sys/stat.h>
58 #include <sys/types.h>
59 #include <unistd.h>
60 #include <wtf/SHA1.h>
61 #include <wtf/gobject/GRefPtr.h>
62 #include <wtf/text/Base64.h>
63 #include <wtf/text/CString.h>
64
65 #if ENABLE(BLOB)
66 #include "BlobData.h"
67 #include "BlobRegistryImpl.h"
68 #include "BlobStorageData.h"
69 #endif
70
71 namespace WebCore {
72
73 #define READ_BUFFER_SIZE 8192
74
75 static bool loadingSynchronousRequest = false;
76
77 class WebCoreSynchronousLoader : public ResourceHandleClient {
78     WTF_MAKE_NONCOPYABLE(WebCoreSynchronousLoader);
79 public:
80
81     WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, SoupSession* session, Vector<char>& data)
82         : m_error(error)
83         , m_response(response)
84         , m_session(session)
85         , m_data(data)
86         , m_finished(false)
87     {
88         // We don't want any timers to fire while we are doing our synchronous load
89         // so we replace the thread default main context. The main loop iterations
90         // will only process GSources associated with this inner context.
91         loadingSynchronousRequest = true;
92         GRefPtr<GMainContext> innerMainContext = adoptGRef(g_main_context_new());
93         g_main_context_push_thread_default(innerMainContext.get());
94         m_mainLoop = adoptGRef(g_main_loop_new(innerMainContext.get(), false));
95
96         adjustMaxConnections(1);
97     }
98
99     ~WebCoreSynchronousLoader()
100     {
101         adjustMaxConnections(-1);
102         g_main_context_pop_thread_default(g_main_context_get_thread_default());
103         loadingSynchronousRequest = false;
104     }
105
106     void adjustMaxConnections(int adjustment)
107     {
108         int maxConnections, maxConnectionsPerHost;
109         g_object_get(m_session,
110                      SOUP_SESSION_MAX_CONNS, &maxConnections,
111                      SOUP_SESSION_MAX_CONNS_PER_HOST, &maxConnectionsPerHost,
112                      NULL);
113         maxConnections += adjustment;
114         maxConnectionsPerHost += adjustment;
115         g_object_set(m_session,
116                      SOUP_SESSION_MAX_CONNS, maxConnections,
117                      SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
118                      NULL);
119
120     }
121
122     virtual bool isSynchronousClient()
123     {
124         return true;
125     }
126
127     virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
128     {
129         m_response = response;
130     }
131
132     virtual void didReceiveData(ResourceHandle*, const char* data, int length, int)
133     {
134         m_data.append(data, length);
135     }
136
137     virtual void didFinishLoading(ResourceHandle*, double)
138     {
139         if (g_main_loop_is_running(m_mainLoop.get()))
140             g_main_loop_quit(m_mainLoop.get());
141         m_finished = true;
142     }
143
144     virtual void didFail(ResourceHandle* handle, const ResourceError& error)
145     {
146         m_error = error;
147         didFinishLoading(handle, 0);
148     }
149
150     void run()
151     {
152         if (!m_finished)
153             g_main_loop_run(m_mainLoop.get());
154     }
155
156 private:
157     ResourceError& m_error;
158     ResourceResponse& m_response;
159     SoupSession* m_session;
160     Vector<char>& m_data;
161     bool m_finished;
162     GRefPtr<GMainLoop> m_mainLoop;
163 };
164
165 class HostTLSCertificateSet {
166 public:
167     void add(GTlsCertificate* certificate)
168     {
169         String certificateHash = computeCertificateHash(certificate);
170         if (!certificateHash.isEmpty())
171             m_certificates.add(certificateHash);
172     }
173
174     bool contains(GTlsCertificate* certificate)
175     {
176         return m_certificates.contains(computeCertificateHash(certificate));
177     }
178
179 private:
180     static String computeCertificateHash(GTlsCertificate* certificate)
181     {
182         GByteArray* data = 0;
183         g_object_get(G_OBJECT(certificate), "certificate", &data, NULL);
184         if (!data)
185             return String();
186
187         static const size_t sha1HashSize = 20;
188         GRefPtr<GByteArray> certificateData = adoptGRef(data);
189         SHA1 sha1;
190         sha1.addBytes(certificateData->data, certificateData->len);
191
192         Vector<uint8_t, sha1HashSize> digest;
193         sha1.computeHash(digest);
194
195         return base64Encode(reinterpret_cast<const char*>(digest.data()), sha1HashSize);
196     }
197
198     HashSet<String> m_certificates;
199 };
200
201 static void cleanupSoupRequestOperation(ResourceHandle*, bool isDestroying);
202 static void sendRequestCallback(GObject*, GAsyncResult*, gpointer);
203 static void readCallback(GObject*, GAsyncResult*, gpointer);
204 static void closeCallback(GObject*, GAsyncResult*, gpointer);
205 static gboolean requestTimeoutCallback(void*);
206 #if ENABLE(WEB_TIMING)
207 static int  milisecondsSinceRequest(double requestTime);
208 #endif
209
210 static bool gIgnoreSSLErrors = false;
211
212 static HashSet<String>& allowsAnyHTTPSCertificateHosts()
213 {
214     DEFINE_STATIC_LOCAL(HashSet<String>, hosts, ());
215     return hosts;
216 }
217
218 typedef HashMap<String, HostTLSCertificateSet> CertificatesMap;
219 static CertificatesMap& clientCertificates()
220 {
221     DEFINE_STATIC_LOCAL(CertificatesMap, certificates, ());
222     return certificates;
223 }
224
225 ResourceHandleInternal::~ResourceHandleInternal()
226 {
227 }
228
229 static SoupSession* sessionFromContext(NetworkingContext* context)
230 {
231     return (context && context->isValid()) ? context->soupSession() : ResourceHandle::defaultSession();
232 }
233
234 ResourceHandle::~ResourceHandle()
235 {
236     cleanupSoupRequestOperation(this, true);
237 }
238
239 static void ensureSessionIsInitialized(SoupSession* session)
240 {
241     if (g_object_get_data(G_OBJECT(session), "webkit-init"))
242         return;
243
244     if (session == ResourceHandle::defaultSession()) {
245         SoupCookieJar* jar = SOUP_COOKIE_JAR(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
246         if (!jar)
247             soup_session_add_feature(session, SOUP_SESSION_FEATURE(soupCookieJar()));
248         else
249             setSoupCookieJar(jar);
250     }
251
252 #if !LOG_DISABLED
253     if (!soup_session_get_feature(session, SOUP_TYPE_LOGGER) && LogNetwork.state == WTFLogChannelOn) {
254         SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(SOUP_LOGGER_LOG_BODY), -1);
255         soup_session_add_feature(session, SOUP_SESSION_FEATURE(logger));
256         g_object_unref(logger);
257     }
258 #endif // !LOG_DISABLED
259
260     if (!soup_session_get_feature(session, SOUP_TYPE_REQUESTER)) {
261         SoupRequester* requester = soup_requester_new();
262         soup_session_add_feature(session, SOUP_SESSION_FEATURE(requester));
263         g_object_unref(requester);
264     }
265
266     g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
267 }
268
269 SoupSession* ResourceHandleInternal::soupSession()
270 {
271     SoupSession* session = sessionFromContext(m_context.get());
272     ensureSessionIsInitialized(session);
273     return session;
274 }
275
276 static void gotHeadersCallback(SoupMessage* msg, gpointer data)
277 {
278     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
279     if (!handle)
280         return;
281     ResourceHandleInternal* d = handle->getInternal();
282     if (d->m_cancelled)
283         return;
284
285 #if ENABLE(WEB_TIMING)
286     if (d->m_response.resourceLoadTiming())
287         d->m_response.resourceLoadTiming()->receiveHeadersEnd = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
288 #endif
289
290     // The original response will be needed later to feed to willSendRequest in
291     // restartedCallback() in case we are redirected. For this reason, so we store it
292     // here.
293     ResourceResponse response;
294     response.updateFromSoupMessage(msg);
295
296     d->m_response = response;
297 }
298
299 // Called each time the message is going to be sent again except the first time.
300 // It's used mostly to let webkit know about redirects.
301 static void restartedCallback(SoupMessage* msg, gpointer data)
302 {
303     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
304     if (!handle)
305         return;
306     ResourceHandleInternal* d = handle->getInternal();
307     if (d->m_cancelled)
308         return;
309
310     GOwnPtr<char> uri(soup_uri_to_string(soup_message_get_uri(msg), false));
311     String location = String::fromUTF8(uri.get());
312     KURL newURL = KURL(handle->firstRequest().url(), location);
313
314     ResourceRequest request = handle->firstRequest();
315     request.setURL(newURL);
316     request.setHTTPMethod(msg->method);
317
318     // Should not set Referer after a redirect from a secure resource to non-secure one.
319     if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https")) {
320         request.clearHTTPReferrer();
321         soup_message_headers_remove(msg->request_headers, "Referer");
322     }
323
324     if (d->client())
325         d->client()->willSendRequest(handle, request, d->m_response);
326
327     if (d->m_cancelled)
328         return;
329
330 #if ENABLE(WEB_TIMING)
331     d->m_response.setResourceLoadTiming(ResourceLoadTiming::create());
332     d->m_response.resourceLoadTiming()->requestTime = monotonicallyIncreasingTime();
333 #endif
334
335     // Update the first party in case the base URL changed with the redirect
336     String firstPartyString = request.firstPartyForCookies().string();
337     if (!firstPartyString.isEmpty()) {
338         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
339         soup_message_set_first_party(d->m_soupMessage.get(), firstParty.get());
340     }
341 }
342
343 static void wroteBodyDataCallback(SoupMessage*, SoupBuffer* buffer, gpointer data)
344 {
345     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
346     if (!handle)
347         return;
348
349     ASSERT(buffer);
350     ResourceHandleInternal* internal = handle->getInternal();
351     internal->m_bodyDataSent += buffer->length;
352
353     if (internal->m_cancelled)
354         return;
355     ResourceHandleClient* client = handle->client();
356     if (!client)
357         return;
358
359     client->didSendData(handle.get(), internal->m_bodyDataSent, internal->m_bodySize);
360 }
361
362 static void cleanupSoupRequestOperation(ResourceHandle* handle, bool isDestroying = false)
363 {
364     ResourceHandleInternal* d = handle->getInternal();
365
366     if (d->m_soupRequest)
367         d->m_soupRequest.clear();
368
369     if (d->m_inputStream)
370         d->m_inputStream.clear();
371
372     d->m_cancellable.clear();
373
374     if (d->m_soupMessage) {
375         g_signal_handlers_disconnect_matched(d->m_soupMessage.get(), G_SIGNAL_MATCH_DATA,
376                                              0, 0, 0, 0, handle);
377         g_object_set_data(G_OBJECT(d->m_soupMessage.get()), "handle", 0);
378         d->m_soupMessage.clear();
379     }
380
381     if (d->m_buffer) {
382         g_slice_free1(READ_BUFFER_SIZE, d->m_buffer);
383         d->m_buffer = 0;
384     }
385
386     if (d->m_timeoutSource) {
387         g_source_destroy(d->m_timeoutSource.get());
388         d->m_timeoutSource.clear();
389     }
390
391     if (!isDestroying)
392         handle->deref();
393 }
394
395 static bool handleUnignoredTLSErrors(ResourceHandle* handle)
396 {
397     ResourceHandleInternal* d = handle->getInternal();
398     const ResourceResponse& response = d->m_response;
399
400     if (!response.soupMessageTLSErrors() || gIgnoreSSLErrors)
401         return false;
402
403     String lowercaseHostURL = handle->firstRequest().url().host().lower();
404     if (allowsAnyHTTPSCertificateHosts().contains(lowercaseHostURL))
405         return false;
406
407     // We aren't ignoring errors globally, but the user may have already decided to accept this certificate.
408     CertificatesMap::iterator i = clientCertificates().find(lowercaseHostURL);
409     if (i != clientCertificates().end() && i->value.contains(response.soupMessageCertificate()))
410         return false;
411
412     handle->client()->didFail(handle, ResourceError::tlsError(d->m_soupRequest.get(), response.soupMessageTLSErrors(), response.soupMessageCertificate()));
413     return true;
414 }
415
416 static void sendRequestCallback(GObject*, GAsyncResult* res, gpointer data)
417 {
418     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
419
420     ResourceHandleInternal* d = handle->getInternal();
421     ResourceHandleClient* client = handle->client();
422     SoupMessage* soupMessage = d->m_soupMessage.get();
423
424     if (d->m_cancelled || !client) {
425         cleanupSoupRequestOperation(handle.get());
426         return;
427     }
428
429     if (d->m_defersLoading) {
430         d->m_deferredResult = res;
431         return;
432     }
433
434     GOwnPtr<GError> error;
435     GInputStream* in = soup_request_send_finish(d->m_soupRequest.get(), res, &error.outPtr());
436     if (error) {
437         client->didFail(handle.get(), ResourceError::httpError(soupMessage, error.get(), d->m_soupRequest.get()));
438         cleanupSoupRequestOperation(handle.get());
439         return;
440     }
441
442     d->m_inputStream = adoptGRef(in);
443     d->m_buffer = static_cast<char*>(g_slice_alloc(READ_BUFFER_SIZE));
444
445     if (soupMessage) {
446         if (handle->shouldContentSniff() && soupMessage->status_code != SOUP_STATUS_NOT_MODIFIED) {
447             const char* sniffedType = soup_request_get_content_type(d->m_soupRequest.get());
448             d->m_response.setSniffedContentType(sniffedType);
449         }
450         d->m_response.updateFromSoupMessage(soupMessage);
451
452         if (handleUnignoredTLSErrors(handle.get())) {
453             cleanupSoupRequestOperation(handle.get());
454             return;
455         }
456
457     } else {
458         d->m_response.setURL(handle->firstRequest().url());
459         const gchar* contentType = soup_request_get_content_type(d->m_soupRequest.get());
460         d->m_response.setMimeType(extractMIMETypeFromMediaType(contentType));
461         d->m_response.setTextEncodingName(extractCharsetFromMediaType(contentType));
462         d->m_response.setExpectedContentLength(soup_request_get_content_length(d->m_soupRequest.get()));
463     }
464
465     client->didReceiveResponse(handle.get(), d->m_response);
466
467     if (d->m_cancelled) {
468         cleanupSoupRequestOperation(handle.get());
469         return;
470     }
471
472     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE,
473                               G_PRIORITY_DEFAULT, d->m_cancellable.get(), readCallback, handle.get());
474 }
475
476 static bool addFileToSoupMessageBody(SoupMessage* message, const String& fileNameString, size_t offset, size_t lengthToSend, unsigned long& totalBodySize)
477 {
478     GOwnPtr<GError> error;
479     CString fileName = fileSystemRepresentation(fileNameString);
480     GMappedFile* fileMapping = g_mapped_file_new(fileName.data(), false, &error.outPtr());
481     if (error)
482         return false;
483
484     gsize bufferLength = lengthToSend;
485     if (!lengthToSend)
486         bufferLength = g_mapped_file_get_length(fileMapping);
487     totalBodySize += bufferLength;
488
489     SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping) + offset,
490                                                         bufferLength,
491                                                         fileMapping,
492                                                         reinterpret_cast<GDestroyNotify>(g_mapped_file_unref));
493     soup_message_body_append_buffer(message->request_body, soupBuffer);
494     soup_buffer_free(soupBuffer);
495     return true;
496 }
497
498 #if ENABLE(BLOB)
499 static bool blobIsOutOfDate(const BlobDataItem& blobItem)
500 {
501     ASSERT(blobItem.type == BlobDataItem::File);
502     if (!isValidFileTime(blobItem.expectedModificationTime))
503         return false;
504
505     time_t fileModificationTime;
506     if (!getFileModificationTime(blobItem.path, fileModificationTime))
507         return true;
508
509     return fileModificationTime != static_cast<time_t>(blobItem.expectedModificationTime);
510 }
511
512 static void addEncodedBlobItemToSoupMessageBody(SoupMessage* message, const BlobDataItem& blobItem, unsigned long& totalBodySize)
513 {
514     if (blobItem.type == BlobDataItem::Data) {
515         totalBodySize += blobItem.length;
516         soup_message_body_append(message->request_body, SOUP_MEMORY_TEMPORARY,
517                                  blobItem.data->data() + blobItem.offset, blobItem.length);
518         return;
519     }
520
521     ASSERT(blobItem.type == BlobDataItem::File);
522     if (blobIsOutOfDate(blobItem))
523         return;
524
525     addFileToSoupMessageBody(message,
526                              blobItem.path,
527                              blobItem.offset,
528                              blobItem.length == BlobDataItem::toEndOfFile ? 0 : blobItem.length,
529                              totalBodySize);
530 }
531
532 static void addEncodedBlobToSoupMessageBody(SoupMessage* message, const FormDataElement& element, unsigned long& totalBodySize)
533 {
534     RefPtr<BlobStorageData> blobData = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(KURL(ParsedURLString, element.m_url));
535     if (!blobData)
536         return;
537
538     for (size_t i = 0; i < blobData->items().size(); ++i)
539         addEncodedBlobItemToSoupMessageBody(message, blobData->items()[i], totalBodySize);
540 }
541 #endif // ENABLE(BLOB)
542
543 static bool addFormElementsToSoupMessage(SoupMessage* message, const char*, FormData* httpBody, unsigned long& totalBodySize)
544 {
545     soup_message_body_set_accumulate(message->request_body, FALSE);
546     size_t numElements = httpBody->elements().size();
547     for (size_t i = 0; i < numElements; i++) {
548         const FormDataElement& element = httpBody->elements()[i];
549
550         if (element.m_type == FormDataElement::data) {
551             totalBodySize += element.m_data.size();
552             soup_message_body_append(message->request_body, SOUP_MEMORY_TEMPORARY,
553                                      element.m_data.data(), element.m_data.size());
554             continue;
555         }
556
557         if (element.m_type == FormDataElement::encodedFile) {
558             if (!addFileToSoupMessageBody(message ,
559                                          element.m_filename,
560                                          0 /* offset */,
561                                          0 /* lengthToSend */,
562                                          totalBodySize))
563                 return false;
564             continue;
565         }
566
567 #if ENABLE(BLOB)
568         ASSERT(element.m_type == FormDataElement::encodedBlob);
569         addEncodedBlobToSoupMessageBody(message, element, totalBodySize);
570 #endif
571     }
572     return true;
573 }
574
575 #if ENABLE(WEB_TIMING)
576 static int milisecondsSinceRequest(double requestTime)
577 {
578     return static_cast<int>((monotonicallyIncreasingTime() - requestTime) * 1000.0);
579 }
580
581 static void wroteBodyCallback(SoupMessage*, gpointer data)
582 {
583     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
584     if (!handle)
585         return;
586
587     ResourceHandleInternal* d = handle->getInternal();
588     if (!d->m_response.resourceLoadTiming())
589         return;
590
591     d->m_response.resourceLoadTiming()->sendEnd = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
592 }
593
594 static void requestStartedCallback(SoupSession*, SoupMessage* soupMessage, SoupSocket*, gpointer)
595 {
596     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
597     if (!handle)
598         return;
599
600     ResourceHandleInternal* d = handle->getInternal();
601     if (!d->m_response.resourceLoadTiming())
602         return;
603
604     d->m_response.resourceLoadTiming()->sendStart = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
605     if (d->m_response.resourceLoadTiming()->sslStart != -1) {
606         // WebCore/inspector/front-end/RequestTimingView.js assumes
607         // that SSL time is included in connection time so must
608         // substract here the SSL delta that will be added later (see
609         // WebInspector.RequestTimingView.createTimingTable in the
610         // file above for more details).
611         d->m_response.resourceLoadTiming()->sendStart -=
612             d->m_response.resourceLoadTiming()->sslEnd - d->m_response.resourceLoadTiming()->sslStart;
613     }
614 }
615
616 static void networkEventCallback(SoupMessage*, GSocketClientEvent event, GIOStream*, gpointer data)
617 {
618     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
619     if (!handle)
620         return;
621     ResourceHandleInternal* d = handle->getInternal();
622     if (d->m_cancelled)
623         return;
624
625     int deltaTime = milisecondsSinceRequest(d->m_response.resourceLoadTiming()->requestTime);
626     switch (event) {
627     case G_SOCKET_CLIENT_RESOLVING:
628         d->m_response.resourceLoadTiming()->dnsStart = deltaTime;
629         break;
630     case G_SOCKET_CLIENT_RESOLVED:
631         d->m_response.resourceLoadTiming()->dnsEnd = deltaTime;
632         break;
633     case G_SOCKET_CLIENT_CONNECTING:
634         d->m_response.resourceLoadTiming()->connectStart = deltaTime;
635         if (d->m_response.resourceLoadTiming()->dnsStart != -1)
636             // WebCore/inspector/front-end/RequestTimingView.js assumes
637             // that DNS time is included in connection time so must
638             // substract here the DNS delta that will be added later (see
639             // WebInspector.RequestTimingView.createTimingTable in the
640             // file above for more details).
641             d->m_response.resourceLoadTiming()->connectStart -=
642                 d->m_response.resourceLoadTiming()->dnsEnd - d->m_response.resourceLoadTiming()->dnsStart;
643         break;
644     case G_SOCKET_CLIENT_CONNECTED:
645         // Web Timing considers that connection time involves dns, proxy & TLS negotiation...
646         // so we better pick G_SOCKET_CLIENT_COMPLETE for connectEnd
647         break;
648     case G_SOCKET_CLIENT_PROXY_NEGOTIATING:
649         d->m_response.resourceLoadTiming()->proxyStart = deltaTime;
650         break;
651     case G_SOCKET_CLIENT_PROXY_NEGOTIATED:
652         d->m_response.resourceLoadTiming()->proxyEnd = deltaTime;
653         break;
654     case G_SOCKET_CLIENT_TLS_HANDSHAKING:
655         d->m_response.resourceLoadTiming()->sslStart = deltaTime;
656         break;
657     case G_SOCKET_CLIENT_TLS_HANDSHAKED:
658         d->m_response.resourceLoadTiming()->sslEnd = deltaTime;
659         break;
660     case G_SOCKET_CLIENT_COMPLETE:
661         d->m_response.resourceLoadTiming()->connectEnd = deltaTime;
662         break;
663     default:
664         ASSERT_NOT_REACHED();
665         break;
666     }
667 }
668 #endif
669
670 static const char* gSoupRequestInitiaingPageIDKey = "wk-soup-request-initiaing-page-id";
671
672 static void setSoupRequestInitiaingPageIDFromNetworkingContext(SoupRequest* request, NetworkingContext* context)
673 {
674     if (!context || !context->isValid())
675         return;
676
677     uint64_t* initiatingPageIDPtr = static_cast<uint64_t*>(fastMalloc(sizeof(uint64_t)));
678     *initiatingPageIDPtr = context->initiatingPageID();
679     g_object_set_data_full(G_OBJECT(request), g_intern_static_string(gSoupRequestInitiaingPageIDKey), initiatingPageIDPtr, fastFree);
680 }
681
682 static bool createSoupMessageForHandleAndRequest(ResourceHandle* handle, const ResourceRequest& request)
683 {
684     ASSERT(handle);
685
686     ResourceHandleInternal* d = handle->getInternal();
687     ASSERT(d->m_soupRequest);
688
689     d->m_soupMessage = adoptGRef(soup_request_http_get_message(SOUP_REQUEST_HTTP(d->m_soupRequest.get())));
690     if (!d->m_soupMessage)
691         return false;
692
693     SoupMessage* soupMessage = d->m_soupMessage.get();
694     request.updateSoupMessage(soupMessage);
695
696     g_object_set_data(G_OBJECT(soupMessage), "handle", handle);
697     if (!handle->shouldContentSniff())
698         soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_SNIFFER);
699
700     String firstPartyString = request.firstPartyForCookies().string();
701     if (!firstPartyString.isEmpty()) {
702         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
703         soup_message_set_first_party(soupMessage, firstParty.get());
704     }
705
706     FormData* httpBody = request.httpBody();
707     CString contentType = request.httpContentType().utf8().data();
708     if (httpBody && !httpBody->isEmpty() && !addFormElementsToSoupMessage(soupMessage, contentType.data(), httpBody, d->m_bodySize)) {
709         // We failed to prepare the body data, so just fail this load.
710         d->m_soupMessage.clear();
711         return false;
712     }
713
714     // Make sure we have an Accept header for subresources; some sites
715     // want this to serve some of their subresources
716     if (!soup_message_headers_get_one(soupMessage->request_headers, "Accept"))
717         soup_message_headers_append(soupMessage->request_headers, "Accept", "*/*");
718
719     // In the case of XHR .send() and .send("") explicitly tell libsoup to send a zero content-lenght header
720     // for consistency with other backends (e.g. Chromium's) and other UA implementations like FF. It's done
721     // in the backend here instead of in XHR code since in XHR CORS checking prevents us from this kind of
722     // late header manipulation.
723     if ((request.httpMethod() == "POST" || request.httpMethod() == "PUT")
724         && (!request.httpBody() || request.httpBody()->isEmpty()))
725         soup_message_headers_set_content_length(soupMessage->request_headers, 0);
726
727     g_signal_connect(d->m_soupMessage.get(), "got-headers", G_CALLBACK(gotHeadersCallback), handle);
728     g_signal_connect(d->m_soupMessage.get(), "restarted", G_CALLBACK(restartedCallback), handle);
729     g_signal_connect(d->m_soupMessage.get(), "wrote-body-data", G_CALLBACK(wroteBodyDataCallback), handle);
730
731 #if ENABLE(WEB_TIMING)
732     d->m_response.setResourceLoadTiming(ResourceLoadTiming::create());
733     g_signal_connect(d->m_soupMessage.get(), "network-event", G_CALLBACK(networkEventCallback), handle);
734     g_signal_connect(d->m_soupMessage.get(), "wrote-body", G_CALLBACK(wroteBodyCallback), handle);
735 #endif
736
737     return true;
738 }
739
740 static bool createSoupRequestAndMessageForHandle(ResourceHandle* handle, bool isHTTPFamilyRequest)
741 {
742     ResourceHandleInternal* d = handle->getInternal();
743     SoupRequester* requester = SOUP_REQUESTER(soup_session_get_feature(d->soupSession(), SOUP_TYPE_REQUESTER));
744
745     GOwnPtr<GError> error;
746     const ResourceRequest& request = handle->firstRequest();
747     d->m_soupRequest = adoptGRef(soup_requester_request(requester, request.urlStringForSoup().utf8().data(), &error.outPtr()));
748     if (error) {
749         d->m_soupRequest.clear();
750         return false;
751     }
752
753     // Non-HTTP family requests do not need a soupMessage, as it's callbacks really only apply to HTTP.
754     if (isHTTPFamilyRequest && !createSoupMessageForHandleAndRequest(handle, request)) {
755         d->m_soupRequest.clear();
756         return false;
757     }
758
759     return true;
760 }
761
762 bool ResourceHandle::start(NetworkingContext* context)
763 {
764     ASSERT(!d->m_soupMessage);
765
766     // The frame could be null if the ResourceHandle is not associated to any
767     // Frame, e.g. if we are downloading a file.
768     // If the frame is not null but the page is null this must be an attempted
769     // load from an unload handler, so let's just block it.
770     // If both the frame and the page are not null the context is valid.
771     if (context && !context->isValid())
772         return false;
773
774     // Used to set the keep track of custom SoupSessions for ports that support it (EFL).
775     d->m_context = context;
776
777     if (!(d->m_user.isEmpty() || d->m_pass.isEmpty())) {
778         // If credentials were specified for this request, add them to the url,
779         // so that they will be passed to NetworkRequest.
780         KURL urlWithCredentials(firstRequest().url());
781         urlWithCredentials.setUser(d->m_user);
782         urlWithCredentials.setPass(d->m_pass);
783         d->m_firstRequest.setURL(urlWithCredentials);
784     }
785
786     // Only allow the POST and GET methods for non-HTTP requests.
787     const ResourceRequest& request = firstRequest();
788     bool isHTTPFamilyRequest = request.url().protocolIsInHTTPFamily();
789     if (!isHTTPFamilyRequest && request.httpMethod() != "GET" && request.httpMethod() != "POST") {
790         this->scheduleFailure(InvalidURLFailure); // Error must not be reported immediately
791         return true;
792     }
793
794     if (!createSoupRequestAndMessageForHandle(this, isHTTPFamilyRequest)) {
795         this->scheduleFailure(InvalidURLFailure); // Error must not be reported immediately
796         return true;
797     }
798
799     setSoupRequestInitiaingPageIDFromNetworkingContext(d->m_soupRequest.get(), context);
800
801     // Send the request only if it's not been explicitly deferred.
802     if (!d->m_defersLoading)
803         sendPendingRequest();
804
805     return true;
806 }
807
808 void ResourceHandle::sendPendingRequest()
809 {
810 #if ENABLE(WEB_TIMING)
811     if (d->m_response.resourceLoadTiming())
812         d->m_response.resourceLoadTiming()->requestTime = monotonicallyIncreasingTime();
813 #endif
814
815     if (d->m_firstRequest.timeoutInterval() > 0) {
816         // soup_add_timeout returns a GSource* whose only reference is owned by
817         // the context. We need to have our own reference to it, hence not using adoptRef.
818         d->m_timeoutSource = soup_add_timeout(g_main_context_get_thread_default(),
819             d->m_firstRequest.timeoutInterval() * 1000, requestTimeoutCallback, this);
820     }
821
822     // Balanced by a deref() in cleanupSoupRequestOperation, which should always run.
823     ref();
824
825     d->m_cancellable = adoptGRef(g_cancellable_new());
826     soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, this);
827 }
828
829 void ResourceHandle::cancel()
830 {
831     d->m_cancelled = true;
832     if (d->m_soupMessage)
833         soup_session_cancel_message(d->soupSession(), d->m_soupMessage.get(), SOUP_STATUS_CANCELLED);
834     else if (d->m_cancellable)
835         g_cancellable_cancel(d->m_cancellable.get());
836 }
837
838 void ResourceHandle::setHostAllowsAnyHTTPSCertificate(const String& host)
839 {
840     allowsAnyHTTPSCertificateHosts().add(host.lower());
841 }
842
843 void ResourceHandle::setClientCertificate(const String& host, GTlsCertificate* certificate)
844 {
845     clientCertificates().add(host.lower(), HostTLSCertificateSet()).iterator->value.add(certificate);
846 }
847
848 void ResourceHandle::setIgnoreSSLErrors(bool ignoreSSLErrors)
849 {
850     gIgnoreSSLErrors = ignoreSSLErrors;
851 }
852
853 #if PLATFORM(GTK)
854 void ResourceHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge)
855 {
856     if (client())
857         client()->didReceiveAuthenticationChallenge(this, challenge);
858 }
859
860 void ResourceHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge&)
861 {
862 }
863 #endif
864
865 static bool waitingToSendRequest(ResourceHandle* handle)
866 {
867     // We need to check for d->m_soupRequest because the request may have raised a failure
868     // (for example invalid URLs). We cannot  simply check for d->m_scheduledFailure because
869     // it's cleared as soon as the failure event is fired.
870     return handle->getInternal()->m_soupRequest && !handle->getInternal()->m_cancellable;
871 }
872
873 void ResourceHandle::platformSetDefersLoading(bool defersLoading)
874 {
875     if (d->m_cancelled)
876         return;
877
878     // Except when canceling a possible timeout timer, we only need to take action here to UN-defer loading.
879     if (defersLoading) {
880         if (d->m_timeoutSource) {
881             g_source_destroy(d->m_timeoutSource.get());
882             d->m_timeoutSource.clear();
883         }
884         return;
885     }
886
887     if (waitingToSendRequest(this)) {
888         sendPendingRequest();
889         return;
890     }
891
892     if (d->m_deferredResult) {
893         GRefPtr<GAsyncResult> asyncResult = adoptGRef(d->m_deferredResult.leakRef());
894
895         if (d->m_inputStream)
896             readCallback(G_OBJECT(d->m_inputStream.get()), asyncResult.get(), this);
897         else
898             sendRequestCallback(G_OBJECT(d->m_soupRequest.get()), asyncResult.get(), this);
899     }
900 }
901
902 bool ResourceHandle::loadsBlocked()
903 {
904     return false;
905 }
906
907 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
908 {
909     // Not having this function means that we'll ask the user about re-posting a form
910     // even when we go back to a page that's still in the cache.
911     notImplemented();
912     return false;
913 }
914
915 void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data)
916 {
917 #if ENABLE(BLOB)
918     if (request.url().protocolIs("blob")) {
919         blobRegistry().loadResourceSynchronously(request, error, response, data);
920         return;
921     }
922 #endif
923  
924     ASSERT(!loadingSynchronousRequest);
925     if (loadingSynchronousRequest) // In practice this cannot happen, but if for some reason it does,
926         return;                    // we want to avoid accidentally going into an infinite loop of requests.
927
928     WebCoreSynchronousLoader syncLoader(error, response, sessionFromContext(context), data);
929     RefPtr<ResourceHandle> handle = create(context, request, &syncLoader, false /*defersLoading*/, false /*shouldContentSniff*/);
930     if (!handle)
931         return;
932
933     // If the request has already failed, do not run the main loop, or else we'll block indefinitely.
934     if (handle->d->m_scheduledFailureType != NoFailure)
935         return;
936
937     syncLoader.run();
938 }
939
940 static void closeCallback(GObject*, GAsyncResult* res, gpointer data)
941 {
942     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
943     ResourceHandleInternal* d = handle->getInternal();
944
945     g_input_stream_close_finish(d->m_inputStream.get(), res, 0);
946
947     ResourceHandleClient* client = handle->client();
948     if (client && loadingSynchronousRequest)
949         client->didFinishLoading(handle.get(), 0);
950
951     cleanupSoupRequestOperation(handle.get());
952 }
953
954 static void readCallback(GObject*, GAsyncResult* asyncResult, gpointer data)
955 {
956     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
957
958     ResourceHandleInternal* d = handle->getInternal();
959     ResourceHandleClient* client = handle->client();
960
961     if (d->m_cancelled || !client) {
962         cleanupSoupRequestOperation(handle.get());
963         return;
964     }
965
966     if (d->m_defersLoading) {
967         d->m_deferredResult = asyncResult;
968         return;
969     }
970
971     GOwnPtr<GError> error;
972     gssize bytesRead = g_input_stream_read_finish(d->m_inputStream.get(), asyncResult, &error.outPtr());
973     if (error) {
974         client->didFail(handle.get(), ResourceError::genericIOError(error.get(), d->m_soupRequest.get()));
975         cleanupSoupRequestOperation(handle.get());
976         return;
977     }
978
979     if (!bytesRead) {
980         // We inform WebCore of load completion now instead of waiting for the input
981         // stream to close because the input stream is closed asynchronously. If this
982         // is a synchronous request, we wait until the closeCallback, because we don't
983         // want to halt the internal main loop before the input stream closes.
984         if (client && !loadingSynchronousRequest) {
985             client->didFinishLoading(handle.get(), 0);
986             handle->setClient(0); // Unset the client so that we do not try to access th
987                                   // client in the closeCallback.
988         }
989         g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT, 0, closeCallback, handle.get());
990         return;
991     }
992
993     // It's mandatory to have sent a response before sending data
994     ASSERT(!d->m_response.isNull());
995
996     client->didReceiveData(handle.get(), d->m_buffer, bytesRead, bytesRead);
997
998     // didReceiveData may cancel the load, which may release the last reference.
999     if (d->m_cancelled || !client) {
1000         cleanupSoupRequestOperation(handle.get());
1001         return;
1002     }
1003
1004     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
1005                               d->m_cancellable.get(), readCallback, handle.get());
1006 }
1007
1008 static gboolean requestTimeoutCallback(gpointer data)
1009 {
1010     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
1011     handle->client()->didFail(handle.get(), ResourceError::timeoutError(handle->getInternal()->m_firstRequest.url().string()));
1012     handle->cancel();
1013
1014     return FALSE;
1015 }
1016
1017 #if PLATFORM(GTK)
1018 static void authenicateCallback(SoupSession* session, SoupMessage* soupMessage, SoupAuth* soupAuth, gboolean retrying)
1019 {
1020     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(G_OBJECT(soupMessage), "handle"));
1021     if (!handle)
1022         return;
1023
1024     // We don't need to pass a client here, because GTK+ does not yet use the AuthenticationClient to
1025     // respond to the AuthenticationChallenge -- instead dealing with the Soup objects directly.
1026     handle->didReceiveAuthenticationChallenge(AuthenticationChallenge(session, soupMessage, soupAuth, retrying, 0));
1027 }
1028 #endif
1029
1030 SoupSession* ResourceHandle::defaultSession()
1031 {
1032     static SoupSession* session = 0;
1033     // Values taken from http://www.browserscope.org/  following
1034     // the rule "Do What Every Other Modern Browser Is Doing". They seem
1035     // to significantly improve page loading time compared to soup's
1036     // default values.
1037     static const int maxConnections = 35;
1038     static const int maxConnectionsPerHost = 6;
1039
1040     if (!session) {
1041         session = soup_session_async_new();
1042         g_object_set(session,
1043                      SOUP_SESSION_MAX_CONNS, maxConnections,
1044                      SOUP_SESSION_MAX_CONNS_PER_HOST, maxConnectionsPerHost,
1045                      SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_DECODER,
1046                      SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_CONTENT_SNIFFER,
1047                      SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_PROXY_RESOLVER_DEFAULT,
1048                      SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
1049                      NULL);
1050 #if PLATFORM(GTK)
1051         g_signal_connect(session, "authenticate", G_CALLBACK(authenicateCallback), 0);
1052 #endif
1053
1054 #if ENABLE(WEB_TIMING)
1055         g_signal_connect(session, "request-started", G_CALLBACK(requestStartedCallback), 0);
1056 #endif
1057     }
1058
1059     return session;
1060 }
1061
1062 uint64_t ResourceHandle::getSoupRequestInitiaingPageID(SoupRequest* request)
1063 {
1064     uint64_t* initiatingPageIDPtr = static_cast<uint64_t*>(g_object_get_data(G_OBJECT(request), gSoupRequestInitiaingPageIDKey));
1065     return initiatingPageIDPtr ? *initiatingPageIDPtr : 0;
1066 }
1067
1068 }