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