mshow: extract file name logic

pull/1/merge
Christian Neukirchen 8 years ago
parent d52fa6bf4d
commit ec6bee84f6

@ -96,6 +96,9 @@ char *
mimetype(char *ct) mimetype(char *ct)
{ {
char *s; char *s;
if (!ct)
return 0;
for (s = ct; *s && *s != ';' && *s != ' ' && *s != '\t'; s++) for (s = ct; *s && *s != ';' && *s != ' ' && *s != '\t'; s++)
; ;
@ -106,6 +109,9 @@ char *
tlmimetype(char *ct) tlmimetype(char *ct)
{ {
char *s; char *s;
if (!ct)
return 0;
for (s = ct; *s && *s != ';' && *s != ' ' && *s != '\t' && *s != '/'; s++) for (s = ct; *s && *s != ';' && *s != ' ' && *s != '\t' && *s != '/'; s++)
; ;
@ -118,17 +124,31 @@ typedef enum {
MIME_PRUNE, MIME_PRUNE,
} mime_action; } mime_action;
typedef mime_action (*mime_callback)(int, char *, char *, size_t); typedef mime_action (*mime_callback)(int, struct message *, char *, size_t);
char *
mime_filename(struct message *msg)
{
char *filename = 0, *fn, *fne, *v;
if ((v = blaze822_hdr(msg, "content-disposition"))) {
if (blaze822_mime_parameter(v, "filename", &fn, &fne))
filename = strndup(fn, fne-fn);
} else if ((v = blaze822_hdr(msg, "content-type"))) {
if (blaze822_mime_parameter(v, "name", &fn, &fne))
filename = strndup(fn, fne-fn);
}
return filename;
}
mime_action mime_action
render_mime(int depth, char *ct, char *body, size_t bodylen) render_mime(int depth, struct message *msg, char *body, size_t bodylen)
{ {
char *ct = blaze822_hdr(msg, "content-type");
char *mt = mimetype(ct); char *mt = mimetype(ct);
char *tlmt = tlmimetype(ct); char *tlmt = tlmimetype(ct);
char *filename = mime_filename(msg);
char *filename = 0, *fn, *fne;
if (blaze822_mime_parameter(ct, "name", &fn, &fne))
filename = strndup(fn, fne-fn);
mimecount++; mimecount++;
@ -137,7 +157,7 @@ render_mime(int depth, char *ct, char *body, size_t bodylen)
printf("--- "); printf("--- ");
printf("%d: %s size=%zd", mimecount, mt, bodylen); printf("%d: %s size=%zd", mimecount, mt, bodylen);
if (filename) { if (filename) {
printf(" name=%s", filename); printf(" name=\"%s\"", filename);
free(filename); free(filename);
} }
@ -206,10 +226,11 @@ nofilter:
} }
mime_action mime_action
reply_mime(int depth, char *ct, char *body, size_t bodylen) reply_mime(int depth, struct message *msg, char *body, size_t bodylen)
{ {
(void) depth; (void) depth;
char *ct = blaze822_hdr(msg, "Content-Type");
char *mt = mimetype(ct); char *mt = mimetype(ct);
char *tlmt = tlmimetype(ct); char *tlmt = tlmimetype(ct);
@ -234,17 +255,18 @@ reply_mime(int depth, char *ct, char *body, size_t bodylen)
} }
mime_action mime_action
list_mime(int depth, char *ct, char *body, size_t bodylen) list_mime(int depth, struct message *msg, char *body, size_t bodylen)
{ {
(void) body; (void) body;
char *ct = blaze822_hdr(msg, "content-type");
char *mt = mimetype(ct); char *mt = mimetype(ct);
char *fn, *fne; char *filename = mime_filename(msg);
printf("%*.s%d: %s size=%zd", depth*2, "", ++mimecount, mt, bodylen); printf("%*.s%d: %s size=%zd", depth*2, "", ++mimecount, mt, bodylen);
if (blaze822_mime_parameter(ct, "name", &fn, &fne)) { if (filename) {
printf(" name="); printf(" name=\"%s\"", filename);
fwrite(fn, 1, fne-fn, stdout); free(filename);
} }
printf("\n"); printf("\n");
@ -261,7 +283,7 @@ walk_mime(struct message *msg, int depth, mime_callback visit)
if (blaze822_mime_body(msg, &ct, &body, &bodylen, &bodychunk)) { if (blaze822_mime_body(msg, &ct, &body, &bodylen, &bodychunk)) {
mime_action r = visit(depth, ct, body, bodylen); mime_action r = visit(depth, msg, body, bodylen);
if (r == MIME_CONTINUE) { if (r == MIME_CONTINUE) {
if (strncmp(ct, "multipart/", 10) == 0) { if (strncmp(ct, "multipart/", 10) == 0) {
@ -334,14 +356,12 @@ writefile(char *name, char *buf, ssize_t len)
} }
mime_action mime_action
extract_mime(int depth, char *ct, char *body, size_t bodylen) extract_mime(int depth, struct message *msg, char *body, size_t bodylen)
{ {
(void) body; (void) body;
(void) depth; (void) depth;
char *filename = 0, *fn, *fne; char *filename = mime_filename(msg);
if (blaze822_mime_parameter(ct, "name", &fn, &fne))
filename = strndup(fn, fne-fn);
mimecount++; mimecount++;

@ -64,6 +64,8 @@ blaze822_mime_body(struct message *msg,
int int
blaze822_mime_parameter(char *s, char *name, char **starto, char **stopo) blaze822_mime_parameter(char *s, char *name, char **starto, char **stopo)
{ {
if (!s)
return 0;
s = strchr(s, ';'); s = strchr(s, ';');
if (!s) if (!s)
return 0; return 0;

Loading…
Cancel
Save