diff options
author | Omar Polo <op@omarpolo.com> | 2022-04-08 15:14:09 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2022-04-08 15:14:09 +0000 |
commit | 18bd83915eab0f06b7e2920d0d71a39108b2d641 (patch) | |
tree | 77573b6b88a3f24a0459f4a5901e6eeb239c1e5e /mime.c | |
parent | ca44613693732f925bb60419342e9a62e4ddf000 (diff) |
sort the MIME mappings and do a binary search to match
Diffstat (limited to 'mime.c')
-rw-r--r-- | mime.c | 31 |
1 files changed, 27 insertions, 4 deletions
@@ -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; } |