2009-03-30 Gustavo Noronha Silva <gustavo.noronha@collabora.co.uk>
[WebKit-https.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 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  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25
26 #include "config.h"
27 #include "ResourceHandle.h"
28
29 #include "Base64.h"
30 #include "CookieJarSoup.h"
31 #include "ChromeClient.h"
32 #include "CString.h"
33 #include "DocLoader.h"
34 #include "FileSystem.h"
35 #include "Frame.h"
36 #include "HTTPParsers.h"
37 #include "Logging.h"
38 #include "MIMETypeRegistry.h"
39 #include "NotImplemented.h"
40 #include "Page.h"
41 #include "ResourceError.h"
42 #include "ResourceHandleClient.h"
43 #include "ResourceHandleInternal.h"
44 #include "ResourceResponse.h"
45 #include "TextEncoding.h"
46
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <gio/gio.h>
50 #include <gtk/gtk.h>
51 #include <libsoup/soup.h>
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55
56 #if PLATFORM(GTK)
57 #define USE_GLIB_BASE64
58 #endif
59
60 namespace WebCore {
61
62 class WebCoreSynchronousLoader : public ResourceHandleClient, Noncopyable {
63 public:
64     WebCoreSynchronousLoader(ResourceError&, ResourceResponse &, Vector<char>&);
65     ~WebCoreSynchronousLoader();
66
67     virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
68     virtual void didReceiveData(ResourceHandle*, const char*, int, int lengthReceived);
69     virtual void didFinishLoading(ResourceHandle*);
70     virtual void didFail(ResourceHandle*, const ResourceError&);
71
72     void run();
73
74 private:
75     ResourceError& m_error;
76     ResourceResponse& m_response;
77     Vector<char>& m_data;
78     bool m_finished;
79     GMainLoop* m_mainLoop;
80 };
81
82 WebCoreSynchronousLoader::WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, Vector<char>& data)
83     : m_error(error)
84     , m_response(response)
85     , m_data(data)
86     , m_finished(false)
87 {
88     m_mainLoop = g_main_loop_new(0, false);
89 }
90
91 WebCoreSynchronousLoader::~WebCoreSynchronousLoader()
92 {
93     g_main_loop_unref(m_mainLoop);
94 }
95
96 void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
97 {
98     m_response = response;
99 }
100
101 void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
102 {
103     m_data.append(data, length);
104 }
105
106 void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*)
107 {
108     g_main_loop_quit(m_mainLoop);
109     m_finished = true;
110 }
111
112 void WebCoreSynchronousLoader::didFail(ResourceHandle* handle, const ResourceError& error)
113 {
114     m_error = error;
115     didFinishLoading(handle);
116 }
117
118 void WebCoreSynchronousLoader::run()
119 {
120     if (!m_finished)
121         g_main_loop_run(m_mainLoop);
122 }
123
124 enum
125 {
126     ERROR_TRANSPORT,
127     ERROR_UNKNOWN_PROTOCOL,
128     ERROR_BAD_NON_HTTP_METHOD,
129     ERROR_UNABLE_TO_OPEN_FILE,
130 };
131
132 static void cleanupGioOperation(ResourceHandleInternal* handle);
133
134 ResourceHandleInternal::~ResourceHandleInternal()
135 {
136     if (m_msg) {
137         g_object_unref(m_msg);
138         m_msg = 0;
139     }
140
141     cleanupGioOperation(this);
142
143     if (m_idleHandler) {
144         g_source_remove(m_idleHandler);
145         m_idleHandler = 0;
146     }
147 }
148
149 ResourceHandle::~ResourceHandle()
150 {
151 }
152
153 static void fillResponseFromMessage(SoupMessage* msg, ResourceResponse* response)
154 {
155     SoupMessageHeadersIter iter;
156     const char* name = 0;
157     const char* value = 0;
158     soup_message_headers_iter_init(&iter, msg->response_headers);
159     while (soup_message_headers_iter_next(&iter, &name, &value))
160         response->setHTTPHeaderField(name, value);
161
162     GHashTable* contentTypeParameters = 0;
163     String contentType = soup_message_headers_get_content_type(msg->response_headers, &contentTypeParameters);
164
165     // When the server sends multiple Content-Type headers, soup will
166     // give us their values concatenated with commas as a separator;
167     // we need to handle this and use only one value. We use the first
168     // value, and add all the parameters, afterwards, if any.
169     Vector<String> contentTypes;
170     contentType.split(',', true, contentTypes);
171     contentType = contentTypes[0];
172
173     if (contentTypeParameters) {
174         GString* parametersString = g_string_new(0);
175         GHashTableIter hashTableIter;
176         gpointer hashKey;
177         gpointer hashValue;
178
179         g_hash_table_iter_init(&hashTableIter, contentTypeParameters);
180         while (g_hash_table_iter_next(&hashTableIter, &hashKey, &hashValue)) {
181             g_string_append(parametersString, "; ");
182             soup_header_g_string_append_param(parametersString, static_cast<char*>(hashKey), static_cast<char*>(hashValue));
183         }
184         contentType += String(parametersString->str);
185
186         g_string_free(parametersString, true);
187         g_hash_table_destroy(contentTypeParameters);
188     }
189
190     response->setMimeType(extractMIMETypeFromMediaType(contentType));
191
192     char* uri = soup_uri_to_string(soup_message_get_uri(msg), false);
193     response->setURL(KURL(KURL(), uri));
194     g_free(uri);
195     response->setTextEncodingName(extractCharsetFromMediaType(contentType));
196     response->setExpectedContentLength(soup_message_headers_get_content_length(msg->response_headers));
197     response->setHTTPStatusCode(msg->status_code);
198     response->setHTTPStatusText(msg->reason_phrase);
199     response->setSuggestedFilename(filenameFromHTTPContentDisposition(response->httpHeaderField("Content-Disposition")));
200 }
201
202 // Called each time the message is going to be sent again except the first time.
203 // It's used mostly to let webkit know about redirects.
204 static void restartedCallback(SoupMessage* msg, gpointer data)
205 {
206     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
207     if (!handle)
208         return;
209     ResourceHandleInternal* d = handle->getInternal();
210     if (d->m_cancelled)
211         return;
212
213     char* uri = soup_uri_to_string(soup_message_get_uri(msg), false);
214     String location = String(uri);
215     g_free(uri);
216     KURL newURL = KURL(handle->request().url(), location);
217
218     // FIXME: This is needed because some servers use broken URIs in
219     // their Location header, when redirecting, such as URIs with
220     // white spaces instead of %20; this should be fixed in soup, in
221     // the future, and this work-around removed.
222     // See http://bugzilla.gnome.org/show_bug.cgi?id=575378.
223     SoupURI* soup_uri = soup_uri_new(newURL.string().utf8().data());
224     soup_message_set_uri(msg, soup_uri);
225     soup_uri_free(soup_uri);
226
227     ResourceRequest request = handle->request();
228     ResourceResponse response;
229     request.setURL(newURL);
230     fillResponseFromMessage(msg, &response);
231     if (d->client())
232         d->client()->willSendRequest(handle, request, response);
233
234     d->m_request.setURL(newURL);
235 }
236
237 static void gotHeadersCallback(SoupMessage* msg, gpointer data)
238 {
239     // For 401, we will accumulate the resource body, and only use it
240     // in case authentication with the soup feature doesn't happen
241     if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
242         soup_message_body_set_accumulate(msg->response_body, TRUE);
243         return;
244     }
245
246     // For all the other responses, we handle each chunk ourselves,
247     // and we don't need msg->response_body to contain all of the data
248     // we got, when we finish downloading.
249     soup_message_body_set_accumulate(msg->response_body, FALSE);
250
251     // The 304 status code (SOUP_STATUS_NOT_MODIFIED) needs to be fed
252     // into WebCore, as opposed to other kinds of redirections, which
253     // are handled by soup directly, so we special-case it here and in
254     // gotChunk.
255     if (SOUP_STATUS_IS_TRANSPORT_ERROR(msg->status_code)
256         || (SOUP_STATUS_IS_REDIRECTION(msg->status_code) && (msg->status_code != SOUP_STATUS_NOT_MODIFIED)))
257         return;
258
259     // We still don't know anything about Content-Type, so we will try
260     // sniffing the contents of the file, and then report that we got
261     // headers; we will not do content sniffing for 304 responses,
262     // though, since they do not have a body.
263     if ((msg->status_code != SOUP_STATUS_NOT_MODIFIED)
264         && !soup_message_headers_get_content_type(msg->response_headers, NULL))
265         return;
266
267     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
268     if (!handle)
269         return;
270     ResourceHandleInternal* d = handle->getInternal();
271     if (d->m_cancelled)
272         return;
273     ResourceHandleClient* client = handle->client();
274     if (!client)
275         return;
276
277     fillResponseFromMessage(msg, &d->m_response);
278     client->didReceiveResponse(handle, d->m_response);
279     d->m_reportedHeaders = true;
280 }
281
282 static void gotChunkCallback(SoupMessage* msg, SoupBuffer* chunk, gpointer data)
283 {
284     if (SOUP_STATUS_IS_TRANSPORT_ERROR(msg->status_code)
285         || (SOUP_STATUS_IS_REDIRECTION(msg->status_code) && (msg->status_code != SOUP_STATUS_NOT_MODIFIED))
286         || (msg->status_code == SOUP_STATUS_UNAUTHORIZED))
287         return;
288
289     ResourceHandle* handle = static_cast<ResourceHandle*>(data);
290     if (!handle)
291         return;
292     ResourceHandleInternal* d = handle->getInternal();
293     if (d->m_cancelled)
294         return;
295     ResourceHandleClient* client = handle->client();
296     if (!client)
297         return;
298
299     if (!d->m_reportedHeaders) {
300         gboolean uncertain;
301         char* contentType = g_content_type_guess(d->m_request.url().lastPathComponent().utf8().data(), reinterpret_cast<const guchar*>(chunk->data), chunk->length, &uncertain);
302         soup_message_headers_set_content_type(msg->response_headers, contentType, NULL);
303         g_free(contentType);
304
305         fillResponseFromMessage(msg, &d->m_response);
306         client->didReceiveResponse(handle, d->m_response);
307         d->m_reportedHeaders = true;
308     }
309
310     client->didReceiveData(handle, chunk->data, chunk->length, false);
311 }
312
313 // Called at the end of the message, with all the necessary about the last informations.
314 // Doesn't get called for redirects.
315 static void finishedCallback(SoupSession *session, SoupMessage* msg, gpointer data)
316 {
317     RefPtr<ResourceHandle>handle = adoptRef(static_cast<ResourceHandle*>(data));
318     // TODO: maybe we should run this code even if there's no client?
319     if (!handle)
320         return;
321
322     ResourceHandleInternal* d = handle->getInternal();
323
324     ResourceHandleClient* client = handle->client();
325     if (!client)
326         return;
327
328     if (d->m_cancelled)
329         return;
330
331     if (SOUP_STATUS_IS_TRANSPORT_ERROR(msg->status_code)) {
332         char* uri = soup_uri_to_string(soup_message_get_uri(msg), false);
333         ResourceError error("webkit-network-error", ERROR_TRANSPORT, uri, String::fromUTF8(msg->reason_phrase));
334         g_free(uri);
335         client->didFail(handle.get(), error);
336         return;
337     }
338
339     if (msg->status_code == SOUP_STATUS_UNAUTHORIZED) {
340         fillResponseFromMessage(msg, &d->m_response);
341         client->didReceiveResponse(handle.get(), d->m_response);
342
343         // WebCore might have cancelled the job in the while
344         if (d->m_cancelled)
345             return;
346
347         if (msg->response_body->data)
348             client->didReceiveData(handle.get(), msg->response_body->data, msg->response_body->length, true);
349     }
350
351     client->didFinishLoading(handle.get());
352 }
353
354 // parseDataUrl() is taken from the CURL http backend.
355 static gboolean parseDataUrl(gpointer callback_data)
356 {
357     ResourceHandle* handle = static_cast<ResourceHandle*>(callback_data);
358     ResourceHandleClient* client = handle->client();
359
360     handle->getInternal()->m_idleHandler = 0;
361
362     ASSERT(client);
363     if (!client)
364         return false;
365
366     String url = handle->request().url().string();
367     ASSERT(url.startsWith("data:", false));
368
369     int index = url.find(',');
370     if (index == -1) {
371         client->cannotShowURL(handle);
372         return false;
373     }
374
375     String mediaType = url.substring(5, index - 5);
376     String data = url.substring(index + 1);
377
378     bool isBase64 = mediaType.endsWith(";base64", false);
379     if (isBase64)
380         mediaType = mediaType.left(mediaType.length() - 7);
381
382     if (mediaType.isEmpty())
383         mediaType = "text/plain;charset=US-ASCII";
384
385     String mimeType = extractMIMETypeFromMediaType(mediaType);
386     String charset = extractCharsetFromMediaType(mediaType);
387
388     ResourceResponse response;
389     response.setMimeType(mimeType);
390
391     if (isBase64) {
392         data = decodeURLEscapeSequences(data);
393         response.setTextEncodingName(charset);
394         client->didReceiveResponse(handle, response);
395
396         // Use the GLib Base64 if available, since WebCore's decoder isn't
397         // general-purpose and fails on Acid3 test 97 (whitespace).
398 #ifdef USE_GLIB_BASE64
399         size_t outLength = 0;
400         char* outData = 0;
401         outData = reinterpret_cast<char*>(g_base64_decode(data.utf8().data(), &outLength));
402         if (outData && outLength > 0)
403             client->didReceiveData(handle, outData, outLength, 0);
404         g_free(outData);
405 #else
406         Vector<char> out;
407         if (base64Decode(data.latin1().data(), data.latin1().length(), out) && out.size() > 0)
408             client->didReceiveData(handle, out.data(), out.size(), 0);
409 #endif
410     } else {
411         // We have to convert to UTF-16 early due to limitations in KURL
412         data = decodeURLEscapeSequences(data, TextEncoding(charset));
413         response.setTextEncodingName("UTF-16");
414         client->didReceiveResponse(handle, response);
415         if (data.length() > 0)
416             client->didReceiveData(handle, reinterpret_cast<const char*>(data.characters()), data.length() * sizeof(UChar), 0);
417     }
418
419     client->didFinishLoading(handle);
420
421     return false;
422 }
423
424 bool ResourceHandle::startData(String urlString)
425 {
426     ResourceHandleInternal* d = this->getInternal();
427
428     // If parseDataUrl is called synchronously the job is not yet effectively started
429     // and webkit won't never know that the data has been parsed even didFinishLoading is called.
430     d->m_idleHandler = g_idle_add(parseDataUrl, this);
431     return true;
432 }
433
434 static SoupSession* createSoupSession()
435 {
436     return soup_session_async_new();
437 }
438
439 static void ensureSessionIsInitialized(SoupSession* session)
440 {
441     if (g_object_get_data(G_OBJECT(session), "webkit-init"))
442         return;
443
444     SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
445     if (!jar)
446         soup_session_add_feature(session, SOUP_SESSION_FEATURE(defaultCookieJar()));
447     else
448         setDefaultCookieJar(jar);
449
450     if (!soup_session_get_feature(session, SOUP_TYPE_LOGGER) && LogNetwork.state == WTFLogChannelOn) {
451         SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(SOUP_LOGGER_LOG_BODY), -1);
452         soup_logger_attach(logger, session);
453         g_object_unref(logger);
454     }
455
456     g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
457 }
458
459 bool ResourceHandle::startHttp(String urlString)
460 {
461     SoupSession* session = defaultSession();
462     ensureSessionIsInitialized(session);
463
464     SoupMessage* msg;
465     msg = soup_message_new(request().httpMethod().utf8().data(), urlString.utf8().data());
466     g_signal_connect(msg, "restarted", G_CALLBACK(restartedCallback), this);
467     g_signal_connect(msg, "got-headers", G_CALLBACK(gotHeadersCallback), this);
468     g_signal_connect(msg, "got-chunk", G_CALLBACK(gotChunkCallback), this);
469
470     g_object_set_data(G_OBJECT(msg), "resourceHandle", reinterpret_cast<void*>(this));
471
472     HTTPHeaderMap customHeaders = d->m_request.httpHeaderFields();
473     if (!customHeaders.isEmpty()) {
474         HTTPHeaderMap::const_iterator end = customHeaders.end();
475         for (HTTPHeaderMap::const_iterator it = customHeaders.begin(); it != end; ++it)
476             soup_message_headers_append(msg->request_headers, it->first.string().utf8().data(), it->second.utf8().data());
477     }
478
479     FormData* httpBody = d->m_request.httpBody();
480     if (httpBody && !httpBody->isEmpty()) {
481         size_t numElements = httpBody->elements().size();
482
483         // handle the most common case (i.e. no file upload)
484         if (numElements < 2) {
485             Vector<char> body;
486             httpBody->flatten(body);
487             soup_message_set_request(msg, d->m_request.httpContentType().utf8().data(),
488                                      SOUP_MEMORY_COPY, body.data(), body.size());
489         } else {
490             /*
491              * we have more than one element to upload, and some may
492              * be (big) files, which we will want to mmap instead of
493              * copying into memory; TODO: support upload of non-local
494              * (think sftp://) files by using GIO?
495              */
496             soup_message_body_set_accumulate(msg->request_body, FALSE);
497             for (size_t i = 0; i < numElements; i++) {
498                 const FormDataElement& element = httpBody->elements()[i];
499
500                 if (element.m_type == FormDataElement::data)
501                     soup_message_body_append(msg->request_body, SOUP_MEMORY_TEMPORARY, element.m_data.data(), element.m_data.size());
502                 else {
503                     /*
504                      * mapping for uploaded files code inspired by technique used in
505                      * libsoup's simple-httpd test
506                      */
507                     GError* error = 0;
508                     gchar* fileName = filenameFromString(element.m_filename);
509                     GMappedFile* fileMapping = g_mapped_file_new(fileName, false, &error);
510
511                     g_free(fileName);
512
513                     if (error) {
514                         ResourceError resourceError("webkit-network-error", ERROR_UNABLE_TO_OPEN_FILE, urlString, error->message);
515                         g_error_free(error);
516
517                         d->client()->didFail(this, resourceError);
518
519                         g_object_unref(msg);
520                         return false;
521                     }
522
523                     SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping),
524                                                                         g_mapped_file_get_length(fileMapping),
525                                                                         fileMapping, reinterpret_cast<GDestroyNotify>(g_mapped_file_free));
526                     soup_message_body_append_buffer(msg->request_body, soupBuffer);
527                     soup_buffer_free(soupBuffer);
528                 }
529             }
530         }
531     }
532
533     d->m_msg = static_cast<SoupMessage*>(g_object_ref(msg));
534     // balanced by a deref() in finishedCallback, which should always run
535     ref();
536
537     soup_session_queue_message(session, d->m_msg, finishedCallback, this);
538
539     return true;
540 }
541
542 static gboolean reportUnknownProtocolError(gpointer callback_data)
543 {
544     ResourceHandle* handle = static_cast<ResourceHandle*>(callback_data);
545     ResourceHandleInternal* d = handle->getInternal();
546     ResourceHandleClient* client = handle->client();
547
548     if (d->m_cancelled || !client) {
549         handle->deref();
550         return false;
551     }
552
553     KURL url = handle->request().url();
554     ResourceError error("webkit-network-error", ERROR_UNKNOWN_PROTOCOL, url.string(), url.protocol());
555     client->didFail(handle, error);
556
557     handle->deref();
558     return false;
559 }
560
561 bool ResourceHandle::start(Frame* frame)
562 {
563     ASSERT(!d->m_msg);
564
565
566     // The frame could be null if the ResourceHandle is not associated to any
567     // Frame, e.g. if we are downloading a file.
568     // If the frame is not null but the page is null this must be an attempted
569     // load from an onUnload handler, so let's just block it.
570     if (frame && !frame->page())
571         return false;
572
573     KURL url = request().url();
574     String urlString = url.string();
575     String protocol = url.protocol();
576
577     // Used to set the authentication dialog toplevel; may be NULL
578     d->m_frame = frame;
579
580     if (equalIgnoringCase(protocol, "data"))
581         return startData(urlString);
582
583     if ((equalIgnoringCase(protocol, "http") || equalIgnoringCase(protocol, "https")) && SOUP_URI_VALID_FOR_HTTP(soup_uri_new(urlString.utf8().data())))
584         return startHttp(urlString);
585
586     if (equalIgnoringCase(protocol, "file") || equalIgnoringCase(protocol, "ftp") || equalIgnoringCase(protocol, "ftps"))
587         // FIXME: should we be doing any other protocols here?
588         return startGio(url);
589
590     // Error must not be reported immediately, but through an idle function.
591     // Despite error, we should return true so a proper handle is created,
592     // to which this failure can be reported.
593     ref();
594     d->m_idleHandler = g_idle_add(reportUnknownProtocolError, this);
595     return true;
596 }
597
598 void ResourceHandle::cancel()
599 {
600     d->m_cancelled = true;
601     if (d->m_msg)
602         soup_session_cancel_message(defaultSession(), d->m_msg, SOUP_STATUS_CANCELLED);
603     else if (d->m_cancellable)
604         g_cancellable_cancel(d->m_cancellable);
605 }
606
607 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
608 {
609     ASSERT_NOT_REACHED();
610     return 0;
611 }
612
613 bool ResourceHandle::supportsBufferedData()
614 {
615     return false;
616 }
617
618 void ResourceHandle::setDefersLoading(bool defers)
619 {
620     d->m_defersLoading = defers;
621     notImplemented();
622 }
623
624 bool ResourceHandle::loadsBlocked()
625 {
626     return false;
627 }
628
629 bool ResourceHandle::willLoadFromCache(ResourceRequest&)
630 {
631     // Not having this function means that we'll ask the user about re-posting a form
632     // even when we go back to a page that's still in the cache.
633     notImplemented();
634     return false;
635 }
636
637 void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data, Frame* frame)
638 {
639     WebCoreSynchronousLoader syncLoader(error, response, data);
640     ResourceHandle handle(request, &syncLoader, true, false, true);
641
642     handle.start(frame);
643     syncLoader.run();
644 }
645
646 // GIO-based loader
647
648 static inline ResourceError networkErrorForFile(GFile* file, GError* error)
649 {
650     // FIXME: Map gio errors to a more detailed error code when we have it in WebKit.
651     gchar* uri = g_file_get_uri(file);
652     ResourceError resourceError("webkit-network-error", ERROR_TRANSPORT, uri, error ? String::fromUTF8(error->message) : String());
653     g_free(uri);
654     return resourceError;
655 }
656
657 static void cleanupGioOperation(ResourceHandleInternal* d)
658 {
659     if (d->m_gfile) {
660         g_object_set_data(G_OBJECT(d->m_gfile), "webkit-resource", 0);
661         g_object_unref(d->m_gfile);
662         d->m_gfile = 0;
663     }
664
665     if (d->m_cancellable) {
666         g_object_unref(d->m_cancellable);
667         d->m_cancellable = 0;
668     }
669
670     if (d->m_inputStream) {
671         g_object_set_data(G_OBJECT(d->m_inputStream), "webkit-resource", 0);
672         g_object_unref(d->m_inputStream);
673         d->m_inputStream = 0;
674     }
675
676     if (d->m_buffer) {
677         g_free(d->m_buffer);
678         d->m_buffer = 0;
679     }
680 }
681
682 static void closeCallback(GObject* source, GAsyncResult* res, gpointer)
683 {
684     ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
685     if (!handle)
686         return;
687
688     ResourceHandleInternal* d = handle->getInternal();
689     ResourceHandleClient* client = handle->client();
690
691     g_input_stream_close_finish(d->m_inputStream, res, 0);
692     cleanupGioOperation(d);
693     client->didFinishLoading(handle);
694 }
695
696 static void readCallback(GObject* source, GAsyncResult* res, gpointer)
697 {
698     // didReceiveData may cancel the load, which may release the last reference.
699     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
700     if (!handle)
701         return;
702
703     ResourceHandleInternal* d = handle->getInternal();
704     ResourceHandleClient* client = handle->client();
705
706     if (d->m_cancelled || !client) {
707         cleanupGioOperation(d);
708         return;
709     }
710
711     GError *error = 0;
712
713     gssize bytesRead = g_input_stream_read_finish(d->m_inputStream, res, &error);
714     if (error) {
715         ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
716         g_error_free(error);
717         cleanupGioOperation(d);
718         client->didFail(handle.get(), resourceError);
719         return;
720     }
721
722     if (!bytesRead) {
723         g_input_stream_close_async(d->m_inputStream, G_PRIORITY_DEFAULT,
724                                    0, closeCallback, 0);
725         return;
726     }
727
728     d->m_total += bytesRead;
729     client->didReceiveData(handle.get(), d->m_buffer, bytesRead, d->m_total);
730
731     if (d->m_cancelled) {
732         cleanupGioOperation(d);
733         return;
734     }
735
736     g_input_stream_read_async(d->m_inputStream, d->m_buffer, d->m_bufferSize,
737                               G_PRIORITY_DEFAULT, d->m_cancellable,
738                               readCallback, 0);
739 }
740
741 static void openCallback(GObject* source, GAsyncResult* res, gpointer)
742 {
743     ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
744     if (!handle)
745         return;
746
747     ResourceHandleInternal* d = handle->getInternal();
748     ResourceHandleClient* client = handle->client();
749
750     if (d->m_cancelled || !client) {
751         cleanupGioOperation(d);
752         return;
753     }
754
755     GError *error = 0;
756     GFileInputStream* in = g_file_read_finish(G_FILE(source), res, &error);
757     if (error) {
758         ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
759         g_error_free(error);
760         cleanupGioOperation(d);
761         client->didFail(handle, resourceError);
762         return;
763     }
764
765     d->m_inputStream = G_INPUT_STREAM(in);
766     d->m_bufferSize = 8192;
767     d->m_buffer = static_cast<char*>(g_malloc(d->m_bufferSize));
768     d->m_total = 0;
769     g_object_set_data(G_OBJECT(d->m_inputStream), "webkit-resource", handle);
770     g_input_stream_read_async(d->m_inputStream, d->m_buffer, d->m_bufferSize,
771                               G_PRIORITY_DEFAULT, d->m_cancellable,
772                               readCallback, 0);
773 }
774
775 static void queryInfoCallback(GObject* source, GAsyncResult* res, gpointer)
776 {
777     ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
778     if (!handle)
779         return;
780
781     ResourceHandleInternal* d = handle->getInternal();
782     ResourceHandleClient* client = handle->client();
783
784     if (d->m_cancelled) {
785         cleanupGioOperation(d);
786         return;
787     }
788
789     ResourceResponse response;
790
791     char* uri = g_file_get_uri(d->m_gfile);
792     response.setURL(KURL(KURL(), uri));
793     g_free(uri);
794
795     GError *error = 0;
796     GFileInfo* info = g_file_query_info_finish(d->m_gfile, res, &error);
797
798     if (error) {
799         // FIXME: to be able to handle ftp URIs properly, we must
800         // check if the error is G_IO_ERROR_NOT_MOUNTED, and if so,
801         // call g_file_mount_enclosing_volume() to mount the ftp
802         // server (and then keep track of the fact that we mounted it,
803         // and set a timeout to unmount it later after it's been idle
804         // for a while).
805
806         ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
807         g_error_free(error);
808         cleanupGioOperation(d);
809         client->didFail(handle, resourceError);
810         return;
811     }
812
813     if (g_file_info_get_file_type(info) != G_FILE_TYPE_REGULAR) {
814         // FIXME: what if the URI points to a directory? Should we
815         // generate a listing? How? What do other backends do here?
816
817         ResourceError resourceError = networkErrorForFile(d->m_gfile, 0);
818         cleanupGioOperation(d);
819         client->didFail(handle, resourceError);
820         return;
821     }
822
823     response.setMimeType(g_file_info_get_content_type(info));
824     response.setExpectedContentLength(g_file_info_get_size(info));
825
826     GTimeVal tv;
827     g_file_info_get_modification_time(info, &tv);
828     response.setLastModifiedDate(tv.tv_sec);
829
830     client->didReceiveResponse(handle, response);
831
832     g_file_read_async(d->m_gfile, G_PRIORITY_DEFAULT, d->m_cancellable,
833                       openCallback, 0);
834 }
835
836 bool ResourceHandle::startGio(KURL url)
837 {
838     if (request().httpMethod() != "GET" && request().httpMethod() != "POST") {
839         ResourceError error("webkit-network-error", ERROR_BAD_NON_HTTP_METHOD, url.string(), request().httpMethod());
840         d->client()->didFail(this, error);
841         return false;
842     }
843
844     // GIO doesn't know how to handle refs and queries, so remove them
845     // TODO: use KURL.fileSystemPath after KURLGtk and FileSystemGtk are
846     // using GIO internally, and providing URIs instead of file paths
847     url.removeRef();
848     url.setQuery(String());
849     url.setPort(0);
850
851     // we avoid the escaping for local files, because
852     // g_filename_from_uri (used internally by GFile) has problems
853     // decoding strings with arbitrary percent signs
854     if (url.isLocalFile())
855         d->m_gfile = g_file_new_for_path(url.prettyURL().utf8().data() + sizeof("file://") - 1);
856     else
857         d->m_gfile = g_file_new_for_uri(url.string().utf8().data());
858     g_object_set_data(G_OBJECT(d->m_gfile), "webkit-resource", this);
859     d->m_cancellable = g_cancellable_new();
860     g_file_query_info_async(d->m_gfile,
861                             G_FILE_ATTRIBUTE_STANDARD_TYPE ","
862                             G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
863                             G_FILE_ATTRIBUTE_STANDARD_SIZE,
864                             G_FILE_QUERY_INFO_NONE,
865                             G_PRIORITY_DEFAULT, d->m_cancellable,
866                             queryInfoCallback, 0);
867     return true;
868 }
869
870 SoupSession* ResourceHandle::defaultSession()
871 {
872     static SoupSession* session = createSoupSession();;
873
874     return session;
875 }
876
877 }
878