<commit_before>/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2012 Red Hat, Inc.

   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.

   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, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include "spice-client.h"
#include "spice-proxy.h"

struct _SpiceProxyPrivate {
    gchar *protocol;
    gchar *hostname;
    guint port;
    gchar *user;
    gchar *password;
};

#define SPICE_PROXY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), SPICE_TYPE_PROXY, SpiceProxyPrivate))

G_DEFINE_TYPE(SpiceProxy, spice_proxy, G_TYPE_OBJECT);

enum  {
    SPICE_PROXY_DUMMY_PROPERTY,
    SPICE_PROXY_PROTOCOL,
    SPICE_PROXY_USER,
    SPICE_PROXY_PASSWORD,
    SPICE_PROXY_HOSTNAME,
    SPICE_PROXY_PORT
};

G_GNUC_INTERNAL
SpiceProxy* spice_proxy_new(void)
{
    SpiceProxy * self = NULL;
    self = (SpiceProxy*)g_object_new(SPICE_TYPE_PROXY, NULL);
    return self;
}

G_GNUC_INTERNAL
gboolean spice_proxy_parse(SpiceProxy *self, const gchar *proxyuri, GError **error)
{
    gchar *dup, *uri;
    gboolean success = FALSE;
    size_t len;

    g_return_val_if_fail(self != NULL, FALSE);
    g_return_val_if_fail(proxyuri != NULL, FALSE);

    uri = dup = g_strdup(proxyuri);
    /* FIXME: use GUri when it is ready... only support http atm */
    /* the code is voluntarily not parsing thoroughly the uri */
    if (g_ascii_strncasecmp("http://", uri, 7) == 0)
        uri += 7;

    /* remove trailing slash */
    len = strlen(uri);
    for (; len > 0; len--)
        if (uri[len-1] == '/')
            uri[len-1] = '\0';
        else
            break;

    spice_proxy_set_protocol(self, "http");
    spice_proxy_set_port(self, 3128);

    /* max 2 parts, host:port */
    gchar **proxyv = g_strsplit(uri, ":", 2);
    const gchar *proxy_port = NULL;

    if (proxyv[0] == NULL || strlen(proxyv[0]) == 0) {
        g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                    "Invalid hostname in proxy address");
        goto end;
    }

    spice_proxy_set_hostname(self, proxyv[0]);
    if (proxyv[0] != NULL)
        proxy_port = proxyv[1];

    if (proxy_port != NULL) {
        char *endptr;
        guint port = strtoul(proxy_port, &endptr, 10);
        if (*endptr != '\0') {
            g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                        "Invalid proxy port: %s", proxy_port);
            goto end;
        }<commit_msg>proxy: parse user and pass from uri<commit_after>/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2012 Red Hat, Inc.

   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.

   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, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include "glib-compat.h"
#include "spice-client.h"
#include "spice-proxy.h"

struct _SpiceProxyPrivate {
    gchar *protocol;
    gchar *hostname;
    guint port;
    gchar *user;
    gchar *password;
};

#define SPICE_PROXY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), SPICE_TYPE_PROXY, SpiceProxyPrivate))

G_DEFINE_TYPE(SpiceProxy, spice_proxy, G_TYPE_OBJECT);

enum  {
    SPICE_PROXY_DUMMY_PROPERTY,
    SPICE_PROXY_PROTOCOL,
    SPICE_PROXY_USER,
    SPICE_PROXY_PASSWORD,
    SPICE_PROXY_HOSTNAME,
    SPICE_PROXY_PORT
};

G_GNUC_INTERNAL
SpiceProxy* spice_proxy_new(void)
{
    SpiceProxy * self = NULL;
    self = (SpiceProxy*)g_object_new(SPICE_TYPE_PROXY, NULL);
    return self;
}

G_GNUC_INTERNAL
gboolean spice_proxy_parse(SpiceProxy *self, const gchar *proxyuri, GError **error)
{
    gchar *dup, *uri;
    gboolean success = FALSE;
    size_t len;

    g_return_val_if_fail(self != NULL, FALSE);
    g_return_val_if_fail(proxyuri != NULL, FALSE);

    uri = dup = g_strdup(proxyuri);
    /* FIXME: use GUri when it is ready... only support http atm */
    /* the code is voluntarily not parsing thoroughly the uri */
    if (g_ascii_strncasecmp("http://", uri, 7) == 0)
        uri += 7;

    /* remove trailing slash */
    len = strlen(uri);
    for (; len > 0; len--)
        if (uri[len-1] == '/')
            uri[len-1] = '\0';
        else
            break;

    spice_proxy_set_protocol(self, "http");
    spice_proxy_set_port(self, 3128);

    /* yes, that parser is bad, we need GUri... */
    if (strstr(uri, "@")) {
        gchar *saveptr, *saveptr2;
        gchar *next = strstr(uri, "@") + 1;
        gchar *auth = strtok_r(uri, "@", &saveptr);
        const gchar *user = strtok_r(auth, ":", &saveptr2);
        const gchar *pass = strtok_r(NULL, ":", &saveptr2);
        spice_proxy_set_user(self, user);
        spice_proxy_set_password(self, pass);
        uri = next;
    }

    /* max 2 parts, host:port */
    gchar **proxyv = g_strsplit(uri, ":", 2);
    const gchar *proxy_port = NULL;

    if (proxyv[0] == NULL || strlen(proxyv[0]) == 0) {
        g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                    "Invalid hostname in proxy address");
        goto end;
    }

    spice_proxy_set_hostname(self, proxyv[0]);
    if (proxyv[0] != NULL)
        proxy_port = proxyv[1];

    if (proxy_port != NULL) {
        char *endptr;
        guint port = strtoul(proxy_port, &endptr, 10);
        if (*endptr != '\0') {
            g_set_error(error, SPICE_CLIENT_ERROR, SPICE_CLIENT_ERROR_FAILED,
                        "Invalid proxy port: %s", proxy_port);
            goto end;
        }
{'fraction_non_alphanumeric': 0.11236641221374045, 'fraction_numerical': 0.009465648854961832, 'mean_word_length': 2.925104853205512, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '63690', 'n_tokens_mistral': 2262, 'n_tokens_neox': 2222, 'n_words': 625}