|
Line 0
a/WebCore/platform/network/soup/cache/webkit/soup-cache.c_sec1
|
|
|
1 |
/* |
| 2 |
* soup-cache.c |
| 3 |
* |
| 4 |
* Copyright (C) 2009, 2010 Igalia S.L. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Library General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Library General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Library General Public License |
| 17 |
* along with this library; see the file COPYING.LIB. If not, write to |
| 18 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 |
* Boston, MA 02110-1301, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* TODO: |
| 23 |
* - Need to hook the feature in the sync SoupSession. |
| 24 |
* - Need more tests. |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
#include <config.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "soup-cache.h" |
| 32 |
#include "soup-cache-private.h" |
| 33 |
#include <libsoup/soup.h> |
| 34 |
#include <gio/gio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
|
| 37 |
static SoupSessionFeatureInterface *webkit_soup_cache_default_feature_interface; |
| 38 |
static void webkit_soup_cache_session_feature_init (SoupSessionFeatureInterface *feature_interface, gpointer interface_data); |
| 39 |
|
| 40 |
#define DEFAULT_MAX_SIZE 50 * 1024 * 1024 |
| 41 |
#define MAX_ENTRY_DATA_PERCENTAGE 10 /* Percentage of the total size |
| 42 |
of the cache that can be |
| 43 |
filled by a single entry */ |
| 44 |
|
| 45 |
typedef struct _WebKitSoupCacheEntry { |
| 46 |
char *key; |
| 47 |
char *filename; |
| 48 |
guint freshness_lifetime; |
| 49 |
gboolean must_revalidate; |
| 50 |
GString *data; |
| 51 |
gsize pos; |
| 52 |
gsize length; |
| 53 |
time_t corrected_initial_age; |
| 54 |
time_t response_time; |
| 55 |
gboolean writing; |
| 56 |
gboolean dirty; |
| 57 |
gboolean got_body; |
| 58 |
gboolean being_validated; |
| 59 |
SoupMessageHeaders *headers; |
| 60 |
GOutputStream *stream; |
| 61 |
GError *error; |
| 62 |
guint hits; |
| 63 |
GCancellable *cancellable; |
| 64 |
} WebKitSoupCacheEntry; |
| 65 |
|
| 66 |
struct _WebKitSoupCachePrivate { |
| 67 |
char *cache_dir; |
| 68 |
GHashTable *cache; |
| 69 |
guint n_pending; |
| 70 |
SoupSession *session; |
| 71 |
WebKitSoupCacheType cache_type; |
| 72 |
guint size; |
| 73 |
guint max_size; |
| 74 |
guint max_entry_data_size; /* Computed value. Here for performance reasons */ |
| 75 |
GList *lru_start; |
| 76 |
}; |
| 77 |
|
| 78 |
typedef struct { |
| 79 |
WebKitSoupCache *cache; |
| 80 |
WebKitSoupCacheEntry *entry; |
| 81 |
SoupMessage *msg; |
| 82 |
gulong got_chunk_handler; |
| 83 |
gulong got_body_handler; |
| 84 |
gulong restarted_handler; |
| 85 |
} WebKitSoupCacheWritingFixture; |
| 86 |
|
| 87 |
enum { |
| 88 |
PROP_0, |
| 89 |
PROP_CACHE_DIR, |
| 90 |
PROP_CACHE_TYPE |
| 91 |
}; |
| 92 |
|
| 93 |
#define WEBKIT_SOUP_CACHE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), WEBKIT_TYPE_SOUP_CACHE, WebKitSoupCachePrivate)) |
| 94 |
|
| 95 |
G_DEFINE_TYPE_WITH_CODE (WebKitSoupCache, webkit_soup_cache, G_TYPE_OBJECT, |
| 96 |
G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE, |
| 97 |
webkit_soup_cache_session_feature_init)) |
| 98 |
|
| 99 |
static gboolean webkit_soup_cache_entry_remove (WebKitSoupCache *cache, WebKitSoupCacheEntry *entry); |
| 100 |
static void make_room_for_new_entry (WebKitSoupCache *cache, guint length_to_add); |
| 101 |
static gboolean cache_accepts_entries_of_size (WebKitSoupCache *cache, guint length_to_add); |
| 102 |
|
| 103 |
static WebKitSoupCacheability |
| 104 |
get_cacheability (WebKitSoupCache *cache, SoupMessage *msg) |
| 105 |
{ |
| 106 |
WebKitSoupCacheability cacheability; |
| 107 |
const char *cache_control; |
| 108 |
|
| 109 |
/* 1. The request method must be cacheable */ |
| 110 |
if (msg->method == SOUP_METHOD_GET) |
| 111 |
cacheability = WEBKIT_SOUP_CACHE_CACHEABLE; |
| 112 |
else if (msg->method == SOUP_METHOD_HEAD || |
| 113 |
msg->method == SOUP_METHOD_TRACE || |
| 114 |
msg->method == SOUP_METHOD_CONNECT) |
| 115 |
return WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 116 |
else |
| 117 |
return (WEBKIT_SOUP_CACHE_UNCACHEABLE | WEBKIT_SOUP_CACHE_INVALIDATES); |
| 118 |
|
| 119 |
cache_control = soup_message_headers_get (msg->response_headers, "Cache-Control"); |
| 120 |
if (cache_control) { |
| 121 |
GHashTable *hash; |
| 122 |
WebKitSoupCachePrivate *priv = WEBKIT_SOUP_CACHE_GET_PRIVATE (cache); |
| 123 |
|
| 124 |
hash = soup_header_parse_param_list (cache_control); |
| 125 |
|
| 126 |
/* Shared caches MUST NOT store private resources */ |
| 127 |
if (priv->cache_type == WEBKIT_SOUP_CACHE_SHARED) { |
| 128 |
if (g_hash_table_lookup_extended (hash, "private", NULL, NULL)) { |
| 129 |
soup_header_free_param_list (hash); |
| 130 |
return WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/* 2. The 'no-store' cache directive does not appear in the |
| 135 |
* headers |
| 136 |
*/ |
| 137 |
if (g_hash_table_lookup_extended (hash, "no-store", NULL, NULL)) { |
| 138 |
soup_header_free_param_list (hash); |
| 139 |
return WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 140 |
} |
| 141 |
|
| 142 |
/* This does not appear in section 2.1, but I think it makes |
| 143 |
* sense to check it too? |
| 144 |
*/ |
| 145 |
if (g_hash_table_lookup_extended (hash, "no-cache", NULL, NULL)) { |
| 146 |
soup_header_free_param_list (hash); |
| 147 |
return WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
switch (msg->status_code) { |
| 152 |
case SOUP_STATUS_PARTIAL_CONTENT: |
| 153 |
/* We don't cache partial responses, but they only |
| 154 |
* invalidate cached full responses if the headers |
| 155 |
* don't match. |
| 156 |
*/ |
| 157 |
cacheability = WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 158 |
break; |
| 159 |
|
| 160 |
case SOUP_STATUS_NOT_MODIFIED: |
| 161 |
/* A 304 response validates an existing cache entry */ |
| 162 |
cacheability = WEBKIT_SOUP_CACHE_VALIDATES; |
| 163 |
break; |
| 164 |
|
| 165 |
case SOUP_STATUS_MULTIPLE_CHOICES: |
| 166 |
case SOUP_STATUS_MOVED_PERMANENTLY: |
| 167 |
case SOUP_STATUS_GONE: |
| 168 |
/* FIXME: cacheable unless indicated otherwise */ |
| 169 |
cacheability = WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 170 |
break; |
| 171 |
|
| 172 |
case SOUP_STATUS_FOUND: |
| 173 |
case SOUP_STATUS_TEMPORARY_REDIRECT: |
| 174 |
/* FIXME: cacheable if explicitly indicated */ |
| 175 |
cacheability = WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 176 |
break; |
| 177 |
|
| 178 |
case SOUP_STATUS_SEE_OTHER: |
| 179 |
case SOUP_STATUS_FORBIDDEN: |
| 180 |
case SOUP_STATUS_NOT_FOUND: |
| 181 |
case SOUP_STATUS_METHOD_NOT_ALLOWED: |
| 182 |
return (WEBKIT_SOUP_CACHE_UNCACHEABLE | WEBKIT_SOUP_CACHE_INVALIDATES); |
| 183 |
|
| 184 |
default: |
| 185 |
/* Any 5xx status or any 4xx status not handled above |
| 186 |
* is uncacheable but doesn't break the cache. |
| 187 |
*/ |
| 188 |
if ((msg->status_code >= SOUP_STATUS_BAD_REQUEST && |
| 189 |
msg->status_code <= SOUP_STATUS_FAILED_DEPENDENCY) || |
| 190 |
msg->status_code >= SOUP_STATUS_INTERNAL_SERVER_ERROR) |
| 191 |
return WEBKIT_SOUP_CACHE_UNCACHEABLE; |
| 192 |
|
| 193 |
/* An unrecognized 2xx, 3xx, or 4xx response breaks |
| 194 |
* the cache. |
| 195 |
*/ |
| 196 |
if ((msg->status_code > SOUP_STATUS_PARTIAL_CONTENT && |
| 197 |
msg->status_code < SOUP_STATUS_MULTIPLE_CHOICES) || |
| 198 |
(msg->status_code > SOUP_STATUS_TEMPORARY_REDIRECT && |
| 199 |
msg->status_code < SOUP_STATUS_INTERNAL_SERVER_ERROR)) |
| 200 |
return (WEBKIT_SOUP_CACHE_UNCACHEABLE | WEBKIT_SOUP_CACHE_INVALIDATES); |
| 201 |
break; |
| 202 |
} |
| 203 |
|
| 204 |
return cacheability; |
| 205 |
} |
| 206 |
|
| 207 |
static void |
| 208 |
webkit_soup_cache_entry_free (WebKitSoupCacheEntry *entry, gboolean purge) |
| 209 |
{ |
| 210 |
if (purge) { |
| 211 |
GFile *file = g_file_new_for_path (entry->filename); |
| 212 |
g_file_delete (file, NULL, NULL); |
| 213 |
g_object_unref (file); |
| 214 |
} |
| 215 |
|
| 216 |
g_free (entry->filename); |
| 217 |
entry->filename = NULL; |
| 218 |
g_free (entry->key); |
| 219 |
entry->key = NULL; |
| 220 |
|
| 221 |
if (entry->headers) { |
| 222 |
soup_message_headers_free (entry->headers); |
| 223 |
entry->headers = NULL; |
| 224 |
} |
| 225 |
|
| 226 |
if (entry->data) { |
| 227 |
g_string_free (entry->data, TRUE); |
| 228 |
entry->data = NULL; |
| 229 |
} |
| 230 |
if (entry->error) { |
| 231 |
g_error_free (entry->error); |
| 232 |
entry->error = NULL; |
| 233 |
} |
| 234 |
if (entry->cancellable) { |
| 235 |
g_object_unref (entry->cancellable); |
| 236 |
entry->cancellable = NULL; |
| 237 |
} |
| 238 |
|
| 239 |
g_slice_free (WebKitSoupCacheEntry, entry); |
| 240 |
} |
| 241 |
|
| 242 |
static void |
| 243 |
copy_headers (const char *name, const char *value, SoupMessageHeaders *headers) |
| 244 |
{ |
| 245 |
soup_message_headers_append (headers, name, value); |
| 246 |
} |
| 247 |
|
| 248 |
static void |
| 249 |
update_headers (const char *name, const char *value, SoupMessageHeaders *headers) |
| 250 |
{ |
| 251 |
if (soup_message_headers_get (headers, name)) |
| 252 |
soup_message_headers_replace (headers, name, value); |
| 253 |
else |
| 254 |
soup_message_headers_append (headers, name, value); |
| 255 |
} |
| 256 |
|
| 257 |
static guint |
| 258 |
webkit_soup_cache_entry_get_current_age (WebKitSoupCacheEntry *entry) |
| 259 |
{ |
| 260 |
time_t now = time (NULL); |
| 261 |
time_t resident_time; |
| 262 |
|
| 263 |
resident_time = now - entry->response_time; |
| 264 |
return entry->corrected_initial_age + resident_time; |
| 265 |
} |
| 266 |
|
| 267 |
static gboolean |
| 268 |
webkit_soup_cache_entry_is_fresh_enough (WebKitSoupCacheEntry *entry, int min_fresh) |
| 269 |
{ |
| 270 |
unsigned limit = (min_fresh == -1) ? webkit_soup_cache_entry_get_current_age (entry) : min_fresh; |
| 271 |
return entry->freshness_lifetime > limit; |
| 272 |
} |
| 273 |
|
| 274 |
static char * |
| 275 |
soup_message_get_cache_key (SoupMessage *msg) |
| 276 |
{ |
| 277 |
SoupURI *uri = soup_message_get_uri (msg); |
| 278 |
return soup_uri_to_string (uri, FALSE); |
| 279 |
} |
| 280 |
|
| 281 |
static void |
| 282 |
webkit_soup_cache_entry_set_freshness (WebKitSoupCacheEntry *entry, SoupMessage *msg, WebKitSoupCache *cache) |
| 283 |
{ |
| 284 |
const char *cache_control; |
| 285 |
const char *expires, *date, *last_modified; |
| 286 |
GHashTable *hash; |
| 287 |
|
| 288 |
hash = NULL; |
| 289 |
|
| 290 |
cache_control = soup_message_headers_get (entry->headers, "Cache-Control"); |
| 291 |
if (cache_control) { |
| 292 |
const char *max_age, *s_maxage; |
| 293 |
gint64 freshness_lifetime = 0; |
| 294 |
WebKitSoupCachePrivate *priv = WEBKIT_SOUP_CACHE_GET_PRIVATE (cache); |
| 295 |
|
| 296 |
hash = soup_header_parse_param_list (cache_control); |
| 297 |
|
| 298 |
/* Should we re-validate the entry when it goes stale */ |
| 299 |
entry->must_revalidate = (gboolean)g_hash_table_lookup (hash, "must-revalidate"); |
| 300 |
|
| 301 |
/* Section 2.3.1 */ |
| 302 |
if (priv->cache_type == WEBKIT_SOUP_CACHE_SHARED) { |
| 303 |
s_maxage = g_hash_table_lookup (hash, "s-maxage"); |
| 304 |
if (s_maxage) { |
| 305 |
freshness_lifetime = g_ascii_strtoll (s_maxage, NULL, 10); |
| 306 |
if (freshness_lifetime) { |
| 307 |
/* Implies proxy-revalidate. TODO: is it true? */ |
| 308 |
entry->must_revalidate = TRUE; |
| 309 |
soup_header_free_param_list (hash); |
| 310 |
return; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/* If 'max-age' cache directive is present, use that */ |
| 316 |
max_age = g_hash_table_lookup (hash, "max-age"); |
| 317 |
if (max_age) |
| 318 |
freshness_lifetime = g_ascii_strtoll (max_age, NULL, 10); |
| 319 |
|
| 320 |
if (freshness_lifetime) { |
| 321 |
entry->freshness_lifetime = (guint)MIN (freshness_lifetime, G_MAXUINT32); |
| 322 |
soup_header_free_param_list (hash); |
| 323 |
return; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (hash != NULL) |
| 328 |
soup_header_free_param_list (hash); |
| 329 |
|
| 330 |
/* If the 'Expires' response header is present, use its value |
| 331 |
* minus the value of the 'Date' response header |
| 332 |
*/ |
| 333 |
expires = soup_message_headers_get (entry->headers, "Expires"); |
| 334 |
date = soup_message_headers_get (entry->headers, "Date"); |
| 335 |
if (expires && date) { |
| 336 |
SoupDate *expires_d, *date_d; |
| 337 |
time_t expires_t, date_t; |
| 338 |
|
| 339 |
expires_d = soup_date_new_from_string (expires); |
| 340 |
if (expires_d) { |
| 341 |
date_d = soup_date_new_from_string (date); |
| 342 |
|
| 343 |
expires_t = soup_date_to_time_t (expires_d); |
| 344 |
date_t = soup_date_to_time_t (date_d); |
| 345 |
|
| 346 |
soup_date_free (expires_d); |
| 347 |
soup_date_free (date_d); |
| 348 |
|
| 349 |
if (expires_t && date_t) { |
| 350 |
entry->freshness_lifetime = (guint)MAX (expires_t - date_t, 0); |
| 351 |
return; |
| 352 |
} |
| 353 |
} else { |
| 354 |
/* If Expires is not a valid date we should |
| 355 |
treat it as already expired, see section |
| 356 |
3.3 */ |
| 357 |
entry->freshness_lifetime = 0; |
| 358 |
return; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
/* Otherwise an heuristic may be used */ |
| 363 |
|
| 364 |
/* Heuristics MUST NOT be used with these status codes |
| 365 |
(section 2.3.1.1) */ |
| 366 |
if (msg->status_code != SOUP_STATUS_OK && |
| 367 |
msg->status_code != SOUP_STATUS_NON_AUTHORITATIVE && |
| 368 |
msg->status_code != SOUP_STATUS_PARTIAL_CONTENT && |
| 369 |
msg->status_code != SOUP_STATUS_MULTIPLE_CHOICES && |
| 370 |
msg->status_code != SOUP_STATUS_MOVED_PERMANENTLY && |
| 371 |
msg->status_code != SOUP_STATUS_GONE) |
| 372 |
goto expire; |
| 373 |
|
| 374 |
/* TODO: attach warning 113 if response's current_age is more |
| 375 |
than 24h (section 2.3.1.1) when using heuristics */ |
| 376 |
|
| 377 |
/* Last-Modified based heuristic */ |
| 378 |
last_modified = soup_message_headers_get (entry->headers, "Last-Modified"); |
| 379 |
if (last_modified) { |
| 380 |
SoupDate *soup_date; |
| 381 |
time_t now, last_modified_t; |
| 382 |
|
| 383 |
soup_date = soup_date_new_from_string (last_modified); |
| 384 |
last_modified_t = soup_date_to_time_t (soup_date); |
| 385 |
now = time (NULL); |
| 386 |
|
| 387 |
#define HEURISTIC_FACTOR 0.1 /* From Section 2.3.1.1 */ |
| 388 |
|
| 389 |
entry->freshness_lifetime = MAX (0, (now - last_modified_t) * HEURISTIC_FACTOR); |
| 390 |
soup_date_free (soup_date); |
| 391 |
} |
| 392 |
|
| 393 |
return; |
| 394 |
|
| 395 |
expire: |
| 396 |
/* If all else fails, make the entry expire immediately */ |
| 397 |
entry->freshness_lifetime = 0; |
| 398 |
} |
| 399 |
|
| 400 |
static WebKitSoupCacheEntry * |
| 401 |
webkit_soup_cache_entry_new (WebKitSoupCache *cache, SoupMessage *msg, time_t request_time, time_t response_time) |
| 402 |
{ |
| 403 |
WebKitSoupCacheEntry *entry; |
| 404 |
SoupMessageHeaders *headers; |
| 405 |
const char *date; |
| 406 |
char *md5; |
| 407 |
|
| 408 |
entry = g_slice_new0 (WebKitSoupCacheEntry); |
| 409 |
entry->dirty = FALSE; |
| 410 |
entry->writing = FALSE; |
| 411 |
entry->got_body = FALSE; |
| 412 |
entry->being_validated = FALSE; |
| 413 |
entry->data = g_string_new (NULL); |
| 414 |
entry->pos = 0; |
| 415 |
entry->error = NULL; |
| 416 |
|
| 417 |
/* key & filename */ |
| 418 |
entry->key = soup_message_get_cache_key (msg); |
| 419 |
md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, entry->key, -1); |
| 420 |
entry->filename = g_build_filename (cache->priv->cache_dir, md5, NULL); |
| 421 |
g_free (md5); |
| 422 |
|
| 423 |
/* Headers */ |
| 424 |
headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE); |
| 425 |
soup_message_headers_foreach (msg->response_headers, |
| 426 |
(SoupMessageHeadersForeachFunc)copy_headers, |
| 427 |
headers); |
| 428 |
entry->headers = headers; |
| 429 |
|
| 430 |
/* LRU list */ |
| 431 |
entry->hits = 0; |
| 432 |
|
| 433 |
/* Section 2.3.1, Freshness Lifetime */ |
| 434 |
webkit_soup_cache_entry_set_freshness (entry, msg, cache); |
| 435 |
|
| 436 |
/* Section 2.3.2, Calculating Age */ |
| 437 |
date = soup_message_headers_get (entry->headers, "Date"); |
| 438 |
|
| 439 |
if (date) { |
| 440 |
SoupDate *soup_date; |
| 441 |
const char *age; |
| 442 |
time_t date_value, apparent_age, corrected_received_age, response_delay, age_value = 0; |
| 443 |
|
| 444 |
soup_date = soup_date_new_from_string (date); |
| 445 |
date_value = soup_date_to_time_t (soup_date); |
| 446 |
soup_date_free (soup_date); |
| 447 |
|
| 448 |
age = soup_message_headers_get (entry->headers, "Age"); |
| 449 |
if (age) |
| 450 |
age_value = g_ascii_strtoll (age, NULL, 10); |
| 451 |
|
| 452 |
entry->response_time = response_time; |
| 453 |
apparent_age = MAX (0, entry->response_time - date_value); |
| 454 |
corrected_received_age = MAX (apparent_age, age_value); |
| 455 |
response_delay = entry->response_time - request_time; |
| 456 |
entry->corrected_initial_age = corrected_received_age + response_delay; |
| 457 |
} else { |
| 458 |
/* Is this correct ? */ |
| 459 |
entry->corrected_initial_age = time (NULL); |
| 460 |
} |
| 461 |
|
| 462 |
return entry; |
| 463 |
} |
| 464 |
|
| 465 |
static void |
| 466 |
webkit_soup_cache_writing_fixture_free (WebKitSoupCacheWritingFixture *fixture) |
| 467 |
{ |
| 468 |
/* Free fixture. And disconnect signals, we don't want to |
| 469 |
listen to more SoupMessage events as we're finished with |
| 470 |
this resource */ |
| 471 |
if (g_signal_handler_is_connected (fixture->msg, fixture->got_chunk_handler)) |
| 472 |
g_signal_handler_disconnect (fixture->msg, fixture->got_chunk_handler); |
| 473 |
if (g_signal_handler_is_connected (fixture->msg, fixture->got_body_handler)) |
| 474 |
g_signal_handler_disconnect (fixture->msg, fixture->got_body_handler); |
| 475 |
if (g_signal_handler_is_connected (fixture->msg, fixture->restarted_handler)) |
| 476 |
g_signal_handler_disconnect (fixture->msg, fixture->restarted_handler); |
| 477 |
g_object_unref (fixture->msg); |
| 478 |
g_object_unref (fixture->cache); |
| 479 |
g_slice_free (WebKitSoupCacheWritingFixture, fixture); |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
close_ready_cb (GObject *source, GAsyncResult *result, WebKitSoupCacheWritingFixture *fixture) |
| 484 |
{ |
| 485 |
WebKitSoupCacheEntry *entry = fixture->entry; |
| 486 |
WebKitSoupCache *cache = fixture->cache; |
| 487 |
GOutputStream *stream = G_OUTPUT_STREAM (source); |
| 488 |
goffset content_length; |
| 489 |
|
| 490 |
g_warn_if_fail (entry->error == NULL); |
| 491 |
|
| 492 |
/* FIXME: what do we do on error ? */ |
| 493 |
|
| 494 |
if (stream) { |
| 495 |
g_output_stream_close_finish (stream, result, NULL); |
| 496 |
g_object_unref (stream); |
| 497 |
} |
| 498 |
entry->stream = NULL; |
| 499 |
|
| 500 |
content_length = soup_message_headers_get_content_length (entry->headers); |
| 501 |
|
| 502 |
/* If the process was cancelled, then delete the entry from |
| 503 |
the cache. Do it also if the size of a chunked resource is |
| 504 |
too much for the cache */ |
| 505 |
if (g_cancellable_is_cancelled (entry->cancellable)) { |
| 506 |
entry->dirty = FALSE; |
| 507 |
webkit_soup_cache_entry_remove (cache, entry); |
| 508 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 509 |
entry = NULL; |
| 510 |
} else if ((soup_message_headers_get_encoding (entry->headers) == SOUP_ENCODING_CHUNKED) || |
| 511 |
entry->length != content_length) { |
| 512 |
/** Two options here: |
| 513 |
* |
| 514 |
* 1. "chunked" data, entry was temporarily added to |
| 515 |
* cache (as content-length is 0) and now that we have |
| 516 |
* the actual size we have to evaluate if we want it |
| 517 |
* in the cache or not |
| 518 |
* |
| 519 |
* 2. Content-Length has a different value than actual |
| 520 |
* length, means that the content was encoded for |
| 521 |
* transmission (typically compressed) and thus we |
| 522 |
* have to substract the content-length value that was |
| 523 |
* added to the cache and add the unencoded length |
| 524 |
**/ |
| 525 |
gint length_to_add = entry->length - content_length; |
| 526 |
|
| 527 |
/* Make room in cache if needed */ |
| 528 |
if (cache_accepts_entries_of_size (cache, length_to_add)) { |
| 529 |
make_room_for_new_entry (cache, length_to_add); |
| 530 |
|
| 531 |
cache->priv->size += length_to_add; |
| 532 |
} else { |
| 533 |
entry->dirty = FALSE; |
| 534 |
webkit_soup_cache_entry_remove (cache, entry); |
| 535 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 536 |
entry = NULL; |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
if (entry) { |
| 541 |
/* Get rid of the GString in memory for the resource now */ |
| 542 |
if (entry->data) { |
| 543 |
g_string_free (entry->data, TRUE); |
| 544 |
entry->data = NULL; |
| 545 |
} |
| 546 |
|
| 547 |
entry->dirty = FALSE; |
| 548 |
entry->writing = FALSE; |
| 549 |
entry->got_body = FALSE; |
| 550 |
entry->pos = 0; |
| 551 |
|
| 552 |
g_object_unref (entry->cancellable); |
| 553 |
entry->cancellable = NULL; |
| 554 |
} |
| 555 |
|
| 556 |
cache->priv->n_pending--; |
| 557 |
|
| 558 |
/* Frees */ |
| 559 |
webkit_soup_cache_writing_fixture_free (fixture); |
| 560 |
} |
| 561 |
|
| 562 |
static void |
| 563 |
write_ready_cb (GObject *source, GAsyncResult *result, WebKitSoupCacheWritingFixture *fixture) |
| 564 |
{ |
| 565 |
GOutputStream *stream = G_OUTPUT_STREAM (source); |
| 566 |
GError *error = NULL; |
| 567 |
gssize write_size; |
| 568 |
WebKitSoupCacheEntry *entry = fixture->entry; |
| 569 |
|
| 570 |
if (g_cancellable_is_cancelled (entry->cancellable)) { |
| 571 |
g_output_stream_close_async (stream, |
| 572 |
G_PRIORITY_LOW, |
| 573 |
entry->cancellable, |
| 574 |
(GAsyncReadyCallback)close_ready_cb, |
| 575 |
fixture); |
| 576 |
return; |
| 577 |
} |
| 578 |
|
| 579 |
write_size = g_output_stream_write_finish (stream, result, &error); |
| 580 |
if (write_size <= 0 || error) { |
| 581 |
if (error) |
| 582 |
entry->error = error; |
| 583 |
g_output_stream_close_async (stream, |
| 584 |
G_PRIORITY_LOW, |
| 585 |
entry->cancellable, |
| 586 |
(GAsyncReadyCallback)close_ready_cb, |
| 587 |
fixture); |
| 588 |
/* FIXME: We should completely stop caching the |
| 589 |
resource at this point */ |
| 590 |
} else { |
| 591 |
entry->pos += write_size; |
| 592 |
|
| 593 |
/* Are we still writing and is there new data to write |
| 594 |
already ? */ |
| 595 |
if (entry->data && entry->pos < entry->data->len) { |
| 596 |
g_output_stream_write_async (entry->stream, |
| 597 |
entry->data->str + entry->pos, |
| 598 |
entry->data->len - entry->pos, |
| 599 |
G_PRIORITY_LOW, |
| 600 |
entry->cancellable, |
| 601 |
(GAsyncReadyCallback)write_ready_cb, |
| 602 |
fixture); |
| 603 |
} else { |
| 604 |
entry->writing = FALSE; |
| 605 |
|
| 606 |
if (entry->got_body) { |
| 607 |
/* If we already received 'got-body' |
| 608 |
and we have written all the data, |
| 609 |
we can close the stream */ |
| 610 |
g_output_stream_close_async (entry->stream, |
| 611 |
G_PRIORITY_LOW, |
| 612 |
entry->cancellable, |
| 613 |
(GAsyncReadyCallback)close_ready_cb, |
| 614 |
fixture); |
| 615 |
} |
| 616 |
} |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
static void |
| 621 |
msg_got_chunk_cb (SoupMessage *msg, SoupBuffer *chunk, WebKitSoupCacheWritingFixture *fixture) |
| 622 |
{ |
| 623 |
WebKitSoupCacheEntry *entry = fixture->entry; |
| 624 |
|
| 625 |
g_return_if_fail (chunk->data && chunk->length); |
| 626 |
g_return_if_fail (entry); |
| 627 |
|
| 628 |
/* Ignore this if the writing or appending was cancelled */ |
| 629 |
if (!g_cancellable_is_cancelled (entry->cancellable)) { |
| 630 |
g_string_append_len (entry->data, chunk->data, chunk->length); |
| 631 |
entry->length = entry->data->len; |
| 632 |
|
| 633 |
if (!cache_accepts_entries_of_size (fixture->cache, entry->length)) { |
| 634 |
/* Quickly cancel the caching of the resource */ |
| 635 |
g_cancellable_cancel (entry->cancellable); |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
/* FIXME: remove the error check when we cancel the caching at |
| 640 |
the first write error */ |
| 641 |
/* Only write if the entry stream is ready */ |
| 642 |
if (entry->writing == FALSE && entry->error == NULL && entry->stream) { |
| 643 |
GString *data = entry->data; |
| 644 |
entry->writing = TRUE; |
| 645 |
g_output_stream_write_async (entry->stream, |
| 646 |
data->str + entry->pos, |
| 647 |
data->len - entry->pos, |
| 648 |
G_PRIORITY_LOW, |
| 649 |
entry->cancellable, |
| 650 |
(GAsyncReadyCallback)write_ready_cb, |
| 651 |
fixture); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
static void |
| 656 |
msg_got_body_cb (SoupMessage *msg, WebKitSoupCacheWritingFixture *fixture) |
| 657 |
{ |
| 658 |
WebKitSoupCacheEntry *entry = fixture->entry; |
| 659 |
g_return_if_fail (entry); |
| 660 |
|
| 661 |
entry->got_body = TRUE; |
| 662 |
|
| 663 |
if (!entry->stream && entry->pos != entry->length) |
| 664 |
/* The stream is not ready to be written but we still |
| 665 |
have data to write, we'll write it when the stream |
| 666 |
is opened for writing */ |
| 667 |
return; |
| 668 |
|
| 669 |
|
| 670 |
if (entry->pos != entry->length) { |
| 671 |
/* If we still have data to write, write it, |
| 672 |
write_ready_cb will close the stream */ |
| 673 |
if (entry->writing == FALSE && entry->error == NULL && entry->stream) { |
| 674 |
g_output_stream_write_async (entry->stream, |
| 675 |
entry->data->str + entry->pos, |
| 676 |
entry->data->len - entry->pos, |
| 677 |
G_PRIORITY_LOW, |
| 678 |
entry->cancellable, |
| 679 |
(GAsyncReadyCallback)write_ready_cb, |
| 680 |
fixture); |
| 681 |
} |
| 682 |
return; |
| 683 |
} |
| 684 |
|
| 685 |
if (entry->stream && !entry->writing) |
| 686 |
g_output_stream_close_async (entry->stream, |
| 687 |
G_PRIORITY_LOW, |
| 688 |
entry->cancellable, |
| 689 |
(GAsyncReadyCallback)close_ready_cb, |
| 690 |
fixture); |
| 691 |
} |
| 692 |
|
| 693 |
static gboolean |
| 694 |
webkit_soup_cache_entry_remove (WebKitSoupCache *cache, WebKitSoupCacheEntry *entry) |
| 695 |
{ |
| 696 |
GList *lru_item; |
| 697 |
|
| 698 |
/* if (entry->dirty && !g_cancellable_is_cancelled (entry->cancellable)) { */ |
| 699 |
if (entry->dirty) { |
| 700 |
g_cancellable_cancel (entry->cancellable); |
| 701 |
return FALSE; |
| 702 |
} |
| 703 |
|
| 704 |
g_assert (g_list_length (cache->priv->lru_start) == g_hash_table_size (cache->priv->cache)); |
| 705 |
|
| 706 |
/* Remove from cache */ |
| 707 |
if (!g_hash_table_remove (cache->priv->cache, entry->key)) |
| 708 |
return FALSE; |
| 709 |
|
| 710 |
/* Remove from LRU */ |
| 711 |
lru_item = g_list_find (cache->priv->lru_start, entry); |
| 712 |
cache->priv->lru_start = g_list_delete_link (cache->priv->lru_start, lru_item); |
| 713 |
|
| 714 |
/* Adjust cache size */ |
| 715 |
cache->priv->size -= entry->length; |
| 716 |
|
| 717 |
g_assert (g_list_length (cache->priv->lru_start) == g_hash_table_size (cache->priv->cache)); |
| 718 |
|
| 719 |
return TRUE; |
| 720 |
} |
| 721 |
|
| 722 |
static gint |
| 723 |
lru_compare_func (gconstpointer a, gconstpointer b) |
| 724 |
{ |
| 725 |
WebKitSoupCacheEntry *entry_a = (WebKitSoupCacheEntry *)a; |
| 726 |
WebKitSoupCacheEntry *entry_b = (WebKitSoupCacheEntry *)b; |
| 727 |
|
| 728 |
/** The rationale of this sorting func is |
| 729 |
* |
| 730 |
* 1. sort by hits -> LRU algorithm, then |
| 731 |
* |
| 732 |
* 2. sort by freshness lifetime, we better discard first |
| 733 |
* entries that are close to expire |
| 734 |
* |
| 735 |
* 3. sort by size, replace first small size resources as they |
| 736 |
* are cheaper to download |
| 737 |
**/ |
| 738 |
|
| 739 |
/* Sort by hits */ |
| 740 |
if (entry_a->hits != entry_b->hits) |
| 741 |
return entry_a->hits - entry_b->hits; |
| 742 |
|
| 743 |
/* Sort by freshness_lifetime */ |
| 744 |
if (entry_a->freshness_lifetime != entry_b->freshness_lifetime) |
| 745 |
return entry_a->freshness_lifetime - entry_b->freshness_lifetime; |
| 746 |
|
| 747 |
/* Sort by size */ |
| 748 |
return entry_a->length - entry_b->length; |
| 749 |
} |
| 750 |
|
| 751 |
static gboolean |
| 752 |
cache_accepts_entries_of_size (WebKitSoupCache *cache, guint length_to_add) |
| 753 |
{ |
| 754 |
/* We could add here some more heuristics. TODO: review how |
| 755 |
this is done by other HTTP caches */ |
| 756 |
|
| 757 |
return length_to_add <= cache->priv->max_entry_data_size; |
| 758 |
} |
| 759 |
|
| 760 |
static void |
| 761 |
make_room_for_new_entry (WebKitSoupCache *cache, guint length_to_add) |
| 762 |
{ |
| 763 |
GList *lru_entry = cache->priv->lru_start; |
| 764 |
|
| 765 |
/* Check that there is enough room for the new entry. This is |
| 766 |
an approximation as we're not working out the size of the |
| 767 |
cache file or the size of the headers for performance |
| 768 |
reasons. TODO: check if that would be really that expensive */ |
| 769 |
|
| 770 |
while (lru_entry && |
| 771 |
(length_to_add + cache->priv->size > cache->priv->max_size)) { |
| 772 |
WebKitSoupCacheEntry *old_entry = (WebKitSoupCacheEntry *)lru_entry->data; |
| 773 |
|
| 774 |
/* Discard entries. Once cancelled resources will be |
| 775 |
* freed in close_ready_cb |
| 776 |
*/ |
| 777 |
if (webkit_soup_cache_entry_remove (cache, old_entry)) { |
| 778 |
webkit_soup_cache_entry_free (old_entry, TRUE); |
| 779 |
lru_entry = cache->priv->lru_start; |
| 780 |
} else |
| 781 |
lru_entry = g_list_next (lru_entry); |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
static gboolean |
| 786 |
webkit_soup_cache_entry_insert_by_key (WebKitSoupCache *cache, |
| 787 |
const char *key, |
| 788 |
WebKitSoupCacheEntry *entry, |
| 789 |
gboolean sort) |
| 790 |
{ |
| 791 |
guint length_to_add = 0; |
| 792 |
|
| 793 |
if (soup_message_headers_get_encoding (entry->headers) != SOUP_ENCODING_CHUNKED) |
| 794 |
length_to_add = soup_message_headers_get_content_length (entry->headers); |
| 795 |
|
| 796 |
/* Check if we are going to store the resource depending on its size */ |
| 797 |
if (length_to_add) { |
| 798 |
if (!cache_accepts_entries_of_size (cache, length_to_add)) |
| 799 |
return FALSE; |
| 800 |
|
| 801 |
/* Make room for new entry if needed */ |
| 802 |
make_room_for_new_entry (cache, length_to_add); |
| 803 |
} |
| 804 |
|
| 805 |
g_hash_table_insert (cache->priv->cache, g_strdup (key), entry); |
| 806 |
|
| 807 |
/* Compute new cache size */ |
| 808 |
cache->priv->size += length_to_add; |
| 809 |
|
| 810 |
/* Update LRU */ |
| 811 |
if (sort) |
| 812 |
cache->priv->lru_start = g_list_insert_sorted (cache->priv->lru_start, entry, lru_compare_func); |
| 813 |
else |
| 814 |
cache->priv->lru_start = g_list_prepend (cache->priv->lru_start, entry); |
| 815 |
|
| 816 |
g_assert (g_list_length (cache->priv->lru_start) == g_hash_table_size (cache->priv->cache)); |
| 817 |
|
| 818 |
return TRUE; |
| 819 |
} |
| 820 |
|
| 821 |
static void |
| 822 |
msg_restarted_cb (SoupMessage *msg, WebKitSoupCacheEntry *entry) |
| 823 |
{ |
| 824 |
/* FIXME: What should we do here exactly? */ |
| 825 |
} |
| 826 |
|
| 827 |
static void |
| 828 |
append_to_ready_cb (GObject *source, GAsyncResult *result, WebKitSoupCacheWritingFixture *fixture) |
| 829 |
{ |
| 830 |
GFile *file = (GFile *)source; |
| 831 |
GOutputStream *stream; |
| 832 |
WebKitSoupCacheEntry *entry = fixture->entry; |
| 833 |
|
| 834 |
stream = (GOutputStream *)g_file_append_to_finish (file, result, &entry->error); |
| 835 |
|
| 836 |
if (g_cancellable_is_cancelled (entry->cancellable) || entry->error) { |
| 837 |
fixture->cache->priv->n_pending--; |
| 838 |
entry->dirty = FALSE; |
| 839 |
webkit_soup_cache_entry_remove (fixture->cache, entry); |
| 840 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 841 |
webkit_soup_cache_writing_fixture_free (fixture); |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
entry->stream = g_object_ref (stream); |
| 846 |
g_object_unref (file); |
| 847 |
|
| 848 |
/* If we already got all the data we have to initiate the |
| 849 |
writing here, since we won't get more 'got-chunk' |
| 850 |
signals */ |
| 851 |
if (entry->got_body) { |
| 852 |
GString *data = entry->data; |
| 853 |
|
| 854 |
/* It could happen that reading the data from server |
| 855 |
was completed before this happens. In that case |
| 856 |
there is no data */ |
| 857 |
if (data) { |
| 858 |
entry->writing = TRUE; |
| 859 |
g_output_stream_write_async (entry->stream, |
| 860 |
data->str + entry->pos, |
| 861 |
data->len - entry->pos, |
| 862 |
G_PRIORITY_LOW, |
| 863 |
entry->cancellable, |
| 864 |
(GAsyncReadyCallback)write_ready_cb, |
| 865 |
fixture); |
| 866 |
} |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
typedef struct { |
| 871 |
time_t request_time; |
| 872 |
SoupSessionFeature *feature; |
| 873 |
gulong got_headers_handler; |
| 874 |
} RequestHelper; |
| 875 |
|
| 876 |
static void |
| 877 |
msg_got_headers_cb (SoupMessage *msg, gpointer user_data) |
| 878 |
{ |
| 879 |
WebKitSoupCache *cache; |
| 880 |
WebKitSoupCacheability cacheable; |
| 881 |
RequestHelper *helper; |
| 882 |
time_t request_time, response_time; |
| 883 |
|
| 884 |
response_time = time (NULL); |
| 885 |
|
| 886 |
helper = (RequestHelper *)user_data; |
| 887 |
cache = WEBKIT_SOUP_CACHE (helper->feature); |
| 888 |
request_time = helper->request_time; |
| 889 |
g_signal_handlers_disconnect_by_func (msg, msg_got_headers_cb, user_data); |
| 890 |
g_slice_free (RequestHelper, helper); |
| 891 |
|
| 892 |
cacheable = webkit_soup_cache_get_cacheability (cache, msg); |
| 893 |
|
| 894 |
if (cacheable & WEBKIT_SOUP_CACHE_CACHEABLE) { |
| 895 |
WebKitSoupCacheEntry *entry; |
| 896 |
char *key; |
| 897 |
GFile *file; |
| 898 |
WebKitSoupCacheWritingFixture *fixture; |
| 899 |
|
| 900 |
/* Check if we are already caching this resource */ |
| 901 |
key = soup_message_get_cache_key (msg); |
| 902 |
entry = g_hash_table_lookup (cache->priv->cache, key); |
| 903 |
g_free (key); |
| 904 |
|
| 905 |
if (entry && entry->dirty) |
| 906 |
return; |
| 907 |
|
| 908 |
/* Create a new entry, deleting any old one if present */ |
| 909 |
if (entry) { |
| 910 |
webkit_soup_cache_entry_remove (cache, entry); |
| 911 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 912 |
} |
| 913 |
|
| 914 |
entry = webkit_soup_cache_entry_new (cache, msg, request_time, response_time); |
| 915 |
entry->hits = 1; |
| 916 |
|
| 917 |
/* Do not continue if it can not be stored */ |
| 918 |
if (!webkit_soup_cache_entry_insert_by_key (cache, (const gchar *)entry->key, entry, TRUE)) { |
| 919 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
fixture = g_slice_new0 (WebKitSoupCacheWritingFixture); |
| 924 |
fixture->cache = g_object_ref (cache); |
| 925 |
fixture->entry = entry; |
| 926 |
fixture->msg = g_object_ref (msg); |
| 927 |
|
| 928 |
/* We connect now to these signals and buffer the data |
| 929 |
if it comes before the file is ready for writing */ |
| 930 |
fixture->got_chunk_handler = |
| 931 |
g_signal_connect (msg, "got-chunk", G_CALLBACK (msg_got_chunk_cb), fixture); |
| 932 |
fixture->got_body_handler = |
| 933 |
g_signal_connect (msg, "got-body", G_CALLBACK (msg_got_body_cb), fixture); |
| 934 |
fixture->restarted_handler = |
| 935 |
g_signal_connect (msg, "restarted", G_CALLBACK (msg_restarted_cb), entry); |
| 936 |
|
| 937 |
/* Prepare entry */ |
| 938 |
file = g_file_new_for_path (entry->filename); |
| 939 |
cache->priv->n_pending++; |
| 940 |
|
| 941 |
entry->dirty = TRUE; |
| 942 |
entry->cancellable = g_cancellable_new (); |
| 943 |
g_file_append_to_async (file, 0, |
| 944 |
G_PRIORITY_LOW, entry->cancellable, |
| 945 |
(GAsyncReadyCallback)append_to_ready_cb, |
| 946 |
fixture); |
| 947 |
} else if (cacheable & WEBKIT_SOUP_CACHE_INVALIDATES) { |
| 948 |
char *key; |
| 949 |
WebKitSoupCacheEntry *entry; |
| 950 |
|
| 951 |
key = soup_message_get_cache_key (msg); |
| 952 |
entry = g_hash_table_lookup (cache->priv->cache, key); |
| 953 |
g_free (key); |
| 954 |
|
| 955 |
if (entry) { |
| 956 |
if (webkit_soup_cache_entry_remove (cache, entry)) |
| 957 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 958 |
} |
| 959 |
} else if (cacheable & WEBKIT_SOUP_CACHE_VALIDATES) { |
| 960 |
char *key; |
| 961 |
WebKitSoupCacheEntry *entry; |
| 962 |
|
| 963 |
key = soup_message_get_cache_key (msg); |
| 964 |
entry = g_hash_table_lookup (cache->priv->cache, key); |
| 965 |
g_free (key); |
| 966 |
|
| 967 |
g_return_if_fail (entry); |
| 968 |
|
| 969 |
entry->being_validated = FALSE; |
| 970 |
|
| 971 |
/* We update the headers of the existing cache item, |
| 972 |
plus its age */ |
| 973 |
soup_message_headers_foreach (msg->response_headers, |
| 974 |
(SoupMessageHeadersForeachFunc)update_headers, |
| 975 |
entry->headers); |
| 976 |
webkit_soup_cache_entry_set_freshness (entry, msg, cache); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
GInputStream * |
| 981 |
webkit_soup_cache_send_response (WebKitSoupCache *cache, SoupMessage *msg) |
| 982 |
{ |
| 983 |
char *key; |
| 984 |
WebKitSoupCacheEntry *entry; |
| 985 |
char *current_age; |
| 986 |
GInputStream *stream = NULL; |
| 987 |
GFile *file; |
| 988 |
|
| 989 |
g_return_val_if_fail (WEBKIT_IS_SOUP_CACHE (cache), NULL); |
| 990 |
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL); |
| 991 |
|
| 992 |
key = soup_message_get_cache_key (msg); |
| 993 |
entry = g_hash_table_lookup (cache->priv->cache, key); |
| 994 |
g_return_val_if_fail (entry, NULL); |
| 995 |
|
| 996 |
/* If we are told to send a response from cache any validation |
| 997 |
in course is over by now */ |
| 998 |
entry->being_validated = FALSE; |
| 999 |
|
| 1000 |
/* Headers */ |
| 1001 |
soup_message_headers_foreach (entry->headers, |
| 1002 |
(SoupMessageHeadersForeachFunc)update_headers, |
| 1003 |
msg->response_headers); |
| 1004 |
|
| 1005 |
/* Add 'Age' header with the current age */ |
| 1006 |
current_age = g_strdup_printf ("%d", webkit_soup_cache_entry_get_current_age (entry)); |
| 1007 |
soup_message_headers_replace (msg->response_headers, |
| 1008 |
"Age", |
| 1009 |
current_age); |
| 1010 |
g_free (current_age); |
| 1011 |
|
| 1012 |
/* TODO: the original idea was to save reads, but current code |
| 1013 |
assumes that a stream is always returned. Need to reach |
| 1014 |
some agreement here. Also we have to handle the situation |
| 1015 |
were the file was no longer there (for example files |
| 1016 |
removed without notifying the cache */ |
| 1017 |
file = g_file_new_for_path (entry->filename); |
| 1018 |
stream = (GInputStream *)g_file_read (file, NULL, NULL); |
| 1019 |
|
| 1020 |
return stream; |
| 1021 |
} |
| 1022 |
|
| 1023 |
static void |
| 1024 |
request_started (SoupSessionFeature *feature, SoupSession *session, |
| 1025 |
SoupMessage *msg, SoupSocket *socket) |
| 1026 |
{ |
| 1027 |
RequestHelper *helper = g_slice_new0 (RequestHelper); |
| 1028 |
helper->request_time = time (NULL); |
| 1029 |
helper->feature = feature; |
| 1030 |
helper->got_headers_handler = g_signal_connect (msg, "got-headers", |
| 1031 |
G_CALLBACK (msg_got_headers_cb), |
| 1032 |
helper); |
| 1033 |
} |
| 1034 |
|
| 1035 |
static void |
| 1036 |
attach (SoupSessionFeature *feature, SoupSession *session) |
| 1037 |
{ |
| 1038 |
WebKitSoupCache *cache = WEBKIT_SOUP_CACHE (feature); |
| 1039 |
cache->priv->session = session; |
| 1040 |
|
| 1041 |
webkit_soup_cache_default_feature_interface->attach (feature, session); |
| 1042 |
} |
| 1043 |
|
| 1044 |
static void |
| 1045 |
webkit_soup_cache_session_feature_init (SoupSessionFeatureInterface *feature_interface, |
| 1046 |
gpointer interface_data) |
| 1047 |
{ |
| 1048 |
webkit_soup_cache_default_feature_interface = |
| 1049 |
g_type_default_interface_peek (SOUP_TYPE_SESSION_FEATURE); |
| 1050 |
|
| 1051 |
feature_interface->attach = attach; |
| 1052 |
feature_interface->request_started = request_started; |
| 1053 |
} |
| 1054 |
|
| 1055 |
static void |
| 1056 |
webkit_soup_cache_init (WebKitSoupCache *cache) |
| 1057 |
{ |
| 1058 |
WebKitSoupCachePrivate *priv; |
| 1059 |
|
| 1060 |
priv = cache->priv = WEBKIT_SOUP_CACHE_GET_PRIVATE (cache); |
| 1061 |
|
| 1062 |
priv->cache = g_hash_table_new_full (g_str_hash, |
| 1063 |
g_str_equal, |
| 1064 |
(GDestroyNotify)g_free, |
| 1065 |
NULL); |
| 1066 |
|
| 1067 |
/* LRU */ |
| 1068 |
priv->lru_start = NULL; |
| 1069 |
|
| 1070 |
/* */ |
| 1071 |
priv->n_pending = 0; |
| 1072 |
|
| 1073 |
/* Cache size */ |
| 1074 |
priv->max_size = DEFAULT_MAX_SIZE; |
| 1075 |
priv->max_entry_data_size = priv->max_size / MAX_ENTRY_DATA_PERCENTAGE; |
| 1076 |
priv->size = 0; |
| 1077 |
} |
| 1078 |
|
| 1079 |
static void |
| 1080 |
remove_cache_item (gpointer key, |
| 1081 |
gpointer value, |
| 1082 |
WebKitSoupCache *cache) |
| 1083 |
{ |
| 1084 |
WebKitSoupCacheEntry *entry = g_hash_table_lookup (cache->priv->cache, (const gchar *)key); |
| 1085 |
if (webkit_soup_cache_entry_remove (cache, entry)) |
| 1086 |
webkit_soup_cache_entry_free (entry, FALSE); |
| 1087 |
} |
| 1088 |
|
| 1089 |
static void |
| 1090 |
webkit_soup_cache_finalize (GObject *object) |
| 1091 |
{ |
| 1092 |
WebKitSoupCachePrivate *priv; |
| 1093 |
|
| 1094 |
priv = WEBKIT_SOUP_CACHE (object)->priv; |
| 1095 |
|
| 1096 |
g_hash_table_foreach (priv->cache, (GHFunc)remove_cache_item, object); |
| 1097 |
g_hash_table_destroy (priv->cache); |
| 1098 |
g_free (priv->cache_dir); |
| 1099 |
|
| 1100 |
g_list_free (priv->lru_start); |
| 1101 |
priv->lru_start = NULL; |
| 1102 |
|
| 1103 |
G_OBJECT_CLASS (webkit_soup_cache_parent_class)->finalize (object); |
| 1104 |
} |
| 1105 |
|
| 1106 |
static void |
| 1107 |
webkit_soup_cache_set_property (GObject *object, guint prop_id, |
| 1108 |
const GValue *value, GParamSpec *pspec) |
| 1109 |
{ |
| 1110 |
WebKitSoupCachePrivate *priv = WEBKIT_SOUP_CACHE (object)->priv; |
| 1111 |
|
| 1112 |
switch (prop_id) { |
| 1113 |
case PROP_CACHE_DIR: |
| 1114 |
priv->cache_dir = g_value_dup_string (value); |
| 1115 |
/* Create directory if it does not exist (FIXME: should we?) */ |
| 1116 |
if (!g_file_test (priv->cache_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) |
| 1117 |
g_mkdir_with_parents (priv->cache_dir, 0700); |
| 1118 |
break; |
| 1119 |
case PROP_CACHE_TYPE: |
| 1120 |
priv->cache_type = g_value_get_enum (value); |
| 1121 |
/* TODO: clear private entries and issue a warning if moving to shared? */ |
| 1122 |
break; |
| 1123 |
default: |
| 1124 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 1125 |
break; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
static void |
| 1130 |
webkit_soup_cache_get_property (GObject *object, guint prop_id, |
| 1131 |
GValue *value, GParamSpec *pspec) |
| 1132 |
{ |
| 1133 |
WebKitSoupCachePrivate *priv = WEBKIT_SOUP_CACHE (object)->priv; |
| 1134 |
|
| 1135 |
switch (prop_id) { |
| 1136 |
case PROP_CACHE_DIR: |
| 1137 |
g_value_set_string (value, priv->cache_dir); |
| 1138 |
break; |
| 1139 |
case PROP_CACHE_TYPE: |
| 1140 |
g_value_set_enum (value, priv->cache_type); |
| 1141 |
break; |
| 1142 |
default: |
| 1143 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 1144 |
break; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
static void |
| 1149 |
webkit_soup_cache_constructed (GObject *object) |
| 1150 |
{ |
| 1151 |
WebKitSoupCachePrivate *priv; |
| 1152 |
|
| 1153 |
priv = WEBKIT_SOUP_CACHE (object)->priv; |
| 1154 |
|
| 1155 |
if (!priv->cache_dir) { |
| 1156 |
/* Set a default cache dir, different for each user */ |
| 1157 |
priv->cache_dir = g_build_filename (g_get_user_cache_dir (), |
| 1158 |
"httpcache", |
| 1159 |
NULL); |
| 1160 |
if (!g_file_test (priv->cache_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) |
| 1161 |
g_mkdir_with_parents (priv->cache_dir, 0700); |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (G_OBJECT_CLASS (webkit_soup_cache_parent_class)->constructed) |
| 1165 |
G_OBJECT_CLASS (webkit_soup_cache_parent_class)->constructed (object); |
| 1166 |
} |
| 1167 |
|
| 1168 |
#define WEBKIT_SOUP_CACHE_TYPE_TYPE (webkit_soup_cache_type_get_type ()) |
| 1169 |
static GType |
| 1170 |
webkit_soup_cache_type_get_type (void) |
| 1171 |
{ |
| 1172 |
static GType cache_type = 0; |
| 1173 |
|
| 1174 |
static const GEnumValue cache_types[] = { |
| 1175 |
{ WEBKIT_SOUP_CACHE_SINGLE_USER, "Single user cache", "user" }, |
| 1176 |
{ WEBKIT_SOUP_CACHE_SHARED, "Shared cache", "shared" }, |
| 1177 |
{ 0, NULL, NULL } |
| 1178 |
}; |
| 1179 |
|
| 1180 |
if (!cache_type) { |
| 1181 |
cache_type = g_enum_register_static ("WebKitSoupCacheType", cache_types); |
| 1182 |
} |
| 1183 |
return cache_type; |
| 1184 |
} |
| 1185 |
|
| 1186 |
static void |
| 1187 |
webkit_soup_cache_class_init (WebKitSoupCacheClass *cache_class) |
| 1188 |
{ |
| 1189 |
GObjectClass *gobject_class = (GObjectClass *)cache_class; |
| 1190 |
|
| 1191 |
gobject_class->finalize = webkit_soup_cache_finalize; |
| 1192 |
gobject_class->constructed = webkit_soup_cache_constructed; |
| 1193 |
gobject_class->set_property = webkit_soup_cache_set_property; |
| 1194 |
gobject_class->get_property = webkit_soup_cache_get_property; |
| 1195 |
|
| 1196 |
cache_class->get_cacheability = get_cacheability; |
| 1197 |
|
| 1198 |
g_object_class_install_property (gobject_class, PROP_CACHE_DIR, |
| 1199 |
g_param_spec_string ("cache-dir", |
| 1200 |
"Cache directory", |
| 1201 |
"The directory to store the cache files", |
| 1202 |
NULL, |
| 1203 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 1204 |
|
| 1205 |
g_object_class_install_property (gobject_class, PROP_CACHE_TYPE, |
| 1206 |
g_param_spec_enum ("cache-type", |
| 1207 |
"Cache type", |
| 1208 |
"Whether the cache is private or shared", |
| 1209 |
WEBKIT_SOUP_CACHE_TYPE_TYPE, |
| 1210 |
WEBKIT_SOUP_CACHE_SINGLE_USER, |
| 1211 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 1212 |
|
| 1213 |
g_type_class_add_private (cache_class, sizeof (WebKitSoupCachePrivate)); |
| 1214 |
} |
| 1215 |
|
| 1216 |
/** |
| 1217 |
* webkit_soup_cache_new: |
| 1218 |
* @cache_dir: the directory to store the cached data, or %NULL to use the default one |
| 1219 |
* @cache_type: the #WebKitSoupCacheType of the cache |
| 1220 |
* |
| 1221 |
* Creates a new #WebKitSoupCache. |
| 1222 |
* |
| 1223 |
* Returns: a new #WebKitSoupCache |
| 1224 |
* |
| 1225 |
* Since: 2.28 |
| 1226 |
**/ |
| 1227 |
WebKitSoupCache * |
| 1228 |
webkit_soup_cache_new (const char *cache_dir, WebKitSoupCacheType cache_type) |
| 1229 |
{ |
| 1230 |
return g_object_new (WEBKIT_TYPE_SOUP_CACHE, |
| 1231 |
"cache-dir", cache_dir, |
| 1232 |
"cache-type", cache_type, |
| 1233 |
NULL); |
| 1234 |
} |
| 1235 |
|
| 1236 |
/** |
| 1237 |
* webkit_soup_cache_has_response: |
| 1238 |
* @cache: a #WebKitSoupCache |
| 1239 |
* @msg: a #SoupMessage |
| 1240 |
* |
| 1241 |
* This function calculates whether the @cache object has a proper |
| 1242 |
* response for the request @msg given the flags both in the request |
| 1243 |
* and the cached reply and the time ellapsed since it was cached. |
| 1244 |
* |
| 1245 |
* Returns: whether or not the @cache has a valid response for @msg |
| 1246 |
**/ |
| 1247 |
WebKitSoupCacheResponse |
| 1248 |
webkit_soup_cache_has_response (WebKitSoupCache *cache, SoupMessage *msg) |
| 1249 |
{ |
| 1250 |
char *key; |
| 1251 |
WebKitSoupCacheEntry *entry; |
| 1252 |
const char *cache_control; |
| 1253 |
GHashTable *hash; |
| 1254 |
gpointer value; |
| 1255 |
gboolean must_revalidate; |
| 1256 |
int max_age, max_stale, min_fresh; |
| 1257 |
GList *lru_item, *item; |
| 1258 |
|
| 1259 |
key = soup_message_get_cache_key (msg); |
| 1260 |
entry = g_hash_table_lookup (cache->priv->cache, key); |
| 1261 |
|
| 1262 |
/* 1. The presented Request-URI and that of stored response |
| 1263 |
* match |
| 1264 |
*/ |
| 1265 |
if (!entry) |
| 1266 |
return WEBKIT_SOUP_CACHE_RESPONSE_STALE; |
| 1267 |
|
| 1268 |
/* Increase hit count. Take sorting into account */ |
| 1269 |
entry->hits++; |
| 1270 |
lru_item = g_list_find (cache->priv->lru_start, entry); |
| 1271 |
item = lru_item; |
| 1272 |
while (item->next && lru_compare_func (item->data, item->next->data) > 0) |
| 1273 |
item = g_list_next (item); |
| 1274 |
|
| 1275 |
if (item != lru_item) { |
| 1276 |
cache->priv->lru_start = g_list_remove_link (cache->priv->lru_start, lru_item); |
| 1277 |
item = g_list_insert_sorted (item, lru_item->data, lru_compare_func); |
| 1278 |
g_list_free (lru_item); |
| 1279 |
} |
| 1280 |
|
| 1281 |
if (entry->dirty || entry->being_validated) |
| 1282 |
return WEBKIT_SOUP_CACHE_RESPONSE_STALE; |
| 1283 |
|
| 1284 |
/* 2. The request method associated with the stored response |
| 1285 |
* allows it to be used for the presented request |
| 1286 |
*/ |
| 1287 |
|
| 1288 |
/* In practice this means we only return our resource for GET, |
| 1289 |
* cacheability for other methods is a TODO in the RFC |
| 1290 |
* (TODO: although we could return the headers for HEAD |
| 1291 |
* probably). |
| 1292 |
*/ |
| 1293 |
if (msg->method != SOUP_METHOD_GET) |
| 1294 |
return WEBKIT_SOUP_CACHE_RESPONSE_STALE; |
| 1295 |
|
| 1296 |
/* 3. Selecting request-headers nominated by the stored |
| 1297 |
* response (if any) match those presented. |
| 1298 |
*/ |
| 1299 |
|
| 1300 |
/* TODO */ |
| 1301 |
|
| 1302 |
/* 4. The presented request and stored response are free from |
| 1303 |
* directives that would prevent its use. |
| 1304 |
*/ |
| 1305 |
|
| 1306 |
must_revalidate = FALSE; |
| 1307 |
max_age = max_stale = min_fresh = -1; |
| 1308 |
|
| 1309 |
cache_control = soup_message_headers_get (msg->request_headers, "Cache-Control"); |
| 1310 |
if (cache_control) { |
| 1311 |
hash = soup_header_parse_param_list (cache_control); |
| 1312 |
|
| 1313 |
if (g_hash_table_lookup_extended (hash, "no-store", NULL, NULL)) { |
| 1314 |
g_hash_table_destroy (hash); |
| 1315 |
return WEBKIT_SOUP_CACHE_RESPONSE_STALE; |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (g_hash_table_lookup_extended (hash, "no-cache", NULL, NULL)) { |
| 1319 |
entry->must_revalidate = TRUE; |
| 1320 |
} |
| 1321 |
|
| 1322 |
if (g_hash_table_lookup_extended (hash, "max-age", NULL, &value)) { |
| 1323 |
max_age = (int)MIN (g_ascii_strtoll (value, NULL, 10), G_MAXINT32); |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* max-stale can have no value set, we need to use _extended */ |
| 1327 |
if (g_hash_table_lookup_extended (hash, "max-stale", NULL, &value)) { |
| 1328 |
if (value) |
| 1329 |
max_stale = (int)MIN (g_ascii_strtoll (value, NULL, 10), G_MAXINT32); |
| 1330 |
else |
| 1331 |
max_stale = G_MAXINT32; |
| 1332 |
} |
| 1333 |
|
| 1334 |
value = g_hash_table_lookup (hash, "min-fresh"); |
| 1335 |
if (value) |
| 1336 |
min_fresh = (int)MIN (g_ascii_strtoll (value, NULL, 10), G_MAXINT32); |
| 1337 |
|
| 1338 |
g_hash_table_destroy (hash); |
| 1339 |
|
| 1340 |
if (max_age != -1) { |
| 1341 |
guint current_age = webkit_soup_cache_entry_get_current_age (entry); |
| 1342 |
|
| 1343 |
/* If we are over max-age and max-stale is not |
| 1344 |
set, do not use the value from the cache |
| 1345 |
without validation */ |
| 1346 |
if (max_age <= current_age && max_stale == -1) |
| 1347 |
return WEBKIT_SOUP_CACHE_RESPONSE_NEEDS_VALIDATION; |
| 1348 |
} |
| 1349 |
} |
| 1350 |
|
| 1351 |
/* 5. The stored response is either: fresh, allowed to be |
| 1352 |
* served stale or succesfully validated |
| 1353 |
*/ |
| 1354 |
/* TODO consider also proxy-revalidate & s-maxage */ |
| 1355 |
if (entry->must_revalidate) |
| 1356 |
return WEBKIT_SOUP_CACHE_RESPONSE_NEEDS_VALIDATION; |
| 1357 |
|
| 1358 |
if (!webkit_soup_cache_entry_is_fresh_enough (entry, min_fresh)) { |
| 1359 |
/* Not fresh, can it be served stale? */ |
| 1360 |
if (max_stale != -1) { |
| 1361 |
/* G_MAXINT32 means we accept any staleness */ |
| 1362 |
if (max_stale == G_MAXINT32) |
| 1363 |
return WEBKIT_SOUP_CACHE_RESPONSE_FRESH; |
| 1364 |
|
| 1365 |
if ((webkit_soup_cache_entry_get_current_age (entry) - entry->freshness_lifetime) <= max_stale) |
| 1366 |
return WEBKIT_SOUP_CACHE_RESPONSE_FRESH; |
| 1367 |
} |
| 1368 |
|
| 1369 |
return WEBKIT_SOUP_CACHE_RESPONSE_NEEDS_VALIDATION; |
| 1370 |
} |
| 1371 |
|
| 1372 |
return WEBKIT_SOUP_CACHE_RESPONSE_FRESH; |
| 1373 |
} |
| 1374 |
|
| 1375 |
/** |
| 1376 |
* webkit_soup_cache_get_cacheability: |
| 1377 |
* @cache: a #WebKitSoupCache |
| 1378 |
* @msg: a #SoupMessage |
| 1379 |
* |
| 1380 |
* Calculates whether the @msg can be cached or not. |
| 1381 |
* |
| 1382 |
* Returns: a #WebKitSoupCacheability value indicating whether the @msg can be cached or not. |
| 1383 |
**/ |
| 1384 |
WebKitSoupCacheability |
| 1385 |
webkit_soup_cache_get_cacheability (WebKitSoupCache *cache, SoupMessage *msg) |
| 1386 |
{ |
| 1387 |
g_return_val_if_fail (WEBKIT_IS_SOUP_CACHE (cache), WEBKIT_SOUP_CACHE_UNCACHEABLE); |
| 1388 |
g_return_val_if_fail (SOUP_IS_MESSAGE (msg), WEBKIT_SOUP_CACHE_UNCACHEABLE); |
| 1389 |
|
| 1390 |
return WEBKIT_SOUP_CACHE_GET_CLASS (cache)->get_cacheability (cache, msg); |
| 1391 |
} |
| 1392 |
|
| 1393 |
static gboolean |
| 1394 |
force_flush_timeout (gpointer data) |
| 1395 |
{ |
| 1396 |
gboolean *forced = (gboolean *)data; |
| 1397 |
*forced = TRUE; |
| 1398 |
|
| 1399 |
return FALSE; |
| 1400 |
} |
| 1401 |
|
| 1402 |
/** |
| 1403 |
* webkit_soup_cache_flush: |
| 1404 |
* @cache: a #WebKitSoupCache |
| 1405 |
* @session: the #SoupSession associated with the @cache |
| 1406 |
* |
| 1407 |
* This function will force all pending writes in the @cache to be |
| 1408 |
* committed to disk. For doing so it will iterate the #GMainContext |
| 1409 |
* associated with the @session (which can be the default one) as long |
| 1410 |
* as needed. |
| 1411 |
**/ |
| 1412 |
void |
| 1413 |
webkit_soup_cache_flush (WebKitSoupCache *cache) |
| 1414 |
{ |
| 1415 |
GMainContext *async_context; |
| 1416 |
SoupSession *session; |
| 1417 |
guint timeout_id; |
| 1418 |
gboolean forced = FALSE; |
| 1419 |
|
| 1420 |
g_return_if_fail (WEBKIT_IS_SOUP_CACHE (cache)); |
| 1421 |
|
| 1422 |
session = cache->priv->session; |
| 1423 |
g_return_if_fail (SOUP_IS_SESSION (session)); |
| 1424 |
async_context = soup_session_get_async_context (session); |
| 1425 |
|
| 1426 |
/* We give cache 10 secs to finish */ |
| 1427 |
timeout_id = g_timeout_add (10000, force_flush_timeout, &forced); |
| 1428 |
|
| 1429 |
while (!forced && cache->priv->n_pending > 0) |
| 1430 |
g_main_context_iteration (async_context, FALSE); |
| 1431 |
|
| 1432 |
if (!forced) |
| 1433 |
g_source_remove (timeout_id); |
| 1434 |
else |
| 1435 |
g_warning ("Cache flush finished despite %d pending requests", cache->priv->n_pending); |
| 1436 |
} |
| 1437 |
|
| 1438 |
static void |
| 1439 |
clear_cache_item (gpointer key, |
| 1440 |
gpointer value, |
| 1441 |
WebKitSoupCache *cache) |
| 1442 |
{ |
| 1443 |
WebKitSoupCacheEntry *entry = g_hash_table_lookup (cache->priv->cache, (const gchar *)key); |
| 1444 |
if (webkit_soup_cache_entry_remove (cache, entry)) |
| 1445 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 1446 |
} |
| 1447 |
|
| 1448 |
/** |
| 1449 |
* webkit_soup_cache_clear: |
| 1450 |
* @cache: a #WebKitSoupCache |
| 1451 |
* |
| 1452 |
* Will remove all entries in the @cache plus all the cache files |
| 1453 |
* associated with them. |
| 1454 |
**/ |
| 1455 |
void |
| 1456 |
webkit_soup_cache_clear (WebKitSoupCache *cache) |
| 1457 |
{ |
| 1458 |
GHashTable *hash; |
| 1459 |
|
| 1460 |
g_return_if_fail (WEBKIT_IS_SOUP_CACHE (cache)); |
| 1461 |
|
| 1462 |
hash = cache->priv->cache; |
| 1463 |
g_return_if_fail (hash); |
| 1464 |
|
| 1465 |
g_hash_table_foreach (hash, (GHFunc)clear_cache_item, cache); |
| 1466 |
} |
| 1467 |
|
| 1468 |
SoupMessage * |
| 1469 |
webkit_soup_cache_generate_conditional_request (WebKitSoupCache *cache, SoupMessage *original) |
| 1470 |
{ |
| 1471 |
SoupMessage *msg; |
| 1472 |
SoupURI *uri; |
| 1473 |
WebKitSoupCacheEntry *entry; |
| 1474 |
char *key; |
| 1475 |
const char *value; |
| 1476 |
|
| 1477 |
g_return_val_if_fail (WEBKIT_IS_SOUP_CACHE (cache), NULL); |
| 1478 |
g_return_val_if_fail (SOUP_IS_MESSAGE (original), NULL); |
| 1479 |
|
| 1480 |
/* First copy the data we need from the original message */ |
| 1481 |
uri = soup_message_get_uri (original); |
| 1482 |
msg = soup_message_new_from_uri (original->method, uri); |
| 1483 |
|
| 1484 |
soup_message_headers_foreach (original->request_headers, |
| 1485 |
(SoupMessageHeadersForeachFunc)copy_headers, |
| 1486 |
msg->request_headers); |
| 1487 |
|
| 1488 |
/* Now add the validator entries in the header from the cached |
| 1489 |
data */ |
| 1490 |
key = soup_message_get_cache_key (original); |
| 1491 |
entry = g_hash_table_lookup (cache->priv->cache, key); |
| 1492 |
g_free (key); |
| 1493 |
|
| 1494 |
g_return_val_if_fail (entry, NULL); |
| 1495 |
|
| 1496 |
entry->being_validated = TRUE; |
| 1497 |
|
| 1498 |
value = soup_message_headers_get (entry->headers, "Last-Modified"); |
| 1499 |
if (value) |
| 1500 |
soup_message_headers_append (msg->request_headers, |
| 1501 |
"If-Modified-Since", |
| 1502 |
value); |
| 1503 |
value = soup_message_headers_get (entry->headers, "ETag"); |
| 1504 |
if (value) |
| 1505 |
soup_message_headers_append (msg->request_headers, |
| 1506 |
"If-None-Match", |
| 1507 |
value); |
| 1508 |
return msg; |
| 1509 |
} |
| 1510 |
|
| 1511 |
#define WEBKIT_SOUP_CACHE_FILE "soup.cache" |
| 1512 |
|
| 1513 |
#define WEBKIT_SOUP_CACHE_HEADERS_FORMAT "{ss}" |
| 1514 |
#define WEBKIT_SOUP_CACHE_PHEADERS_FORMAT "(ssbuuuuua" WEBKIT_SOUP_CACHE_HEADERS_FORMAT ")" |
| 1515 |
#define WEBKIT_SOUP_CACHE_ENTRIES_FORMAT "a" WEBKIT_SOUP_CACHE_PHEADERS_FORMAT |
| 1516 |
|
| 1517 |
/* Basically the same format than above except that some strings are |
| 1518 |
prepended with &. This way the GVariant returns a pointer to the |
| 1519 |
data instead of duplicating the string */ |
| 1520 |
#define WEBKIT_SOUP_CACHE_DECODE_HEADERS_FORMAT "{&s&s}" |
| 1521 |
|
| 1522 |
static void |
| 1523 |
pack_entry (gpointer data, |
| 1524 |
gpointer user_data) |
| 1525 |
{ |
| 1526 |
WebKitSoupCacheEntry *entry = (WebKitSoupCacheEntry *) data; |
| 1527 |
SoupMessageHeadersIter iter; |
| 1528 |
const gchar *header_key, *header_value; |
| 1529 |
GVariantBuilder *headers_builder; |
| 1530 |
GVariantBuilder *entries_builder = (GVariantBuilder *)user_data; |
| 1531 |
|
| 1532 |
/* Do not store non-consolidated entries */ |
| 1533 |
if (entry->dirty || entry->writing || !entry->key) |
| 1534 |
return; |
| 1535 |
|
| 1536 |
/* Pack headers */ |
| 1537 |
headers_builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY); |
| 1538 |
soup_message_headers_iter_init (&iter, entry->headers); |
| 1539 |
while (soup_message_headers_iter_next (&iter, &header_key, &header_value)) { |
| 1540 |
if (g_utf8_validate (header_value, -1, NULL)) |
| 1541 |
g_variant_builder_add (headers_builder, WEBKIT_SOUP_CACHE_HEADERS_FORMAT, |
| 1542 |
header_key, header_value); |
| 1543 |
} |
| 1544 |
|
| 1545 |
/* Entry data */ |
| 1546 |
g_variant_builder_add (entries_builder, WEBKIT_SOUP_CACHE_PHEADERS_FORMAT, |
| 1547 |
entry->key, entry->filename, entry->must_revalidate, |
| 1548 |
entry->freshness_lifetime, entry->corrected_initial_age, |
| 1549 |
entry->response_time, entry->hits, entry->length, headers_builder); |
| 1550 |
|
| 1551 |
g_variant_builder_unref (headers_builder); |
| 1552 |
} |
| 1553 |
|
| 1554 |
void |
| 1555 |
webkit_soup_cache_dump (WebKitSoupCache *cache) |
| 1556 |
{ |
| 1557 |
WebKitSoupCachePrivate *priv = WEBKIT_SOUP_CACHE_GET_PRIVATE (cache); |
| 1558 |
gchar *filename; |
| 1559 |
GVariantBuilder *entries_builder; |
| 1560 |
GVariant *cache_variant; |
| 1561 |
|
| 1562 |
if (!g_list_length (cache->priv->lru_start)) |
| 1563 |
return; |
| 1564 |
|
| 1565 |
/* Create the builder and iterate over all entries */ |
| 1566 |
entries_builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY); |
| 1567 |
g_list_foreach (cache->priv->lru_start, pack_entry, entries_builder); |
| 1568 |
|
| 1569 |
/* Serialize and dump */ |
| 1570 |
cache_variant = g_variant_new (WEBKIT_SOUP_CACHE_ENTRIES_FORMAT, entries_builder); |
| 1571 |
g_variant_builder_unref (entries_builder); |
| 1572 |
|
| 1573 |
filename = g_build_filename (priv->cache_dir, WEBKIT_SOUP_CACHE_FILE, NULL); |
| 1574 |
g_file_set_contents (filename, (const gchar *)g_variant_get_data (cache_variant), |
| 1575 |
g_variant_get_size (cache_variant), NULL); |
| 1576 |
g_free (filename); |
| 1577 |
g_variant_unref (cache_variant); |
| 1578 |
} |
| 1579 |
|
| 1580 |
void |
| 1581 |
webkit_soup_cache_load (WebKitSoupCache *cache) |
| 1582 |
{ |
| 1583 |
gchar *filename = NULL, *contents = NULL; |
| 1584 |
GVariant *cache_variant; |
| 1585 |
GVariantIter *entries_iter, *headers_iter; |
| 1586 |
GVariantType *variant_format; |
| 1587 |
gsize length; |
| 1588 |
WebKitSoupCacheEntry *entry; |
| 1589 |
WebKitSoupCachePrivate *priv = cache->priv; |
| 1590 |
|
| 1591 |
filename = g_build_filename (priv->cache_dir, WEBKIT_SOUP_CACHE_FILE, NULL); |
| 1592 |
if (!g_file_get_contents (filename, &contents, &length, NULL)) { |
| 1593 |
g_free (filename); |
| 1594 |
return; |
| 1595 |
} |
| 1596 |
g_free (filename); |
| 1597 |
|
| 1598 |
variant_format = g_variant_type_new (WEBKIT_SOUP_CACHE_ENTRIES_FORMAT); |
| 1599 |
cache_variant = g_variant_new_from_data (variant_format, (const gchar *)contents, length, FALSE, NULL, NULL); |
| 1600 |
g_variant_type_free (variant_format); |
| 1601 |
|
| 1602 |
g_variant_get (cache_variant, WEBKIT_SOUP_CACHE_ENTRIES_FORMAT, &entries_iter); |
| 1603 |
entry = g_slice_new0 (WebKitSoupCacheEntry); |
| 1604 |
|
| 1605 |
while (g_variant_iter_loop (entries_iter, WEBKIT_SOUP_CACHE_PHEADERS_FORMAT, |
| 1606 |
&entry->key, &entry->filename, &entry->must_revalidate, |
| 1607 |
&entry->freshness_lifetime, &entry->corrected_initial_age, |
| 1608 |
&entry->response_time, &entry->hits, &entry->length, |
| 1609 |
&headers_iter)) { |
| 1610 |
const gchar *header_key, *header_value; |
| 1611 |
|
| 1612 |
/* SoupMessage Headers */ |
| 1613 |
entry->headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE); |
| 1614 |
while (g_variant_iter_loop (headers_iter, WEBKIT_SOUP_CACHE_DECODE_HEADERS_FORMAT, &header_key, &header_value)) |
| 1615 |
soup_message_headers_append (entry->headers, header_key, header_value); |
| 1616 |
|
| 1617 |
/* Insert in cache */ |
| 1618 |
if (!webkit_soup_cache_entry_insert_by_key (cache, (const gchar *)entry->key, entry, FALSE)) |
| 1619 |
webkit_soup_cache_entry_free (entry, TRUE); |
| 1620 |
|
| 1621 |
/* New entry for the next iteration. This creates an |
| 1622 |
extra object the last iteration but it's worth it |
| 1623 |
as we save several if's */ |
| 1624 |
entry = g_slice_new0 (WebKitSoupCacheEntry); |
| 1625 |
} |
| 1626 |
/* Remove last created entry */ |
| 1627 |
g_slice_free (WebKitSoupCacheEntry, entry); |
| 1628 |
|
| 1629 |
/* Sort LRU (shouldn't be needed). First reverse as elements |
| 1630 |
* are always prepended when inserting |
| 1631 |
*/ |
| 1632 |
cache->priv->lru_start = g_list_reverse (cache->priv->lru_start); |
| 1633 |
cache->priv->lru_start = g_list_sort (cache->priv->lru_start, lru_compare_func); |
| 1634 |
|
| 1635 |
/* frees */ |
| 1636 |
g_variant_iter_free (entries_iter); |
| 1637 |
g_variant_unref (cache_variant); |
| 1638 |
} |
| 1639 |
|
| 1640 |
void |
| 1641 |
webkit_soup_cache_set_max_size (WebKitSoupCache *cache, |
| 1642 |
guint max_size) |
| 1643 |
{ |
| 1644 |
cache->priv->max_size = max_size; |
| 1645 |
cache->priv->max_entry_data_size = cache->priv->max_size / MAX_ENTRY_DATA_PERCENTAGE; |
| 1646 |
} |
| 1647 |
|
| 1648 |
guint |
| 1649 |
webkit_soup_cache_get_max_size (WebKitSoupCache *cache) |
| 1650 |
{ |
| 1651 |
return cache->priv->max_size; |
| 1652 |
} |