diff options
-rw-r--r-- | cgit.h | 2 | ||||
-rw-r--r-- | shared.c | 65 | ||||
-rw-r--r-- | ui-plain.c | 28 |
3 files changed, 44 insertions, 51 deletions
@@ -391,6 +391,6 @@ extern int readfile(const char *path, char **buf, size_t *size); | |||
391 | 391 | ||
392 | extern char *expand_macros(const char *txt); | 392 | extern char *expand_macros(const char *txt); |
393 | 393 | ||
394 | extern char *get_mimetype_from_file(const char *filename, const char *ext); | 394 | extern char *get_mimetype_for_filename(const char *filename); |
395 | 395 | ||
396 | #endif /* CGIT_H */ | 396 | #endif /* CGIT_H */ |
@@ -561,42 +561,51 @@ char *expand_macros(const char *txt) | |||
561 | return result; | 561 | return result; |
562 | } | 562 | } |
563 | 563 | ||
564 | char *get_mimetype_from_file(const char *filename, const char *ext) | 564 | char *get_mimetype_for_filename(const char *filename) |
565 | { | 565 | { |
566 | static const char *delimiters; | 566 | static const char *delimiters; |
567 | char *result; | 567 | char *ext = NULL, *iterate, *mimetype = NULL, *token; |
568 | FILE *fd; | ||
569 | char line[1024]; | 568 | char line[1024]; |
570 | char *mimetype; | 569 | FILE *fd; |
571 | char *token; | 570 | struct string_list_item *mime; |
572 | |||
573 | if (!filename) | ||
574 | return NULL; | ||
575 | 571 | ||
576 | fd = fopen(filename, "r"); | 572 | if (filename == NULL) |
577 | if (!fd) | ||
578 | return NULL; | 573 | return NULL; |
579 | 574 | ||
580 | delimiters = " \t\r\n"; | 575 | ext = strrchr(filename, '.'); |
581 | result = NULL; | 576 | |
582 | 577 | if (ext && *(++ext)) { | |
583 | /* loop over all lines in the file */ | 578 | mime = string_list_lookup(&ctx.cfg.mimetypes, ext); |
584 | while (!result && fgets(line, sizeof(line), fd)) { | 579 | if (mime) { |
585 | mimetype = strtok(line, delimiters); | 580 | /* We could just pass the pointer here, but would have to care |
586 | 581 | * whether or not to free the memory. Instead just dup. */ | |
587 | /* skip empty lines and comment lines */ | 582 | mimetype = xstrdup(mime->util); |
588 | if (!mimetype || (mimetype[0] == '#')) | 583 | } else if (ctx.cfg.mimetype_file != NULL) { |
589 | continue; | 584 | fd = fopen(ctx.cfg.mimetype_file, "r"); |
590 | 585 | if (fd == NULL) | |
591 | /* loop over all extensions of mimetype */ | 586 | return NULL; |
592 | while ((token = strtok(NULL, delimiters))) { | 587 | |
593 | if (!strcasecmp(ext, token)) { | 588 | delimiters = " \t\r\n"; |
594 | result = xstrdup(mimetype); | 589 | |
595 | break; | 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 | } | ||
596 | } | 605 | } |
606 | fclose(fd); | ||
597 | } | 607 | } |
598 | } | 608 | } |
599 | fclose(fd); | ||
600 | 609 | ||
601 | return result; | 610 | return mimetype; |
602 | } | 611 | } |
@@ -19,10 +19,8 @@ struct walk_tree_context { | |||
19 | static int print_object(const unsigned char *sha1, const char *path) | 19 | static int print_object(const unsigned char *sha1, const char *path) |
20 | { | 20 | { |
21 | enum object_type type; | 21 | enum object_type type; |
22 | char *buf, *ext; | 22 | char *buf, *mimetype; |
23 | unsigned long size; | 23 | unsigned long size; |
24 | struct string_list_item *mime; | ||
25 | int freemime; | ||
26 | 24 | ||
27 | type = sha1_object_info(sha1, &size); | 25 | type = sha1_object_info(sha1, &size); |
28 | if (type == OBJ_BAD) { | 26 | if (type == OBJ_BAD) { |
@@ -35,22 +33,10 @@ static int print_object(const unsigned char *sha1, const char *path) | |||
35 | cgit_print_error_page(404, "Not found", "Not found"); | 33 | cgit_print_error_page(404, "Not found", "Not found"); |
36 | return 0; | 34 | return 0; |
37 | } | 35 | } |
38 | ctx.page.mimetype = NULL; | 36 | |
39 | ext = strrchr(path, '.'); | 37 | mimetype = get_mimetype_for_filename(path); |
40 | freemime = 0; | 38 | ctx.page.mimetype = mimetype; |
41 | if (ext && *(++ext)) { | 39 | |
42 | mime = string_list_lookup(&ctx.cfg.mimetypes, ext); | ||
43 | if (mime) { | ||
44 | ctx.page.mimetype = (char *)mime->util; | ||
45 | ctx.page.charset = NULL; | ||
46 | } else { | ||
47 | ctx.page.mimetype = get_mimetype_from_file(ctx.cfg.mimetype_file, ext); | ||
48 | if (ctx.page.mimetype) { | ||
49 | freemime = 1; | ||
50 | ctx.page.charset = NULL; | ||
51 | } | ||
52 | } | ||
53 | } | ||
54 | if (!ctx.page.mimetype) { | 40 | if (!ctx.page.mimetype) { |
55 | if (buffer_is_binary(buf, size)) { | 41 | if (buffer_is_binary(buf, size)) { |
56 | ctx.page.mimetype = "application/octet-stream"; | 42 | ctx.page.mimetype = "application/octet-stream"; |
@@ -64,9 +50,7 @@ static int print_object(const unsigned char *sha1, const char *path) | |||
64 | ctx.page.etag = sha1_to_hex(sha1); | 50 | ctx.page.etag = sha1_to_hex(sha1); |
65 | cgit_print_http_headers(); | 51 | cgit_print_http_headers(); |
66 | html_raw(buf, size); | 52 | html_raw(buf, size); |
67 | /* If we allocated this, then casting away const is safe. */ | 53 | free(mimetype); |
68 | if (freemime) | ||
69 | free((char*) ctx.page.mimetype); | ||
70 | return 1; | 54 | return 1; |
71 | } | 55 | } |
72 | 56 | ||