fix filetype detecton by suffix so that multiple dots dont confuse it. (thanks kvik)

This commit is contained in:
Ori Bernstein 2019-12-08 11:54:59 -08:00
parent 480d7b8f5f
commit 02e6003fc8

View file

@ -871,11 +871,21 @@ printinreplyto(Biobuf *out, char *dir)
return Bprint(out, "In-Reply-To: <%s>\n", buf); return Bprint(out, "In-Reply-To: <%s>\n", buf);
} }
int
hassuffix(char *a, char *b)
{
int na, nb;
na = strlen(a), nb = strlen(b);
if(na <= nb + 1 || a[na - nb - 1] != '.')
return 0;
return strcmp(a + (na - nb), b) == 0;
}
Attach* Attach*
mkattach(char *file, char *type, int ainline) mkattach(char *file, char *type, int ainline)
{ {
int n, pfd[2]; int n, pfd[2];
char *p;
char ftype[64]; char ftype[64];
Attach *a; Attach *a;
Ctype *c; Ctype *c;
@ -902,31 +912,22 @@ mkattach(char *file, char *type, int ainline)
} }
/* pick a type depending on extension */ /* pick a type depending on extension */
p = strchr(file, '.');
if(p != nil)
p++;
/* check the builtin extensions */
if(p != nil){
for(c = ctype; c->ext != nil; c++) for(c = ctype; c->ext != nil; c++)
if(strcmp(p, c->ext) == 0){ if(hassuffix(file, c->ext)){
a->type = c->type; a->type = c->type;
a->ctype = c; a->ctype = c;
return a; return a;
} }
}
/* try the mime types file */ /* try the mime types file */
if(p != nil){
if(mimetypes == nil) if(mimetypes == nil)
readmimetypes(); readmimetypes();
for(c = mimetypes; c != nil && c->ext != nil; c++) for(c = mimetypes; c != nil && c->ext != nil; c++)
if(strcmp(p, c->ext) == 0){ if(hassuffix(file, c->ext)){
a->type = c->type; a->type = c->type;
a->ctype = c; a->ctype = c;
return a; return a;
} }
}
/* run file to figure out the type */ /* run file to figure out the type */
a->type = "application/octet-stream"; /* safest default */ a->type = "application/octet-stream"; /* safest default */