about summary refs log tree commit diff stats
path: root/shared.c
diff options
context:
space:
mode:
authorJason A. Donenfeld2015-08-17 14:35:20 +0200
committerJason A. Donenfeld2015-08-17 14:49:28 +0200
commit73f199be3f0b03fbaee9b9b30ed3d782e3395af7 (patch)
tree84822aa88daca47ef66e7ef7ff998ce35c972ab4 /shared.c
parentui-summary: send images plain for about page (diff)
downloadcgit-73f199be3f0b03fbaee9b9b30ed3d782e3395af7.tar.gz
cgit-73f199be3f0b03fbaee9b9b30ed3d782e3395af7.zip
mime: rewrite detection function
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'shared.c')
-rw-r--r--shared.c62
1 files changed, 26 insertions, 36 deletions
diff --git a/shared.c b/shared.c index 225ce15..3d91a76 100644 --- a/shared.c +++ b/shared.c
@@ -563,49 +563,39 @@ char *expand_macros(const char *txt)
563 563
564char *get_mimetype_for_filename(const char *filename) 564char *get_mimetype_for_filename(const char *filename)
565{ 565{
566 static const char *delimiters; 566 char *ext, *mimetype, *token, line[1024];
567 char *ext = NULL, *iterate, *mimetype = NULL, *token; 567 FILE *file;
568 char line[1024];
569 FILE *fd;
570 struct string_list_item *mime; 568 struct string_list_item *mime;
571 569
572 if (filename == NULL) 570 if (!filename)
573 return NULL; 571 return NULL;
574 572
575 ext = strrchr(filename, '.'); 573 ext = strrchr(filename, '.');
574 if (!ext)
575 return NULL;
576 ++ext;
577 if (!ext[0])
578 return NULL;
579 mime = string_list_lookup(&ctx.cfg.mimetypes, ext);
580 if (mime)
581 return xstrdup(mime->util);
576 582
577 if (ext && *(++ext)) { 583 if (!ctx.cfg.mimetype_file)
578 mime = string_list_lookup(&ctx.cfg.mimetypes, ext); 584 return NULL;
579 if (mime) { 585 file = fopen(ctx.cfg.mimetype_file, "r");
580 /* We could just pass the pointer here, but would have to care 586 if (!file)
581 * whether or not to free the memory. Instead just dup. */ 587 return NULL;
582 mimetype = xstrdup(mime->util); 588 while (fgets(line, sizeof(line), file)) {
583 } else if (ctx.cfg.mimetype_file != NULL) { 589 if (!line[0] || line[0] == '#')
584 fd = fopen(ctx.cfg.mimetype_file, "r"); 590 continue;
585 if (fd == NULL) 591 mimetype = strtok(line, " \t\r\n");
586 return NULL; 592 while ((token = strtok(NULL, " \t\r\n"))) {
587 593 if (!strcasecmp(ext, token)) {
588 delimiters = " \t\r\n"; 594 fclose(file);
589 595 return xstrdup(mimetype);
590 /* loop over all lines in the file */
591 while (mimetype == NULL && fgets(line, sizeof(line), fd)) {
592 iterate = strtok(line, delimiters);
593
594 /* skip empty lines and comment lines */
595 if (iterate == NULL || (iterate[0] == '#'))
596 continue;
597
598 /* loop over all extensions of mimetype */
599 while ((token = strtok(NULL, delimiters))) {
600 if (strcasecmp(ext, token) == 0) {
601 mimetype = xstrdup(iterate);
602 break;
603 }
604 }
605 } 596 }
606 fclose(fd);
607 } 597 }
608 } 598 }
609 599 fclose(file);
610 return mimetype; 600 return NULL;
611} 601}