Upgrade to 2.62.2
[platform/upstream/libsoup.git] / libsoup / soup-cache.c
index 13e04b9..682625e 100755 (executable)
 #endif
 
 #include <string.h>
+#include <glib/gstdio.h>
 
 #include "soup-cache.h"
 #include "soup-body-input-stream.h"
+#include "soup-cache-client-input-stream.h"
 #include "soup-cache-input-stream.h"
 #include "soup-cache-private.h"
 #include "soup-content-processor.h"
 #include "soup-message-private.h"
 #include "soup.h"
 #include "soup-message-private.h"
-#include "TIZEN.h"
-
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
-#include <glib/gstdio.h>
-
-#if ENABLE(TIZEN_TV_SOUP_CACHE_OPTIMISE_LOAD_TIME)
-#include <dirent.h>
-#endif
-
-#endif
-
 
 /**
  * SECTION:soup-cache
@@ -93,11 +84,7 @@ static void soup_cache_content_processor_init (SoupContentProcessorInterface *in
 #define SOUP_CACHE_FILE "soup.cache2"
 
 #define SOUP_CACHE_HEADERS_FORMAT "{ss}"
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-#define SOUP_CACHE_PHEADERS_FORMAT "(sbuuuuuqsa" SOUP_CACHE_HEADERS_FORMAT ")"
-#else
 #define SOUP_CACHE_PHEADERS_FORMAT "(sbuuuuuqa" SOUP_CACHE_HEADERS_FORMAT ")"
-#endif
 #define SOUP_CACHE_ENTRIES_FORMAT "(qa" SOUP_CACHE_PHEADERS_FORMAT ")"
 
 /* Basically the same format than above except that some strings are
@@ -120,9 +107,6 @@ typedef struct _SoupCacheEntry {
        guint32 hits;
        GCancellable *cancellable;
        guint16 status_code;
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       char *user_agent;
-#endif
 } SoupCacheEntry;
 
 struct _SoupCachePrivate {
@@ -143,9 +127,8 @@ enum {
        PROP_CACHE_TYPE
 };
 
-#define SOUP_CACHE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SOUP_TYPE_CACHE, SoupCachePrivate))
-
 G_DEFINE_TYPE_WITH_CODE (SoupCache, soup_cache, G_TYPE_OBJECT,
+                         G_ADD_PRIVATE (SoupCache)
                         G_IMPLEMENT_INTERFACE (SOUP_TYPE_SESSION_FEATURE,
                                                soup_cache_session_feature_init)
                         G_IMPLEMENT_INTERFACE (SOUP_TYPE_CONTENT_PROCESSOR,
@@ -187,15 +170,10 @@ get_cacheability (SoupCache *cache, SoupMessage *msg)
        if (content_type && !g_ascii_strcasecmp (content_type, "multipart/x-mixed-replace"))
                return SOUP_CACHE_UNCACHEABLE;
 
-#if ENABLE(TIZEN_TV_NO_CACHE_ABOUT_VIDEO_AND_AUDIO)
-       if (content_type && (!g_ascii_strncasecmp (content_type, "video/", 6) || !g_ascii_strncasecmp (content_type, "audio/", 6)))
-               return SOUP_CACHE_UNCACHEABLE;
-#endif
-
        cache_control = soup_message_headers_get_list (msg->response_headers, "Cache-Control");
        if (cache_control && *cache_control) {
                GHashTable *hash;
-               SoupCachePrivate *priv = SOUP_CACHE_GET_PRIVATE (cache);
+               SoupCachePrivate *priv = soup_cache_get_instance_private (cache);
 
                hash = soup_header_parse_param_list (cache_control);
 
@@ -298,10 +276,6 @@ static void
 soup_cache_entry_free (SoupCacheEntry *entry)
 {
        g_free (entry->uri);
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       g_free (entry->user_agent);
-       entry->user_agent = NULL;
-#endif
        g_clear_pointer (&entry->headers, soup_message_headers_free);
        g_clear_object (&entry->cancellable);
 
@@ -343,36 +317,6 @@ soup_cache_entry_get_current_age (SoupCacheEntry *entry)
        return entry->corrected_initial_age + resident_time;
 }
 
-#if ENABLE_TIZEN_UPDATE_CORRECTED_INITIAL_AGE_FOR_CACHE
-static guint
-soup_cache_entry_update_corrected_initial_age (SoupCacheEntry *entry)
-{
-       SoupDate *soup_date;
-       const char *age, *date;
-       time_t date_value, apparent_age, corrected_received_age, age_value = 0;
-
-       date = soup_message_headers_get_one (entry->headers, "Date");
-
-       if (date) {
-               soup_date = soup_date_new_from_string (date);
-               date_value = soup_date_to_time_t (soup_date);
-               soup_date_free (soup_date);
-
-               age = soup_message_headers_get_one (entry->headers, "Age");
-               if (age)
-                       age_value = g_ascii_strtoll (age, NULL, 10);
-
-               apparent_age = entry->response_time - date_value;
-               corrected_received_age = MAX (apparent_age, age_value);
-               entry->corrected_initial_age = corrected_received_age;
-       } else {
-               entry->corrected_initial_age = time (NULL);
-       }
-       TIZEN_LOGI("Update corrected_initial_age(%d)", entry->corrected_initial_age);
-       return 0;
-}
-#endif
-
 static gboolean
 soup_cache_entry_is_fresh_enough (SoupCacheEntry *entry, gint min_fresh)
 {
@@ -403,7 +347,7 @@ soup_cache_entry_set_freshness (SoupCacheEntry *entry, SoupMessage *msg, SoupCac
                const char *max_age, *s_maxage;
                gint64 freshness_lifetime = 0;
                GHashTable *hash;
-               SoupCachePrivate *priv = SOUP_CACHE_GET_PRIVATE (cache);
+               SoupCachePrivate *priv = soup_cache_get_instance_private (cache);
 
                hash = soup_header_parse_param_list (cache_control);
 
@@ -513,10 +457,6 @@ soup_cache_entry_new (SoupCache *cache, SoupMessage *msg, time_t request_time, t
 {
        SoupCacheEntry *entry;
        const char *date;
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       GString *str;
-       const char *ua;
-#endif
 
        entry = g_slice_new0 (SoupCacheEntry);
        entry->dirty = FALSE;
@@ -529,15 +469,6 @@ soup_cache_entry_new (SoupCache *cache, SoupMessage *msg, time_t request_time, t
        entry->headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE);
        copy_end_to_end_headers (msg->response_headers, entry->headers);
 
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       /* User Agent */
-       ua = soup_message_headers_get_one(msg->request_headers, "User-Agent");
-       if (ua) {
-               str = g_string_new(ua);
-               entry->user_agent = str->str;
-       }
-#endif
-
        /* LRU list */
        entry->hits = 0;
 
@@ -679,24 +610,10 @@ soup_cache_entry_insert (SoupCache *cache,
        SoupCacheEntry *old_entry;
 
        /* Fill the key */
-#if ENABLE(TIZEN_TV_CHECKING_DELETED_ENTRY_FILE)
-       if (!entry->key)
-#endif
        entry->key = get_cache_key_from_uri ((const char *) entry->uri);
 
        if (soup_message_headers_get_encoding (entry->headers) == SOUP_ENCODING_CONTENT_LENGTH)
-#if ENABLE(TIZEN_TV_COMPUTING_DISK_CACHE_SIZE)
-       {
-               if (entry->length) {
-                       length_to_add = entry->length;
-               }
-               else {
-                       length_to_add = soup_message_headers_get_content_length (entry->headers);
-               }
-       }
-#else
                length_to_add = soup_message_headers_get_content_length (entry->headers);
-#endif
 
        /* Check if we are going to store the resource depending on its size */
        if (length_to_add) {
@@ -754,12 +671,8 @@ GInputStream *
 soup_cache_send_response (SoupCache *cache, SoupMessage *msg)
 {
        SoupCacheEntry *entry;
-       char *current_age;
-       GInputStream *file_stream, *body_stream, *cache_stream;
+       GInputStream *file_stream, *body_stream, *cache_stream, *client_stream;
        GFile *file;
-#if ENABLE(TIZEN_TV_ADD_X_SOUP_MESSAGE_HEADERS)
-       char *entry_length;
-#endif
 
        g_return_val_if_fail (SOUP_IS_CACHE (cache), NULL);
        g_return_val_if_fail (SOUP_IS_MESSAGE (msg), NULL);
@@ -785,29 +698,15 @@ soup_cache_send_response (SoupCache *cache, SoupMessage *msg)
           in course is over by now */
        entry->being_validated = FALSE;
 
+       /* Message starting */
+       soup_message_starting (msg);
+
        /* Status */
        soup_message_set_status (msg, entry->status_code);
 
        /* Headers */
        copy_end_to_end_headers (entry->headers, msg->response_headers);
 
-       /* Add 'Age' header with the current age */
-       current_age = g_strdup_printf ("%d", soup_cache_entry_get_current_age (entry));
-       soup_message_headers_replace (msg->response_headers,
-                                     "Age",
-                                     current_age);
-       g_free (current_age);
-
-#if ENABLE(TIZEN_TV_ADD_X_SOUP_MESSAGE_HEADERS)
-       /* Add 'X-From-Cache' header */
-       soup_message_headers_append(msg->response_headers, "X-From-Cache", "true");
-
-       /* Add 'X-Entry-Length' header */
-       entry_length = g_strdup_printf("%" G_GSIZE_FORMAT, entry->length);
-       soup_message_headers_append(msg->response_headers, "X-Entry-Length", entry_length);
-       g_free (entry_length);
-#endif
-
        /* Create the cache stream. */
        soup_message_disable_feature (msg, SOUP_TYPE_CACHE);
        cache_stream = soup_message_setup_body_istream (body_stream, msg,
@@ -815,7 +714,10 @@ soup_cache_send_response (SoupCache *cache, SoupMessage *msg)
                                                        SOUP_STAGE_ENTITY_BODY);
        g_object_unref (body_stream);
 
-       return cache_stream;
+       client_stream = soup_cache_client_input_stream_new (cache_stream);
+       g_object_unref (cache_stream);
+
+       return client_stream;
 }
 
 static void
@@ -826,11 +728,17 @@ msg_got_headers_cb (SoupMessage *msg, gpointer user_data)
 }
 
 static void
-request_started (SoupSessionFeature *feature, SoupSession *session,
-                SoupMessage *msg, SoupSocket *socket)
+msg_starting_cb (SoupMessage *msg, gpointer user_data)
 {
        g_object_set_data (G_OBJECT (msg), "request-time", GINT_TO_POINTER (time (NULL)));
-       g_signal_connect (msg, "got-headers", G_CALLBACK (msg_got_headers_cb), NULL);
+       g_signal_connect (msg, "got-headers", G_CALLBACK (msg_got_headers_cb), user_data);
+       g_signal_handlers_disconnect_by_func (msg, msg_starting_cb, user_data);
+}
+
+static void
+request_queued (SoupSessionFeature *feature, SoupSession *session, SoupMessage *msg)
+{
+       g_signal_connect (msg, "starting", G_CALLBACK (msg_starting_cb), feature);
 }
 
 static void
@@ -850,7 +758,7 @@ soup_cache_session_feature_init (SoupSessionFeatureInterface *feature_interface,
                g_type_default_interface_peek (SOUP_TYPE_SESSION_FEATURE);
 
        feature_interface->attach = attach;
-       feature_interface->request_started = request_started;
+       feature_interface->request_queued = request_queued;
 }
 
 typedef struct {
@@ -947,7 +855,7 @@ soup_cache_content_processor_wrap_input (SoupContentProcessor *processor,
                soup_cache_entry_remove (cache, entry, TRUE);
 
        request_time = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (msg), "request-time"));
-       response_time = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (msg), "request-time"));
+       response_time = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (msg), "response-time"));
        entry = soup_cache_entry_new (cache, msg, request_time, response_time);
        entry->hits = 1;
        entry->dirty = TRUE;
@@ -990,7 +898,7 @@ soup_cache_init (SoupCache *cache)
 {
        SoupCachePrivate *priv;
 
-       priv = cache->priv = SOUP_CACHE_GET_PRIVATE (cache);
+       priv = cache->priv = soup_cache_get_instance_private (cache);
 
        priv->cache = g_hash_table_new (g_direct_hash, g_direct_equal);
        /* LRU */
@@ -1020,7 +928,7 @@ soup_cache_finalize (GObject *object)
 
        priv = SOUP_CACHE (object)->priv;
 
-       // Cannot use g_hash_table_foreach as callbacks must not modify the hash table
+       /* Cannot use g_hash_table_foreach as callbacks must not modify the hash table */
        entries = g_hash_table_get_values (priv->cache);
        g_list_foreach (entries, remove_cache_item, object);
        g_list_free (entries);
@@ -1041,8 +949,17 @@ soup_cache_set_property (GObject *object, guint prop_id,
 
        switch (prop_id) {
        case PROP_CACHE_DIR:
+               g_assert (!priv->cache_dir);
+
                priv->cache_dir = g_value_dup_string (value);
-               /* Create directory if it does not exist (FIXME: should we?) */
+
+               if (!priv->cache_dir)
+                       /* Set a default cache dir, different for each user */
+                       priv->cache_dir = g_build_filename (g_get_user_cache_dir (),
+                                                           "httpcache",
+                                                           NULL);
+
+               /* Create directory if it does not exist */
                if (!g_file_test (priv->cache_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
                        g_mkdir_with_parents (priv->cache_dir, 0700);
                break;
@@ -1076,32 +993,11 @@ soup_cache_get_property (GObject *object, guint prop_id,
 }
 
 static void
-soup_cache_constructed (GObject *object)
-{
-       SoupCachePrivate *priv;
-
-       priv = SOUP_CACHE (object)->priv;
-
-       if (!priv->cache_dir) {
-               /* Set a default cache dir, different for each user */
-               priv->cache_dir = g_build_filename (g_get_user_cache_dir (),
-                                                   "httpcache",
-                                                   NULL);
-               if (!g_file_test (priv->cache_dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
-                       g_mkdir_with_parents (priv->cache_dir, 0700);
-       }
-
-       if (G_OBJECT_CLASS (soup_cache_parent_class)->constructed)
-               G_OBJECT_CLASS (soup_cache_parent_class)->constructed (object);
-}
-
-static void
 soup_cache_class_init (SoupCacheClass *cache_class)
 {
        GObjectClass *gobject_class = (GObjectClass *)cache_class;
 
        gobject_class->finalize = soup_cache_finalize;
-       gobject_class->constructed = soup_cache_constructed;
        gobject_class->set_property = soup_cache_set_property;
        gobject_class->get_property = soup_cache_get_property;
 
@@ -1121,8 +1017,6 @@ soup_cache_class_init (SoupCacheClass *cache_class)
                                                            SOUP_TYPE_CACHE_TYPE,
                                                            SOUP_CACHE_SINGLE_USER,
                                                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
-
-       g_type_class_add_private (cache_class, sizeof (SoupCachePrivate));
 }
 
 /**
@@ -1138,7 +1032,10 @@ soup_cache_class_init (SoupCacheClass *cache_class)
 
 /**
  * soup_cache_new:
- * @cache_dir: the directory to store the cached data, or %NULL to use the default one
+ * @cache_dir: (allow-none): the directory to store the cached data, or %NULL
+ *   to use the default one. Note that since the cache isn't safe to access for
+ *   multiple processes at once, and the default directory isn't namespaced by
+ *   process, clients are strongly discouraged from passing %NULL.
  * @cache_type: the #SoupCacheType of the cache
  *
  * Creates a new #SoupCache.
@@ -1173,18 +1070,11 @@ SoupCacheResponse
 soup_cache_has_response (SoupCache *cache, SoupMessage *msg)
 {
        SoupCacheEntry *entry;
-       const char *cache_control, *pragma;
+       const char *cache_control;
        gpointer value;
        int max_age, max_stale, min_fresh;
        GList *lru_item, *item;
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       const char *ua;
-#endif
-#if ENABLE(TIZEN_CACHE_FILE_SIZE_VALIDATION)
-       GFile *file;
-       GFileInfo *file_info;
-       goffset file_size;
-#endif
+
        entry = soup_cache_entry_lookup (cache, msg);
 
        /* 1. The presented Request-URI and that of stored response
@@ -1193,23 +1083,7 @@ soup_cache_has_response (SoupCache *cache, SoupMessage *msg)
        if (!entry)
                return SOUP_CACHE_RESPONSE_STALE;
 
-#if ENABLE(TIZEN_CACHE_FILE_SIZE_VALIDATION)
-       file = get_file_from_entry (cache, entry);
-       file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_SIZE,
-                     G_FILE_QUERY_INFO_NONE, NULL, NULL);
-
-       if (file_info && (file_size = g_file_info_get_size (file_info)) != entry->length) {
-               soup_cache_entry_remove(cache, entry, TRUE);
-               g_file_delete (file, NULL, NULL);
-               g_object_unref (file_info);
-               g_object_unref (file);
-               return SOUP_CACHE_RESPONSE_STALE;
-       }
-       g_object_unref (file_info);
-       g_object_unref (file);
-#endif
-
-/* Increase hit count. Take sorting into account */
+       /* Increase hit count. Take sorting into account */
        entry->hits++;
        lru_item = g_list_find (cache->priv->lru_start, entry);
        item = lru_item;
@@ -1257,18 +1131,9 @@ soup_cache_has_response (SoupCache *cache, SoupMessage *msg)
 
        /* For HTTP 1.0 compatibility. RFC2616 section 14.9.4
         */
-       pragma = soup_message_headers_get_list (msg->request_headers, "Pragma");
-       if (pragma && soup_header_contains (pragma, "no-cache"))
+       if (soup_message_headers_header_contains (msg->request_headers, "Pragma", "no-cache"))
                return SOUP_CACHE_RESPONSE_STALE;
 
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       ua = soup_message_headers_get_one (msg->request_headers, "User-Agent");
-       if (ua && entry->user_agent) {
-               if (strcmp (ua, entry->user_agent))
-                       return SOUP_CACHE_RESPONSE_STALE;
-       }
-#endif
-
        cache_control = soup_message_headers_get_list (msg->request_headers, "Cache-Control");
        if (cache_control && *cache_control) {
                GHashTable *hash = soup_header_parse_param_list (cache_control);
@@ -1283,11 +1148,7 @@ soup_cache_has_response (SoupCache *cache, SoupMessage *msg)
                        return SOUP_CACHE_RESPONSE_STALE;
                }
 
-#if ENABLE (TIZEN_HANDLE_MALFORMED_MAX_AGE_HEADER)
                if (g_hash_table_lookup_extended (hash, "max-age", NULL, &value) && value) {
-#else
-               if (g_hash_table_lookup_extended (hash, "max-age", NULL, &value)) {
-#endif
                        max_age = (int)MIN (g_ascii_strtoll (value, NULL, 10), G_MAXINT32);
                        /* Forcing cache revalidaton
                         */
@@ -1325,12 +1186,17 @@ soup_cache_has_response (SoupCache *cache, SoupMessage *msg)
        /* 6. The stored response is either: fresh, allowed to be
         * served stale or succesfully validated
         */
-       /* TODO consider also proxy-revalidate & s-maxage */
-       if (entry->must_revalidate)
-               return SOUP_CACHE_RESPONSE_NEEDS_VALIDATION;
-
        if (!soup_cache_entry_is_fresh_enough (entry, min_fresh)) {
                /* Not fresh, can it be served stale? */
+
+               /* When the must-revalidate directive is present in a
+                * response received by a cache, that cache MUST NOT
+                * use the entry after it becomes stale
+                */
+               /* TODO consider also proxy-revalidate & s-maxage */
+               if (entry->must_revalidate)
+                       return SOUP_CACHE_RESPONSE_NEEDS_VALIDATION;
+
                if (max_stale != -1) {
                        /* G_MAXINT32 means we accept any staleness */
                        if (max_stale == G_MAXINT32)
@@ -1414,7 +1280,6 @@ soup_cache_flush (SoupCache *cache)
                g_warning ("Cache flush finished despite %d pending requests", cache->priv->n_pending);
 }
 
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
 typedef void (* SoupCacheForeachFileFunc) (SoupCache *cache, const char *name, gpointer user_data);
 
 static void
@@ -1434,49 +1299,13 @@ soup_cache_foreach_file (SoupCache *cache, SoupCacheForeachFileFunc func, gpoint
        g_dir_close (dir);
 }
 
-#if ENABLE(TIZEN_TV_SOUP_CACHE_OPTIMISE_LOAD_TIME)
-
-/*
- * When a list of regular files is required, examining the d_type field returned from readdir() is faster
- * than using g_file_test(G_FILE_TEST_IS_REGULAR).
-*/
-
 static void
-soup_cache_foreach_regular_file (SoupCache *cache, SoupCacheForeachFileFunc func, gpointer user_data)
+clear_cache_item (gpointer data,
+                 gpointer user_data)
 {
-       DIR *dir;
-       const char *name;
-       SoupCachePrivate *priv = cache->priv;
-
-       dir = opendir(priv->cache_dir);
-       if (dir) {
-               struct dirent *dp;
-               struct dirent dent_buf;
-               while (!readdir_r(dir, &dent_buf, &dp) && dp) {
-                       if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
-                               continue;
-
-                       name = dp->d_name;
-                       if (g_str_has_prefix (name, "soup."))
-                               continue;
-                       else if (dp->d_type == DT_UNKNOWN) {
-                               // This path should not be executed normally, but is included as a fail safe.
-                               gchar *path = g_build_filename(priv->cache_dir, name, NULL);
-                               const int isreg = g_file_test (path, G_FILE_TEST_IS_REGULAR);
-                               g_free(path);
-                               if (!isreg)
-                                       continue;
-                       } else if (dp->d_type != DT_REG)
-                               continue;
-
-                       func (cache, name, user_data);
-               }
-               closedir(dir);
-       }
+       soup_cache_entry_remove ((SoupCache *) user_data, (SoupCacheEntry *) data, TRUE);
 }
 
-#endif
-
 static void
 delete_cache_file (SoupCache *cache, const char *name, gpointer user_data)
 {
@@ -1486,42 +1315,11 @@ delete_cache_file (SoupCache *cache, const char *name, gpointer user_data)
        g_unlink (path);
        g_free (path);
 }
-#endif
-
-static void
-clear_cache_item (gpointer data,
-                 gpointer user_data)
-{
-       soup_cache_entry_remove ((SoupCache *) user_data, (SoupCacheEntry *) data, TRUE);
-}
 
 static void
 clear_cache_files (SoupCache *cache)
 {
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
        soup_cache_foreach_file (cache, delete_cache_file, NULL);
-#else
-       GFileInfo *file_info;
-       GFileEnumerator *file_enumerator;
-       GFile *cache_dir_file = g_file_new_for_path (cache->priv->cache_dir);
-
-       file_enumerator = g_file_enumerate_children (cache_dir_file, G_FILE_ATTRIBUTE_STANDARD_NAME,
-                                                    G_FILE_QUERY_INFO_NONE, NULL, NULL);
-       if (file_enumerator) {
-               while ((file_info = g_file_enumerator_next_file (file_enumerator, NULL, NULL)) != NULL) {
-                       const char *filename = g_file_info_get_name (file_info);
-
-                       if (strcmp (filename, SOUP_CACHE_FILE) != 0) {
-                               GFile *cache_file = g_file_get_child (cache_dir_file, filename);
-                               g_file_delete (cache_file, NULL, NULL);
-                               g_object_unref (cache_file);
-                       }
-                       g_object_unref (file_info);
-               }
-               g_object_unref (file_enumerator);
-       }
-       g_object_unref (cache_dir_file);
-#endif
 }
 
 /**
@@ -1540,7 +1338,7 @@ soup_cache_clear (SoupCache *cache)
        g_return_if_fail (SOUP_IS_CACHE (cache));
        g_return_if_fail (cache->priv->cache);
 
-       // Cannot use g_hash_table_foreach as callbacks must not modify the hash table
+       /* Cannot use g_hash_table_foreach as callbacks must not modify the hash table */
        entries = g_hash_table_get_values (cache->priv->cache);
        g_list_foreach (entries, clear_cache_item, cache);
        g_list_free (entries);
@@ -1577,6 +1375,7 @@ soup_cache_generate_conditional_request (SoupCache *cache, SoupMessage *original
        /* Copy the data we need from the original message */
        uri = soup_message_get_uri (original);
        msg = soup_message_new_from_uri (original->method, uri);
+       soup_message_set_flags (msg, soup_message_get_flags (original));
        soup_message_disable_feature (msg, SOUP_TYPE_CACHE);
 
        soup_message_headers_foreach (original->request_headers,
@@ -1629,10 +1428,6 @@ soup_cache_update_from_conditional_request (SoupCache   *cache,
                copy_end_to_end_headers (msg->response_headers, entry->headers);
 
                soup_cache_entry_set_freshness (entry, msg, cache);
-#if ENABLE_TIZEN_UPDATE_CORRECTED_INITIAL_AGE_FOR_CACHE
-               soup_cache_entry_update_corrected_initial_age (entry);
-               entry->response_time = time (NULL);
-#endif
        }
 }
 
@@ -1650,12 +1445,6 @@ pack_entry (gpointer data,
                return;
 
        g_variant_builder_open (entries_builder, G_VARIANT_TYPE (SOUP_CACHE_PHEADERS_FORMAT));
-#if ENABLE(TIZEN_FIX_PACK_ENTRY)
-       if (!g_utf8_validate (entry->uri, -1, NULL)) {
-               g_variant_builder_close (entries_builder);
-               return;
-       }
-#endif
        g_variant_builder_add (entries_builder, "s", entry->uri);
        g_variant_builder_add (entries_builder, "b", entry->must_revalidate);
        g_variant_builder_add (entries_builder, "u", entry->freshness_lifetime);
@@ -1664,9 +1453,7 @@ pack_entry (gpointer data,
        g_variant_builder_add (entries_builder, "u", entry->hits);
        g_variant_builder_add (entries_builder, "u", entry->length);
        g_variant_builder_add (entries_builder, "q", entry->status_code);
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       g_variant_builder_add (entries_builder, "s", entry->user_agent);
-#endif
+
        /* Pack headers */
        g_variant_builder_open (entries_builder, G_VARIANT_TYPE ("a" SOUP_CACHE_HEADERS_FORMAT));
        soup_message_headers_iter_init (&iter, entry->headers);
@@ -1695,27 +1482,14 @@ pack_entry (gpointer data,
 void
 soup_cache_dump (SoupCache *cache)
 {
-       SoupCachePrivate *priv = SOUP_CACHE_GET_PRIVATE (cache);
+       SoupCachePrivate *priv = soup_cache_get_instance_private (cache);
        char *filename;
        GVariantBuilder entries_builder;
        GVariant *cache_variant;
 
        if (!g_list_length (cache->priv->lru_start))
-#if ENABLE(TIZEN_FIX_CACHE_DUMP)
-       {
-               GFile *file;
-               filename = g_build_filename (priv->cache_dir, SOUP_CACHE_FILE, NULL);
-               file = g_file_new_for_path (filename);
-               if (file) {
-                       g_file_delete (file, NULL, NULL);
-                       g_object_unref (file);
-               }
-               g_free (filename);
                return;
-       }
-#else
-       return;
-#endif
+
        /* Create the builder and iterate over all entries */
        g_variant_builder_init (&entries_builder, G_VARIANT_TYPE (SOUP_CACHE_ENTRIES_FORMAT));
        g_variant_builder_add (&entries_builder, "q", SOUP_CACHE_CURRENT_VERSION);
@@ -1733,7 +1507,6 @@ soup_cache_dump (SoupCache *cache)
        g_variant_unref (cache_variant);
 }
 
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
 static inline guint32
 get_key_from_cache_filename (const char *name)
 {
@@ -1749,10 +1522,7 @@ insert_cache_file (SoupCache *cache, const char *name, GHashTable *leaked_entrie
        gchar *path;
 
        path = g_build_filename (cache->priv->cache_dir, name, NULL);
-#if !ENABLE(TIZEN_TV_SOUP_CACHE_OPTIMISE_LOAD_TIME)
-       if (g_file_test (path, G_FILE_TEST_IS_REGULAR))
-#endif
-       {
+       if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
                guint32 key = get_key_from_cache_filename (name);
 
                if (key) {
@@ -1762,7 +1532,6 @@ insert_cache_file (SoupCache *cache, const char *name, GHashTable *leaked_entrie
        }
        g_free (path);
 }
-#endif
 
 /**
  * soup_cache_load:
@@ -1785,14 +1554,10 @@ soup_cache_load (SoupCache *cache)
        SoupCacheEntry *entry;
        SoupCachePrivate *priv = cache->priv;
        guint16 version, status_code;
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
        GHashTable *leaked_entries = NULL;
        GHashTableIter iter;
        gpointer value;
-#endif
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       const char *ua;
-#endif
+
        filename = g_build_filename (priv->cache_dir, SOUP_CACHE_FILE, NULL);
        if (!g_file_get_contents (filename, &contents, &length, NULL)) {
                g_free (filename);
@@ -1812,26 +1577,13 @@ soup_cache_load (SoupCache *cache)
                return;
        }
 
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
        leaked_entries = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
-#if ENABLE(TIZEN_TV_SOUP_CACHE_OPTIMISE_LOAD_TIME)
-       soup_cache_foreach_regular_file (cache, (SoupCacheForeachFileFunc)insert_cache_file, leaked_entries);
-#else
        soup_cache_foreach_file (cache, (SoupCacheForeachFileFunc)insert_cache_file, leaked_entries);
-#endif
-#endif
 
-#if ENABLE(TIZEN_USER_AGENT_CHECK_IN_CACHE)
-       while (g_variant_iter_loop (entries_iter, SOUP_CACHE_PHEADERS_FORMAT,
-                                   &url, &must_revalidate, &freshness_lifetime, &corrected_initial_age,
-                                   &response_time, &hits, &length, &status_code, &ua,
-                                   &headers_iter)) {
-#else
        while (g_variant_iter_loop (entries_iter, SOUP_CACHE_PHEADERS_FORMAT,
                                    &url, &must_revalidate, &freshness_lifetime, &corrected_initial_age,
                                    &response_time, &hits, &length, &status_code,
                                    &headers_iter)) {
-#endif
                const char *header_key, *header_value;
                SoupMessageHeaders *headers;
                SoupMessageHeadersIter soup_headers_iter;
@@ -1860,43 +1612,18 @@ soup_cache_load (SoupCache *cache)
                entry->length = length;
                entry->headers = headers;
                entry->status_code = status_code;
-#if ENABLE(TIZEN_TV_CHECKING_DELETED_ENTRY_FILE)
-               entry->key = get_cache_key_from_uri ((const char *) entry->uri);
-
-#if ENABLE(TIZEN_TV_SOUP_CACHE_OPTIMISE_LOAD_TIME) && ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
-               // Check against "leaked_entries" to see if the file exists.  This avoids the need to call g_file_query_exists(), which is much slower.
-               if (g_hash_table_lookup(leaked_entries, GUINT_TO_POINTER(entry->key)) == NULL) {
-                       soup_cache_entry_free (entry);
-                       continue;
-               }
-#else
-               file = get_file_from_entry (cache, entry);
-               if (file) {
-                       gboolean file_exist = g_file_query_exists (file, NULL);
-                       g_object_unref(file);
-                       if (!file_exist) {
-                               soup_cache_entry_free (entry);
-                               continue;
-                       }
-               }
-#endif
-#endif
 
                if (!soup_cache_entry_insert (cache, entry, FALSE))
                        soup_cache_entry_free (entry);
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
                else
                        g_hash_table_remove (leaked_entries, GUINT_TO_POINTER (entry->key));
-#endif
        }
 
        /* Remove the leaked files */
-#if ENABLE(TIZEN_TV_SOUP_CACHE_CLEAN_LEAKED_RESOURCES)
        g_hash_table_iter_init (&iter, leaked_entries);
        while (g_hash_table_iter_next (&iter, NULL, &value))
                g_unlink ((char *)value);
        g_hash_table_destroy (leaked_entries);
-#endif
 
        cache->priv->lru_start = g_list_reverse (cache->priv->lru_start);
 
@@ -1937,18 +1664,3 @@ soup_cache_get_max_size (SoupCache *cache)
 {
        return cache->priv->max_size;
 }
-
-#if ENABLE (TIZEN_UPDATE_CACHE_ENTRY_CONTENT_TYPE_HEADER)
-void soup_cache_entry_set_content_type (SoupSession *session, SoupMessage *msg, const char *content_type)
-{
-       SoupCacheEntry *entry;
-       SoupCache *cache = (SoupCache *)soup_session_get_feature (session, SOUP_TYPE_CACHE);
-
-       if (!cache)
-               return;
-
-       entry = soup_cache_entry_lookup (cache, msg);
-       if (entry)
-           soup_message_headers_replace (entry->headers, "Content-Type", content_type);
-}
-#endif