diff options
-rw-r--r-- | src/gmutil.c | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/src/gmutil.c b/src/gmutil.c index fda8489b..9bd74ee0 100644 --- a/src/gmutil.c +++ b/src/gmutil.c | |||
@@ -22,10 +22,12 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ | |||
22 | 22 | ||
23 | #include "gmutil.h" | 23 | #include "gmutil.h" |
24 | 24 | ||
25 | #include <the_Foundation/regexp.h> | 25 | #include <the_Foundation/file.h> |
26 | #include <the_Foundation/fileinfo.h> | ||
26 | #include <the_Foundation/object.h> | 27 | #include <the_Foundation/object.h> |
27 | #include <the_Foundation/path.h> | 28 | #include <the_Foundation/path.h> |
28 | #include <the_Foundation/regexp.h> | 29 | #include <the_Foundation/regexp.h> |
30 | #include <the_Foundation/regexp.h> | ||
29 | 31 | ||
30 | iRegExp *newGemtextLink_RegExp(void) { | 32 | iRegExp *newGemtextLink_RegExp(void) { |
31 | return new_RegExp("=>\\s*([^\\s]+)(\\s.*)?", 0); | 33 | return new_RegExp("=>\\s*([^\\s]+)(\\s.*)?", 0); |
@@ -513,18 +515,6 @@ const char *mediaType_Path(const iString *path) { | |||
513 | if (endsWithCase_String(path, ".gmi") || endsWithCase_String(path, ".gemini")) { | 515 | if (endsWithCase_String(path, ".gmi") || endsWithCase_String(path, ".gemini")) { |
514 | return "text/gemini; charset=utf-8"; | 516 | return "text/gemini; charset=utf-8"; |
515 | } | 517 | } |
516 | /* TODO: It would be better to default to text/plain, but switch to | ||
517 | application/octet-stream if the contents fail to parse as UTF-8. */ | ||
518 | else if (endsWithCase_String(path, ".txt") || | ||
519 | endsWithCase_String(path, ".md") || | ||
520 | endsWithCase_String(path, ".c") || | ||
521 | endsWithCase_String(path, ".h") || | ||
522 | endsWithCase_String(path, ".cc") || | ||
523 | endsWithCase_String(path, ".hh") || | ||
524 | endsWithCase_String(path, ".cpp") || | ||
525 | endsWithCase_String(path, ".hpp")) { | ||
526 | return "text/plain"; | ||
527 | } | ||
528 | else if (endsWithCase_String(path, ".pem")) { | 518 | else if (endsWithCase_String(path, ".pem")) { |
529 | return "application/x-pem-file"; | 519 | return "application/x-pem-file"; |
530 | } | 520 | } |
@@ -558,7 +548,30 @@ const char *mediaType_Path(const iString *path) { | |||
558 | else if (endsWithCase_String(path, ".mid")) { | 548 | else if (endsWithCase_String(path, ".mid")) { |
559 | return "audio/midi"; | 549 | return "audio/midi"; |
560 | } | 550 | } |
561 | return "application/octet-stream"; | 551 | else if (endsWithCase_String(path, ".txt") || |
552 | endsWithCase_String(path, ".md") || | ||
553 | endsWithCase_String(path, ".c") || | ||
554 | endsWithCase_String(path, ".h") || | ||
555 | endsWithCase_String(path, ".cc") || | ||
556 | endsWithCase_String(path, ".hh") || | ||
557 | endsWithCase_String(path, ".cpp") || | ||
558 | endsWithCase_String(path, ".hpp")) { | ||
559 | return "text/plain"; | ||
560 | } | ||
561 | const char *mtype = "application/octet-stream"; | ||
562 | /* If the file is reasonably small and looks like UTF-8, we'll display it as text/plain. */ | ||
563 | if (fileExists_FileInfo(path) && fileSize_FileInfo(path) <= 5000000) { | ||
564 | iFile *f = new_File(path); | ||
565 | if (open_File(f, readOnly_FileMode)) { | ||
566 | iBlock *content = readAll_File(f); | ||
567 | if (isUtf8_Rangecc(range_Block(content))) { | ||
568 | mtype = "text/plain; charset=utf-8"; | ||
569 | } | ||
570 | delete_Block(content); | ||
571 | } | ||
572 | iRelease(f); | ||
573 | } | ||
574 | return mtype; | ||
562 | } | 575 | } |
563 | 576 | ||
564 | static void replaceAllChars_String_(iString *d, char c, const char *replacement) { | 577 | static void replaceAllChars_String_(iString *d, char c, const char *replacement) { |