mirror of https://github.com/omar-polo/gmid.git
allow add_mime to fail
add_mime nows allocate dinamically copies of the passed strings, so that we can actually free what we parse from the config file. This matters a lot especially with lengthy `types' block: strings that reach the internal mapping are never free'd, so every manual addition is leaked.
This commit is contained in:
parent
aa6b8cf8ac
commit
d8d170aa5e
4
gmid.c
4
gmid.c
|
@ -251,7 +251,8 @@ static int
|
||||||
listener_main(struct imsgbuf *ibuf)
|
listener_main(struct imsgbuf *ibuf)
|
||||||
{
|
{
|
||||||
drop_priv();
|
drop_priv();
|
||||||
load_default_mime(&conf.mime);
|
if (load_default_mime(&conf.mime) == -1)
|
||||||
|
fatal("load_default_mime: %s", strerror(errno));
|
||||||
load_vhosts();
|
load_vhosts();
|
||||||
loop(ctx, sock4, sock6, ibuf);
|
loop(ctx, sock4, sock6, ibuf);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -286,6 +287,7 @@ free_config(void)
|
||||||
|
|
||||||
v = conf.verbose;
|
v = conf.verbose;
|
||||||
|
|
||||||
|
free_mime(&conf.mime);
|
||||||
free(conf.chroot);
|
free(conf.chroot);
|
||||||
free(conf.user);
|
free(conf.user);
|
||||||
memset(&conf, 0, sizeof(conf));
|
memset(&conf, 0, sizeof(conf));
|
||||||
|
|
9
gmid.h
9
gmid.h
|
@ -176,8 +176,8 @@ struct vhost {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct etm { /* extension to mime */
|
struct etm { /* extension to mime */
|
||||||
const char *mime;
|
char *mime;
|
||||||
const char *ext;
|
char *ext;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct mime {
|
struct mime {
|
||||||
|
@ -353,9 +353,10 @@ int logger_main(int, struct imsgbuf*);
|
||||||
|
|
||||||
/* mime.c */
|
/* mime.c */
|
||||||
void init_mime(struct mime*);
|
void init_mime(struct mime*);
|
||||||
void add_mime(struct mime*, const char*, const char*);
|
int add_mime(struct mime*, const char*, const char*);
|
||||||
void load_default_mime(struct mime*);
|
int load_default_mime(struct mime*);
|
||||||
const char *mime(struct vhost*, const char*);
|
const char *mime(struct vhost*, const char*);
|
||||||
|
void free_mime(struct mime *);
|
||||||
|
|
||||||
/* server.c */
|
/* server.c */
|
||||||
extern int shutting_down;
|
extern int shutting_down;
|
||||||
|
|
59
mime.c
59
mime.c
|
@ -32,30 +32,44 @@ init_mime(struct mime *mime)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* register mime for the given extension */
|
/* register mime for the given extension */
|
||||||
void
|
int
|
||||||
add_mime(struct mime *mime, const char *mt, const char *ext)
|
add_mime(struct mime *mime, const char *mt, const char *ext)
|
||||||
{
|
{
|
||||||
size_t oldcap;
|
char *mimetype, *extension;
|
||||||
|
struct etm *t;
|
||||||
|
size_t newcap;
|
||||||
|
|
||||||
if (mime->len == mime->cap) {
|
if (mime->len == mime->cap) {
|
||||||
oldcap = mime->cap;
|
newcap = mime->cap * 1.5;
|
||||||
mime->cap *= 1.5;
|
t = recallocarray(mime->t, mime->cap, newcap,
|
||||||
mime->t = recallocarray(mime->t, oldcap, mime->cap,
|
|
||||||
sizeof(struct etm));
|
sizeof(struct etm));
|
||||||
if (mime->t == NULL)
|
if (t == NULL)
|
||||||
err(1, "recallocarray");
|
return -1;
|
||||||
|
mime->t = t;
|
||||||
|
mime->cap = newcap;
|
||||||
}
|
}
|
||||||
|
|
||||||
mime->t[mime->len].mime = mt;
|
if ((mimetype = strdup(mt)) == NULL)
|
||||||
mime->t[mime->len].ext = ext;
|
return -1;
|
||||||
|
if ((extension = strdup(ext)) == NULL) {
|
||||||
|
free(mimetype);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
mime->t[mime->len].mime = mimetype;
|
||||||
|
mime->t[mime->len].ext = extension;
|
||||||
mime->len++;
|
mime->len++;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* load a default set of common mime-extension associations */
|
/* load a default set of common mime-extension associations */
|
||||||
void
|
int
|
||||||
load_default_mime(struct mime *mime)
|
load_default_mime(struct mime *mime)
|
||||||
{
|
{
|
||||||
const struct etm *i, m[] = {
|
const struct mapping {
|
||||||
|
const char *mime;
|
||||||
|
const char *ext;
|
||||||
|
} m[] = {
|
||||||
{"application/pdf", "pdf"},
|
{"application/pdf", "pdf"},
|
||||||
{"image/gif", "gif"},
|
{"image/gif", "gif"},
|
||||||
{"image/jpeg", "jpg"},
|
{"image/jpeg", "jpg"},
|
||||||
|
@ -71,10 +85,14 @@ load_default_mime(struct mime *mime)
|
||||||
{"text/x-patch", "patch"},
|
{"text/x-patch", "patch"},
|
||||||
{"text/xml", "xml"},
|
{"text/xml", "xml"},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
}, *i;
|
||||||
|
|
||||||
for (i = m; i->mime != NULL; ++i)
|
for (i = m; i->mime != NULL; ++i) {
|
||||||
add_mime(mime, i->mime, i->ext);
|
if (add_mime(mime, i->mime, i->ext) == -1)
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
@ -110,3 +128,16 @@ mime(struct vhost *host, const char *path)
|
||||||
|
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
free_mime(struct mime *m)
|
||||||
|
{
|
||||||
|
struct etm *t;
|
||||||
|
|
||||||
|
for (t = m->t; t->mime != NULL; ++t) {
|
||||||
|
free(t->mime);
|
||||||
|
free(t->ext);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(m->t);
|
||||||
|
}
|
||||||
|
|
11
parse.y
11
parse.y
|
@ -219,13 +219,15 @@ option : CHROOT string { conf.chroot = $2; }
|
||||||
yywarn("`mime MIME EXT' is deprecated and will be "
|
yywarn("`mime MIME EXT' is deprecated and will be "
|
||||||
"removed in a future version, please use the new "
|
"removed in a future version, please use the new "
|
||||||
"`types' block.");
|
"`types' block.");
|
||||||
add_mime(&conf.mime, $2, $3);
|
if (add_mime(&conf.mime, $2, $3) == -1)
|
||||||
|
err(1, "add_mime");
|
||||||
}
|
}
|
||||||
| MAP string TOEXT string {
|
| MAP string TOEXT string {
|
||||||
yywarn("`map mime to-ext' is deprecated and will be "
|
yywarn("`map mime to-ext' is deprecated and will be "
|
||||||
"removed in a future version, please use the new "
|
"removed in a future version, please use the new "
|
||||||
"`types' block.");
|
"`types' block.");
|
||||||
add_mime(&conf.mime, $2, $4);
|
if (add_mime(&conf.mime, $2, $4) == -1)
|
||||||
|
err(1, "add_mime");
|
||||||
}
|
}
|
||||||
| PORT NUM { conf.port = check_port_num($2); }
|
| PORT NUM { conf.port = check_port_num($2); }
|
||||||
| PREFORK NUM { conf.prefork = check_prefork_num($2); }
|
| PREFORK NUM { conf.prefork = check_prefork_num($2); }
|
||||||
|
@ -488,7 +490,10 @@ medianames_l : medianames_l medianamesl
|
||||||
| medianamesl
|
| medianamesl
|
||||||
;
|
;
|
||||||
|
|
||||||
medianamesl : numberstring { add_mime(&conf.mime, current_media, $1); }
|
medianamesl : numberstring {
|
||||||
|
if (add_mime(&conf.mime, current_media, $1) == -1)
|
||||||
|
err(1, "add_mime");
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
nl : '\n' optnl
|
nl : '\n' optnl
|
||||||
|
|
Loading…
Reference in New Issue