<commit_before>/*
 * Copyright (C) 2012 Igalia S.L.
 * Copyright (C) 2012 Canonical Ltd.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * Authors: <NAME>. <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* Using the cache feature requires to use the unstable API */
#define LIBSOUP_USE_UNSTABLE_REQUEST_API

#include <glib/gi18n-lib.h>
#include <libsoup/soup-cache.h>
#include <libsoup/soup-request-http.h>

#ifndef LIBSOUP_REQUESTER_DEPRECATED
#include <libsoup/soup-requester.h>
#endif

#include "grl-net-private.h"

#define GRL_LOG_DOMAIN_DEFAULT wc_log_domain
GRL_LOG_DOMAIN_EXTERN(wc_log_domain);

void
init_requester (GrlNetWc *self)
{
#ifndef LIBSOUP_REQUESTER_DEPRECATED
  GrlNetWcPrivate *priv = self->priv;

  priv->requester = soup_requester_new ();
  soup_session_add_feature (priv->session,
                            SOUP_SESSION_FEATURE (priv->requester));
#endif
  init_dump_directory ();
}

void
finalize_requester (GrlNetWc *self)
{
  GrlNetWcPrivate *priv = self->priv;

  cache_down (self);
  g_free (priv->previous_data);

#ifndef LIBSOUP_REQUESTER_DEPRECATED
  g_object_unref (priv->requester);
#endif
}

static inline void
clear_cache (SoupCache *cache)
{
  GFile *cache_dir_file;
  gchar *cache_dir;

  soup_cache_clear (cache);

  g_object_get (cache, "cache-dir", &cache_dir, NULL);
  cache_dir_file = g_file_new_for_path (cache_dir);
  g_free (cache_dir);

  g_file_delete (cache_dir_file, NULL, NULL);
  g_object_unref (G_OBJECT (cache_dir_file));
}

void
cache_down (GrlNetWc *self)
{
  GrlNetWcPrivate *priv = self->priv;
  SoupSessionFeature *cache = soup_session_get_feature (priv->session, SOUP_TYPE_CACHE);

  GRL_DEBUG ("cache down");

  if (!cache)
    return;

  clear_cache (SOUP_CACHE (cache));
  soup_session_remove_feature (priv->session, cache);
}

void
cache_up (GrlNetWc *self)
{
  SoupCache *cache;
  GrlNetWcPrivate *priv = self->priv;
  gchar *dir;

  GRL_DEBUG ("cache up");

  dir = g_dir_make_tmp ("grilo-plugin-cache-XXXXXX", NULL);
  if (!dir)
    return;

  cache = soup_cache_new (dir, SOUP_CACHE_SINGLE_USER);
  g_free (dir);

  soup_session_add_feature (priv->session,
                            SOUP_SESSION_FEATURE (cache));

  if (priv->cache_size) {
    soup_cache_set_max_size (cache, priv->cache_size * 1024 * 1024);
  }

  g_object_unref (cache);
}

gboolean
cache_is_available (GrlNetWc *self)
{
  return soup_session_has_feature (self->priv->session, SOUP_TYPE_CACHE);
}

void
cache_set_size (GrlNetWc *self, guint size)
{
  if (self->priv->cache_size == size)
    return;

  self->priv->cache_size = size;<commit_msg>net: use different function to check cache<commit_after>/*
 * Copyright (C) 2012 Igalia S.L.
 * Copyright (C) 2012 Canonical Ltd.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * Authors: <NAME>. <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

/* Using the cache feature requires to use the unstable API */
#define LIBSOUP_USE_UNSTABLE_REQUEST_API

#include <glib/gi18n-lib.h>
#include <libsoup/soup-cache.h>
#include <libsoup/soup-request-http.h>

#ifndef LIBSOUP_REQUESTER_DEPRECATED
#include <libsoup/soup-requester.h>
#endif

#include "grl-net-private.h"

#define GRL_LOG_DOMAIN_DEFAULT wc_log_domain
GRL_LOG_DOMAIN_EXTERN(wc_log_domain);

void
init_requester (GrlNetWc *self)
{
#ifndef LIBSOUP_REQUESTER_DEPRECATED
  GrlNetWcPrivate *priv = self->priv;

  priv->requester = soup_requester_new ();
  soup_session_add_feature (priv->session,
                            SOUP_SESSION_FEATURE (priv->requester));
#endif
  init_dump_directory ();
}

void
finalize_requester (GrlNetWc *self)
{
  GrlNetWcPrivate *priv = self->priv;

  cache_down (self);
  g_free (priv->previous_data);

#ifndef LIBSOUP_REQUESTER_DEPRECATED
  g_object_unref (priv->requester);
#endif
}

static inline void
clear_cache (SoupCache *cache)
{
  GFile *cache_dir_file;
  gchar *cache_dir;

  soup_cache_clear (cache);

  g_object_get (cache, "cache-dir", &cache_dir, NULL);
  cache_dir_file = g_file_new_for_path (cache_dir);
  g_free (cache_dir);

  g_file_delete (cache_dir_file, NULL, NULL);
  g_object_unref (G_OBJECT (cache_dir_file));
}

void
cache_down (GrlNetWc *self)
{
  GrlNetWcPrivate *priv = self->priv;
  SoupSessionFeature *cache = soup_session_get_feature (priv->session, SOUP_TYPE_CACHE);

  GRL_DEBUG ("cache down");

  if (!cache)
    return;

  clear_cache (SOUP_CACHE (cache));
  soup_session_remove_feature (priv->session, cache);
}

void
cache_up (GrlNetWc *self)
{
  SoupCache *cache;
  GrlNetWcPrivate *priv = self->priv;
  gchar *dir;

  GRL_DEBUG ("cache up");

  dir = g_dir_make_tmp ("grilo-plugin-cache-XXXXXX", NULL);
  if (!dir)
    return;

  cache = soup_cache_new (dir, SOUP_CACHE_SINGLE_USER);
  g_free (dir);

  soup_session_add_feature (priv->session,
                            SOUP_SESSION_FEATURE (cache));

  if (priv->cache_size) {
    soup_cache_set_max_size (cache, priv->cache_size * 1024 * 1024);
  }

  g_object_unref (cache);
}

gboolean
cache_is_available (GrlNetWc *self)
{
  return soup_session_get_feature (self->priv->session, SOUP_TYPE_CACHE) != NULL;
}

void
cache_set_size (GrlNetWc *self, guint size)
{
  if (self->priv->cache_size == size)
    return;

  self->priv->cache_size = size;
{'fraction_non_alphanumeric': 0.1080088170462895, 'fraction_numerical': 0.00911094783247612, 'mean_word_length': 4.231360491929285, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '104477', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2379, 'n_words': 638}