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>
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.
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.
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.
28 #include "ResourceHandle.h"
32 #include "ChromeClient.h"
33 #include "CookieJarSoup.h"
34 #include "CachedResourceLoader.h"
35 #include "FileSystem.h"
37 #include "GOwnPtrSoup.h"
38 #include "HTTPParsers.h"
40 #include "MIMETypeRegistry.h"
41 #include "NotImplemented.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"
54 #include <libsoup/soup.h>
56 #include <sys/types.h>
61 #define READ_BUFFER_SIZE 8192
63 class WebCoreSynchronousLoader : public ResourceHandleClient, public Noncopyable {
65 WebCoreSynchronousLoader(ResourceError&, ResourceResponse &, Vector<char>&);
66 ~WebCoreSynchronousLoader();
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&);
76 ResourceError& m_error;
77 ResourceResponse& m_response;
80 GMainLoop* m_mainLoop;
83 WebCoreSynchronousLoader::WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, Vector<char>& data)
85 , m_response(response)
89 m_mainLoop = g_main_loop_new(0, false);
92 WebCoreSynchronousLoader::~WebCoreSynchronousLoader()
94 g_main_loop_unref(m_mainLoop);
97 void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
99 m_response = response;
102 void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
104 m_data.append(data, length);
107 void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*, double)
109 g_main_loop_quit(m_mainLoop);
113 void WebCoreSynchronousLoader::didFail(ResourceHandle* handle, const ResourceError& error)
116 didFinishLoading(handle, 0);
119 void WebCoreSynchronousLoader::run()
122 g_main_loop_run(m_mainLoop);
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);
131 ResourceHandleInternal::~ResourceHandleInternal()
134 g_object_set_data(G_OBJECT(m_soupRequest.get()), "webkit-resource", 0);
137 g_source_remove(m_idleHandler);
142 ResourceHandle::~ResourceHandle()
144 cleanupSoupRequestOperation(this, true);
147 void ResourceHandle::prepareForURL(const KURL &url)
149 #ifdef HAVE_LIBSOUP_2_29_90
150 GOwnPtr<SoupURI> soupURI(soup_uri_new(url.prettyURL().utf8().data()));
153 soup_session_prepare_for_uri(ResourceHandle::defaultSession(), soupURI.get());
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)
162 if (SOUP_STATUS_IS_TRANSPORT_ERROR(statusCode)
163 || (SOUP_STATUS_IS_REDIRECTION(statusCode) && (statusCode != SOUP_STATUS_NOT_MODIFIED))
164 || (statusCode == SOUP_STATUS_UNAUTHORIZED))
170 static void fillResponseFromMessage(SoupMessage* msg, ResourceResponse* response)
172 response->updateFromSoupMessage(msg);
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)
179 ResourceHandle* handle = static_cast<ResourceHandle*>(data);
182 ResourceHandleInternal* d = handle->getInternal();
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);
190 ResourceRequest request = handle->firstRequest();
191 ResourceResponse response;
192 request.setURL(newURL);
193 request.setHTTPMethod(msg->method);
194 fillResponseFromMessage(msg, &response);
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");
203 d->client()->willSendRequest(handle, request, response);
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());
218 static void gotHeadersCallback(SoupMessage* msg, gpointer data)
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);
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);
234 RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
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())
241 ResourceHandleInternal* d = handle->getInternal();
244 ResourceHandleClient* client = handle->client();
248 fillResponseFromMessage(msg, &d->m_response);
249 client->didReceiveResponse(handle.get(), d->m_response);
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)
256 const char* officialType = soup_message_headers_get_one(msg->response_headers, "Content-Type");
258 if (!officialType || strcmp(officialType, sniffedType))
259 soup_message_headers_set_content_type(msg->response_headers, sniffedType, params);
262 if (statusWillBeHandledBySoup(msg->status_code))
265 RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
268 ResourceHandleInternal* d = handle->getInternal();
271 ResourceHandleClient* client = handle->client();
275 fillResponseFromMessage(msg, &d->m_response);
276 client->didReceiveResponse(handle.get(), d->m_response);
279 static void gotChunkCallback(SoupMessage* msg, SoupBuffer* chunk, gpointer data)
281 if (statusWillBeHandledBySoup(msg->status_code))
284 RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(data);
287 ResourceHandleInternal* d = handle->getInternal();
290 ResourceHandleClient* client = handle->client();
294 client->didReceiveData(handle.get(), chunk->data, chunk->length, false);
297 static gboolean parseDataUrl(gpointer callbackData)
299 ResourceHandle* handle = static_cast<ResourceHandle*>(callbackData);
300 ResourceHandleClient* client = handle->client();
301 ResourceHandleInternal* d = handle->getInternal();
305 d->m_idleHandler = 0;
311 String url = handle->firstRequest().url().string();
312 ASSERT(url.startsWith("data:", false));
314 int index = url.find(',');
316 client->cannotShowURL(handle);
320 String mediaType = url.substring(5, index - 5);
322 bool isBase64 = mediaType.endsWith(";base64", false);
324 mediaType = mediaType.left(mediaType.length() - 7);
326 if (mediaType.isEmpty())
327 mediaType = "text/plain;charset=US-ASCII";
329 String mimeType = extractMIMETypeFromMediaType(mediaType);
330 String charset = extractCharsetFromMediaType(mediaType);
332 ResourceResponse response;
333 response.setURL(handle->firstRequest().url());
334 response.setMimeType(mimeType);
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);
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())
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()));
351 d->m_soupRequest = 0;
352 client->didFinishLoading(handle, 0);
356 d->m_inputStream = adoptPlatformRef(webkit_soup_request_send(d->m_soupRequest.get(), 0, &error.outPtr()));
358 d->m_inputStream = 0;
359 client->didFinishLoading(handle, 0);
363 d->m_buffer = static_cast<char*>(g_slice_alloc0(READ_BUFFER_SIZE));
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
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));
377 static bool startData(ResourceHandle* handle, String urlString)
381 ResourceHandleInternal* d = handle->getInternal();
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);
389 static SoupSession* createSoupSession()
391 return soup_session_async_new();
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
398 #define MAX_CONNECTIONS 60
399 #define MAX_CONNECTIONS_PER_HOST 6
401 static void ensureSessionIsInitialized(SoupSession* session)
403 if (g_object_get_data(G_OBJECT(session), "webkit-init"))
406 SoupCookieJar* jar = reinterpret_cast<SoupCookieJar*>(soup_session_get_feature(session, SOUP_TYPE_COOKIE_JAR));
408 soup_session_add_feature(session, SOUP_SESSION_FEATURE(defaultCookieJar()));
410 setDefaultCookieJar(jar);
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);
418 g_object_set(session,
419 SOUP_SESSION_MAX_CONNS, MAX_CONNECTIONS,
420 SOUP_SESSION_MAX_CONNS_PER_HOST, MAX_CONNECTIONS_PER_HOST,
423 g_object_set_data(G_OBJECT(session), "webkit-init", reinterpret_cast<void*>(0xdeadbeef));
426 static void cleanupSoupRequestOperation(ResourceHandle* handle, bool isDestroying = false)
428 ResourceHandleInternal* d = handle->getInternal();
430 if (d->m_soupRequest) {
431 g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", 0);
432 d->m_soupRequest.clear();
435 if (d->m_inputStream) {
436 g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", 0);
437 d->m_inputStream.clear();
440 d->m_cancellable.clear();
442 if (d->m_soupMessage) {
443 g_signal_handlers_disconnect_matched(d->m_soupMessage.get(), G_SIGNAL_MATCH_DATA,
445 d->m_soupMessage.clear();
449 g_slice_free1(READ_BUFFER_SIZE, d->m_buffer);
457 static void sendRequestCallback(GObject* source, GAsyncResult* res, gpointer userData)
459 RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
463 ResourceHandleInternal* d = handle->getInternal();
464 ResourceHandleClient* client = handle->client();
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);
473 if (d->m_cancelled || !client) {
474 cleanupSoupRequestOperation(handle.get());
478 GOwnPtr<GError> error;
479 GInputStream* in = webkit_soup_request_send_finish(d->m_soupRequest.get(), res, &error.outPtr());
482 SoupMessage* soupMsg = d->m_soupMessage.get();
483 gboolean isTransportError = d->m_soupMessage && SOUP_STATUS_IS_TRANSPORT_ERROR(soupMsg->status_code);
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));
493 cleanupSoupRequestOperation(handle.get());
494 client->didFail(handle.get(), resourceError);
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);
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);
507 // didReceiveData above might have cancelled it
508 if (d->m_cancelled || !client) {
509 cleanupSoupRequestOperation(handle.get());
513 client->didFinishLoading(handle.get(), 0);
517 if (d->m_cancelled) {
518 cleanupSoupRequestOperation(handle.get());
522 d->m_inputStream = adoptPlatformRef(in);
523 d->m_buffer = static_cast<char*>(g_slice_alloc0(READ_BUFFER_SIZE));
526 // readCallback needs it
527 g_object_set_data(G_OBJECT(d->m_inputStream.get()), "webkit-resource", handle.get());
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
533 if (equalIgnoringCase(handle->firstRequest().url().protocol(), "file")
534 && G_IS_FILE_INPUT_STREAM(in)) {
535 ResourceResponse response;
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);
542 if (d->m_cancelled) {
543 cleanupSoupRequestOperation(handle.get());
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);
552 static bool startHttp(ResourceHandle* handle)
556 SoupSession* session = handle->defaultSession();
557 ensureSessionIsInitialized(session);
559 ResourceHandleInternal* d = handle->getInternal();
561 ResourceRequest request(handle->firstRequest());
562 KURL url(request.url());
563 url.removeFragmentIdentifier();
566 GOwnPtr<GError> error;
567 d->m_soupRequest = adoptPlatformRef(webkit_soup_requester_request(d->m_requester.get(), url.string().utf8().data(), session, &error.outPtr()));
569 d->m_soupRequest = 0;
573 g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", handle);
575 d->m_soupMessage = adoptPlatformRef(webkit_soup_request_http_get_message(WEBKIT_SOUP_REQUEST_HTTP(d->m_soupRequest.get())));
576 if (!d->m_soupMessage)
579 SoupMessage* soupMessage = d->m_soupMessage.get();
580 request.updateSoupMessage(soupMessage);
582 if (!handle->shouldContentSniff())
583 soup_message_disable_feature(soupMessage, SOUP_TYPE_CONTENT_SNIFFER);
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);
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());
598 FormData* httpBody = d->m_firstRequest.httpBody();
599 if (httpBody && !httpBody->isEmpty()) {
600 size_t numElements = httpBody->elements().size();
602 // handle the most common case (i.e. no file upload)
603 if (numElements < 2) {
605 httpBody->flatten(body);
606 soup_message_set_request(soupMessage, d->m_firstRequest.httpContentType().utf8().data(),
607 SOUP_MEMORY_COPY, body.data(), body.size());
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?
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];
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());
623 * mapping for uploaded files code inspired by technique used in
624 * libsoup's simple-httpd test
626 GOwnPtr<GError> error;
627 CString fileName = fileSystemRepresentation(element.m_filename);
628 GMappedFile* fileMapping = g_mapped_file_new(fileName.data(), false, &error.outPtr());
631 g_signal_handlers_disconnect_matched(soupMessage, G_SIGNAL_MATCH_DATA,
633 d->m_soupMessage.clear();
638 SoupBuffer* soupBuffer = soup_buffer_new_with_owner(g_mapped_file_get_contents(fileMapping),
639 g_mapped_file_get_length(fileMapping),
641 reinterpret_cast<GDestroyNotify>(g_mapped_file_unref));
642 soup_message_body_append_buffer(soupMessage->request_body, soupBuffer);
643 soup_buffer_free(soupBuffer);
649 // balanced by a deref() in cleanupSoupRequestOperation, which should always run
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", "*/*");
657 d->m_cancellable = adoptPlatformRef(g_cancellable_new());
658 webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
663 bool ResourceHandle::start(NetworkingContext* context)
665 ASSERT(!d->m_soupMessage);
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())
675 KURL url = firstRequest().url();
676 String urlString = url.string();
677 String protocol = url.protocol();
679 // Used to set the authentication dialog toplevel; may be NULL
680 d->m_context = context;
682 if (equalIgnoringCase(protocol, "data"))
683 return startData(this, urlString);
685 if (equalIgnoringCase(protocol, "http") || equalIgnoringCase(protocol, "https")) {
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))
696 // Error must not be reported immediately
697 this->scheduleFailure(InvalidURLFailure);
702 void ResourceHandle::cancel()
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());
711 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
713 ASSERT_NOT_REACHED();
717 bool ResourceHandle::supportsBufferedData()
722 void ResourceHandle::platformSetDefersLoading(bool)
727 bool ResourceHandle::loadsBlocked()
732 bool ResourceHandle::willLoadFromCache(ResourceRequest&, Frame*)
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.
740 void ResourceHandle::loadResourceSynchronously(NetworkingContext* context, const ResourceRequest& request, StoredCredentials /*storedCredentials*/, ResourceError& error, ResourceResponse& response, Vector<char>& data)
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);
752 static void closeCallback(GObject* source, GAsyncResult* res, gpointer)
754 RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
758 ResourceHandleInternal* d = handle->getInternal();
759 ResourceHandleClient* client = handle->client();
761 g_input_stream_close_finish(d->m_inputStream.get(), res, 0);
762 cleanupSoupRequestOperation(handle.get());
764 // The load may have been cancelled, the client may have been
765 // destroyed already. In such cases calling didFinishLoading is a
767 if (d->m_cancelled || !client)
770 client->didFinishLoading(handle.get(), 0);
773 static void readCallback(GObject* source, GAsyncResult* asyncResult, gpointer data)
775 RefPtr<ResourceHandle> handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
779 bool convertToUTF16 = static_cast<bool>(data);
780 ResourceHandleInternal* d = handle->getInternal();
781 ResourceHandleClient* client = handle->client();
783 if (d->m_cancelled || !client) {
784 cleanupSoupRequestOperation(handle.get());
788 GOwnPtr<GError> error;
790 gssize bytesRead = g_input_stream_read_finish(d->m_inputStream.get(), asyncResult, &error.outPtr());
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);
802 g_input_stream_close_async(d->m_inputStream.get(), G_PRIORITY_DEFAULT,
803 0, closeCallback, 0);
807 d->m_total += bytesRead;
808 if (G_LIKELY(!convertToUTF16))
809 client->didReceiveData(handle.get(), d->m_buffer, bytesRead, d->m_total);
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);
816 // didReceiveData may cancel the load, which may release the last reference.
817 if (d->m_cancelled || !client) {
818 cleanupSoupRequestOperation(handle.get());
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);
826 static bool startGio(ResourceHandle* handle, KURL url)
830 if (handle->firstRequest().httpMethod() != "GET" && handle->firstRequest().httpMethod() != "POST")
833 SoupSession* session = handle->defaultSession();
834 ResourceHandleInternal* d = handle->getInternal();
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());
842 CString urlStr = url.string().utf8();
844 GOwnPtr<GError> error;
845 d->m_soupRequest = adoptPlatformRef(webkit_soup_requester_request(d->m_requester.get(), urlStr.data(), session, &error.outPtr()));
847 d->m_soupRequest = 0;
851 g_object_set_data(G_OBJECT(d->m_soupRequest.get()), "webkit-resource", handle);
853 // balanced by a deref() in cleanupSoupRequestOperation, which should always run
856 d->m_cancellable = adoptPlatformRef(g_cancellable_new());
857 webkit_soup_request_send_async(d->m_soupRequest.get(), d->m_cancellable.get(), sendRequestCallback, 0);
862 SoupSession* ResourceHandle::defaultSession()
864 static SoupSession* session = createSoupSession();