shithub: plan9front

Download patch

ref: aacf368c6d4ec9adb9fae28d3ca07d074fd459f5
parent: af95aa431d6e511355a6eb953ab7845f94c0e2d7
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jun 12 10:32:16 EDT 2021

mothra: read the content-type header over file(1) to determine type (thanks james palmer)

this fixes some pages being classified as xml by file(1),
meaning they would be rendered as plain text rather than as html.

--- a/sys/src/cmd/mothra/mothra.c	Fri Jun 11 19:26:50 2021
+++ b/sys/src/cmd/mothra/mothra.c	Sat Jun 12 10:32:16 2021
@@ -1017,8 +1017,9 @@
 		message("getting %s", selection->fullname);
 		if(mothmode && !plumb)
 			typ = -1;
-		else
+		else if((typ = mimetotype(selection->contenttype)) < 0)
 			typ = snooptype(fd);
+
 		switch(typ){
 		default:
 			if(plumb){
--- a/sys/src/cmd/mothra/mothra.h	Fri Jun 11 19:26:50 2021
+++ b/sys/src/cmd/mothra/mothra.h	Sat Jun 12 10:32:16 2021
@@ -28,6 +28,7 @@
 	char *reltext;
 	char fullname[NNAME];
 	char tag[NNAME];
+	char contenttype[NNAME];
 	int map;		/* is this an image map? */
 };
 struct Www{
@@ -97,6 +98,7 @@
 #pragma	varargck type "U" char*
 void message(char *, ...);
 int filetype(int, char *, int);
+int mimetotype(char *);
 int snooptype(int);
 void mkfieldpanel(Rtext *);
 void geturl(char *, int, int, int);
--- a/sys/src/cmd/mothra/snoop.c	Fri Jun 11 19:26:50 2021
+++ b/sys/src/cmd/mothra/snoop.c	Sat Jun 12 10:32:16 2021
@@ -87,8 +87,9 @@
 }
 
 int
-snooptype(int fd)
+mimetotype(char *mime)
 {
+	int i;
 	static struct {
 		char	*typ;
 		int	val;
@@ -110,13 +111,23 @@
 	"image/",			PAGE,
 	"text/",			PLAIN,
 	"message/rfc822",		PLAIN,
-	};
+	};	
+
+	for(i=0; i<nelem(tab); i++)
+		if(strncmp(mime, tab[i].typ, strlen(tab[i].typ)) == 0)
+			return tab[i].val;
+			
+	return -1;
+}
+
+int
+snooptype(int fd)
+{
 	char buf[128];
 	int i;
+	
 	if(filetype(fd, buf, sizeof(buf)) < 0)
 		return -1;
-	for(i=0; i<nelem(tab); i++)
-		if(strncmp(buf, tab[i].typ, strlen(tab[i].typ)) == 0)
-			return tab[i].val;
-	return -1;
+		
+	return mimetotype(buf);
 }
--- a/sys/src/cmd/mothra/url.c	Fri Jun 11 19:26:50 2021
+++ b/sys/src/cmd/mothra/url.c	Sat Jun 12 10:32:16 2021
@@ -215,6 +215,9 @@
 
 	snprint(buf+n, sizeof(buf)-n, "/parsed/fragment");
 	readstr(buf, url->tag, sizeof(url->tag));
+	
+	snprint(buf+n, sizeof(buf)-n, "/contenttype");
+	readstr(buf, url->contenttype, sizeof(url->contenttype));
 
 	snprint(buf+n, sizeof(buf)-n, "/contentencoding");
 	readstr(buf, buf, sizeof(buf));