OSDN Git Service

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