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.
This commit is contained in:
Ori Bernstein 2021-06-12 14:32:16 +00:00
parent af95aa431d
commit aacf368c6d
4 changed files with 25 additions and 8 deletions

View file

@ -1017,8 +1017,9 @@ void geturl(char *urlname, int post, int plumb, int map){
message("getting %s", selection->fullname); message("getting %s", selection->fullname);
if(mothmode && !plumb) if(mothmode && !plumb)
typ = -1; typ = -1;
else else if((typ = mimetotype(selection->contenttype)) < 0)
typ = snooptype(fd); typ = snooptype(fd);
switch(typ){ switch(typ){
default: default:
if(plumb){ if(plumb){

View file

@ -28,6 +28,7 @@ struct Url{
char *reltext; char *reltext;
char fullname[NNAME]; char fullname[NNAME];
char tag[NNAME]; char tag[NNAME];
char contenttype[NNAME];
int map; /* is this an image map? */ int map; /* is this an image map? */
}; };
struct Www{ struct Www{
@ -97,6 +98,7 @@ int Ufmt(Fmt *f);
#pragma varargck type "U" char* #pragma varargck type "U" char*
void message(char *, ...); void message(char *, ...);
int filetype(int, char *, int); int filetype(int, char *, int);
int mimetotype(char *);
int snooptype(int); int snooptype(int);
void mkfieldpanel(Rtext *); void mkfieldpanel(Rtext *);
void geturl(char *, int, int, int); void geturl(char *, int, int, int);

View file

@ -87,8 +87,9 @@ Err1:
} }
int int
snooptype(int fd) mimetotype(char *mime)
{ {
int i;
static struct { static struct {
char *typ; char *typ;
int val; int val;
@ -110,13 +111,23 @@ snooptype(int fd)
"image/", PAGE, "image/", PAGE,
"text/", PLAIN, "text/", PLAIN,
"message/rfc822", PLAIN, "message/rfc822", PLAIN,
}; };
char buf[128];
int i;
if(filetype(fd, buf, sizeof(buf)) < 0)
return -1;
for(i=0; i<nelem(tab); i++) for(i=0; i<nelem(tab); i++)
if(strncmp(buf, tab[i].typ, strlen(tab[i].typ)) == 0) if(strncmp(mime, tab[i].typ, strlen(tab[i].typ)) == 0)
return tab[i].val; return tab[i].val;
return -1; return -1;
} }
int
snooptype(int fd)
{
char buf[128];
int i;
if(filetype(fd, buf, sizeof(buf)) < 0)
return -1;
return mimetotype(buf);
}

View file

@ -215,6 +215,9 @@ urlget(Url *url, int body)
snprint(buf+n, sizeof(buf)-n, "/parsed/fragment"); snprint(buf+n, sizeof(buf)-n, "/parsed/fragment");
readstr(buf, url->tag, sizeof(url->tag)); 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"); snprint(buf+n, sizeof(buf)-n, "/contentencoding");
readstr(buf, buf, sizeof(buf)); readstr(buf, buf, sizeof(buf));