From 56ba3d45bd415d1b949032c312bb188126feb205 Mon Sep 17 00:00:00 2001
From: Michael Adam <obnox@samba.org>
Date: Sun, 6 Dec 2009 12:09:54 +0100
Subject: [PATCH] upstream: refactor assembly of upstream out of upstream_add

Michael
---
 src/reqs.c | 42 ++++++++++++++++++++++++++++++++----------
 1 file changed, 32 insertions(+), 10 deletions(-)

diff --git a/src/reqs.c b/src/reqs.c
index 2c165d9..a956ce8 100644
--- a/src/reqs.c
+++ b/src/reqs.c
@@ -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;