2009-10-19 21:50:50 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2009 Christian Dywan <christian@twotoasts.de>
|
2009-10-26 17:34:11 +00:00
|
|
|
Copyright (C) 2009 Alexander Butenko <a.butenka@gmail.com>
|
2009-10-19 21:50:50 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <midori/midori.h>
|
|
|
|
#include <glib/gstdio.h>
|
2009-10-27 19:03:22 +00:00
|
|
|
#include <stdlib.h>
|
2011-10-31 21:10:39 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
2009-10-19 21:50:50 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-01 04:59:00 +00:00
|
|
|
#if !WEBKIT_CHECK_VERSION (1, 3, 11)
|
|
|
|
|
2009-10-26 17:34:11 +00:00
|
|
|
#define MAXLENGTH 1024 * 1024
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2010-10-17 20:48:32 +00:00
|
|
|
static gchar*
|
|
|
|
web_cache_get_cache_dir (void)
|
|
|
|
{
|
|
|
|
static gchar* cache_dir = NULL;
|
|
|
|
if (!cache_dir)
|
|
|
|
cache_dir = g_build_filename (g_get_user_cache_dir (),
|
|
|
|
PACKAGE_NAME, "web", NULL);
|
|
|
|
return cache_dir;
|
|
|
|
}
|
|
|
|
|
2009-10-19 21:50:50 +00:00
|
|
|
static gchar*
|
2009-10-20 19:39:05 +00:00
|
|
|
web_cache_get_cached_path (MidoriExtension* extension,
|
|
|
|
const gchar* uri)
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
|
|
|
gchar* checksum;
|
2009-10-20 15:28:42 +00:00
|
|
|
gchar* folder;
|
|
|
|
gchar* sub_path;
|
2009-10-21 21:45:46 +00:00
|
|
|
gchar* encoded;
|
2009-10-20 19:39:05 +00:00
|
|
|
gchar* ext;
|
2009-10-19 21:50:50 +00:00
|
|
|
gchar* cached_filename;
|
|
|
|
gchar* cached_path;
|
|
|
|
|
|
|
|
checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
|
2009-10-20 15:28:42 +00:00
|
|
|
folder = g_strdup_printf ("%c%c", checksum[0], checksum[1]);
|
2010-10-17 20:48:32 +00:00
|
|
|
sub_path = g_build_path (G_DIR_SEPARATOR_S,
|
|
|
|
web_cache_get_cache_dir (), folder, NULL);
|
2009-12-27 21:00:39 +00:00
|
|
|
katze_mkdir_with_parents (sub_path, 0700);
|
2009-10-20 15:28:42 +00:00
|
|
|
g_free (folder);
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2009-10-21 21:45:46 +00:00
|
|
|
encoded = soup_uri_encode (uri, "/");
|
2009-10-22 21:28:45 +00:00
|
|
|
ext = g_strdup (g_strrstr (encoded, "."));
|
|
|
|
/* Make sure ext isn't becoming too long */
|
|
|
|
if (ext && ext[0] && ext[1] && ext[2] && ext[3] && ext[4])
|
|
|
|
ext[4] = '\0';
|
2009-10-20 19:39:05 +00:00
|
|
|
cached_filename = g_strdup_printf ("%s%s", checksum, ext ? ext : "");
|
2009-10-22 21:28:45 +00:00
|
|
|
g_free (ext);
|
2009-10-21 21:45:46 +00:00
|
|
|
g_free (encoded);
|
2009-10-19 21:50:50 +00:00
|
|
|
g_free (checksum);
|
2009-10-20 15:28:42 +00:00
|
|
|
cached_path = g_build_filename (sub_path, cached_filename, NULL);
|
2009-10-19 21:50:50 +00:00
|
|
|
g_free (cached_filename);
|
|
|
|
return cached_path;
|
|
|
|
}
|
|
|
|
|
2009-12-27 21:00:39 +00:00
|
|
|
static gboolean
|
2009-10-26 17:34:11 +00:00
|
|
|
web_cache_save_headers (SoupMessage* msg,
|
|
|
|
gchar* filename)
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
2009-10-27 19:03:22 +00:00
|
|
|
gchar* dsc_filename = g_strdup_printf ("%s.dsc.tmp", filename);
|
2009-10-26 17:34:11 +00:00
|
|
|
SoupMessageHeaders* hdrs = msg->response_headers;
|
|
|
|
SoupMessageHeadersIter iter;
|
|
|
|
const gchar* name, *value;
|
|
|
|
FILE* dscfd;
|
|
|
|
|
|
|
|
soup_message_headers_iter_init (&iter, hdrs);
|
2009-10-27 19:03:22 +00:00
|
|
|
dscfd = g_fopen (dsc_filename, "w");
|
2009-12-27 21:00:39 +00:00
|
|
|
g_free (dsc_filename);
|
|
|
|
if (!dscfd)
|
|
|
|
return FALSE;
|
|
|
|
|
2009-10-26 17:34:11 +00:00
|
|
|
while (soup_message_headers_iter_next (&iter, &name, &value))
|
|
|
|
g_fprintf (dscfd, "%s: %s\n", name, value);
|
|
|
|
fclose (dscfd);
|
2009-10-26 22:46:21 +00:00
|
|
|
|
2009-12-27 21:00:39 +00:00
|
|
|
return TRUE;
|
2009-10-26 17:34:11 +00:00
|
|
|
}
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2010-10-20 21:48:36 +00:00
|
|
|
static GHashTable*
|
2009-10-26 17:34:11 +00:00
|
|
|
web_cache_get_headers (gchar* filename)
|
|
|
|
{
|
|
|
|
GHashTable* headers;
|
|
|
|
FILE* file;
|
|
|
|
gchar* dsc_filename;
|
2010-01-17 21:49:13 +00:00
|
|
|
gchar line[128];
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2009-10-26 17:34:11 +00:00
|
|
|
if (!filename)
|
2009-12-18 20:03:00 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* use g_access() instead of g_file_test for better performance */
|
|
|
|
if (g_access (filename, F_OK) != 0)
|
|
|
|
return NULL;
|
2009-10-26 17:34:11 +00:00
|
|
|
|
|
|
|
dsc_filename = g_strdup_printf ("%s.dsc", filename);
|
2009-12-18 20:03:00 +00:00
|
|
|
headers = g_hash_table_new_full (g_str_hash, g_str_equal,
|
|
|
|
(GDestroyNotify)g_free,
|
|
|
|
(GDestroyNotify)g_free);
|
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
if (!(file = g_fopen (dsc_filename, "r")))
|
2009-10-20 19:39:05 +00:00
|
|
|
{
|
2010-01-17 21:49:13 +00:00
|
|
|
g_hash_table_destroy (headers);
|
|
|
|
g_free (dsc_filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while (fgets (line, 128, file))
|
|
|
|
{
|
|
|
|
gchar** data;
|
2009-12-21 22:48:51 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
if (line == NULL)
|
|
|
|
continue;
|
2009-12-21 22:48:51 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
g_strchomp (line);
|
|
|
|
data = g_strsplit (line, ":", 2);
|
|
|
|
if (data[0] && data[1])
|
|
|
|
g_hash_table_insert (headers, g_strdup (data[0]),
|
|
|
|
g_strdup (g_strchug (data[1])));
|
|
|
|
g_strfreev (data);
|
2009-10-20 19:39:05 +00:00
|
|
|
}
|
2010-01-17 21:49:13 +00:00
|
|
|
fclose (file);
|
2009-10-26 17:34:11 +00:00
|
|
|
g_free (dsc_filename);
|
2010-01-17 21:49:13 +00:00
|
|
|
return headers;
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|
2009-10-21 21:45:46 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
static GFile*
|
2009-10-27 19:03:22 +00:00
|
|
|
web_cache_tmp_prepare (gchar* filename)
|
|
|
|
{
|
2010-01-17 21:49:13 +00:00
|
|
|
GFile *file;
|
2009-10-27 19:03:22 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
gchar* tmp_filename = g_strdup_printf ("%s.tmp", filename);
|
2009-12-23 22:39:54 +00:00
|
|
|
if (g_access (tmp_filename, F_OK) == 0)
|
2009-10-27 22:41:22 +00:00
|
|
|
{
|
|
|
|
g_free (tmp_filename);
|
2010-01-17 21:49:13 +00:00
|
|
|
return NULL;
|
2009-10-27 22:41:22 +00:00
|
|
|
}
|
2010-01-17 21:49:13 +00:00
|
|
|
file = g_file_new_for_path (tmp_filename);
|
2009-10-27 19:03:22 +00:00
|
|
|
g_free (tmp_filename);
|
2010-01-17 21:49:13 +00:00
|
|
|
|
|
|
|
return file;
|
2009-10-27 19:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
web_cache_set_content_type (SoupMessage* msg,
|
|
|
|
SoupBuffer* buffer)
|
|
|
|
{
|
2010-01-17 21:49:13 +00:00
|
|
|
gchar* sniffed_type;
|
2009-10-27 19:03:22 +00:00
|
|
|
SoupContentSniffer* sniffer = soup_content_sniffer_new ();
|
2010-01-17 21:49:13 +00:00
|
|
|
if ((sniffed_type = soup_content_sniffer_sniff (sniffer, msg, buffer, NULL)))
|
|
|
|
{
|
|
|
|
g_signal_emit_by_name (msg, "content-sniffed", sniffed_type, NULL);
|
|
|
|
g_free (sniffed_type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const gchar* content_type = soup_message_headers_get_one (
|
|
|
|
msg->response_headers, "Content-Type");
|
|
|
|
g_signal_emit_by_name (msg, "content-sniffed", content_type, NULL);
|
|
|
|
}
|
2009-10-27 19:03:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-01-17 21:49:13 +00:00
|
|
|
web_cache_message_finished_cb (SoupMessage* msg,
|
|
|
|
GOutputStream* stream)
|
2009-10-27 19:03:22 +00:00
|
|
|
{
|
|
|
|
gchar* headers;
|
|
|
|
gchar* tmp_headers;
|
|
|
|
gchar* tmp_data;
|
2010-01-17 21:49:13 +00:00
|
|
|
gchar* filename;
|
2009-10-27 19:03:22 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
filename = g_object_get_data (G_OBJECT (stream), "filename");
|
2009-10-27 19:03:22 +00:00
|
|
|
headers = g_strdup_printf ("%s.dsc", filename);
|
|
|
|
tmp_headers = g_strdup_printf ("%s.dsc.tmp", filename);
|
|
|
|
tmp_data = g_strdup_printf ("%s.tmp", filename);
|
2010-01-17 21:49:13 +00:00
|
|
|
g_output_stream_close (stream, NULL, NULL);
|
2009-10-27 19:03:22 +00:00
|
|
|
|
2009-11-23 19:42:22 +00:00
|
|
|
if (msg->status_code == SOUP_STATUS_OK)
|
|
|
|
{
|
|
|
|
g_rename (tmp_data, filename);
|
|
|
|
g_rename (tmp_headers, headers);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_unlink (tmp_data);
|
|
|
|
g_unlink (tmp_headers);
|
|
|
|
}
|
2009-10-27 19:03:22 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
g_object_unref (stream);
|
2009-10-27 19:03:22 +00:00
|
|
|
g_free (headers);
|
|
|
|
g_free (tmp_headers);
|
|
|
|
g_free (tmp_data);
|
|
|
|
}
|
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
static void web_cache_pause_message (SoupMessage* msg)
|
|
|
|
{
|
|
|
|
SoupSession* session;
|
|
|
|
session = g_object_get_data (G_OBJECT (msg), "session");
|
|
|
|
soup_session_pause_message (session, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void web_cache_unpause_message (SoupMessage* msg)
|
|
|
|
{
|
|
|
|
SoupSession* session;
|
|
|
|
session = g_object_get_data (G_OBJECT (msg), "session");
|
|
|
|
soup_session_unpause_message (session, msg);
|
|
|
|
}
|
|
|
|
|
2009-10-19 21:50:50 +00:00
|
|
|
static void
|
2009-10-26 17:34:11 +00:00
|
|
|
web_cache_message_got_chunk_cb (SoupMessage* msg,
|
|
|
|
SoupBuffer* chunk,
|
2010-01-17 21:49:13 +00:00
|
|
|
GOutputStream* stream)
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
2009-10-26 17:34:11 +00:00
|
|
|
if (!chunk->data || !chunk->length)
|
|
|
|
return;
|
2010-01-17 21:49:13 +00:00
|
|
|
/* FIXME g_output_stream_write_async (stream, chunk->data, chunk->length,
|
|
|
|
G_PRIORITY_DEFAULT, NULL, NULL, (gpointer)chunk->length); */
|
|
|
|
g_output_stream_write (stream, chunk->data, chunk->length, NULL, NULL);
|
|
|
|
}
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
static void
|
|
|
|
web_cache_message_rewrite_async_cb (GFile *file,
|
|
|
|
GAsyncResult* res,
|
|
|
|
SoupMessage* msg)
|
|
|
|
{
|
|
|
|
SoupBuffer *buffer;
|
|
|
|
char *data;
|
|
|
|
gsize length;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (g_file_load_contents_finish (file, res, &data, &length, NULL, &error))
|
2009-10-26 17:34:11 +00:00
|
|
|
{
|
2010-01-17 21:49:13 +00:00
|
|
|
buffer = soup_buffer_new (SOUP_MEMORY_TEMPORARY, data, length);
|
|
|
|
web_cache_set_content_type (msg, buffer);
|
|
|
|
soup_message_body_append_buffer (msg->response_body, buffer);
|
|
|
|
/* FIXME? */
|
|
|
|
web_cache_unpause_message (msg);
|
|
|
|
g_signal_emit_by_name (msg, "got-chunk", buffer, NULL);
|
|
|
|
soup_buffer_free (buffer);
|
|
|
|
g_free (data);
|
|
|
|
soup_message_got_body (msg);
|
|
|
|
soup_message_finished (msg);
|
2009-10-26 17:34:11 +00:00
|
|
|
}
|
|
|
|
g_object_unref (file);
|
2010-01-17 21:49:13 +00:00
|
|
|
g_object_unref (msg);
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-10-26 17:34:11 +00:00
|
|
|
web_cache_message_rewrite (SoupMessage* msg,
|
|
|
|
gchar* filename)
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
2009-10-26 17:34:11 +00:00
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key, value;
|
2010-01-17 21:49:13 +00:00
|
|
|
GFile *file;
|
2009-10-26 17:34:11 +00:00
|
|
|
|
2010-01-17 21:49:13 +00:00
|
|
|
GHashTable* cache_headers = web_cache_get_headers (filename);
|
2009-12-18 20:03:00 +00:00
|
|
|
if (!cache_headers)
|
|
|
|
return;
|
|
|
|
|
2009-10-26 17:34:11 +00:00
|
|
|
soup_message_set_status (msg, SOUP_STATUS_OK);
|
|
|
|
g_hash_table_iter_init (&iter, cache_headers);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, &value))
|
|
|
|
soup_message_headers_replace (msg->response_headers, key, value);
|
|
|
|
g_signal_emit_by_name (msg, "got-headers", NULL);
|
2010-01-17 21:49:13 +00:00
|
|
|
g_hash_table_destroy (cache_headers);
|
|
|
|
|
|
|
|
/* FIXME? It seems libsoup already said "goodbye" by the time
|
|
|
|
the asynchronous function is starting to send data */
|
|
|
|
web_cache_pause_message (msg);
|
|
|
|
file = g_file_new_for_path (filename);
|
|
|
|
g_free (filename);
|
|
|
|
g_object_ref (msg);
|
|
|
|
g_file_load_contents_async (file, NULL,
|
|
|
|
(GAsyncReadyCallback)web_cache_message_rewrite_async_cb, msg);
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-12-18 20:03:00 +00:00
|
|
|
web_cache_mesage_got_headers_cb (SoupMessage* msg,
|
|
|
|
gchar* filename)
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
2009-10-26 17:34:11 +00:00
|
|
|
const gchar* nocache;
|
|
|
|
SoupMessageHeaders *hdrs = msg->response_headers;
|
2010-01-17 21:49:13 +00:00
|
|
|
const char* cl;
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2009-10-26 22:53:26 +00:00
|
|
|
/* Skip files downloaded by the user */
|
|
|
|
if (g_object_get_data (G_OBJECT (msg), "midori-web-cache-download"))
|
|
|
|
return;
|
|
|
|
|
2009-10-26 17:34:11 +00:00
|
|
|
/* Skip big files */
|
2010-01-17 21:49:13 +00:00
|
|
|
cl = soup_message_headers_get_one (hdrs, "Content-Length");
|
2009-10-27 19:03:22 +00:00
|
|
|
if (cl && atoi (cl) > MAXLENGTH)
|
2009-10-26 17:34:11 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
nocache = soup_message_headers_get_one (hdrs, "Pragma");
|
2009-10-27 19:03:22 +00:00
|
|
|
if (!nocache)
|
2009-10-26 17:34:11 +00:00
|
|
|
nocache = soup_message_headers_get_one (hdrs, "Cache-Control");
|
2009-10-27 19:03:22 +00:00
|
|
|
if (nocache && g_regex_match_simple ("no-cache|no-store", nocache,
|
|
|
|
G_REGEX_CASELESS, G_REGEX_MATCH_NOTEMPTY))
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
2009-10-27 19:03:22 +00:00
|
|
|
return;
|
2009-10-26 17:34:11 +00:00
|
|
|
}
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2009-10-26 17:34:11 +00:00
|
|
|
if (msg->status_code == SOUP_STATUS_NOT_MODIFIED)
|
|
|
|
{
|
2010-01-17 21:49:13 +00:00
|
|
|
/* g_debug ("loading from cache: %s", filename); */
|
2009-10-26 17:34:11 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (msg,
|
2009-12-18 20:03:00 +00:00
|
|
|
web_cache_mesage_got_headers_cb, filename);
|
2009-10-26 17:34:11 +00:00
|
|
|
web_cache_message_rewrite (msg, filename);
|
|
|
|
}
|
|
|
|
else if (msg->status_code == SOUP_STATUS_OK)
|
|
|
|
{
|
2010-01-17 21:49:13 +00:00
|
|
|
GFile* file;
|
|
|
|
GOutputStream* ostream;
|
|
|
|
|
|
|
|
/* g_debug ("updating cache: %s", filename); */
|
|
|
|
if (!(file = web_cache_tmp_prepare (filename)))
|
2009-10-27 22:41:22 +00:00
|
|
|
return;
|
2010-01-17 21:49:13 +00:00
|
|
|
if (!web_cache_save_headers (msg, filename))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ostream = (GOutputStream*)g_file_append_to (file,
|
|
|
|
G_FILE_CREATE_PRIVATE | G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL);
|
|
|
|
g_object_unref (file);
|
|
|
|
|
|
|
|
if (!ostream)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (ostream), "filename",
|
|
|
|
filename, (GDestroyNotify)g_free);
|
|
|
|
g_signal_connect (msg, "got-chunk",
|
|
|
|
G_CALLBACK (web_cache_message_got_chunk_cb), ostream);
|
|
|
|
g_signal_connect (msg, "finished",
|
|
|
|
G_CALLBACK (web_cache_message_finished_cb), ostream);
|
2009-10-26 17:34:11 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-19 21:50:50 +00:00
|
|
|
|
|
|
|
static void
|
2009-10-26 17:34:11 +00:00
|
|
|
web_cache_session_request_queued_cb (SoupSession* session,
|
|
|
|
SoupMessage* msg,
|
|
|
|
MidoriExtension* extension)
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
|
|
|
SoupURI* soup_uri = soup_message_get_uri (msg);
|
2009-12-18 20:03:00 +00:00
|
|
|
gchar* uri = soup_uri_to_string (soup_uri, FALSE);
|
2009-10-20 19:39:05 +00:00
|
|
|
|
2011-10-28 20:47:17 +00:00
|
|
|
if (midori_uri_is_http (uri) && !g_strcmp0 (msg->method, "GET"))
|
2009-10-19 21:50:50 +00:00
|
|
|
{
|
2009-10-26 17:34:11 +00:00
|
|
|
gchar* filename = web_cache_get_cached_path (extension, uri);
|
2009-12-18 20:03:00 +00:00
|
|
|
GHashTable* cache_headers;
|
|
|
|
gchar* etag;
|
|
|
|
gchar* last_modified;
|
2009-10-26 17:34:11 +00:00
|
|
|
|
2009-12-18 20:03:00 +00:00
|
|
|
cache_headers = web_cache_get_headers (filename);
|
|
|
|
if (cache_headers)
|
|
|
|
{
|
2009-10-26 17:34:11 +00:00
|
|
|
etag = g_hash_table_lookup (cache_headers, "ETag");
|
|
|
|
last_modified = g_hash_table_lookup (cache_headers, "Last-Modified");
|
|
|
|
if (etag)
|
2009-10-27 22:41:22 +00:00
|
|
|
soup_message_headers_replace (msg->request_headers,
|
2009-10-26 17:34:11 +00:00
|
|
|
"If-None-Match", etag);
|
|
|
|
if (last_modified)
|
2009-10-27 19:03:22 +00:00
|
|
|
soup_message_headers_replace (msg->request_headers,
|
|
|
|
"If-Modified-Since", last_modified);
|
2009-12-18 20:03:00 +00:00
|
|
|
g_hash_table_destroy (cache_headers);
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|
2010-01-17 21:49:13 +00:00
|
|
|
g_object_set_data (G_OBJECT (msg), "session", session);
|
2009-12-18 20:03:00 +00:00
|
|
|
g_signal_connect (msg, "got-headers",
|
|
|
|
G_CALLBACK (web_cache_mesage_got_headers_cb), filename);
|
|
|
|
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|
|
|
|
g_free (uri);
|
|
|
|
}
|
|
|
|
|
2009-10-26 22:53:26 +00:00
|
|
|
static void
|
|
|
|
web_cache_add_download_cb (MidoriBrowser* browser,
|
|
|
|
WebKitDownload* download,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
WebKitNetworkRequest* request = webkit_download_get_network_request (download);
|
|
|
|
SoupMessage* msg = webkit_network_request_get_message (request);
|
|
|
|
if (msg)
|
|
|
|
g_object_set_data (G_OBJECT (msg), "midori-web-cache-download",
|
|
|
|
(gpointer)0xdeadbeef);
|
|
|
|
}
|
|
|
|
|
2009-10-19 21:50:50 +00:00
|
|
|
static void
|
|
|
|
web_cache_deactivate_cb (MidoriExtension* extension,
|
|
|
|
MidoriBrowser* browser);
|
|
|
|
|
|
|
|
static void
|
|
|
|
web_cache_app_add_browser_cb (MidoriApp* app,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
2009-10-26 22:53:26 +00:00
|
|
|
g_signal_connect (browser, "add-download",
|
|
|
|
G_CALLBACK (web_cache_add_download_cb), extension);
|
2009-10-19 21:50:50 +00:00
|
|
|
g_signal_connect (extension, "deactivate",
|
|
|
|
G_CALLBACK (web_cache_deactivate_cb), browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
web_cache_deactivate_cb (MidoriExtension* extension,
|
|
|
|
MidoriBrowser* browser)
|
|
|
|
{
|
|
|
|
MidoriApp* app = midori_extension_get_app (extension);
|
2009-10-23 16:15:28 +00:00
|
|
|
SoupSession* session = webkit_get_default_session ();
|
2009-10-19 21:50:50 +00:00
|
|
|
|
2009-10-23 16:15:28 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
session, web_cache_session_request_queued_cb, extension);
|
2009-10-19 21:50:50 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
extension, web_cache_deactivate_cb, browser);
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
app, web_cache_app_add_browser_cb, extension);
|
2009-10-26 22:53:26 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
browser, web_cache_add_download_cb, extension);
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
web_cache_activate_cb (MidoriExtension* extension,
|
|
|
|
MidoriApp* app)
|
|
|
|
{
|
|
|
|
KatzeArray* browsers;
|
|
|
|
MidoriBrowser* browser;
|
|
|
|
SoupSession* session = webkit_get_default_session ();
|
|
|
|
|
2010-10-17 20:48:32 +00:00
|
|
|
katze_mkdir_with_parents (web_cache_get_cache_dir (), 0700);
|
2009-10-19 21:50:50 +00:00
|
|
|
g_signal_connect (session, "request-queued",
|
|
|
|
G_CALLBACK (web_cache_session_request_queued_cb), extension);
|
|
|
|
|
|
|
|
browsers = katze_object_get_object (app, "browsers");
|
2010-09-11 21:39:51 +00:00
|
|
|
KATZE_ARRAY_FOREACH_ITEM (browser, browsers)
|
2009-10-19 21:50:50 +00:00
|
|
|
web_cache_app_add_browser_cb (app, browser, extension);
|
|
|
|
g_signal_connect (app, "add-browser",
|
|
|
|
G_CALLBACK (web_cache_app_add_browser_cb), extension);
|
|
|
|
|
|
|
|
g_object_unref (browsers);
|
|
|
|
}
|
|
|
|
|
2010-10-17 20:48:32 +00:00
|
|
|
static void
|
|
|
|
web_cache_clear_cache_cb (void)
|
|
|
|
{
|
|
|
|
sokoke_remove_path (web_cache_get_cache_dir (), TRUE);
|
|
|
|
}
|
2011-05-01 04:59:00 +00:00
|
|
|
#endif
|
2010-10-17 20:48:32 +00:00
|
|
|
|
2009-10-19 21:50:50 +00:00
|
|
|
MidoriExtension*
|
|
|
|
extension_init (void)
|
|
|
|
{
|
2011-05-01 04:59:00 +00:00
|
|
|
#if WEBKIT_CHECK_VERSION (1, 3, 11)
|
|
|
|
return NULL;
|
|
|
|
#else
|
2009-10-19 21:50:50 +00:00
|
|
|
MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
|
|
|
"name", _("Web Cache"),
|
|
|
|
"description", _("Cache HTTP communication on disk"),
|
|
|
|
"version", "0.1",
|
|
|
|
"authors", "Christian Dywan <christian@twotoasts.de>",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_signal_connect (extension, "activate",
|
|
|
|
G_CALLBACK (web_cache_activate_cb), NULL);
|
|
|
|
|
2010-10-17 20:48:32 +00:00
|
|
|
sokoke_register_privacy_item ("web-cache", _("Web Cache"),
|
|
|
|
G_CALLBACK (web_cache_clear_cache_cb));
|
|
|
|
|
2009-10-19 21:50:50 +00:00
|
|
|
return extension;
|
2011-05-01 04:59:00 +00:00
|
|
|
#endif
|
2009-10-19 21:50:50 +00:00
|
|
|
}
|