aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gmid.c1
-rw-r--r--gmid.h1
-rw-r--r--mime.c31
3 files changed, 29 insertions, 4 deletions
diff --git a/gmid.c b/gmid.c
index 21ac1f7..12b0700 100644
--- a/gmid.c
+++ b/gmid.c
@@ -253,6 +253,7 @@ listener_main(struct imsgbuf *ibuf)
drop_priv();
if (!conf.mime.skip_defaults && load_default_mime(&conf.mime) == -1)
fatal("load_default_mime: %s", strerror(errno));
+ sort_mime(&conf.mime);
load_vhosts();
loop(ctx, sock4, sock6, ibuf);
return 0;
diff --git a/gmid.h b/gmid.h
index 3bf9798..cc6253a 100644
--- a/gmid.h
+++ b/gmid.h
@@ -362,6 +362,7 @@ int logger_main(int, struct imsgbuf*);
void init_mime(struct mime*);
int add_mime(struct mime*, const char*, const char*);
int load_default_mime(struct mime*);
+void sort_mime(struct mime *);
const char *mime(struct vhost*, const char*);
void free_mime(struct mime *);
diff --git a/mime.c b/mime.c
index 715bec5..8ce73fd 100644
--- a/mime.c
+++ b/mime.c
@@ -111,6 +111,29 @@ path_ext(const char *path)
return NULL;
}
+static int
+mime_comp(const void *a, const void *b)
+{
+ const struct etm *x = a, *y = b;
+
+ return strcmp(x->ext, y->ext);
+}
+
+void
+sort_mime(struct mime *m)
+{
+ qsort(m->t, m->len, sizeof(*m->t), mime_comp);
+}
+
+static int
+mime_find(const void *a, const void *b)
+{
+ const char *ext = a;
+ const struct etm *x = b;
+
+ return strcmp(ext, x->ext);
+}
+
const char *
mime(struct vhost *host, const char *path)
{
@@ -122,10 +145,10 @@ mime(struct vhost *host, const char *path)
if ((ext = path_ext(path)) == NULL)
return def;
- for (t = conf.mime.t; t->mime != NULL; ++t)
- if (!strcmp(ext, t->ext))
- return t->mime;
-
+ t = bsearch(ext, conf.mime.t, conf.mime.len, sizeof(*conf.mime.t),
+ mime_find);
+ if (t != NULL)
+ return t->mime;
return def;
}