2 * Copyright (C) 2008 Alp Toker <alp@atoker.com>
3 * Copyright (C) 2008 Xan Lopez <xan@gnome.org>
4 * Copyright (C) 2008 Collabora Ltd.
5 * Copyright (C) 2009 Holger Hans Peter Freyther
6 * Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #include "ResourceHandle.h"
28 #include "CookieJarSoup.h"
30 #include "DocLoader.h"
32 #include "HTTPParsers.h"
33 #include "MIMETypeRegistry.h"
34 #include "NotImplemented.h"
35 #include "ResourceError.h"
36 #include "ResourceHandleClient.h"
37 #include "ResourceHandleInternal.h"
38 #include "ResourceResponse.h"
39 #include "TextEncoding.h"
44 #include <libsoup/soup.h>
45 #include <libsoup/soup-message.h>
46 #include <sys/types.h>
51 #if GLIB_CHECK_VERSION(2,12,0)
52 #define USE_GLIB_BASE64
58 static SoupSession* session = 0;
60 class WebCoreSynchronousLoader : public ResourceHandleClient, Noncopyable {
62 WebCoreSynchronousLoader(ResourceError&, ResourceResponse &, Vector<char>&);
63 ~WebCoreSynchronousLoader();
65 virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&);
66 virtual void didReceiveData(ResourceHandle*, const char*, int, int lengthReceived);
67 virtual void didFinishLoading(ResourceHandle*);
68 virtual void didFail(ResourceHandle*, const ResourceError&);
73 ResourceError& m_error;
74 ResourceResponse& m_response;
77 GMainLoop* m_mainLoop;
80 WebCoreSynchronousLoader::WebCoreSynchronousLoader(ResourceError& error, ResourceResponse& response, Vector<char>& data)
82 , m_response(response)
86 m_mainLoop = g_main_loop_new(NULL, false);
89 WebCoreSynchronousLoader::~WebCoreSynchronousLoader()
91 g_main_loop_unref(m_mainLoop);
94 void WebCoreSynchronousLoader::didReceiveResponse(ResourceHandle*, const ResourceResponse& response)
96 m_response = response;
99 void WebCoreSynchronousLoader::didReceiveData(ResourceHandle*, const char* data, int length, int)
101 m_data.append(data, length);
104 void WebCoreSynchronousLoader::didFinishLoading(ResourceHandle*)
106 g_main_loop_quit(m_mainLoop);
110 void WebCoreSynchronousLoader::didFail(ResourceHandle* handle, const ResourceError& error)
113 didFinishLoading(handle);
116 void WebCoreSynchronousLoader::run()
119 g_main_loop_run(m_mainLoop);
125 ERROR_UNKNOWN_PROTOCOL,
126 ERROR_BAD_NON_HTTP_METHOD,
127 ERROR_UNABLE_TO_OPEN_FILE,
136 static void freeFileMapping(gpointer data)
138 FileMapping* fileMapping = static_cast<FileMapping*>(data);
139 if (fileMapping->ptr != MAP_FAILED)
140 munmap(fileMapping->ptr, fileMapping->length);
141 g_slice_free(FileMapping, fileMapping);
144 static void cleanupGioOperation(ResourceHandleInternal* handle);
146 ResourceHandleInternal::~ResourceHandleInternal()
149 g_object_unref(m_msg);
153 cleanupGioOperation(this);
156 g_source_remove(m_idleHandler);
161 ResourceHandle::~ResourceHandle()
165 static void fillResponseFromMessage(SoupMessage* msg, ResourceResponse* response)
167 SoupMessageHeadersIter iter;
168 const char* name = NULL;
169 const char* value = NULL;
170 soup_message_headers_iter_init(&iter, msg->response_headers);
171 while (soup_message_headers_iter_next(&iter, &name, &value))
172 response->setHTTPHeaderField(name, value);
174 String contentType = soup_message_headers_get(msg->response_headers, "Content-Type");
175 char* uri = soup_uri_to_string(soup_message_get_uri(msg), FALSE);
176 response->setUrl(KURL(uri));
178 response->setMimeType(extractMIMETypeFromMediaType(contentType));
179 response->setTextEncodingName(extractCharsetFromMediaType(contentType));
180 response->setExpectedContentLength(soup_message_headers_get_content_length(msg->response_headers));
181 response->setHTTPStatusCode(msg->status_code);
182 response->setSuggestedFilename(filenameFromHTTPContentDisposition(response->httpHeaderField("Content-Disposition")));
185 // Called each time the message is going to be sent again except the first time.
186 // It's used mostly to let webkit know about redirects.
187 static void restartedCallback(SoupMessage* msg, gpointer data)
189 ResourceHandle* handle = static_cast<ResourceHandle*>(data);
192 ResourceHandleInternal* d = handle->getInternal();
196 char* uri = soup_uri_to_string(soup_message_get_uri(msg), FALSE);
197 String location = String(uri);
199 KURL newURL = KURL(handle->request().url(), location);
201 ResourceRequest request = handle->request();
202 ResourceResponse response;
203 request.setURL(newURL);
204 fillResponseFromMessage(msg, &response);
206 d->client()->willSendRequest(handle, request, response);
208 d->m_request.setURL(newURL);
211 static void gotHeadersCallback(SoupMessage* msg, gpointer data)
213 if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code))
216 ResourceHandle* handle = static_cast<ResourceHandle*>(data);
219 ResourceHandleInternal* d = handle->getInternal();
222 ResourceHandleClient* client = handle->client();
226 fillResponseFromMessage(msg, &d->m_response);
227 client->didReceiveResponse(handle, d->m_response);
228 soup_message_set_flags(msg, SOUP_MESSAGE_OVERWRITE_CHUNKS);
231 static void gotChunkCallback(SoupMessage* msg, SoupBuffer* chunk, gpointer data)
233 if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code))
236 ResourceHandle* handle = static_cast<ResourceHandle*>(data);
239 ResourceHandleInternal* d = handle->getInternal();
242 ResourceHandleClient* client = handle->client();
246 client->didReceiveData(handle, chunk->data, chunk->length, false);
249 // Called at the end of the message, with all the necessary about the last informations.
250 // Doesn't get called for redirects.
251 static void finishedCallback(SoupSession *session, SoupMessage* msg, gpointer data)
253 ResourceHandle* handle = static_cast<ResourceHandle*>(data);
254 // TODO: maybe we should run this code even if there's no client?
258 ResourceHandleInternal* d = handle->getInternal();
260 ResourceHandleClient* client = handle->client();
267 if (SOUP_STATUS_IS_TRANSPORT_ERROR(msg->status_code)) {
268 char* uri = soup_uri_to_string(soup_message_get_uri(msg), FALSE);
269 ResourceError error("webkit-network-error", ERROR_TRANSPORT, uri, String::fromUTF8(msg->reason_phrase));
271 client->didFail(handle, error);
273 } else if (!SOUP_STATUS_IS_SUCCESSFUL(msg->status_code)) {
274 fillResponseFromMessage(msg, &d->m_response);
275 client->didReceiveResponse(handle, d->m_response);
277 // WebCore might have cancelled the job in the while
281 if (msg->response_body->data)
282 client->didReceiveData(handle, msg->response_body->data, msg->response_body->length, true);
285 client->didFinishLoading(handle);
288 // parseDataUrl() is taken from the CURL http backend.
289 static gboolean parseDataUrl(gpointer callback_data)
291 ResourceHandle* handle = static_cast<ResourceHandle*>(callback_data);
292 ResourceHandleClient* client = handle->client();
294 handle->getInternal()->m_idleHandler = 0;
300 String url = handle->request().url().string();
301 ASSERT(url.startsWith("data:", false));
303 int index = url.find(',');
305 client->cannotShowURL(handle);
309 String mediaType = url.substring(5, index - 5);
310 String data = url.substring(index + 1);
312 bool base64 = mediaType.endsWith(";base64", false);
314 mediaType = mediaType.left(mediaType.length() - 7);
316 if (mediaType.isEmpty())
317 mediaType = "text/plain;charset=US-ASCII";
319 String mimeType = extractMIMETypeFromMediaType(mediaType);
320 String charset = extractCharsetFromMediaType(mediaType);
322 ResourceResponse response;
323 response.setMimeType(mimeType);
326 data = decodeURLEscapeSequences(data);
327 response.setTextEncodingName(charset);
328 client->didReceiveResponse(handle, response);
330 // Use the GLib Base64 if available, since WebCore's decoder isn't
331 // general-purpose and fails on Acid3 test 97 (whitespace).
332 #ifdef USE_GLIB_BASE64
333 size_t outLength = 0;
335 outData = reinterpret_cast<char*>(g_base64_decode(data.utf8().data(), &outLength));
336 if (outData && outLength > 0)
337 client->didReceiveData(handle, outData, outLength, 0);
341 if (base64Decode(data.latin1().data(), data.latin1().length(), out) && out.size() > 0)
342 client->didReceiveData(handle, out.data(), out.size(), 0);
345 // We have to convert to UTF-16 early due to limitations in KURL
346 data = decodeURLEscapeSequences(data, TextEncoding(charset));
347 response.setTextEncodingName("UTF-16");
348 client->didReceiveResponse(handle, response);
349 if (data.length() > 0)
350 client->didReceiveData(handle, reinterpret_cast<const char*>(data.characters()), data.length() * sizeof(UChar), 0);
353 client->didFinishLoading(handle);
358 bool ResourceHandle::startData(String urlString)
360 ResourceHandleInternal* d = this->getInternal();
362 // If parseDataUrl is called synchronously the job is not yet effectively started
363 // and webkit won't never know that the data has been parsed even didFinishLoading is called.
364 d->m_idleHandler = g_idle_add(parseDataUrl, this);
368 bool ResourceHandle::startHttp(String urlString)
371 session = soup_session_async_new();
373 soup_session_add_feature(session, SOUP_SESSION_FEATURE(getCookieJar()));
375 const char* soup_debug = g_getenv("WEBKIT_SOUP_LOGGING");
377 int soup_debug_level = atoi(soup_debug);
379 SoupLogger* logger = soup_logger_new(static_cast<SoupLoggerLogLevel>(soup_debug_level), -1);
380 soup_logger_attach(logger, session);
381 g_object_unref(logger);
386 msg = soup_message_new(request().httpMethod().utf8().data(), urlString.utf8().data());
387 g_signal_connect(msg, "restarted", G_CALLBACK(restartedCallback), this);
389 g_signal_connect(msg, "got-headers", G_CALLBACK(gotHeadersCallback), this);
390 g_signal_connect(msg, "got-chunk", G_CALLBACK(gotChunkCallback), this);
392 HTTPHeaderMap customHeaders = d->m_request.httpHeaderFields();
393 if (!customHeaders.isEmpty()) {
394 HTTPHeaderMap::const_iterator end = customHeaders.end();
395 for (HTTPHeaderMap::const_iterator it = customHeaders.begin(); it != end; ++it)
396 soup_message_headers_append(msg->request_headers, it->first.string().utf8().data(), it->second.utf8().data());
399 FormData* httpBody = d->m_request.httpBody();
400 if (httpBody && !httpBody->isEmpty()) {
401 size_t numElements = httpBody->elements().size();
403 // handle the most common case (i.e. no file upload)
404 if (numElements < 2) {
406 httpBody->flatten(body);
407 soup_message_set_request(msg, d->m_request.httpContentType().utf8().data(),
408 SOUP_MEMORY_COPY, body.data(), body.size());
411 * we have more than one element to upload, and some may
412 * be (big) files, which we will want to mmap instead of
413 * copying into memory; TODO: support upload of non-local
414 * (think sftp://) files by using GIO?
416 * TODO: we can avoid appending all the buffers to the
417 * request_body variable with the following call, but we
418 * need to depend on libsoup > 2.25.4
420 * soup_message_body_set_accumulate(msg->request_body, FALSE);
422 for (size_t i = 0; i < numElements; i++) {
423 const FormDataElement& element = httpBody->elements()[i];
425 if (element.m_type == FormDataElement::data)
426 soup_message_body_append(msg->request_body, SOUP_MEMORY_TEMPORARY, element.m_data.data(), element.m_data.size());
429 * mapping for uploaded files code inspired by technique used in
430 * libsoup's simple-httpd test
432 /* FIXME: Since Linux 2.6.23 we should also use O_CLOEXEC */
433 int fd = open(element.m_filename.utf8().data(), O_RDONLY);
436 ResourceError error("webkit-network-error", ERROR_UNABLE_TO_OPEN_FILE, urlString, strerror(errno));
437 d->client()->didFail(this, error);
445 FileMapping* fileMapping = g_slice_new(FileMapping);
447 fileMapping->ptr = mmap(NULL, statBuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
448 if (fileMapping->ptr == MAP_FAILED) {
449 ResourceError error("webkit-network-error", ERROR_UNABLE_TO_OPEN_FILE, urlString, strerror(errno));
450 d->client()->didFail(this, error);
451 freeFileMapping(fileMapping);
456 fileMapping->length = statBuf.st_size;
460 SoupBuffer* soupBuffer = soup_buffer_new_with_owner(fileMapping->ptr, fileMapping->length, fileMapping, freeFileMapping);
461 soup_message_body_append_buffer(msg->request_body, soupBuffer);
462 soup_buffer_free(soupBuffer);
468 d->m_msg = static_cast<SoupMessage*>(g_object_ref(msg));
469 soup_session_queue_message(session, d->m_msg, finishedCallback, this);
474 bool ResourceHandle::start(Frame* frame)
478 // If we are no longer attached to a Page, this must be an attempted load from an
479 // onUnload handler, so let's just block it.
483 KURL url = request().url();
484 String urlString = url.string();
485 String protocol = url.protocol();
487 if (equalIgnoringCase(protocol, "data"))
488 return startData(urlString);
489 else if ((equalIgnoringCase(protocol, "http") || equalIgnoringCase(protocol, "https")) && SOUP_URI_VALID_FOR_HTTP(soup_uri_new(urlString.utf8().data())))
490 return startHttp(urlString);
491 else if (equalIgnoringCase(protocol, "file") || equalIgnoringCase(protocol, "ftp") || equalIgnoringCase(protocol, "ftps"))
492 // FIXME: should we be doing any other protocols here?
493 return startGio(url);
495 // If we don't call didFail the job is not complete for webkit even false is returned.
497 ResourceError error("webkit-network-error", ERROR_UNKNOWN_PROTOCOL, urlString, protocol);
498 d->client()->didFail(this, error);
504 void ResourceHandle::cancel()
506 d->m_cancelled = true;
508 soup_session_cancel_message(session, d->m_msg, SOUP_STATUS_CANCELLED);
509 // For re-entrancy troubles we call didFinishLoading when the message hasn't been handled yet.
511 client()->didFinishLoading(this);
512 } else if (d->m_cancellable) {
513 g_cancellable_cancel(d->m_cancellable);
515 client()->didFinishLoading(this);
519 PassRefPtr<SharedBuffer> ResourceHandle::bufferedData()
521 ASSERT_NOT_REACHED();
525 bool ResourceHandle::supportsBufferedData()
530 void ResourceHandle::setDefersLoading(bool defers)
532 d->m_defersLoading = defers;
536 bool ResourceHandle::loadsBlocked()
541 bool ResourceHandle::willLoadFromCache(ResourceRequest&)
543 // Not having this function means that we'll ask the user about re-posting a form
544 // even when we go back to a page that's still in the cache.
549 void ResourceHandle::loadResourceSynchronously(const ResourceRequest& request, ResourceError& error, ResourceResponse& response, Vector<char>& data, Frame* frame)
551 WebCoreSynchronousLoader syncLoader(error, response, data);
552 ResourceHandle handle(request, &syncLoader, true, false, true);
560 static inline ResourceError networkErrorForFile(GFile* file, GError* error)
562 // FIXME: Map gio errors to a more detailed error code when we have it in WebKit.
563 gchar* uri = g_file_get_uri(file);
564 ResourceError resourceError("webkit-network-error", ERROR_TRANSPORT, uri, String::fromUTF8(error->message));
566 return resourceError;
569 static void cleanupGioOperation(ResourceHandleInternal* d)
572 g_object_set_data(G_OBJECT(d->m_gfile), "webkit-resource", 0);
573 g_object_unref(d->m_gfile);
576 if (d->m_cancellable) {
577 g_object_unref(d->m_cancellable);
578 d->m_cancellable = NULL;
580 if (d->m_input_stream) {
581 g_object_set_data(G_OBJECT(d->m_input_stream), "webkit-resource", 0);
582 g_object_unref(d->m_input_stream);
583 d->m_input_stream = NULL;
591 static void closeCallback(GObject* source, GAsyncResult* res, gpointer)
593 ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
597 ResourceHandleInternal* d = handle->getInternal();
598 ResourceHandleClient* client = handle->client();
600 g_input_stream_close_finish(d->m_input_stream, res, NULL);
601 cleanupGioOperation(d);
602 client->didFinishLoading(handle);
605 static void readCallback(GObject* source, GAsyncResult* res, gpointer)
607 ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
611 ResourceHandleInternal* d = handle->getInternal();
612 ResourceHandleClient* client = handle->client();
614 if (d->m_cancelled || !client) {
615 cleanupGioOperation(d);
622 nread = g_input_stream_read_finish(d->m_input_stream, res, &error);
624 ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
625 cleanupGioOperation(d);
626 client->didFail(handle, resourceError);
629 g_input_stream_close_async(d->m_input_stream, G_PRIORITY_DEFAULT,
630 NULL, closeCallback, NULL);
635 client->didReceiveData(handle, d->m_buffer, nread, d->m_total);
637 g_input_stream_read_async(d->m_input_stream, d->m_buffer, d->m_bufsize,
638 G_PRIORITY_DEFAULT, d->m_cancellable,
642 static void openCallback(GObject* source, GAsyncResult* res, gpointer)
644 ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
648 ResourceHandleInternal* d = handle->getInternal();
649 ResourceHandleClient* client = handle->client();
651 if (d->m_cancelled || !client) {
652 cleanupGioOperation(d);
656 GFileInputStream* in;
657 GError *error = NULL;
658 in = g_file_read_finish(G_FILE(source), res, &error);
660 ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
661 cleanupGioOperation(d);
662 client->didFail(handle, resourceError);
666 d->m_input_stream = G_INPUT_STREAM(in);
668 d->m_buffer = static_cast<char*>(g_malloc(d->m_bufsize));
670 g_object_set_data(G_OBJECT(d->m_input_stream), "webkit-resource", handle);
671 g_input_stream_read_async(d->m_input_stream, d->m_buffer, d->m_bufsize,
672 G_PRIORITY_DEFAULT, d->m_cancellable,
676 static void queryInfoCallback(GObject* source, GAsyncResult* res, gpointer)
678 ResourceHandle* handle = static_cast<ResourceHandle*>(g_object_get_data(source, "webkit-resource"));
682 ResourceHandleInternal* d = handle->getInternal();
683 ResourceHandleClient* client = handle->client();
685 if (d->m_cancelled) {
686 cleanupGioOperation(d);
690 ResourceResponse response;
692 char* uri = g_file_get_uri(d->m_gfile);
693 response.setUrl(KURL(uri));
696 GError *error = NULL;
697 GFileInfo* info = g_file_query_info_finish(d->m_gfile, res, &error);
700 // FIXME: to be able to handle ftp URIs properly, we must
701 // check if the error is G_IO_ERROR_NOT_MOUNTED, and if so,
702 // call g_file_mount_enclosing_volume() to mount the ftp
703 // server (and then keep track of the fact that we mounted it,
704 // and set a timeout to unmount it later after it's been idle
707 ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
708 cleanupGioOperation(d);
709 client->didFail(handle, resourceError);
713 if (g_file_info_get_file_type(info) != G_FILE_TYPE_REGULAR) {
714 // FIXME: what if the URI points to a directory? Should we
715 // generate a listing? How? What do other backends do here?
717 ResourceError resourceError = networkErrorForFile(d->m_gfile, error);
718 cleanupGioOperation(d);
719 client->didFail(handle, resourceError);
723 response.setMimeType(g_file_info_get_content_type(info));
724 response.setExpectedContentLength(g_file_info_get_size(info));
725 response.setHTTPStatusCode(SOUP_STATUS_OK);
728 g_file_info_get_modification_time(info, &tv);
729 response.setLastModifiedDate(tv.tv_sec);
731 client->didReceiveResponse(handle, response);
733 g_file_read_async(d->m_gfile, G_PRIORITY_DEFAULT, d->m_cancellable,
737 bool ResourceHandle::startGio(KURL url)
739 if (request().httpMethod() != "GET" && request().httpMethod() != "POST") {
740 ResourceError error("webkit-network-error", ERROR_BAD_NON_HTTP_METHOD, url.string(), request().httpMethod());
741 d->client()->didFail(this, error);
745 // GIO doesn't know how to handle refs and queries, so remove them
746 // TODO: use KURL.fileSystemPath after KURLGtk and FileSystemGtk are
747 // using GIO internally, and providing URIs instead of file paths
749 url.setQuery(String());
752 d->m_gfile = g_file_new_for_uri(url.string().utf8().data());
753 g_object_set_data(G_OBJECT(d->m_gfile), "webkit-resource", this);
754 d->m_cancellable = g_cancellable_new();
755 g_file_query_info_async(d->m_gfile,
756 G_FILE_ATTRIBUTE_STANDARD_TYPE ","
757 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
758 G_FILE_ATTRIBUTE_STANDARD_SIZE,
759 G_FILE_QUERY_INFO_NONE,
760 G_PRIORITY_DEFAULT, d->m_cancellable,
761 queryInfoCallback, NULL);