2011-01-29 Dan Winship <danw@gnome.org>
[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 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 "Base64.h"
31 #include "CString.h"
32 #include "ChromeClient.h"
33 #include "CookieJarSoup.h"
34 #include "CachedResourceLoader.h"
35 #include "FileSystem.h"
36 #include "Frame.h"
37 #include "GOwnPtrSoup.h"
38 #include "HTTPParsers.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 "soup-request-http.h"
49 #include "TextEncoding.h"
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <gio/gio.h>
53 #include <glib.h>
54 #include <libsoup/soup.h>
55 #include <sys/stat.h>
56 #include <sys/types.h>
57 #include <unistd.h>
58
59 namespace WebCore {
60
61 #define READ_BUFFER_SIZE 8192
62
63 class WebCoreSynchronousLoader : public ResourceHandleClient {
64     WTF_MAKE_NONCOPYABLE(WebCoreSynchronousLoader);
65 public:
66     WebCoreSynchronousLoader(ResourceError&, ResourceResponse &, Vector<char>&);
67     ~WebCoreSynchronousLoader();
68
69     virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
70     virtual void didReceiveData(ResourceHandle*, const char*, int, int lengthReceived);
71     virtual void didFinishLoading(ResourceHandle*, double /*finishTime*/);
72     virtual void didFail(ResourceHandle*, const ResourceError&);
73
74     void run();
75
76 private:
77     ResourceError& m_error;
78     ResourceResponse& m_response;
79     Vector<char>& m_data;
80     bool m_finished;
81     GMainLoop* m_mainLoop;
82 };
83
84 WebCoreSynchronousLoader::WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, Vector<char>& data)
85     : m_error(error)
86     , m_response(response)
87     , m_data(data)
88     , m_finished(false)
89 {
90     m_mainLoop = g_main_loop_new(0, false);
91 }
92
93 WebCoreSynchronousLoader::~WebCoreSynchronousLoader()
94 {
95     g_main_loop_unref(m_mainLoop);
96 }
97
98 void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
99 {
100     m_response = response;
101 }
102
103 void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
104 {
105     m_data.append(data, length);
106 }
107
108 void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*, double)
109 {
110     g_main_loop_quit(m_mainLoop);
111     m_finished = true;
112 }
113
114 void WebCoreSynchronousLoader::didFail(ResourceHandle* handle, const ResourceError& error)
115 {
116     m_error = error;
117     didFinishLoading(handle, 0);
118 }
119
120 void WebCoreSynchronousLoader::run()
121 {
122     if (!m_finished)
123         g_main_loop_run(m_mainLoop);
124 }
125
126 static void cleanupSoupRequestOperation(ResourceHandle*, bool isDestroying);
127 static void sendRequestCallback(GObject*, GAsyncResult*, gpointer);
128 static void readCallback(GObject*, GAsyncResult*, gpointer);
129 static void closeCallback(GObject*, GAsyncResult*, gpointer);
130 static bool startGio(ResourceHandle*, KURL);
131
132 ResourceHandleInternal::~ResourceHandleInternal()
133 {
134     if (m_soupRequest)
135         g_object_set_data(G_OBJECT(m_soupRequest.get()), "webkit-resource", 0);
136
137     if (m_idleHandler) {
138         g_source_remove(m_idleHandler);
139         m_idleHandler = 0;
140     }
141 }
142
143 ResourceHandle::~ResourceHandle()
144 {
145     cleanupSoupRequestOperation(this, true);
146 }
147
148 void ResourceHandle::prepareForURL(const KURL &url)
149 {
150     GOwnPtr<SoupURI> soupURI(soup_uri_new(url.prettyURL().utf8().data()));
151     if (!soupURI)
152         return;
153     soup_session_prepare_for_uri(ResourceHandle::defaultSession(), soupURI.get());
154 }
155
156 // All other kinds of redirections, except for the *304* status code
157 // (SOUP_STATUS_NOT_MODIFIED) which needs to be fed into WebCore, will be
158 // handled by soup directly.
159 static gboolean statusWillBeHandledBySoup(guint statusCode)
160 {
161     if (SOUP_STATUS_IS_TRANSPORT_ERROR(statusCode)
162         || (SOUP_STATUS_IS_REDIRECTION(statusCode) && (statusCode != SOUP_STATUS_NOT_MODIFIED))
163         || (statusCode == SOUP_STATUS_UNAUTHORIZED))
164         return true;
165
166     return false;
167 }
168
169 static void fillResponseFromMessage(SoupMessage* msg, ResourceResponse* response)
170 {
171     response->updateFromSoupMessage(msg);
172 }
173
174 // Called each time the message is going to be sent again except the first time.
175 // It's used mostly to let webkit know about redirects.
176 static void restartedCallback(SoupMessage* msg, gpointer data)
177 {
178     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
179     if (!handle)
180         return;
181     ResourceHandleInternal* d = handle->getInternal();
182     if (d->m_cancelled)
183         return;
184
185     GOwnPtr<char> uri(soup_uri_to_string(soup_message_get_uri(msg), false));
186     String location = String::fromUTF8(uri.get());
187     KURL newURL = KURL(handle->firstRequest().url(), location);
188
189     ResourceRequest request = handle->firstRequest();
190     ResourceResponse response;
191     request.setURL(newURL);
192     request.setHTTPMethod(msg->method);
193     fillResponseFromMessage(msg, &response);
194
195     // Should not set Referer after a redirect from a secure resource to non-secure one.
196     if (!request.url().protocolIs("https") && protocolIs(request.httpReferrer(), "https")) {
197         request.clearHTTPReferrer();
198         soup_message_headers_remove(msg->request_headers, "Referer");
199     }
200
201     if (d->client())
202         d->client()->willSendRequest(handle, request, response);
203
204     if (d->m_cancelled)
205         return;
206
207     // Update the first party in case the base URL changed with the redirect
208     String firstPartyString = request.firstPartyForCookies().string();
209     if (!firstPartyString.isEmpty()) {
210         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
211         soup_message_set_first_party(d->m_soupMessage.get(), firstParty.get());
212     }
213 }
214
215 static void contentSniffedCallback(SoupMessage*, const char*, GHashTable*, gpointer);
216
217 static void gotHeadersCallback(SoupMessage* msg, gpointer data)
218 {
219     // For 401, we will accumulate the resource body, and only use it
220     // in case authentication with the soup feature doesn't happen.
221     // For 302 we accumulate the body too because it could be used by
222     // some servers to redirect with a clunky http-equiv=REFRESH
223     if (statusWillBeHandledBySoup(msg->status_code)) {
224         soup_message_body_set_accumulate(msg->response_body, TRUE);
225         return;
226     }
227
228     // For all the other responses, we handle each chunk ourselves,
229     // and we don't need msg->response_body to contain all of the data
230     // we got, when we finish downloading.
231     soup_message_body_set_accumulate(msg->response_body, FALSE);
232
233     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
234
235     // The content-sniffed callback will handle the response if WebCore
236     // require us to sniff.
237     if (!handle || statusWillBeHandledBySoup(msg->status_code))
238         return;
239
240     if (handle->shouldContentSniff()) {
241         // Avoid MIME type sniffing if the response comes back as 304 Not Modified.
242         if (msg->status_code == SOUP_STATUS_NOT_MODIFIED) {
243             soup_message_disable_feature(msg, SOUP_TYPE_CONTENT_SNIFFER);
244             g_signal_handlers_disconnect_by_func(msg, reinterpret_cast<gpointer>(contentSniffedCallback), handle.get());
245         } else
246             return;
247     }
248
249     ResourceHandleInternal* d = handle->getInternal();
250     if (d->m_cancelled)
251         return;
252     ResourceHandleClient* client = handle->client();
253     if (!client)
254         return;
255
256     ASSERT(d->m_response.isNull());
257
258     fillResponseFromMessage(msg, &d->m_response);
259     client->didReceiveResponse(handle.get(), d->m_response);
260 }
261
262 // This callback will not be called if the content sniffer is disabled in startHttp.
263 static void contentSniffedCallback(SoupMessage* msg, const char* sniffedType, GHashTable *params, gpointer data)
264 {
265     if (sniffedType) {
266         const char* officialType = soup_message_headers_get_one(msg->response_headers, "Content-Type");
267
268         if (!officialType || strcmp(officialType, sniffedType))
269             soup_message_headers_set_content_type(msg->response_headers, sniffedType, params);
270     }
271
272     if (statusWillBeHandledBySoup(msg->status_code))
273         return;
274
275     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
276     if (!handle)
277         return;
278     ResourceHandleInternal* d = handle->getInternal();
279     if (d->m_cancelled)
280         return;
281     ResourceHandleClient* client = handle->client();
282     if (!client)
283         return;
284
285     ASSERT(d->m_response.isNull());
286
287     fillResponseFromMessage(msg, &d->m_response);
288     client->didReceiveResponse(handle.get(), d->m_response);
289 }
290
291 static void gotChunkCallback(SoupMessage* msg, SoupBuffer* chunk, gpointer data)
292 {
293     if (statusWillBeHandledBySoup(msg->status_code))
294         return;
295
296     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
297     if (!handle)
298         return;
299     ResourceHandleInternal* d = handle->getInternal();
300     if (d->m_cancelled)
301         return;
302     ResourceHandleClient* client = handle->client();
303     if (!client)
304         return;
305
306     ASSERT(!d->m_response.isNull());
307
308     client->didReceiveData(handle.get(), chunk->data, chunk->length, false);
309 }
310
311 static gboolean parseDataUrl(gpointer callbackData)
312 {
313     ResourceHandle* handle = static_cast<ResourceHandle*>(callbackData);
314     ResourceHandleClient* client = handle->client();
315     ResourceHandleInternal* d = handle->getInternal();
316     if (d->m_cancelled)
317         return false;
318
319     d->m_idleHandler = 0;
320
321     ASSERT(client);
322     if (!client)
323         return false;
324
325     String url = handle->firstRequest().url().string();
326     ASSERT(url.startsWith("data:", false));
327
328     int index = url.find(',');
329     if (index == -1) {
330         client->cannotShowURL(handle);
331         return false;
332     }
333
334     String mediaType = url.substring(5, index - 5);
335
336     bool isBase64 = mediaType.endsWith(";base64", false);
337     if (isBase64)
338         mediaType = mediaType.left(mediaType.length() - 7);
339
340     if (mediaType.isEmpty())
341         mediaType = "text/plain;charset=US-ASCII";
342
343     String mimeType = extractMIMETypeFromMediaType(mediaType);
344     String charset = extractCharsetFromMediaType(mediaType);
345
346     ASSERT(d->m_response.isNull());
347
348     d->m_response.setURL(handle->firstRequest().url());
349     d->m_response.setMimeType(mimeType);
350
351     // For non base64 encoded data we have to convert to UTF-16 early
352     // due to limitations in KURL
353     d->m_response.setTextEncodingName(isBase64 ? charset : "UTF-16");
354     client->didReceiveResponse(handle, d->m_response);
355
356     // The load may be cancelled, and the client may be destroyed
357     // by any of the client reporting calls, so we check, and bail
358     // out in either of those cases.
359     if (d->m_cancelled || !handle->client())
360         return false;
361
362     SoupSession* session = handle->defaultSession();
363     GOwnPtr<GError> error;
364     d->m_soupRequest = adoptGRef(webkit_soup_requester_request(d->m_requester.get(), handle->firstRequest().url().string().utf8().data(), session, &error.outPtr()));
365     if (error) {
366         d->m_soupRequest = 0;
367         client->didFinishLoading(handle, 0);
368         return false;
369     }
370
371     d->m_inputStream = adoptGRef(webkit_soup_request_send(d->m_soupRequest.get(), 0, &error.outPtr()));
372     if (error) {
373         d->m_inputStream = 0;
374         client->didFinishLoading(handle, 0);
375         return false;
376     }
377
378     d->m_buffer = static_cast<char*>(g_slice_alloc0(READ_BUFFER_SIZE));
379     d->m_total = 0;
380
381     g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", handle);
382     // balanced by a deref() in cleanupSoupRequestOperation, which should always run
383     handle->ref();
384
385     d->m_cancellable = adoptGRef(g_cancellable_new());
386     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
387                               d->m_cancellable.get(), readCallback, GINT_TO_POINTER(!isBase64));
388
389     return false;
390 }
391
392 static bool startData(ResourceHandle* handle, String urlString)
393 {
394     ASSERT(handle);
395
396     ResourceHandleInternal* d = handle->getInternal();
397
398     // If parseDataUrl is called synchronously the job is not yet effectively started
399     // and webkit won't never know that the data has been parsed even didFinishLoading is called.
400     d->m_idleHandler = g_timeout_add(0, parseDataUrl, handle);
401     return true;
402 }
403
404 static SoupSession* createSoupSession()
405 {
406     return soup_session_async_new();
407 }
408
409 // Values taken from http://stevesouders.com/ua/index.php following
410 // the rule "Do What Every Other Modern Browser Is Doing". They seem
411 // to significantly improve page loading time compared to soup's
412 // default values.
413 #define MAX_CONNECTIONS          60
414 #define MAX_CONNECTIONS_PER_HOST 6
415
416 static void ensureSessionIsInitialized(SoupSession* session)
417 {
418     if (g_object_get_data(G_OBJECT(session), "webkit-init"))
419         return;
420
421     SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
422     if (!jar)
423         soup_session_add_feature(session, SOUP_SESSION_FEATURE(defaultCookieJar()));
424     else
425         setDefaultCookieJar(jar);
426
427     if (!soup_session_get_feature(session, SOUP_TYPE_LOGGER) && LogNetwork.state == WTFLogChannelOn) {
428         SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(SOUP_LOGGER_LOG_BODY), -1);
429         soup_logger_attach(logger, session);
430         g_object_unref(logger);
431     }
432
433     g_object_set(session,
434                  SOUP_SESSION_MAX_CONNS, MAX_CONNECTIONS,
435                  SOUP_SESSION_MAX_CONNS_PER_HOST, MAX_CONNECTIONS_PER_HOST,
436                  NULL);
437
438     g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
439 }
440
441 static void cleanupSoupRequestOperation(ResourceHandle* handle, bool isDestroying = false)
442 {
443     ResourceHandleInternal* d = handle->getInternal();
444
445     if (d->m_soupRequest) {
446         g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", 0);
447         d->m_soupRequest.clear();
448     }
449
450     if (d->m_inputStream) {
451         g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", 0);
452         d->m_inputStream.clear();
453     }
454
455     d->m_cancellable.clear();
456
457     if (d->m_soupMessage) {
458         g_signal_handlers_disconnect_matched(d->m_soupMessage.get(), G_SIGNAL_MATCH_DATA,
459                                              0, 0, 0, 0, handle);
460         d->m_soupMessage.clear();
461     }
462
463     if (d->m_buffer) {
464         g_slice_free1(READ_BUFFER_SIZE, d->m_buffer);
465         d->m_buffer = 0;
466     }
467
468     if (!isDestroying)
469         handle->deref();
470 }
471
472 static void sendRequestCallback(GObject* source, GAsyncResult* res, gpointer userData)
473 {
474     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
475     if (!handle)
476         return;
477
478     ResourceHandleInternal* d = handle->getInternal();
479     ResourceHandleClient* client = handle->client();
480
481     if (d->m_gotChunkHandler) {
482         // No need to call gotChunkHandler anymore. Received data will
483         // be reported by readCallback
484         if (g_signal_handler_is_connected(d->m_soupMessage.get(), d->m_gotChunkHandler))
485             g_signal_handler_disconnect(d->m_soupMessage.get(), d->m_gotChunkHandler);
486     }
487
488     if (d->m_cancelled || !client) {
489         cleanupSoupRequestOperation(handle.get());
490         return;
491     }
492
493     GOwnPtr<GError> error;
494     GInputStream* in = webkit_soup_request_send_finish(d->m_soupRequest.get(), res, &error.outPtr());
495
496     if (error) {
497         SoupMessage* soupMsg = d->m_soupMessage.get();
498         gboolean isTransportError = d->m_soupMessage && SOUP_STATUS_IS_TRANSPORT_ERROR(soupMsg->status_code);
499
500         if (isTransportError || (error->domain == G_IO_ERROR)) {
501             SoupURI* uri = webkit_soup_request_get_uri(d->m_soupRequest.get());
502             GOwnPtr<char> uriStr(soup_uri_to_string(uri, false));
503             gint errorCode = isTransportError ? static_cast<gint>(soupMsg->status_code) : error->code;
504             const gchar* errorMsg = isTransportError ? soupMsg->reason_phrase : error->message;
505             const gchar* quarkStr = isTransportError ? g_quark_to_string(SOUP_HTTP_ERROR) : g_quark_to_string(G_IO_ERROR);
506             ResourceError resourceError(quarkStr, errorCode, uriStr.get(), String::fromUTF8(errorMsg));
507
508             cleanupSoupRequestOperation(handle.get());
509             client->didFail(handle.get(), resourceError);
510             return;
511         }
512
513         if (d->m_soupMessage && statusWillBeHandledBySoup(d->m_soupMessage->status_code)) {
514             ASSERT(d->m_response.isNull());
515
516             fillResponseFromMessage(soupMsg, &d->m_response);
517             client->didReceiveResponse(handle.get(), d->m_response);
518
519             // WebCore might have cancelled the job in the while. We
520             // must check for response_body->length and not
521             // response_body->data as libsoup always creates the
522             // SoupBuffer for the body even if the length is 0
523             if (!d->m_cancelled && soupMsg->response_body->length)
524                 client->didReceiveData(handle.get(), soupMsg->response_body->data, soupMsg->response_body->length, true);
525         }
526
527         // didReceiveData above might have cancelled it
528         if (d->m_cancelled || !client) {
529             cleanupSoupRequestOperation(handle.get());
530             return;
531         }
532
533         client->didFinishLoading(handle.get(), 0);
534         return;
535     }
536
537     if (d->m_cancelled) {
538         cleanupSoupRequestOperation(handle.get());
539         return;
540     }
541
542     d->m_inputStream = adoptGRef(in);
543     d->m_buffer = static_cast<char*>(g_slice_alloc0(READ_BUFFER_SIZE));
544     d->m_total = 0;
545
546     // readCallback needs it
547     g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", handle.get());
548
549     // Ensure a response is sent for any protocols that don't explicitly support responses
550     // through got-headers signal or content sniffing.
551     // (e.g. file and GIO based protocol).
552     if (!handle->shouldContentSniff() && d->m_response.isNull()) {
553         d->m_response.setURL(handle->firstRequest().url());
554         d->m_response.setMimeType(webkit_soup_request_get_content_type(d->m_soupRequest.get()));
555         d->m_response.setExpectedContentLength(webkit_soup_request_get_content_length(d->m_soupRequest.get()));
556         client->didReceiveResponse(handle.get(), d->m_response);
557
558         if (d->m_cancelled) {
559             cleanupSoupRequestOperation(handle.get());
560             return;
561         }
562     }
563
564     if (d->m_defersLoading)
565          soup_session_pause_message(handle->defaultSession(), d->m_soupMessage.get());
566
567     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE,
568                               G_PRIORITY_DEFAULT, d->m_cancellable.get(), readCallback, 0);
569 }
570
571 static bool startHttp(ResourceHandle* handle)
572 {
573     ASSERT(handle);
574
575     SoupSession* session = handle->defaultSession();
576     ensureSessionIsInitialized(session);
577
578     ResourceHandleInternal* d = handle->getInternal();
579
580     ResourceRequest request(handle->firstRequest());
581     KURL url(request.url());
582     url.removeFragmentIdentifier();
583     request.setURL(url);
584
585     GOwnPtr<GError> error;
586     d->m_soupRequest = adoptGRef(webkit_soup_requester_request(d->m_requester.get(), url.string().utf8().data(), session, &error.outPtr()));
587     if (error) {
588         d->m_soupRequest = 0;
589         return false;
590     }
591
592     g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", handle);
593
594     d->m_soupMessage = adoptGRef(webkit_soup_request_http_get_message(WEBKIT_SOUP_REQUEST_HTTP(d->m_soupRequest.get())));
595     if (!d->m_soupMessage)
596         return false;
597
598     SoupMessage* soupMessage = d->m_soupMessage.get();
599     request.updateSoupMessage(soupMessage);
600
601     if (!handle->shouldContentSniff())
602         soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_SNIFFER);
603     else
604         g_signal_connect(soupMessage, "content-sniffed", G_CALLBACK(contentSniffedCallback), handle);
605
606     g_signal_connect(soupMessage, "restarted", G_CALLBACK(restartedCallback), handle);
607     g_signal_connect(soupMessage, "got-headers", G_CALLBACK(gotHeadersCallback), handle);
608     d->m_gotChunkHandler = g_signal_connect(soupMessage, "got-chunk", G_CALLBACK(gotChunkCallback), handle);
609
610     String firstPartyString = request.firstPartyForCookies().string();
611     if (!firstPartyString.isEmpty()) {
612         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
613         soup_message_set_first_party(soupMessage, firstParty.get());
614     }
615
616     FormData* httpBody = d->m_firstRequest.httpBody();
617     if (httpBody && !httpBody->isEmpty()) {
618         size_t numElements = httpBody->elements().size();
619
620         // handle the most common case (i.e. no file upload)
621         if (numElements < 2) {
622             Vector<char> body;
623             httpBody->flatten(body);
624             soup_message_set_request(soupMessage, d->m_firstRequest.httpContentType().utf8().data(),
625                                      SOUP_MEMORY_COPY, body.data(), body.size());
626         } else {
627             /*
628              * we have more than one element to upload, and some may
629              * be (big) files, which we will want to mmap instead of
630              * copying into memory; TODO: support upload of non-local
631              * (think sftp://) files by using GIO?
632              */
633             soup_message_body_set_accumulate(soupMessage->request_body, FALSE);
634             for (size_t i = 0; i < numElements; i++) {
635                 const FormDataElement& element = httpBody->elements()[i];
636
637                 if (element.m_type == FormDataElement::data)
638                     soup_message_body_append(soupMessage->request_body, SOUP_MEMORY_TEMPORARY, element.m_data.data(), element.m_data.size());
639                 else {
640                     /*
641                      * mapping for uploaded files code inspired by technique used in
642                      * libsoup's simple-httpd test
643                      */
644                     GOwnPtr<GError> error;
645                     CString fileName = fileSystemRepresentation(element.m_filename);
646                     GMappedFile* fileMapping = g_mapped_file_new(fileName.data(), false, &error.outPtr());
647
648                     if (error) {
649                         g_signal_handlers_disconnect_matched(soupMessage, G_SIGNAL_MATCH_DATA,
650                                                              0, 0, 0, 0, handle);
651                         d->m_soupMessage.clear();
652
653                         return false;
654                     }
655
656                     SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping),
657                                                                         g_mapped_file_get_length(fileMapping),
658                                                                         fileMapping,
659                                                                         reinterpret_cast<GDestroyNotify>(g_mapped_file_unref));
660                     soup_message_body_append_buffer(soupMessage->request_body, soupBuffer);
661                     soup_buffer_free(soupBuffer);
662                 }
663             }
664         }
665     }
666
667     // balanced by a deref() in cleanupSoupRequestOperation, which should always run
668     handle->ref();
669
670     // Make sure we have an Accept header for subresources; some sites
671     // want this to serve some of their subresources
672     if (!soup_message_headers_get_one(soupMessage->request_headers, "Accept"))
673         soup_message_headers_append(soupMessage->request_headers, "Accept", "*/*");
674
675     // Send the request only if it's not been explicitely deferred.
676     if (!d->m_defersLoading) {
677         d->m_cancellable = adoptGRef(g_cancellable_new());
678         webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
679     }
680
681     return true;
682 }
683
684 bool ResourceHandle::start(NetworkingContext* context)
685 {
686     ASSERT(!d->m_soupMessage);
687
688     // The frame could be null if the ResourceHandle is not associated to any
689     // Frame, e.g. if we are downloading a file.
690     // If the frame is not null but the page is null this must be an attempted
691     // load from an unload handler, so let's just block it.
692     // If both the frame and the page are not null the context is valid.
693     if (context && !context->isValid())
694         return false;
695
696     if (!(d->m_user.isEmpty() || d->m_pass.isEmpty())) {
697         // If credentials were specified for this request, add them to the url,
698         // so that they will be passed to NetworkRequest.
699         KURL urlWithCredentials(firstRequest().url());
700         urlWithCredentials.setUser(d->m_user);
701         urlWithCredentials.setPass(d->m_pass);
702         d->m_firstRequest.setURL(urlWithCredentials);
703     }
704
705     KURL url = firstRequest().url();
706     String urlString = url.string();
707     String protocol = url.protocol();
708
709     // Used to set the authentication dialog toplevel; may be NULL
710     d->m_context = context;
711
712     if (equalIgnoringCase(protocol, "data"))
713         return startData(this, urlString);
714
715     if (equalIgnoringCase(protocol, "http") || equalIgnoringCase(protocol, "https")) {
716         if (startHttp(this))
717             return true;
718     }
719
720     if (equalIgnoringCase(protocol, "file") || equalIgnoringCase(protocol, "ftp") || equalIgnoringCase(protocol, "ftps")) {
721         // FIXME: should we be doing any other protocols here?
722         if (startGio(this, url))
723             return true;
724     }
725
726     // Error must not be reported immediately
727     this->scheduleFailure(InvalidURLFailure);
728
729     return true;
730 }
731
732 void ResourceHandle::cancel()
733 {
734     d->m_cancelled = true;
735     if (d->m_soupMessage)
736         soup_session_cancel_message(defaultSession(), d->m_soupMessage.get(), SOUP_STATUS_CANCELLED);
737     else if (d->m_cancellable)
738         g_cancellable_cancel(d->m_cancellable.get());
739 }
740
741 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
742 {
743     ASSERT_NOT_REACHED();
744     return 0;
745 }
746
747 bool ResourceHandle::supportsBufferedData()
748 {
749     return false;
750 }
751
752 void ResourceHandle::platformSetDefersLoading(bool defersLoading)
753 {
754     // Initial implementation of this method was required for bug #44157.
755
756     if (d->m_cancelled)
757         return;
758
759     if (!defersLoading && !d->m_cancellable && d->m_soupRequest.get()) {
760         d->m_cancellable = adoptGRef(g_cancellable_new());
761         webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
762         return;
763     }
764
765     // Only supported for http(s) transfers. Something similar would
766     // probably be needed for data transfers done with GIO.
767     if (!d->m_soupMessage)
768         return;
769
770     if (defersLoading)
771         soup_session_pause_message(defaultSession(), d->m_soupMessage.get());
772     else
773         soup_session_unpause_message(defaultSession(), d->m_soupMessage.get());
774 }
775
776 bool ResourceHandle::loadsBlocked()
777 {
778     return false;
779 }
780
781 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
782 {
783     // Not having this function means that we'll ask the user about re-posting a form
784     // even when we go back to a page that's still in the cache.
785     notImplemented();
786     return false;
787 }
788
789 void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data)
790 {
791     WebCoreSynchronousLoader syncLoader(error, response, data);
792     // FIXME: we should use the ResourceHandle::create method here,
793     // but it makes us timeout in a couple of tests. See
794     // https://bugs.webkit.org/show_bug.cgi?id=41823
795     RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(request, &syncLoader, false /*defersLoading*/, false /*shouldContentSniff*/));
796     handle->start(context);
797
798     // If the request has already failed, do not run the main loop, or else we'll block indefinitely.
799     if (handle->d->m_scheduledFailureType != NoFailure)
800         return;
801
802     syncLoader.run();
803 }
804
805 static void closeCallback(GObject* source, GAsyncResult* res, gpointer)
806 {
807     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
808     if (!handle)
809         return;
810
811     ResourceHandleInternal* d = handle->getInternal();
812     g_input_stream_close_finish(d->m_inputStream.get(), res, 0);
813     cleanupSoupRequestOperation(handle.get());
814 }
815
816 static void readCallback(GObject* source, GAsyncResult* asyncResult, gpointer data)
817 {
818     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
819     if (!handle)
820         return;
821
822     bool convertToUTF16 = static_cast<bool>(data);
823     ResourceHandleInternal* d = handle->getInternal();
824     ResourceHandleClient* client = handle->client();
825
826     if (d->m_cancelled || !client) {
827         cleanupSoupRequestOperation(handle.get());
828         return;
829     }
830
831     GOwnPtr<GError> error;
832
833     gssize bytesRead = g_input_stream_read_finish(d->m_inputStream.get(), asyncResult, &error.outPtr());
834     if (error) {
835         SoupURI* uri = webkit_soup_request_get_uri(d->m_soupRequest.get());
836         GOwnPtr<char> uriStr(soup_uri_to_string(uri, false));
837         ResourceError resourceError(g_quark_to_string(G_IO_ERROR), error->code, uriStr.get(),
838                                     error ? String::fromUTF8(error->message) : String());
839         cleanupSoupRequestOperation(handle.get());
840         client->didFail(handle.get(), resourceError);
841         return;
842     }
843
844     if (!bytesRead) {
845         // Finish the load. We do not wait for the stream to
846         // close. Instead we better notify WebCore as soon as possible
847         client->didFinishLoading(handle.get(), 0);
848
849         g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT,
850                                    0, closeCallback, 0);
851         return;
852     }
853
854     // It's mandatory to have sent a response before sending data
855     ASSERT(!d->m_response.isNull());
856
857     d->m_total += bytesRead;
858     if (G_LIKELY(!convertToUTF16))
859         client->didReceiveData(handle.get(), d->m_buffer, bytesRead, d->m_total);
860     else {
861         // We have to convert it to UTF-16 due to limitations in KURL
862         String data = String::fromUTF8(d->m_buffer, bytesRead);
863         client->didReceiveData(handle.get(), reinterpret_cast<const char*>(data.characters()), data.length() * sizeof(UChar), 0);
864     }
865
866     // didReceiveData may cancel the load, which may release the last reference.
867     if (d->m_cancelled || !client) {
868         cleanupSoupRequestOperation(handle.get());
869         return;
870     }
871
872     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
873                               d->m_cancellable.get(), readCallback, data);
874 }
875
876 static bool startGio(ResourceHandle* handle, KURL url)
877 {
878     ASSERT(handle);
879
880     if (handle->firstRequest().httpMethod() != "GET" && handle->firstRequest().httpMethod() != "POST")
881         return false;
882
883     SoupSession* session = handle->defaultSession();
884     ResourceHandleInternal* d = handle->getInternal();
885
886     // GIO doesn't know how to handle refs and queries, so remove them
887     // TODO: use KURL.fileSystemPath after KURLGtk and FileSystemGtk are
888     // using GIO internally, and providing URIs instead of file paths
889     url.removeFragmentIdentifier();
890     url.setQuery(String());
891     url.removePort();
892     CString urlStr = url.string().utf8();
893
894     GOwnPtr<GError> error;
895     d->m_soupRequest = adoptGRef(webkit_soup_requester_request(d->m_requester.get(), urlStr.data(), session, &error.outPtr()));
896     if (error) {
897         d->m_soupRequest = 0;
898         return false;
899     }
900
901     g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", handle);
902
903     // balanced by a deref() in cleanupSoupRequestOperation, which should always run
904     handle->ref();
905
906     d->m_cancellable = adoptGRef(g_cancellable_new());
907     webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
908
909     return true;
910 }
911
912 SoupSession* ResourceHandle::defaultSession()
913 {
914     static SoupSession* session = createSoupSession();
915
916     return session;
917 }
918
919 }