<commit_before>/*
 Copyright (C) 2008 <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; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#if HAVE_CONFIG_H
    #include <config.h>
#endif

#include "katze-net.h"

#if HAVE_LIBSOUP
    #include <libsoup/soup.h>
#endif

struct _KatzeNet
{
    GObject parent_instance;

    GHashTable* memory;
    gchar* cache_path;
    guint cache_size;

    #if HAVE_LIBSOUP
    SoupSession* session;
    #endif
};

struct _KatzeNetClass
{
    GObjectClass parent_class;
};

G_DEFINE_TYPE (KatzeNet, katze_net, G_TYPE_OBJECT)

static void
katze_net_finalize (GObject* object);

static void
katze_net_class_init (KatzeNetClass* class)
{
    GObjectClass* gobject_class;

    gobject_class = G_OBJECT_CLASS (class);
    gobject_class->finalize = katze_net_finalize;
}

static void
katze_net_object_maybe_unref (gpointer object)
{
    if (object)
        g_object_unref (object);
}

static void
katze_net_init (KatzeNet* net)
{
    net->memory = g_hash_table_new_full (g_str_hash, g_str_equal,
                                         g_free, katze_net_object_maybe_unref);
    net->cache_path = g_build_filename (g_get_user_cache_dir (),
                                        PACKAGE_NAME, NULL);

    #if HAVE_LIBSOUP
    net->session = soup_session_async_new ();
    #endif
}

static void
katze_net_finalize (GObject* object)
{
    KatzeNet* net = KATZE_NET (object);

    g_hash_table_destroy (net->memory);
    katze_assign (net->cache_path, NULL);

    G_OBJECT_CLASS (katze_net_parent_class)->finalize (object);
}

/**
 * katze_net_new:
 *
 * Instantiates a new #KatzeNet singleton.
 *
 * Subsequent calls will ref the initial instance.
 *
 * Return value: a new #KatzeNet
 **/
KatzeNet*
katze_net_new (void)
{
    KatzeNet* net = g_object_new (KATZE_TYPE_NET,
                                  NULL);

    return net;
}

typedef struct
{
    KatzeNet* net;
    KatzeNetStatusCb status_cb;
    KatzeNetTransferCb transfer_cb;
    gpointer user_data;
    KatzeNetRequest* request;
} KatzeNetPriv;

static void
katze_net_priv_free (KatzeNetPriv* priv)
{
    KatzeNetRequest* request;

    request = priv->request;

    g_free (request->uri);
    g_free (request->mime_type);
    g_free (request->data);

    g_free (request);
    g_free (priv);
}

static gchar*
katze_net_get_cached_path (KatzeNet*    net,
                           const gchar* uri,
                           const gchar* subfolder)
{
    gchar* cache_path;
    gchar* checksum;
    gchar* extension;
    gchar* cached_filename;
    gchar* cached_path;

    if (subfolder)
        cache_path = g_build_filename (net->cache_path, subfolder, NULL);
    else
        cache_path = net->cache_path;
    g_mkdir_with_parents (cache_path, 0755);
    #if GLIB_CHECK_VERSION (2, 16, 0)
    checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
    #else
    checksum = g_strdup_printf ("%u", g_str_hash (uri));
    #endif

    extension = g_strrstr (uri, ".");
    cached_filename = g_strdup_printf ("%s%s%s", checksum,
        extension ? "." : "", extension ? extension : "");
    g_free (checksum);
    cached_path = g_build_filename (cache_path, cached_filename, NULL);
    g_free (cached_filename);
    if (subfolder)
        g_free (cache_path);
    return cached_path;
}

#if HAVE_LIBSOUP
static void
katze_net_got_body_cb (SoupMessage*  msg,
                       KatzeNetPriv* priv);

static void
katze_net_got_headers_cb (SoupMessage*  msg,
                          KatzeNetPriv* priv)
{<commit_msg>Don't doublicate the "." in cached filenames<commit_after>/*
 Copyright (C) 2008 <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; either
 version 2.1 of the License, or (at your option) any later version.

 See the file COPYING for the full license text.
*/

#if HAVE_CONFIG_H
    #include <config.h>
#endif

#include "katze-net.h"

#if HAVE_LIBSOUP
    #include <libsoup/soup.h>
#endif

struct _KatzeNet
{
    GObject parent_instance;

    GHashTable* memory;
    gchar* cache_path;
    guint cache_size;

    #if HAVE_LIBSOUP
    SoupSession* session;
    #endif
};

struct _KatzeNetClass
{
    GObjectClass parent_class;
};

G_DEFINE_TYPE (KatzeNet, katze_net, G_TYPE_OBJECT)

static void
katze_net_finalize (GObject* object);

static void
katze_net_class_init (KatzeNetClass* class)
{
    GObjectClass* gobject_class;

    gobject_class = G_OBJECT_CLASS (class);
    gobject_class->finalize = katze_net_finalize;
}

static void
katze_net_object_maybe_unref (gpointer object)
{
    if (object)
        g_object_unref (object);
}

static void
katze_net_init (KatzeNet* net)
{
    net->memory = g_hash_table_new_full (g_str_hash, g_str_equal,
                                         g_free, katze_net_object_maybe_unref);
    net->cache_path = g_build_filename (g_get_user_cache_dir (),
                                        PACKAGE_NAME, NULL);

    #if HAVE_LIBSOUP
    net->session = soup_session_async_new ();
    #endif
}

static void
katze_net_finalize (GObject* object)
{
    KatzeNet* net = KATZE_NET (object);

    g_hash_table_destroy (net->memory);
    katze_assign (net->cache_path, NULL);

    G_OBJECT_CLASS (katze_net_parent_class)->finalize (object);
}

/**
 * katze_net_new:
 *
 * Instantiates a new #KatzeNet singleton.
 *
 * Subsequent calls will ref the initial instance.
 *
 * Return value: a new #KatzeNet
 **/
KatzeNet*
katze_net_new (void)
{
    KatzeNet* net = g_object_new (KATZE_TYPE_NET,
                                  NULL);

    return net;
}

typedef struct
{
    KatzeNet* net;
    KatzeNetStatusCb status_cb;
    KatzeNetTransferCb transfer_cb;
    gpointer user_data;
    KatzeNetRequest* request;
} KatzeNetPriv;

static void
katze_net_priv_free (KatzeNetPriv* priv)
{
    KatzeNetRequest* request;

    request = priv->request;

    g_free (request->uri);
    g_free (request->mime_type);
    g_free (request->data);

    g_free (request);
    g_free (priv);
}

static gchar*
katze_net_get_cached_path (KatzeNet*    net,
                           const gchar* uri,
                           const gchar* subfolder)
{
    gchar* cache_path;
    gchar* checksum;
    gchar* extension;
    gchar* cached_filename;
    gchar* cached_path;

    if (subfolder)
        cache_path = g_build_filename (net->cache_path, subfolder, NULL);
    else
        cache_path = net->cache_path;
    g_mkdir_with_parents (cache_path, 0755);
    #if GLIB_CHECK_VERSION (2, 16, 0)
    checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1);
    #else
    checksum = g_strdup_printf ("%u", g_str_hash (uri));
    #endif

    extension = g_strrstr (uri, ".");
    cached_filename = g_strdup_printf ("%s%s", checksum,
                                       extension ? extension : "");
    g_free (checksum);
    cached_path = g_build_filename (cache_path, cached_filename, NULL);
    g_free (cached_filename);
    if (subfolder)
        g_free (cache_path);
    return cached_path;
}

#if HAVE_LIBSOUP
static void
katze_net_got_body_cb (SoupMessage*  msg,
                       KatzeNetPriv* priv);

static void
katze_net_got_headers_cb (SoupMessage*  msg,
                          KatzeNetPriv* priv)
{
{'fraction_non_alphanumeric': 0.09174190122145512, 'fraction_numerical': 0.004248539564524694, 'mean_word_length': 2.9793977812995247, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2593880', 'n_tokens_mistral': 2666, 'n_tokens_neox': 2565, 'n_words': 620}