<commit_before>#include "system.h"

#include <netinet/in.h>

#include "build/rpmbuild.h"

#include "url.h"
#include "ftp.h"

static struct urlstring {
    const char *leadin;
    urltype	ret;
} urlstrings[] = {
    { "file://",	URL_IS_PATH },
    { "ftp://",		URL_IS_FTP },
    { "http://",	URL_IS_HTTP },
    { "-",		URL_IS_DASH },
    { NULL,		URL_IS_UNKNOWN }
};

void freeUrlinfo(urlinfo *u)
{
    if (u->ftpControl >= 0)
	close(u->ftpControl);
    FREE(u->url);
    FREE(u->service);
    FREE(u->user);
    FREE(u->password);
    FREE(u->host);
    FREE(u->portstr);
    FREE(u->path);
    FREE(u->proxyu);
    FREE(u->proxyh);

    FREE(u);
}

urlinfo *newUrlinfo(void)
{
    urlinfo *u;
    if ((u = malloc(sizeof(*u))) == NULL)
	return NULL;
    memset(u, 0, sizeof(*u));
    u->proxyp = -1;
    u->port = -1;
    u->ftpControl = -1;
    u->ftpGetFileDoneNeeded = 0;
    return u;
}

static void findUrlinfo(urlinfo **uret, int mustAsk)
{
    urlinfo *u;
    urlinfo **empty;
    static urlinfo **uCache = NULL;
    static int uCount = 0;
    int i;

    if (uret == NULL)
	return;

    u = *uret;

    empty = NULL;
    for (i = 0; i < uCount; i++) {
	urlinfo *ou;
	if ((ou = uCache[i]) == NULL) {
	    if (empty == NULL)
		empty = &uCache[i];
	    continue;
	}
	/* Check for cache-miss condition. A cache miss is
	 *    a) both items are not NULL and don't compare.
	 *    b) either of the items is not NULL.
	 */
	if ((u->service && ou->service && strcmp(u->service, ou->service)) ||
	    (u->service != ou->service))
		continue;
	if ((u->host && ou->host && strcmp(u->host, ou->host)) ||
	    (u->host != ou->host))
		continue;
	if ((u->user && ou->user && strcmp(u->user, ou->user)) ||
	    (u->user != ou->user))
		continue;
	if ((u->password && ou->password && strcmp(u->password, ou->password))||
	    (u->password != ou->password))
		continue;
	if ((u->portstr && ou->portstr && strcmp(u->portstr, ou->portstr)) ||
	    (u->portstr != ou->portstr))
		continue;
	break;	/* Found item in cache */
    }

    if (i == uCount) {
	if (empty == NULL) {
	    uCount++;
	    if (uCache)
		uCache = realloc(uCache, sizeof(*uCache) * uCount);
	    else
		uCache = malloc(sizeof(*uCache));
	    empty = &uCache[i];
	}
	*empty = u;
    } else {
	/* Swap original url and path into the cached structure */
	const char *up = uCache[i]->path;
	uCache[i]->path = u->path;
	u->path = up;
	up = uCache[i]->url;
	uCache[i]->url = u->url;
	u->url = up;
	freeUrlinfo(u);
    }

    /* This URL is now cached. */
    *uret = u = uCache[i];

    /* Perform one-time FTP initialization */
    if (!strcmp(u->service, "ftp")) {
	char *proxy;
	char *proxyport;

	if (mustAsk || (u->user != NULL && u->password == NULL)) {
	    char * prompt;
	    FREE(u->password);
	    prompt = alloca(strlen(u->host) + strlen(u->user) + 40);
	    sprintf(prompt, _("Password for %s@%s: "), u->user, u->host);
	    u->password = strdup(getpass(prompt));
	}

	if (u->proxyh == NULL && (proxy = rpmGetVar(RPMVAR_FTPPROXY)) != NULL) {
	    char *nu = malloc(strlen(u->user) + strlen(u->host) + sizeof("@"));
	    strcpy(nu, u->user);
	    strcat(nu, "@");
	    strcat(nu, u->host);
	    u->proxyu = nu;
	    u->proxyh = strdup(proxy);
	}

	if (u->proxyp < 0 && (proxyport = rpmGetVar(RPMVAR_FTPPORT)) != NULL) {
	    int port;
	    char *end;
	    port = strtol(proxyport, &end, 0);
	    if (*end) {
		fprintf(stderr, _("error: %sport must be a number\n"),
			u->service);
		return;
	    }
	    u->proxyp = port;
	}
    }

    /* Perform one-time HTTP initialization */
    if (!strcmp(u->service, "http")) {
	char *proxy;
	char *proxyport;

	if (u->proxyh == NULL && (proxy = rpmGetVar(RPMVAR_HTTPPROXY)) != NULL) {
	    u->proxyh = strdup(proxy);
	}

	if (u->proxyp < 0 && (proxyport = rpmGetVar(RPMVAR_HTTPPORT)) != NULL) {
	    int port;<commit_msg>Fix proxy FTP coredump.<commit_after>#include "system.h"

#include <netinet/in.h>

#include "build/rpmbuild.h"

#include "url.h"
#include "ftp.h"

static struct urlstring {
    const char *leadin;
    urltype	ret;
} urlstrings[] = {
    { "file://",	URL_IS_PATH },
    { "ftp://",		URL_IS_FTP },
    { "http://",	URL_IS_HTTP },
    { "-",		URL_IS_DASH },
    { NULL,		URL_IS_UNKNOWN }
};

void freeUrlinfo(urlinfo *u)
{
    if (u->ftpControl >= 0)
	close(u->ftpControl);
    FREE(u->url);
    FREE(u->service);
    FREE(u->user);
    FREE(u->password);
    FREE(u->host);
    FREE(u->portstr);
    FREE(u->path);
    FREE(u->proxyu);
    FREE(u->proxyh);

    FREE(u);
}

urlinfo *newUrlinfo(void)
{
    urlinfo *u;
    if ((u = malloc(sizeof(*u))) == NULL)
	return NULL;
    memset(u, 0, sizeof(*u));
    u->proxyp = -1;
    u->port = -1;
    u->ftpControl = -1;
    u->ftpGetFileDoneNeeded = 0;
    return u;
}

static void findUrlinfo(urlinfo **uret, int mustAsk)
{
    urlinfo *u;
    urlinfo **empty;
    static urlinfo **uCache = NULL;
    static int uCount = 0;
    int i;

    if (uret == NULL)
	return;

    u = *uret;

    empty = NULL;
    for (i = 0; i < uCount; i++) {
	urlinfo *ou;
	if ((ou = uCache[i]) == NULL) {
	    if (empty == NULL)
		empty = &uCache[i];
	    continue;
	}
	/* Check for cache-miss condition. A cache miss is
	 *    a) both items are not NULL and don't compare.
	 *    b) either of the items is not NULL.
	 */
	if ((u->service && ou->service && strcmp(u->service, ou->service)) ||
	    (u->service != ou->service))
		continue;
	if ((u->host && ou->host && strcmp(u->host, ou->host)) ||
	    (u->host != ou->host))
		continue;
	if ((u->user && ou->user && strcmp(u->user, ou->user)) ||
	    (u->user != ou->user))
		continue;
	if ((u->password && ou->password && strcmp(u->password, ou->password))||
	    (u->password != ou->password))
		continue;
	if ((u->portstr && ou->portstr && strcmp(u->portstr, ou->portstr)) ||
	    (u->portstr != ou->portstr))
		continue;
	break;	/* Found item in cache */
    }

    if (i == uCount) {
	if (empty == NULL) {
	    uCount++;
	    if (uCache)
		uCache = realloc(uCache, sizeof(*uCache) * uCount);
	    else
		uCache = malloc(sizeof(*uCache));
	    empty = &uCache[i];
	}
	*empty = u;
    } else {
	/* Swap original url and path into the cached structure */
	const char *up = uCache[i]->path;
	uCache[i]->path = u->path;
	u->path = up;
	up = uCache[i]->url;
	uCache[i]->url = u->url;
	u->url = up;
	freeUrlinfo(u);
    }

    /* This URL is now cached. */
    *uret = u = uCache[i];

    /* Perform one-time FTP initialization */
    if (!strcmp(u->service, "ftp")) {
	char *proxy;
	char *proxyport;

	if (mustAsk || (u->user != NULL && u->password == NULL)) {
	    char * prompt;
	    FREE(u->password);
	    prompt = alloca(strlen(u->host) + strlen(u->user) + 40);
	    sprintf(prompt, _("Password for %s@%s: "), u->user, u->host);
	    u->password = strdup(getpass(prompt));
	}

	if (u->proxyh == NULL && (proxy = rpmGetVar(RPMVAR_FTPPROXY)) != NULL) {
	    char *uu = (u->user ? u->user : "anonymous");
	    char *nu = malloc(strlen(u->user) + strlen(uu) + sizeof("@"));
	    strcpy(nu, uu);
	    strcat(nu, "@");
	    strcat(nu, u->host);
	    u->proxyu = nu;
	    u->proxyh = strdup(proxy);
	}

	if (u->proxyp < 0 && (proxyport = rpmGetVar(RPMVAR_FTPPORT)) != NULL) {
	    int port;
	    char *end;
	    port = strtol(proxyport, &end, 0);
	    if (*end) {
		fprintf(stderr, _("error: %sport must be a number\n"),
			u->service);
		return;
	    }
	    u->proxyp = port;
	}
    }

    /* Perform one-time HTTP initialization */
    if (!strcmp(u->service, "http")) {
	char *proxy;
	char *proxyport;

	if (u->proxyh == NULL && (proxy = rpmGetVar(RPMVAR_HTTPPROXY)) != NULL) {
	    u->proxyh = strdup(proxy);
	}

	if (u->proxyp < 0 && (proxyport = rpmGetVar(RPMVAR_HTTPPORT)) != NULL) {
	    int port;
{'fraction_non_alphanumeric': 0.20199922108269505, 'fraction_numerical': 0.0033753083214332078, 'mean_word_length': 2.707410972088547, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 159, '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': '6102569', 'n_tokens_mistral': 3156, 'n_tokens_neox': 2995, 'n_words': 693}