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