From aea44d3194d71f8bd3836c18911204e12e77c237 Mon Sep 17 00:00:00 2001
From: bptato <nincsnevem662@gmail.com>
Date: Wed, 2 Dec 2020 23:20:08 +0100
Subject: Fix Gopher binaries causing w3m to be stuck in download mode; try to
 guess Gopher image type and fallback to png

---
 file.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/file.c b/file.c
index 329d375..4a46513 100644
--- a/file.c
+++ b/file.c
@@ -1713,6 +1713,7 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
     Str tmp;
     Str volatile page = NULL;
 #ifdef USE_GOPHER
+    int gopher_download = FALSE;
 #endif
 #ifdef USE_M17N
     wc_ces charset = WC_CES_US_ASCII;
@@ -1994,11 +1995,14 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
 	    t = "text/html";
 	    break;
 	case 'I':
-	    t = "image/gopher";
+	    t = guessContentType(pu.file);
+	    if(strncasecmp(t, "image/", 6) != 0) {
+		t = "image/png";
+	    }
 	    break;
 	case '5':
 	case '9':
-	    do_download = 1;
+	    gopher_download = TRUE;
 	    break;
 	}
     }
@@ -2120,7 +2124,11 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
 	    Strfputs(s, src);
 	    fclose(src);
 	}
+#ifdef USE_GOPHER
+	if (do_download || gopher_download) {
+#else
 	if (do_download) {
+#endif
 	    char *file;
 	    if (!src)
 		return NULL;
@@ -2157,7 +2165,11 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
     current_content_length = 0;
     if ((p = checkHeader(t_buf, "Content-Length:")) != NULL)
 	current_content_length = strtoclen(p);
+#ifdef USE_GOPHER
+    if (do_download || gopher_download) {
+#else
     if (do_download) {
+#endif
 	/* download only */
 	char *file;
 	TRAP_OFF;
@@ -2223,7 +2235,11 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
 #endif
     else if (w3m_backend) ;
     else if (!(w3m_dump & ~DUMP_FRAME) || is_dump_text_type(t)) {
-	if (!do_download && searchExtViewer(t) != NULL) {
+	if (!do_download && 
+#ifdef USE_GOPHER
+		!gopher_download &&
+#endif
+		searchExtViewer(t) != NULL) {
 	    proc = DO_EXTERNAL;
 	}
 	else {
-- 
cgit v1.2.3