rename mimes to mime ; pass config esplicitly to mime* functions

This commit is contained in:
Omar Polo 2021-01-21 15:01:37 +00:00
parent 20c9ff466b
commit b2a6b61371
4 changed files with 24 additions and 26 deletions

4
gmid.c
View File

@ -304,7 +304,7 @@ listener_main()
struct tls *ctx = NULL;
struct tls_config *tlsconf;
load_default_mime();
load_default_mime(&conf.mime);
if ((tlsconf = tls_config_new()) == NULL)
fatal("tls_config_new");
@ -364,7 +364,7 @@ main(int argc, char **argv)
conf.ipv6 = 0;
conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
init_mime();
init_mime(&conf.mime);
while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
switch (ch) {

11
gmid.h
View File

@ -74,7 +74,7 @@ struct etm { /* extension to mime */
const char *ext;
};
struct mimes {
struct mime {
struct etm *t;
size_t len;
size_t cap;
@ -85,7 +85,7 @@ struct conf {
int port;
int ipv6;
uint32_t protos;
struct mimes mimes;
struct mime mime;
};
extern struct conf conf;
@ -167,10 +167,9 @@ extern int yyparse(void);
extern int yylex(void);
/* mime.c */
void init_mime(void);
void add_mime(const char*, const char*);
void load_default_mime(void);
int load_mime_file(const char*);
void init_mime(struct mime*);
void add_mime(struct mime*, const char*, const char*);
void load_default_mime(struct mime*);
const char *mime(struct vhost*, const char*);
/* server.c */

33
mime.c
View File

@ -21,36 +21,35 @@
#include "gmid.h"
void
init_mime(void)
init_mime(struct mime *mime)
{
conf.mimes.len = 0;
conf.mimes.cap = 16;
mime->len = 0;
mime->cap = 16;
conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm));
if (conf.mimes.t == NULL)
mime->t = calloc(mime->cap, sizeof(struct etm));
if (mime->t == NULL)
fatal("calloc: %s", strerror(errno));
}
/* register mime for the given extension */
void
add_mime(const char *mime, const char *ext)
add_mime(struct mime *mime, const char *mt, const char *ext)
{
if (conf.mimes.len == conf.mimes.cap) {
conf.mimes.cap *= 1.5;
conf.mimes.t = realloc(conf.mimes.t,
conf.mimes.cap * sizeof(struct etm));
if (conf.mimes.t == NULL)
if (mime->len == mime->cap) {
mime->cap *= 1.5;
mime->t = realloc(mime->t, mime->cap * sizeof(struct etm));
if (mime->t == NULL)
fatal("realloc: %s", strerror(errno));
}
conf.mimes.t[conf.mimes.len].mime = mime;
conf.mimes.t[conf.mimes.len].ext = ext;
conf.mimes.len++;
mime->t[mime->len].mime = mt;
mime->t[mime->len].ext = ext;
mime->len++;
}
/* load a default set of common mime-extension associations */
void
load_default_mime()
load_default_mime(struct mime *mime)
{
struct etm *i, m[] = {
{"application/pdf", "pdf"},
@ -69,7 +68,7 @@ load_default_mime()
};
for (i = m; i->mime != NULL; ++i)
add_mime(i->mime, i->ext);
add_mime(mime, i->mime, i->ext);
}
static const char *
@ -100,7 +99,7 @@ mime(struct vhost *host, const char *path)
if ((ext = path_ext(path)) == NULL)
return def;
for (t = conf.mimes.t; t->mime != NULL; ++t)
for (t = conf.mime.t; t->mime != NULL; ++t)
if (!strcmp(ext, t->ext))
return t->mime;

View File

@ -65,7 +65,7 @@ option : TDAEMON TBOOL { conf.foreground = !$2; }
if (tls_config_parse_protocols(&conf.protos, $2) == -1)
errx(1, "invalid protocols string \"%s\"", $2);
}
| TMIME TSTRING TSTRING { add_mime($2, $3); }
| TMIME TSTRING TSTRING { add_mime(&conf.mime, $2, $3); }
;
vhosts : /* empty */