Source-Changes-HG archive

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

[src/trunk]: src/libexec/httpd several clean ups:



details:   https://anonhg.NetBSD.org/src/rev/ccc3d3cc887a
branches:  trunk
changeset: 342543:ccc3d3cc887a
user:      mrg <mrg%NetBSD.org@localhost>
date:      Sun Dec 27 10:21:35 2015 +0000

description:
several clean ups:

- bozostrdup() gains a request parameter, and uses it to determine
  what sort of error handling is required
- bozo_strdup() dies
- size_arrays() reduced slightly, pushing error handling into the caller
- convert to size_t for some array indices
- bozo_set_pref() and bozo_init_prefs() gain httpd parameters
- apply a bunch of manual CSE to vastly reduce the number of times the
  string "request->hr_httpd" appears.
- CGI parse_header() takes a request not httpd now

XXX: lua glue updated to call bozo_init_prefs() with htttpd parameter,
     but i'm only guessing here.

diffstat:

 libexec/httpd/auth-bozo.c        |   18 +-
 libexec/httpd/bozohttpd.8        |    5 +-
 libexec/httpd/bozohttpd.c        |  246 ++++++++++++++++++--------------------
 libexec/httpd/bozohttpd.h        |   22 +-
 libexec/httpd/cgi-bozo.c         |   24 ++-
 libexec/httpd/dir-index-bozo.c   |    8 +-
 libexec/httpd/lua-bozo.c         |   18 +-
 libexec/httpd/lua/glue.c         |    4 +-
 libexec/httpd/main.c             |   51 ++++---
 libexec/httpd/ssl-bozo.c         |   10 +-
 libexec/httpd/tilde-luzah-bozo.c |    6 +-
 11 files changed, 207 insertions(+), 205 deletions(-)

diffs (truncated from 1196 to 300 lines):

diff -r d57cfbf26210 -r ccc3d3cc887a libexec/httpd/auth-bozo.c
--- a/libexec/httpd/auth-bozo.c Sun Dec 27 08:43:51 2015 +0000
+++ b/libexec/httpd/auth-bozo.c Sun Dec 27 10:21:35 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: auth-bozo.c,v 1.17 2015/10/28 09:20:15 shm Exp $       */
+/*     $NetBSD: auth-bozo.c,v 1.18 2015/12/27 10:21:35 mrg Exp $       */
 
 /*     $eterna: auth-bozo.c,v 1.17 2011/11/18 09:21:15 mrg Exp $       */
 
@@ -72,10 +72,10 @@
                if (bozo_check_special_files(request, basename))
                        return 1;
        }
-       request->hr_authrealm = bozostrdup(httpd, dir);
+       request->hr_authrealm = bozostrdup(httpd, request, dir);
 
-       if ((size_t)snprintf(authfile, sizeof(authfile), "%s/%s", dir, AUTH_FILE) >= 
-         sizeof(authfile)) {
+       if ((size_t)snprintf(authfile, sizeof(authfile), "%s/%s", dir,
+                            AUTH_FILE) >= sizeof(authfile)) {
                return bozo_http_error(httpd, 404, request,
                        "authfile path too long");
        }
@@ -136,7 +136,8 @@
 }
 
 int
-bozo_auth_check_headers(bozo_httpreq_t *request, char *val, char *str, ssize_t len)
+bozo_auth_check_headers(bozo_httpreq_t *request, char *val, char *str,
+                       ssize_t len)
 {
        bozohttpd_t *httpd = request->hr_httpd;
 
@@ -159,8 +160,8 @@
                *pass++ = '\0';
                free(request->hr_authuser);
                free(request->hr_authpass);
-               request->hr_authuser = bozostrdup(httpd, authbuf);
-               request->hr_authpass = bozostrdup(httpd, pass);
+               request->hr_authuser = bozostrdup(httpd, request, authbuf);
+               request->hr_authpass = bozostrdup(httpd, request, pass);
                debug((httpd, DEBUG_FAT,
                    "decoded authorization `%s' as `%s':`%s'",
                    str, request->hr_authuser, request->hr_authpass));
@@ -190,7 +191,8 @@
        if (code == 401)
                bozo_printf(httpd,
                        "WWW-Authenticate: Basic realm=\"%s\"\r\n",
-                       request->hr_authrealm ? request->hr_authrealm : "default realm");
+                       request->hr_authrealm ?
+                       request->hr_authrealm : "default realm");
 }
 
 #ifndef NO_CGIBIN_SUPPORT
diff -r d57cfbf26210 -r ccc3d3cc887a libexec/httpd/bozohttpd.8
--- a/libexec/httpd/bozohttpd.8 Sun Dec 27 08:43:51 2015 +0000
+++ b/libexec/httpd/bozohttpd.8 Sun Dec 27 10:21:35 2015 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: bozohttpd.8,v 1.57 2015/12/12 16:57:53 christos Exp $
+.\"    $NetBSD: bozohttpd.8,v 1.58 2015/12/27 10:21:35 mrg Exp $
 .\"
 .\"    $eterna: bozohttpd.8,v 1.101 2011/11/18 01:25:11 mrg Exp $
 .\"
@@ -337,7 +337,8 @@
 option),
 .Nm
 has little internal networking knowledge.
-(Indeed, you can run it on the command line with little change of functionality.)
+(Indeed, you can run it on the command line with little change of
+functionality.)
 A typical
 .Xr inetd.conf 5
 entry would be:
diff -r d57cfbf26210 -r ccc3d3cc887a libexec/httpd/bozohttpd.c
--- a/libexec/httpd/bozohttpd.c Sun Dec 27 08:43:51 2015 +0000
+++ b/libexec/httpd/bozohttpd.c Sun Dec 27 10:21:35 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.c,v 1.72 2015/12/12 18:06:58 christos Exp $  */
+/*     $NetBSD: bozohttpd.c,v 1.73 2015/12/27 10:21:35 mrg Exp $       */
 
 /*     $eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $      */
 
@@ -171,61 +171,59 @@
  * check there's enough space in the prefs and names arrays.
  */
 static int
-size_arrays(bozoprefs_t *bozoprefs, unsigned needed)
+size_arrays(bozoprefs_t *bozoprefs, size_t needed)
 {
        char    **temp;
 
        if (bozoprefs->size == 0) {
                /* only get here first time around */
-               bozoprefs->size = needed;
-               if ((bozoprefs->name = calloc(sizeof(char *), needed)) == NULL) {
-                       (void) fprintf(stderr, "size_arrays: bad alloc\n");
+               bozoprefs->name = calloc(sizeof(char *), needed);
+               if (bozoprefs->name == NULL)
                        return 0;
-               }
-               if ((bozoprefs->value = calloc(sizeof(char *), needed)) == NULL) {
+               bozoprefs->value = calloc(sizeof(char *), needed);
+               if (bozoprefs->value == NULL) {
                        free(bozoprefs->name);
-                       (void) fprintf(stderr, "size_arrays: bad alloc\n");
                        return 0;
                }
-       } else if (bozoprefs->c == bozoprefs->size) {
+               bozoprefs->size = needed;
+       } else if (bozoprefs->count == bozoprefs->size) {
                /* only uses 'needed' when filled array */
-               bozoprefs->size += needed;
                temp = realloc(bozoprefs->name, sizeof(char *) * needed);
-               if (temp == NULL) {
-                       (void) fprintf(stderr, "size_arrays: bad alloc\n");
+               if (temp == NULL)
                        return 0;
-               }
                bozoprefs->name = temp;
                temp = realloc(bozoprefs->value, sizeof(char *) * needed);
-               if (temp == NULL) {
-                       (void) fprintf(stderr, "size_arrays: bad alloc\n");
+               if (temp == NULL)
                        return 0;
-               }
                bozoprefs->value = temp;
+               bozoprefs->size += needed;
        }
        return 1;
 }
 
-static int
+static ssize_t
 findvar(bozoprefs_t *bozoprefs, const char *name)
 {
-       unsigned        i;
+       size_t  i;
 
-       for (i = 0 ; i < bozoprefs->c && strcmp(bozoprefs->name[i], name) != 0; i++)
-               ;
-       return (i == bozoprefs->c) ? -1 : (int)i;
+       for (i = 0; i < bozoprefs->count; i++)
+               if (strcmp(bozoprefs->name[i], name) == 0)
+                       return (ssize_t)i;
+       return -1;
 }
 
 int
-bozo_set_pref(bozoprefs_t *bozoprefs, const char *name, const char *value)
+bozo_set_pref(bozohttpd_t *httpd, bozoprefs_t *bozoprefs,
+             const char *name, const char *value)
 {
-       int     i;
+       ssize_t i;
 
        if ((i = findvar(bozoprefs, name)) < 0) {
                /* add the element to the array */
-               if (size_arrays(bozoprefs, bozoprefs->size + 15)) {
-                       bozoprefs->name[i = bozoprefs->c++] = strdup(name);
-               }
+               if (!size_arrays(bozoprefs, bozoprefs->size + 15))
+                       return 0;
+               i = bozoprefs->count++;
+               bozoprefs->name[i] = bozostrdup(httpd, NULL, name);
        } else {
                /* replace the element in the array */
                if (bozoprefs->value[i]) {
@@ -233,8 +231,7 @@
                        bozoprefs->value[i] = NULL;
                }
        }
-       /* sanity checks for range of values go here */
-       bozoprefs->value[i] = strdup(value);
+       bozoprefs->value[i] = bozostrdup(httpd, NULL, value);
        return 1;
 }
 
@@ -244,10 +241,10 @@
 char *
 bozo_get_pref(bozoprefs_t *bozoprefs, const char *name)
 {
-       int     i;
+       ssize_t i;
 
-       return ((i = findvar(bozoprefs, name)) < 0) ? NULL :
-                       bozoprefs->value[i];
+       i = findvar(bozoprefs, name);
+       return i < 0 ? NULL : bozoprefs->value[i];
 }
 
 char *
@@ -309,9 +306,9 @@
        }
 
        /* allocate private copies */
-       *file = bozostrdup(httpd, *file);
+       *file = bozostrdup(httpd, NULL, *file);
        if (*query)
-               *query = bozostrdup(httpd, *query);
+               *query = bozostrdup(httpd, NULL, *query);
 
        debug((httpd, DEBUG_FAT,
                "url: method: \"%s\" file: \"%s\" query: \"%s\" proto: \"%s\"",
@@ -372,6 +369,7 @@
 addmerge_header(bozo_httpreq_t *request, char *val,
                char *str, ssize_t len)
 {
+       struct  bozohttpd_t *httpd = request->hr_httpd;
        struct  bozoheaders *hdr;
 
        USE_ARG(len);
@@ -386,7 +384,7 @@
                char *nval;
 
                if (asprintf(&nval, "%s, %s", hdr->h_value, str) == -1) {
-                       (void)bozo_http_error(request->hr_httpd, 500, NULL,
+                       (void)bozo_http_error(httpd, 500, NULL,
                             "memory allocation failure");
                        return NULL;
                }
@@ -395,12 +393,12 @@
        } else {
                /* nope, create a new one */
 
-               hdr = bozomalloc(request->hr_httpd, sizeof *hdr);
-               hdr->h_header = bozostrdup(request->hr_httpd, val);
+               hdr = bozomalloc(httpd, sizeof *hdr);
+               hdr->h_header = bozostrdup(httpd, request, val);
                if (str && *str)
-                       hdr->h_value = bozostrdup(request->hr_httpd, str);
+                       hdr->h_value = bozostrdup(httpd, request, str);
                else
-                       hdr->h_value = bozostrdup(request->hr_httpd, " ");
+                       hdr->h_value = bozostrdup(httpd, request, " ");
 
                SIMPLEQ_INSERT_TAIL(&request->hr_headers, hdr, h_next);
                request->hr_nheaders++;
@@ -416,13 +414,14 @@
 static int
 process_proto(bozo_httpreq_t *request, const char *proto)
 {
+       struct  bozohttpd_t *httpd = request->hr_httpd;
        char    majorstr[16], *minorstr;
        int     majorint, minorint;
 
        if (proto == NULL) {
 got_proto_09:
-               request->hr_proto = request->hr_httpd->consts.http_09;
-               debug((request->hr_httpd, DEBUG_FAT, "request %s is http/0.9",
+               request->hr_proto = httpd->consts.http_09;
+               debug((httpd, DEBUG_FAT, "request %s is http/0.9",
                        request->hr_file));
                return 0;
        }
@@ -446,25 +445,25 @@
                goto got_proto_09;
        case 1:
                if (minorint == 0)
-                       request->hr_proto = request->hr_httpd->consts.http_10;
+                       request->hr_proto = httpd->consts.http_10;
                else if (minorint == 1)
-                       request->hr_proto = request->hr_httpd->consts.http_11;
+                       request->hr_proto = httpd->consts.http_11;
                else
                        break;
 
-               debug((request->hr_httpd, DEBUG_FAT, "request %s is %s",
+               debug((httpd, DEBUG_FAT, "request %s is %s",
                    request->hr_file, request->hr_proto));
                SIMPLEQ_INIT(&request->hr_headers);
                request->hr_nheaders = 0;
                return 0;
        }
 bad:
-       return bozo_http_error(request->hr_httpd, 404, NULL, "unknown prototype");
+       return bozo_http_error(httpd, 404, NULL, "unknown prototype");
 }
 
 /*
  * process each type of HTTP method, setting this HTTP requests
- # method type.
+ * method type.
  */
 static struct method_map {
        const char *name;
@@ -486,9 +485,10 @@
 static int
 process_method(bozo_httpreq_t *request, const char *method)
 {
+       struct  bozohttpd_t *httpd = request->hr_httpd;
        struct  method_map *mmp;
 
-       if (request->hr_proto == request->hr_httpd->consts.http_11)
+       if (request->hr_proto == httpd->consts.http_11)
                request->hr_allow = "GET, HEAD, POST";
 
        for (mmp = method_map; mmp->name; mmp++)
@@ -498,7 +498,7 @@
                        return 0;
                }



Home | Main Index | Thread Index | Old Index