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.
This commit is contained in:
parent
a5e9d486c8
commit
f47ab3a37e
@ -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 $ */
|
/* $eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $ */
|
||||||
|
|
||||||
@ -936,16 +936,14 @@ handle_redirect(bozo_httpreq_t *request,
|
|||||||
* eg. https:// */
|
* eg. https:// */
|
||||||
|
|
||||||
if (url == NULL) {
|
if (url == NULL) {
|
||||||
if (asprintf(&urlbuf, "/%s/", request->hr_file) == -1)
|
bozo_asprintf(httpd, &urlbuf, "/%s/", request->hr_file);
|
||||||
bozo_err(httpd, 1, "asprintf");
|
|
||||||
url = urlbuf;
|
url = urlbuf;
|
||||||
} else
|
} else
|
||||||
urlbuf = NULL;
|
urlbuf = NULL;
|
||||||
|
|
||||||
#ifndef NO_USER_SUPPORT
|
#ifndef NO_USER_SUPPORT
|
||||||
if (request->hr_user && !absolute) {
|
if (request->hr_user && !absolute) {
|
||||||
if (asprintf(&userbuf, "/~%s%s", request->hr_user, url) == -1)
|
bozo_asprintf(httpd, &userbuf, "/~%s%s", request->hr_user, url);
|
||||||
bozo_err(httpd, 1, "asprintf");
|
|
||||||
url = userbuf;
|
url = userbuf;
|
||||||
} else
|
} else
|
||||||
userbuf = NULL;
|
userbuf = NULL;
|
||||||
@ -1143,9 +1141,9 @@ check_virtual(bozo_httpreq_t *request)
|
|||||||
debug((httpd, DEBUG_OBESE, "found it punch it"));
|
debug((httpd, DEBUG_OBESE, "found it punch it"));
|
||||||
request->hr_virthostname =
|
request->hr_virthostname =
|
||||||
bozostrdup(httpd, d->d_name);
|
bozostrdup(httpd, d->d_name);
|
||||||
if (asprintf(&s, "%s/%s", httpd->virtbase,
|
bozo_asprintf(httpd, &s, "%s/%s",
|
||||||
request->hr_virthostname) == -1)
|
httpd->virtbase,
|
||||||
bozo_err(httpd, 1, "asprintf");
|
request->hr_virthostname);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1397,8 +1395,7 @@ transform_request(bozo_httpreq_t *request, int *isindex)
|
|||||||
}
|
}
|
||||||
if (strchr(file + 2, '/') == NULL) {
|
if (strchr(file + 2, '/') == NULL) {
|
||||||
char *userredirecturl;
|
char *userredirecturl;
|
||||||
if (asprintf(&userredirecturl, "%s/", file) == -1)
|
bozo_asprintf(httpd, &userredirecturl, "%s/", file);
|
||||||
bozo_err(httpd, 1, "asprintf");
|
|
||||||
handle_redirect(request, userredirecturl, 0);
|
handle_redirect(request, userredirecturl, 0);
|
||||||
free(userredirecturl);
|
free(userredirecturl);
|
||||||
return 0;
|
return 0;
|
||||||
@ -1559,8 +1556,7 @@ bozo_process_request(bozo_httpreq_t *request)
|
|||||||
fd = -1;
|
fd = -1;
|
||||||
encoding = NULL;
|
encoding = NULL;
|
||||||
if (can_gzip(request)) {
|
if (can_gzip(request)) {
|
||||||
if (asprintf(&file, "%s.gz", request->hr_file) == -1)
|
bozo_asprintf(httpd, &file, "%s.gz", request->hr_file);
|
||||||
bozo_err(httpd, 1, "asprintf");
|
|
||||||
fd = open(file, O_RDONLY);
|
fd = open(file, O_RDONLY);
|
||||||
if (fd >= 0)
|
if (fd >= 0)
|
||||||
encoding = "gzip";
|
encoding = "gzip";
|
||||||
@ -1794,6 +1790,32 @@ bozo_err(bozohttpd_t *httpd, int code, const char *fmt, ...)
|
|||||||
exit(code);
|
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
|
* this escapes HTML tags. returns allocated escaped
|
||||||
* string if needed, or NULL on allocation failure or
|
* string if needed, or NULL on allocation failure or
|
||||||
@ -1945,8 +1967,7 @@ bozo_http_error(bozohttpd_t *httpd, int code, bozo_httpreq_t *request,
|
|||||||
if (user_escaped == NULL)
|
if (user_escaped == NULL)
|
||||||
user_escaped = request->hr_user;
|
user_escaped = request->hr_user;
|
||||||
/* expand username to ~user/ */
|
/* expand username to ~user/ */
|
||||||
if (asprintf(&user, "~%s/", user_escaped) == -1)
|
bozo_asprintf(httpd, &user, "~%s/", user_escaped);
|
||||||
bozo_err(httpd, 1, "asprintf");
|
|
||||||
if (user_escaped != request->hr_user)
|
if (user_escaped != request->hr_user)
|
||||||
free(user_escaped);
|
free(user_escaped);
|
||||||
}
|
}
|
||||||
@ -2125,7 +2146,7 @@ bozomalloc(bozohttpd_t *httpd, size_t size)
|
|||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
(void)bozo_http_error(httpd, 500, NULL,
|
(void)bozo_http_error(httpd, 500, NULL,
|
||||||
"memory allocation failure");
|
"memory allocation failure");
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
return (p);
|
return (p);
|
||||||
}
|
}
|
||||||
@ -2138,8 +2159,8 @@ bozostrdup(bozohttpd_t *httpd, const char *str)
|
|||||||
p = strdup(str);
|
p = strdup(str);
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
(void)bozo_http_error(httpd, 500, NULL,
|
(void)bozo_http_error(httpd, 500, NULL,
|
||||||
"memory allocation failure");
|
"memory allocation failure");
|
||||||
exit(1);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
return (p);
|
return (p);
|
||||||
}
|
}
|
||||||
@ -2218,11 +2239,11 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
|
|||||||
bozo_err(httpd, 1, "gethostname");
|
bozo_err(httpd, 1, "gethostname");
|
||||||
httpd->virthostname[MAXHOSTNAMELEN] = '\0';
|
httpd->virthostname[MAXHOSTNAMELEN] = '\0';
|
||||||
} else {
|
} 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) {
|
if ((portnum = bozo_get_pref(prefs, "port number")) != NULL) {
|
||||||
httpd->bindport = strdup(portnum);
|
httpd->bindport = bozo_strdup(httpd, portnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* go over preferences now */
|
/* go over preferences now */
|
||||||
@ -2235,7 +2256,7 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
|
|||||||
httpd->logstderr = 1;
|
httpd->logstderr = 1;
|
||||||
}
|
}
|
||||||
if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) {
|
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) {
|
if ((cp = bozo_get_pref(prefs, "background")) != NULL) {
|
||||||
httpd->background = atoi(cp);
|
httpd->background = atoi(cp);
|
||||||
@ -2245,14 +2266,14 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
|
|||||||
httpd->foreground = 1;
|
httpd->foreground = 1;
|
||||||
}
|
}
|
||||||
if ((cp = bozo_get_pref(prefs, "pid file")) != NULL) {
|
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 &&
|
if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL &&
|
||||||
strcmp(cp, "true") == 0) {
|
strcmp(cp, "true") == 0) {
|
||||||
httpd->unknown_slash = 1;
|
httpd->unknown_slash = 1;
|
||||||
}
|
}
|
||||||
if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) {
|
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 &&
|
if ((cp = bozo_get_pref(prefs, "enable users")) != NULL &&
|
||||||
strcmp(cp, "true") == 0) {
|
strcmp(cp, "true") == 0) {
|
||||||
@ -2275,11 +2296,12 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
|
|||||||
httpd->dir_indexing = 1;
|
httpd->dir_indexing = 1;
|
||||||
}
|
}
|
||||||
if ((cp = bozo_get_pref(prefs, "public_html")) != NULL) {
|
if ((cp = bozo_get_pref(prefs, "public_html")) != NULL) {
|
||||||
httpd->public_html = strdup(cp);
|
httpd->public_html = bozo_strdup(httpd, cp);
|
||||||
}
|
}
|
||||||
httpd->server_software =
|
httpd->server_software =
|
||||||
strdup(bozo_get_pref(prefs, "server software"));
|
bozo_strdup(httpd, bozo_get_pref(prefs, "server software"));
|
||||||
httpd->index_html = strdup(bozo_get_pref(prefs, "index.html"));
|
httpd->index_html =
|
||||||
|
bozo_strdup(httpd, bozo_get_pref(prefs, "index.html"));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* initialise ssl and daemon mode if necessary.
|
* initialise ssl and daemon mode if necessary.
|
||||||
@ -2290,9 +2312,9 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
|
|||||||
if ((username = bozo_get_pref(prefs, "username")) == NULL) {
|
if ((username = bozo_get_pref(prefs, "username")) == NULL) {
|
||||||
if ((pw = getpwuid(uid = 0)) == NULL)
|
if ((pw = getpwuid(uid = 0)) == NULL)
|
||||||
bozo_err(httpd, 1, "getpwuid(0): %s", strerror(errno));
|
bozo_err(httpd, 1, "getpwuid(0): %s", strerror(errno));
|
||||||
httpd->username = strdup(pw->pw_name);
|
httpd->username = bozo_strdup(httpd, pw->pw_name);
|
||||||
} else {
|
} else {
|
||||||
httpd->username = strdup(username);
|
httpd->username = bozo_strdup(httpd, username);
|
||||||
if ((pw = getpwnam(httpd->username)) == NULL)
|
if ((pw = getpwnam(httpd->username)) == NULL)
|
||||||
bozo_err(httpd, 1, "getpwnam(%s): %s", httpd->username,
|
bozo_err(httpd, 1, "getpwnam(%s): %s", httpd->username,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
@ -2307,7 +2329,7 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs_t *prefs, const char *vhost,
|
|||||||
* handle chroot.
|
* handle chroot.
|
||||||
*/
|
*/
|
||||||
if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) {
|
if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) {
|
||||||
httpd->rootdir = strdup(chrootdir);
|
httpd->rootdir = bozo_strdup(httpd, chrootdir);
|
||||||
if (chdir(httpd->rootdir) == -1)
|
if (chdir(httpd->rootdir) == -1)
|
||||||
bozo_err(httpd, 1, "chdir(%s): %s", httpd->rootdir,
|
bozo_err(httpd, 1, "chdir(%s): %s", httpd->rootdir,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
@ -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 $ */
|
/* $eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $ */
|
||||||
|
|
||||||
@ -223,6 +223,10 @@ void bozo_warn(bozohttpd_t *, const char *, ...)
|
|||||||
void bozo_err(bozohttpd_t *, int, const char *, ...)
|
void bozo_err(bozohttpd_t *, int, const char *, ...)
|
||||||
BOZO_PRINTFLIKE(3, 4)
|
BOZO_PRINTFLIKE(3, 4)
|
||||||
BOZO_DEAD;
|
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_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *);
|
||||||
|
|
||||||
int bozo_check_special_files(bozo_httpreq_t *, const char *);
|
int bozo_check_special_files(bozo_httpreq_t *, const char *);
|
||||||
|
@ -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 $ */
|
/* $eterna: ssl-bozo.c,v 1.15 2011/11/18 09:21:15 mrg Exp $ */
|
||||||
|
|
||||||
@ -302,13 +302,13 @@ bozo_ssl_set_opts(bozohttpd_t *httpd, const char *cert, const char *priv)
|
|||||||
{
|
{
|
||||||
sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
|
sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
|
||||||
|
|
||||||
sslinfo->certificate_file = bozostrdup(httpd, cert);
|
sslinfo->certificate_file = bozo_strdup(httpd, cert);
|
||||||
sslinfo->privatekey_file = bozostrdup(httpd, priv);
|
sslinfo->privatekey_file = bozo_strdup(httpd, priv);
|
||||||
debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
|
debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
|
||||||
sslinfo->certificate_file,
|
sslinfo->certificate_file,
|
||||||
sslinfo->privatekey_file));
|
sslinfo->privatekey_file));
|
||||||
if (!httpd->bindport)
|
if (!httpd->bindport)
|
||||||
httpd->bindport = bozostrdup(httpd, "https");
|
httpd->bindport = bozo_strdup(httpd, "https");
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -316,7 +316,7 @@ bozo_ssl_set_ciphers(bozohttpd_t *httpd, const char *ciphers)
|
|||||||
{
|
{
|
||||||
sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
|
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));
|
debug((httpd, DEBUG_NORMAL, "using ciphers: %s", sslinfo->ciphers));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user