2011-01-03 Martin Robinson <mrobinson@igalia.com>
[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, 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     ASSERT(d->m_response.isNull());
249
250     fillResponseFromMessage(msg, &d->m_response);
251     client->didReceiveResponse(handle.get(), d->m_response);
252 }
253
254 // This callback will not be called if the content sniffer is disabled in startHttp.
255 static void contentSniffedCallback(SoupMessage* msg, const char* sniffedType, GHashTable *params, gpointer data)
256 {
257     if (sniffedType) {
258         const char* officialType = soup_message_headers_get_one(msg->response_headers, "Content-Type");
259
260         if (!officialType || strcmp(officialType, sniffedType))
261             soup_message_headers_set_content_type(msg->response_headers, sniffedType, params);
262     }
263
264     if (statusWillBeHandledBySoup(msg->status_code))
265         return;
266
267     RefPtr<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     ASSERT(d->m_response.isNull());
278
279     fillResponseFromMessage(msg, &d->m_response);
280     client->didReceiveResponse(handle.get(), d->m_response);
281 }
282
283 static void gotChunkCallback(SoupMessage* msg, SoupBuffer* chunk, gpointer data)
284 {
285     if (statusWillBeHandledBySoup(msg->status_code))
286         return;
287
288     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
289     if (!handle)
290         return;
291     ResourceHandleInternal* d = handle->getInternal();
292     if (d->m_cancelled)
293         return;
294     ResourceHandleClient* client = handle->client();
295     if (!client)
296         return;
297
298     ASSERT(!d->m_response.isNull());
299
300     client->didReceiveData(handle.get(), chunk->data, chunk->length, false);
301 }
302
303 static gboolean parseDataUrl(gpointer callbackData)
304 {
305     ResourceHandle* handle = static_cast<ResourceHandle*>(callbackData);
306     ResourceHandleClient* client = handle->client();
307     ResourceHandleInternal* d = handle->getInternal();
308     if (d->m_cancelled)
309         return false;
310
311     d->m_idleHandler = 0;
312
313     ASSERT(client);
314     if (!client)
315         return false;
316
317     String url = handle->firstRequest().url().string();
318     ASSERT(url.startsWith("data:", false));
319
320     int index = url.find(',');
321     if (index == -1) {
322         client->cannotShowURL(handle);
323         return false;
324     }
325
326     String mediaType = url.substring(5, index - 5);
327
328     bool isBase64 = mediaType.endsWith(";base64", false);
329     if (isBase64)
330         mediaType = mediaType.left(mediaType.length() - 7);
331
332     if (mediaType.isEmpty())
333         mediaType = "text/plain;charset=US-ASCII";
334
335     String mimeType = extractMIMETypeFromMediaType(mediaType);
336     String charset = extractCharsetFromMediaType(mediaType);
337
338     ASSERT(d->m_response.isNull());
339
340     d->m_response.setURL(handle->firstRequest().url());
341     d->m_response.setMimeType(mimeType);
342
343     // For non base64 encoded data we have to convert to UTF-16 early
344     // due to limitations in KURL
345     d->m_response.setTextEncodingName(isBase64 ? charset : "UTF-16");
346     client->didReceiveResponse(handle, d->m_response);
347
348     // The load may be cancelled, and the client may be destroyed
349     // by any of the client reporting calls, so we check, and bail
350     // out in either of those cases.
351     if (d->m_cancelled || !handle->client())
352         return false;
353
354     SoupSession* session = handle->defaultSession();
355     GOwnPtr<GError> error;
356     d->m_soupRequest = adoptGRef(webkit_soup_requester_request(d->m_requester.get(), handle->firstRequest().url().string().utf8().data(), session, &error.outPtr()));
357     if (error) {
358         d->m_soupRequest = 0;
359         client->didFinishLoading(handle, 0);
360         return false;
361     }
362
363     d->m_inputStream = adoptGRef(webkit_soup_request_send(d->m_soupRequest.get(), 0, &error.outPtr()));
364     if (error) {
365         d->m_inputStream = 0;
366         client->didFinishLoading(handle, 0);
367         return false;
368     }
369
370     d->m_buffer = static_cast<char*>(g_slice_alloc0(READ_BUFFER_SIZE));
371     d->m_total = 0;
372
373     g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", handle);
374     // balanced by a deref() in cleanupSoupRequestOperation, which should always run
375     handle->ref();
376
377     d->m_cancellable = adoptGRef(g_cancellable_new());
378     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
379                               d->m_cancellable.get(), readCallback, GINT_TO_POINTER(!isBase64));
380
381     return false;
382 }
383
384 static bool startData(ResourceHandle* handle, String urlString)
385 {
386     ASSERT(handle);
387
388     ResourceHandleInternal* d = handle->getInternal();
389
390     // If parseDataUrl is called synchronously the job is not yet effectively started
391     // and webkit won't never know that the data has been parsed even didFinishLoading is called.
392     d->m_idleHandler = g_timeout_add(0, parseDataUrl, handle);
393     return true;
394 }
395
396 static SoupSession* createSoupSession()
397 {
398     return soup_session_async_new();
399 }
400
401 // Values taken from http://stevesouders.com/ua/index.php following
402 // the rule "Do What Every Other Modern Browser Is Doing". They seem
403 // to significantly improve page loading time compared to soup's
404 // default values.
405 #define MAX_CONNECTIONS          60
406 #define MAX_CONNECTIONS_PER_HOST 6
407
408 static void ensureSessionIsInitialized(SoupSession* session)
409 {
410     if (g_object_get_data(G_OBJECT(session), "webkit-init"))
411         return;
412
413     SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
414     if (!jar)
415         soup_session_add_feature(session, SOUP_SESSION_FEATURE(defaultCookieJar()));
416     else
417         setDefaultCookieJar(jar);
418
419     if (!soup_session_get_feature(session, SOUP_TYPE_LOGGER) && LogNetwork.state == WTFLogChannelOn) {
420         SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(SOUP_LOGGER_LOG_BODY), -1);
421         soup_logger_attach(logger, session);
422         g_object_unref(logger);
423     }
424
425     g_object_set(session,
426                  SOUP_SESSION_MAX_CONNS, MAX_CONNECTIONS,
427                  SOUP_SESSION_MAX_CONNS_PER_HOST, MAX_CONNECTIONS_PER_HOST,
428                  NULL);
429
430     g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
431 }
432
433 static void cleanupSoupRequestOperation(ResourceHandle* handle, bool isDestroying = false)
434 {
435     ResourceHandleInternal* d = handle->getInternal();
436
437     if (d->m_soupRequest) {
438         g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", 0);
439         d->m_soupRequest.clear();
440     }
441
442     if (d->m_inputStream) {
443         g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", 0);
444         d->m_inputStream.clear();
445     }
446
447     d->m_cancellable.clear();
448
449     if (d->m_soupMessage) {
450         g_signal_handlers_disconnect_matched(d->m_soupMessage.get(), G_SIGNAL_MATCH_DATA,
451                                              0, 0, 0, 0, handle);
452         d->m_soupMessage.clear();
453     }
454
455     if (d->m_buffer) {
456         g_slice_free1(READ_BUFFER_SIZE, d->m_buffer);
457         d->m_buffer = 0;
458     }
459
460     if (!isDestroying)
461         handle->deref();
462 }
463
464 static void sendRequestCallback(GObject* source, GAsyncResult* res, gpointer userData)
465 {
466     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
467     if (!handle)
468         return;
469
470     ResourceHandleInternal* d = handle->getInternal();
471     ResourceHandleClient* client = handle->client();
472
473     if (d->m_gotChunkHandler) {
474         // No need to call gotChunkHandler anymore. Received data will
475         // be reported by readCallback
476         if (g_signal_handler_is_connected(d->m_soupMessage.get(), d->m_gotChunkHandler))
477             g_signal_handler_disconnect(d->m_soupMessage.get(), d->m_gotChunkHandler);
478     }
479
480     if (d->m_cancelled || !client) {
481         cleanupSoupRequestOperation(handle.get());
482         return;
483     }
484
485     GOwnPtr<GError> error;
486     GInputStream* in = webkit_soup_request_send_finish(d->m_soupRequest.get(), res, &error.outPtr());
487
488     if (error) {
489         SoupMessage* soupMsg = d->m_soupMessage.get();
490         gboolean isTransportError = d->m_soupMessage && SOUP_STATUS_IS_TRANSPORT_ERROR(soupMsg->status_code);
491
492         if (isTransportError || (error->domain == G_IO_ERROR)) {
493             SoupURI* uri = webkit_soup_request_get_uri(d->m_soupRequest.get());
494             GOwnPtr<char> uriStr(soup_uri_to_string(uri, false));
495             gint errorCode = isTransportError ? static_cast<gint>(soupMsg->status_code) : error->code;
496             const gchar* errorMsg = isTransportError ? soupMsg->reason_phrase : error->message;
497             const gchar* quarkStr = isTransportError ? g_quark_to_string(SOUP_HTTP_ERROR) : g_quark_to_string(G_IO_ERROR);
498             ResourceError resourceError(quarkStr, errorCode, uriStr.get(), String::fromUTF8(errorMsg));
499
500             cleanupSoupRequestOperation(handle.get());
501             client->didFail(handle.get(), resourceError);
502             return;
503         }
504
505         if (d->m_soupMessage && statusWillBeHandledBySoup(d->m_soupMessage->status_code)) {
506             ASSERT(d->m_response.isNull());
507
508             fillResponseFromMessage(soupMsg, &d->m_response);
509             client->didReceiveResponse(handle.get(), d->m_response);
510
511             // WebCore might have cancelled the job in the while. We
512             // must check for response_body->length and not
513             // response_body->data as libsoup always creates the
514             // SoupBuffer for the body even if the length is 0
515             if (!d->m_cancelled && soupMsg->response_body->length)
516                 client->didReceiveData(handle.get(), soupMsg->response_body->data, soupMsg->response_body->length, true);
517         }
518
519         // didReceiveData above might have cancelled it
520         if (d->m_cancelled || !client) {
521             cleanupSoupRequestOperation(handle.get());
522             return;
523         }
524
525         client->didFinishLoading(handle.get(), 0);
526         return;
527     }
528
529     if (d->m_cancelled) {
530         cleanupSoupRequestOperation(handle.get());
531         return;
532     }
533
534     d->m_inputStream = adoptGRef(in);
535     d->m_buffer = static_cast<char*>(g_slice_alloc0(READ_BUFFER_SIZE));
536     d->m_total = 0;
537
538     // readCallback needs it
539     g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", handle.get());
540
541     // Ensure a response is sent for any protocols that don't explicitly support responses
542     // through got-headers signal or content sniffing.
543     // (e.g. file and GIO based protocol).
544     if (!handle->shouldContentSniff() && d->m_response.isNull()) {
545         d->m_response.setURL(handle->firstRequest().url());
546         d->m_response.setMimeType(webkit_soup_request_get_content_type(d->m_soupRequest.get()));
547         d->m_response.setExpectedContentLength(webkit_soup_request_get_content_length(d->m_soupRequest.get()));
548         client->didReceiveResponse(handle.get(), d->m_response);
549
550         if (d->m_cancelled) {
551             cleanupSoupRequestOperation(handle.get());
552             return;
553         }
554     }
555
556     if (d->m_defersLoading)
557          soup_session_pause_message(handle->defaultSession(), d->m_soupMessage.get());
558
559     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE,
560                               G_PRIORITY_DEFAULT, d->m_cancellable.get(), readCallback, 0);
561 }
562
563 static bool startHttp(ResourceHandle* handle)
564 {
565     ASSERT(handle);
566
567     SoupSession* session = handle->defaultSession();
568     ensureSessionIsInitialized(session);
569
570     ResourceHandleInternal* d = handle->getInternal();
571
572     ResourceRequest request(handle->firstRequest());
573     KURL url(request.url());
574     url.removeFragmentIdentifier();
575     request.setURL(url);
576
577     GOwnPtr<GError> error;
578     d->m_soupRequest = adoptGRef(webkit_soup_requester_request(d->m_requester.get(), url.string().utf8().data(), session, &error.outPtr()));
579     if (error) {
580         d->m_soupRequest = 0;
581         return false;
582     }
583
584     g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", handle);
585
586     d->m_soupMessage = adoptGRef(webkit_soup_request_http_get_message(WEBKIT_SOUP_REQUEST_HTTP(d->m_soupRequest.get())));
587     if (!d->m_soupMessage)
588         return false;
589
590     SoupMessage* soupMessage = d->m_soupMessage.get();
591     request.updateSoupMessage(soupMessage);
592
593     if (!handle->shouldContentSniff())
594         soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_SNIFFER);
595     else
596         g_signal_connect(soupMessage, "content-sniffed", G_CALLBACK(contentSniffedCallback), handle);
597
598     g_signal_connect(soupMessage, "restarted", G_CALLBACK(restartedCallback), handle);
599     g_signal_connect(soupMessage, "got-headers", G_CALLBACK(gotHeadersCallback), handle);
600     d->m_gotChunkHandler = g_signal_connect(soupMessage, "got-chunk", G_CALLBACK(gotChunkCallback), handle);
601
602 #ifdef HAVE_LIBSOUP_2_29_90
603     String firstPartyString = request.firstPartyForCookies().string();
604     if (!firstPartyString.isEmpty()) {
605         GOwnPtr<SoupURI> firstParty(soup_uri_new(firstPartyString.utf8().data()));
606         soup_message_set_first_party(soupMessage, firstParty.get());
607     }
608 #endif
609
610     FormData* httpBody = d->m_firstRequest.httpBody();
611     if (httpBody && !httpBody->isEmpty()) {
612         size_t numElements = httpBody->elements().size();
613
614         // handle the most common case (i.e. no file upload)
615         if (numElements < 2) {
616             Vector<char> body;
617             httpBody->flatten(body);
618             soup_message_set_request(soupMessage, d->m_firstRequest.httpContentType().utf8().data(),
619                                      SOUP_MEMORY_COPY, body.data(), body.size());
620         } else {
621             /*
622              * we have more than one element to upload, and some may
623              * be (big) files, which we will want to mmap instead of
624              * copying into memory; TODO: support upload of non-local
625              * (think sftp://) files by using GIO?
626              */
627             soup_message_body_set_accumulate(soupMessage->request_body, FALSE);
628             for (size_t i = 0; i < numElements; i++) {
629                 const FormDataElement& element = httpBody->elements()[i];
630
631                 if (element.m_type == FormDataElement::data)
632                     soup_message_body_append(soupMessage->request_body, SOUP_MEMORY_TEMPORARY, element.m_data.data(), element.m_data.size());
633                 else {
634                     /*
635                      * mapping for uploaded files code inspired by technique used in
636                      * libsoup's simple-httpd test
637                      */
638                     GOwnPtr<GError> error;
639                     CString fileName = fileSystemRepresentation(element.m_filename);
640                     GMappedFile* fileMapping = g_mapped_file_new(fileName.data(), false, &error.outPtr());
641
642                     if (error) {
643                         g_signal_handlers_disconnect_matched(soupMessage, G_SIGNAL_MATCH_DATA,
644                                                              0, 0, 0, 0, handle);
645                         d->m_soupMessage.clear();
646
647                         return false;
648                     }
649
650                     SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping),
651                                                                         g_mapped_file_get_length(fileMapping),
652                                                                         fileMapping,
653                                                                         reinterpret_cast<GDestroyNotify>(g_mapped_file_unref));
654                     soup_message_body_append_buffer(soupMessage->request_body, soupBuffer);
655                     soup_buffer_free(soupBuffer);
656                 }
657             }
658         }
659     }
660
661     // balanced by a deref() in cleanupSoupRequestOperation, which should always run
662     handle->ref();
663
664     // Make sure we have an Accept header for subresources; some sites
665     // want this to serve some of their subresources
666     if (!soup_message_headers_get_one(soupMessage->request_headers, "Accept"))
667         soup_message_headers_append(soupMessage->request_headers, "Accept", "*/*");
668
669     // Send the request only if it's not been explicitely deferred.
670     if (!d->m_defersLoading) {
671         d->m_cancellable = adoptGRef(g_cancellable_new());
672         webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
673     }
674
675     return true;
676 }
677
678 bool ResourceHandle::start(NetworkingContext* context)
679 {
680     ASSERT(!d->m_soupMessage);
681
682     // The frame could be null if the ResourceHandle is not associated to any
683     // Frame, e.g. if we are downloading a file.
684     // If the frame is not null but the page is null this must be an attempted
685     // load from an unload handler, so let's just block it.
686     // If both the frame and the page are not null the context is valid.
687     if (context && !context->isValid())
688         return false;
689
690     if (!(d->m_user.isEmpty() || d->m_pass.isEmpty())) {
691         // If credentials were specified for this request, add them to the url,
692         // so that they will be passed to NetworkRequest.
693         KURL urlWithCredentials(firstRequest().url());
694         urlWithCredentials.setUser(d->m_user);
695         urlWithCredentials.setPass(d->m_pass);
696         d->m_firstRequest.setURL(urlWithCredentials);
697     }
698
699     KURL url = firstRequest().url();
700     String urlString = url.string();
701     String protocol = url.protocol();
702
703     // Used to set the authentication dialog toplevel; may be NULL
704     d->m_context = context;
705
706     if (equalIgnoringCase(protocol, "data"))
707         return startData(this, urlString);
708
709     if (equalIgnoringCase(protocol, "http") || equalIgnoringCase(protocol, "https")) {
710         if (startHttp(this))
711             return true;
712     }
713
714     if (equalIgnoringCase(protocol, "file") || equalIgnoringCase(protocol, "ftp") || equalIgnoringCase(protocol, "ftps")) {
715         // FIXME: should we be doing any other protocols here?
716         if (startGio(this, url))
717             return true;
718     }
719
720     // Error must not be reported immediately
721     this->scheduleFailure(InvalidURLFailure);
722
723     return true;
724 }
725
726 void ResourceHandle::cancel()
727 {
728     d->m_cancelled = true;
729     if (d->m_soupMessage)
730         soup_session_cancel_message(defaultSession(), d->m_soupMessage.get(), SOUP_STATUS_CANCELLED);
731     else if (d->m_cancellable)
732         g_cancellable_cancel(d->m_cancellable.get());
733 }
734
735 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
736 {
737     ASSERT_NOT_REACHED();
738     return 0;
739 }
740
741 bool ResourceHandle::supportsBufferedData()
742 {
743     return false;
744 }
745
746 void ResourceHandle::platformSetDefersLoading(bool defersLoading)
747 {
748     // Initial implementation of this method was required for bug #44157.
749
750     if (d->m_cancelled)
751         return;
752
753     if (!defersLoading && !d->m_cancellable && d->m_soupRequest.get()) {
754         d->m_cancellable = adoptGRef(g_cancellable_new());
755         webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
756         return;
757     }
758
759     // Only supported for http(s) transfers. Something similar would
760     // probably be needed for data transfers done with GIO.
761     if (!d->m_soupMessage)
762         return;
763
764     if (defersLoading)
765         soup_session_pause_message(defaultSession(), d->m_soupMessage.get());
766     else
767         soup_session_unpause_message(defaultSession(), d->m_soupMessage.get());
768 }
769
770 bool ResourceHandle::loadsBlocked()
771 {
772     return false;
773 }
774
775 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
776 {
777     // Not having this function means that we'll ask the user about re-posting a form
778     // even when we go back to a page that's still in the cache.
779     notImplemented();
780     return false;
781 }
782
783 void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data)
784 {
785     WebCoreSynchronousLoader syncLoader(error, response, data);
786     // FIXME: we should use the ResourceHandle::create method here,
787     // but it makes us timeout in a couple of tests. See
788     // https://bugs.webkit.org/show_bug.cgi?id=41823
789     RefPtr<ResourceHandle> handle = adoptRef(new ResourceHandle(request, &syncLoader, false /*defersLoading*/, false /*shouldContentSniff*/));
790     handle->start(context);
791
792     syncLoader.run();
793 }
794
795 static void closeCallback(GObject* source, GAsyncResult* res, gpointer)
796 {
797     RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
798     if (!handle)
799         return;
800
801     ResourceHandleInternal* d = handle->getInternal();
802     ResourceHandleClient* client = handle->client();
803
804     g_input_stream_close_finish(d->m_inputStream.get(), res, 0);
805     cleanupSoupRequestOperation(handle.get());
806
807     // The load may have been cancelled, the client may have been
808     // destroyed already. In such cases calling didFinishLoading is a
809     // bad idea.
810     if (d->m_cancelled || !client)
811         return;
812
813     client->didFinishLoading(handle.get(), 0);
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         g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT,
846                                    0, closeCallback, 0);
847         return;
848     }
849
850     // It's mandatory to have sent a response before sending data
851     ASSERT(!d->m_response.isNull());
852
853     d->m_total += bytesRead;
854     if (G_LIKELY(!convertToUTF16))
855         client->didReceiveData(handle.get(), d->m_buffer, bytesRead, d->m_total);
856     else {
857         // We have to convert it to UTF-16 due to limitations in KURL
858         String data = String::fromUTF8(d->m_buffer, bytesRead);
859         client->didReceiveData(handle.get(), reinterpret_cast<const char*>(data.characters()), data.length() * sizeof(UChar), 0);
860     }
861
862     // didReceiveData may cancel the load, which may release the last reference.
863     if (d->m_cancelled || !client) {
864         cleanupSoupRequestOperation(handle.get());
865         return;
866     }
867
868     g_input_stream_read_async(d->m_inputStream.get(), d->m_buffer, READ_BUFFER_SIZE, G_PRIORITY_DEFAULT,
869                               d->m_cancellable.get(), readCallback, data);
870 }
871
872 static bool startGio(ResourceHandle* handle, KURL url)
873 {
874     ASSERT(handle);
875
876     if (handle->firstRequest().httpMethod() != "GET" && handle->firstRequest().httpMethod() != "POST")
877         return false;
878
879     SoupSession* session = handle->defaultSession();
880     ResourceHandleInternal* d = handle->getInternal();
881
882     // GIO doesn't know how to handle refs and queries, so remove them
883     // TODO: use KURL.fileSystemPath after KURLGtk and FileSystemGtk are
884     // using GIO internally, and providing URIs instead of file paths
885     url.removeFragmentIdentifier();
886     url.setQuery(String());
887     url.removePort();
888     CString urlStr = url.string().utf8();
889
890     GOwnPtr<GError> error;
891     d->m_soupRequest = adoptGRef(webkit_soup_requester_request(d->m_requester.get(), urlStr.data(), session, &error.outPtr()));
892     if (error) {
893         d->m_soupRequest = 0;
894         return false;
895     }
896
897     g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", handle);
898
899     // balanced by a deref() in cleanupSoupRequestOperation, which should always run
900     handle->ref();
901
902     d->m_cancellable = adoptGRef(g_cancellable_new());
903     webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
904
905     return true;
906 }
907
908 SoupSession* ResourceHandle::defaultSession()
909 {
910     static SoupSession* session = createSoupSession();
911
912     return session;
913 }
914
915 }