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