Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/httpd Introduce bozo_strdup and bozo_asprintf to add...



details:   https://anonhg.NetBSD.org/src/rev/7f4602875b1a
branches:  trunk
changeset: 342203:7f4602875b1a
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Dec 12 18:06:58 2015 +0000

description:
Introduce bozo_strdup and bozo_asprintf to add error checking and reduce
code duplication.

Note that bozo_strdup is different that bozostrdup; the _ routines exit
loging error to syslog or stderr, whereas the non _ routines send error
responses to the http client.

diffstat:

 libexec/httpd/bozohttpd.c |  80 +++++++++++++++++++++++++++++-----------------
 libexec/httpd/bozohttpd.h |   6 ++-
 libexec/httpd/ssl-bozo.c  |  10 ++--
 3 files changed, 61 insertions(+), 35 deletions(-)

diffs (257 lines):

diff -r 44a015979182 -r 7f4602875b1a libexec/httpd/bozohttpd.c
--- a/libexec/httpd/bozohttpd.c Sat Dec 12 17:48:18 2015 +0000
+++ b/libexec/httpd/bozohttpd.c Sat Dec 12 18:06:58 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.c,v 1.71 2015/10/31 00:55:17 christos Exp $  */
+/*     $NetBSD: bozohttpd.c,v 1.72 2015/12/12 18:06:58 christos Exp $  */
 
 /*     $eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $      */
 
@@ -936,16 +936,14 @@
                           * eg. https:// */
 
        if (url == NULL) {
-               if (asprintf(&urlbuf, "/%s/", request->hr_file) == -1)
-                       bozo_err(httpd, 1, "asprintf");
+               bozo_asprintf(httpd, &urlbuf, "/%s/", request->hr_file);
                url = urlbuf;
        } else
                urlbuf = NULL;
 
 #ifndef NO_USER_SUPPORT
        if (request->hr_user && !absolute) {
-               if (asprintf(&userbuf, "/~%s%s", request->hr_user, url) == -1)
-                       bozo_err(httpd, 1, "asprintf");
+               bozo_asprintf(httpd, &userbuf, "/~%s%s", request->hr_user, url);
                url = userbuf;
        } else
                userbuf = NULL;
@@ -1143,9 +1141,9 @@
                                        debug((httpd, DEBUG_OBESE, "found it punch it"));
                                        request->hr_virthostname =
                                            bozostrdup(httpd, d->d_name);
-                                       if (asprintf(&s, "%s/%s", httpd->virtbase,
-                                           request->hr_virthostname) == -1)
-                                               bozo_err(httpd, 1, "asprintf");
+                                       bozo_asprintf(httpd, &s, "%s/%s",
+                                           httpd->virtbase,
+                                           request->hr_virthostname);
                                        break;
                                }
                        }
@@ -1397,8 +1395,7 @@
                }
                if (strchr(file + 2, '/') == NULL) {
                        char *userredirecturl;
-                       if (asprintf(&userredirecturl, "%s/", file) == -1)
-                               bozo_err(httpd, 1, "asprintf");
+                       bozo_asprintf(httpd, &userredirecturl, "%s/", file);
                        handle_redirect(request, userredirecturl, 0);
                        free(userredirecturl);
                        return 0;
@@ -1559,8 +1556,7 @@
        fd = -1;
        encoding = NULL;
        if (can_gzip(request)) {
-               if (asprintf(&file, "%s.gz", request->hr_file) == -1)
-                       bozo_err(httpd, 1, "asprintf");
+               bozo_asprintf(httpd, &file, "%s.gz", request->hr_file);
                fd = open(file, O_RDONLY);
                if (fd >= 0)
                        encoding = "gzip";
@@ -1794,6 +1790,32 @@
        exit(code);
 }
 
+char *
+bozo_strdup(bozohttpd_t *httpd, const char *str)
+{
+       char    *p;
+
+       p = strdup(str);
+       if (p == NULL)
+               bozo_err(httpd, EXIT_FAILURE, "strdup");
+
+       return p;
+}
+
+void
+bozo_asprintf(bozohttpd_t *httpd, char **str, const char *fmt, ...)
+{
+       va_list ap;
+       int e;
+
+       va_start(ap, fmt);
+       e = vasprintf(str, fmt, ap);
+       va_end(ap);
+
+       if (e < 0)
+               bozo_err(httpd, EXIT_FAILURE, "asprintf");
+}
+
 /*
  * this escapes HTML tags.  returns allocated escaped
  * string if needed, or NULL on allocation failure or
@@ -1945,8 +1967,7 @@
                        if (user_escaped == NULL)
                                user_escaped = request->hr_user;
                        /* expand username to ~user/ */
-                       if (asprintf(&user, "~%s/", user_escaped) == -1)
-                               bozo_err(httpd, 1, "asprintf");
+                       bozo_asprintf(httpd, &user, "~%s/", user_escaped);
                        if (user_escaped != request->hr_user)
                                free(user_escaped);
                }
@@ -2125,7 +2146,7 @@
        if (p == NULL) {
                (void)bozo_http_error(httpd, 500, NULL,
                                "memory allocation failure");
-               exit(1);
+               exit(EXIT_FAILURE);
        }
        return (p);
 }
@@ -2138,8 +2159,8 @@
        p = strdup(str);
        if (p == NULL) {
                (void)bozo_http_error(httpd, 500, NULL,
-                                       "memory allocation failure");
-               exit(1);
+                   "memory allocation failure");
+               exit(EXIT_FAILURE);
        }
        return (p);
 }
@@ -2218,11 +2239,11 @@
                        bozo_err(httpd, 1, "gethostname");
                httpd->virthostname[MAXHOSTNAMELEN] = '\0';
        } else {
-               httpd->virthostname = strdup(vhost);
+               httpd->virthostname = bozo_strdup(httpd, vhost);
        }
-       httpd->slashdir = strdup(root);
+       httpd->slashdir = bozo_strdup(httpd, root);
        if ((portnum = bozo_get_pref(prefs, "port number")) != NULL) {
-               httpd->bindport = strdup(portnum);
+               httpd->bindport = bozo_strdup(httpd, portnum);
        }
 
        /* go over preferences now */
@@ -2235,7 +2256,7 @@
                httpd->logstderr = 1;
        }
        if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) {
-               httpd->bindaddress = strdup(cp);
+               httpd->bindaddress = bozo_strdup(httpd, cp);
        }
        if ((cp = bozo_get_pref(prefs, "background")) != NULL) {
                httpd->background = atoi(cp);
@@ -2245,14 +2266,14 @@
                httpd->foreground = 1;
        }
        if ((cp = bozo_get_pref(prefs, "pid file")) != NULL) {
-               httpd->pidfile = strdup(cp);
+               httpd->pidfile = bozo_strdup(httpd, cp);
        }
        if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL &&
            strcmp(cp, "true") == 0) {
                httpd->unknown_slash = 1;
        }
        if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) {
-               httpd->virtbase = strdup(cp);
+               httpd->virtbase = bozo_strdup(httpd, cp);
        }
        if ((cp = bozo_get_pref(prefs, "enable users")) != NULL &&
            strcmp(cp, "true") == 0) {
@@ -2275,11 +2296,12 @@
                httpd->dir_indexing = 1;
        }
        if ((cp = bozo_get_pref(prefs, "public_html")) != NULL) {
-               httpd->public_html = strdup(cp);
+               httpd->public_html = bozo_strdup(httpd, cp);
        }
        httpd->server_software =
-                       strdup(bozo_get_pref(prefs, "server software"));
-       httpd->index_html = strdup(bozo_get_pref(prefs, "index.html"));
+           bozo_strdup(httpd, bozo_get_pref(prefs, "server software"));
+       httpd->index_html =
+           bozo_strdup(httpd, bozo_get_pref(prefs, "index.html"));
 
        /*
         * initialise ssl and daemon mode if necessary.
@@ -2290,9 +2312,9 @@
        if ((username = bozo_get_pref(prefs, "username")) == NULL) {
                if ((pw = getpwuid(uid = 0)) == NULL)
                        bozo_err(httpd, 1, "getpwuid(0): %s", strerror(errno));
-               httpd->username = strdup(pw->pw_name);
+               httpd->username = bozo_strdup(httpd, pw->pw_name);
        } else {
-               httpd->username = strdup(username);
+               httpd->username = bozo_strdup(httpd, username);
                if ((pw = getpwnam(httpd->username)) == NULL)
                        bozo_err(httpd, 1, "getpwnam(%s): %s", httpd->username,
                                        strerror(errno));
@@ -2307,7 +2329,7 @@
         * handle chroot.
         */
        if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) {
-               httpd->rootdir = strdup(chrootdir);
+               httpd->rootdir = bozo_strdup(httpd, chrootdir);
                if (chdir(httpd->rootdir) == -1)
                        bozo_err(httpd, 1, "chdir(%s): %s", httpd->rootdir,
                                strerror(errno));
diff -r 44a015979182 -r 7f4602875b1a libexec/httpd/bozohttpd.h
--- a/libexec/httpd/bozohttpd.h Sat Dec 12 17:48:18 2015 +0000
+++ b/libexec/httpd/bozohttpd.h Sat Dec 12 18:06:58 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.h,v 1.39 2015/12/12 16:57:53 christos Exp $  */
+/*     $NetBSD: bozohttpd.h,v 1.40 2015/12/12 18:06:58 christos Exp $  */
 
 /*     $eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $       */
 
@@ -223,6 +223,10 @@
 void   bozo_err(bozohttpd_t *, int, const char *, ...)
                BOZO_PRINTFLIKE(3, 4)
                BOZO_DEAD;
+void   bozo_asprintf(bozohttpd_t *, char **, const char *, ...)
+               BOZO_PRINTFLIKE(3, 4);
+char   *bozo_strdup(bozohttpd_t *, const char *);
+
 int    bozo_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *);
 
 int    bozo_check_special_files(bozo_httpreq_t *, const char *);
diff -r 44a015979182 -r 7f4602875b1a libexec/httpd/ssl-bozo.c
--- a/libexec/httpd/ssl-bozo.c  Sat Dec 12 17:48:18 2015 +0000
+++ b/libexec/httpd/ssl-bozo.c  Sat Dec 12 18:06:58 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ssl-bozo.c,v 1.19 2015/12/12 16:57:53 christos Exp $   */
+/*     $NetBSD: ssl-bozo.c,v 1.20 2015/12/12 18:06:58 christos Exp $   */
 
 /*     $eterna: ssl-bozo.c,v 1.15 2011/11/18 09:21:15 mrg Exp $        */
 
@@ -302,13 +302,13 @@
 {
        sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
 
-       sslinfo->certificate_file = bozostrdup(httpd, cert);
-       sslinfo->privatekey_file = bozostrdup(httpd, priv);
+       sslinfo->certificate_file = bozo_strdup(httpd, cert);
+       sslinfo->privatekey_file = bozo_strdup(httpd, priv);
        debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
            sslinfo->certificate_file,
            sslinfo->privatekey_file));
        if (!httpd->bindport)
-               httpd->bindport = bozostrdup(httpd, "https");
+               httpd->bindport = bozo_strdup(httpd, "https");
 }
 
 void
@@ -316,7 +316,7 @@
 {
        sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
 
-       sslinfo->ciphers = bozostrdup(httpd, ciphers);
+       sslinfo->ciphers = bozo_strdup(httpd, ciphers);
        debug((httpd, DEBUG_NORMAL, "using ciphers: %s", sslinfo->ciphers));
 }
 



Home | Main Index | Thread Index | Old Index