upstream: refactor assembly of upstream out of upstream_add

Michael
This commit is contained in:
Michael Adam 2009-12-06 12:09:54 +01:00
parent 34dbeb3626
commit 56ba3d45bd

View File

@ -301,19 +301,19 @@ static int extract_ssl_url (const char *url, struct request_s *request)
}
#ifdef UPSTREAM_SUPPORT
/*
* Add an entry to the upstream list
/**
* Construct an upstream struct from input data.
*/
void upstream_add (const char *host, int port, const char *domain)
static struct upstream *upstream_build (const char *host, int port, const char *domain)
{
char *ptr;
struct upstream *up =
(struct upstream *) safemalloc (sizeof (struct upstream));
struct upstream *up;
up = (struct upstream *) safemalloc (sizeof (struct upstream));
if (!up) {
log_message (LOG_ERR,
"Unable to allocate memory in upstream_add()");
return;
"Unable to allocate memory in upstream_build()");
return NULL;
}
up->host = up->domain = NULL;
@ -323,7 +323,7 @@ void upstream_add (const char *host, int port, const char *domain)
if (!host || host[0] == '\0' || port < 1) {
log_message (LOG_WARNING,
"Nonsense upstream rule: invalid host or port");
goto upstream_cleanup;
goto fail;
}
up->host = safestrdup (host);
@ -335,7 +335,7 @@ void upstream_add (const char *host, int port, const char *domain)
if (!domain || domain[0] == '\0') {
log_message (LOG_WARNING,
"Nonsense no-upstream rule: empty domain");
goto upstream_cleanup;
goto fail;
}
ptr = strchr (domain, '/');
@ -366,7 +366,7 @@ void upstream_add (const char *host, int port, const char *domain)
|| domain == '\0') {
log_message (LOG_WARNING,
"Nonsense upstream rule: invalid parameters");
goto upstream_cleanup;
goto fail;
}
up->host = safestrdup (host);
@ -377,6 +377,28 @@ void upstream_add (const char *host, int port, const char *domain)
host, port, domain);
}
return up;
fail:
safefree (up->host);
safefree (up->domain);
safefree (up);
return NULL;
}
/*
* Add an entry to the upstream list
*/
void upstream_add (const char *host, int port, const char *domain)
{
struct upstream *up;
up = upstream_build (host, port, domain);
if (up == NULL) {
return;
}
if (!up->domain && !up->ip) { /* always add default to end */
struct upstream *tmp = config.upstream_list;