move mimes into the struct config

This commit is contained in:
Omar Polo 2021-01-18 18:48:37 +00:00
parent 982069a120
commit a010b0ddc5
2 changed files with 36 additions and 35 deletions

21
gmid.h
View File

@ -64,11 +64,24 @@ struct vhost {
extern struct vhost hosts[HOSTSLEN];
struct etm { /* extension to mime */
const char *mime;
const char *ext;
};
struct mimes {
char *def;
struct etm *t;
size_t len;
size_t cap;
};
struct conf {
int foreground;
int port;
int ipv6;
uint32_t protos;
int foreground;
int port;
int ipv6;
uint32_t protos;
struct mimes mimes;
};
extern struct conf conf;

50
mime.c
View File

@ -20,31 +20,18 @@
#include "gmid.h"
struct etm { /* extension to mime */
const char *mime;
const char *ext;
};
struct mimes {
char *def;
struct etm *t;
size_t len;
size_t cap;
};
struct mimes mimes;
void
init_mime(void)
{
mimes.len = 0;
mimes.cap = 2;
conf.mimes.len = 0;
conf.mimes.cap = 2;
if ((mimes.t = calloc(mimes.cap, sizeof(struct etm))) == NULL)
conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm));
if (conf.mimes.t == NULL)
fatal("calloc: %s", strerror(errno));
mimes.def = strdup("application/octet-stream");
if (mimes.def == NULL)
conf.mimes.def = strdup("application/octet-stream");
if (conf.mimes.def == NULL)
fatal("strdup: %s", strerror(errno));
}
@ -52,8 +39,8 @@ init_mime(void)
void
set_default_mime(const char *m)
{
free(mimes.def);
if ((mimes.def = strdup(m)) == NULL)
free(conf.mimes.def);
if ((conf.mimes.def = strdup(m)) == NULL)
fatal("strdup: %s", strerror(errno));
}
@ -61,16 +48,17 @@ set_default_mime(const char *m)
void
add_mime(const char *mime, const char *ext)
{
if (mimes.len == mimes.cap) {
mimes.cap *= 1.5;
mimes.t = realloc(mimes.t, mimes.cap * sizeof(struct etm));
if (mimes.t == NULL)
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)
fatal("realloc: %s", strerror(errno));
}
mimes.t[mimes.len].mime = mime;
mimes.t[mimes.len].ext = ext;
mimes.len++;
conf.mimes.t[conf.mimes.len].mime = mime;
conf.mimes.t[conf.mimes.len].ext = ext;
conf.mimes.len++;
}
/* load a default set of common mime-extension associations */
@ -120,11 +108,11 @@ mime(const char *path)
struct etm *t;
if ((ext = path_ext(path)) == NULL)
return mimes.def;
return conf.mimes.def;
for (t = mimes.t; t->mime != NULL; ++t)
for (t = conf.mimes.t; t->mime != NULL; ++t)
if (!strcmp(ext, t->ext))
return t->mime;
return mimes.def;
return conf.mimes.def;
}