summary refs log tree commit diff
path: root/mmime.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2017-04-06 20:54:01 +0200
committerLeah Neukirchen <leah@vuxu.org>2017-04-06 21:02:16 +0200
commitd75c446aff168a399f4d169a7e44220f1acec63e (patch)
treee14b0dbe27bdff413f7bee78b7f11681374dafd5 /mmime.c
parent935f4bf5b92bb066f7c1aa9fbe41b685e7677180 (diff)
downloadmblaze-d75c446aff168a399f4d169a7e44220f1acec63e.tar.gz
mblaze-d75c446aff168a399f4d169a7e44220f1acec63e.tar.xz
mblaze-d75c446aff168a399f4d169a7e44220f1acec63e.zip
mmime: use RFC2231 for Content-Disposition when needed
Diffstat (limited to 'mmime.c')
-rw-r--r--mmime.c44
1 files changed, 41 insertions, 3 deletions
diff --git a/mmime.c b/mmime.c
index 0f61e0c..9a359a3 100644
--- a/mmime.c
+++ b/mmime.c
@@ -113,7 +113,45 @@ basenam(const char *s)
 	return r ? r + 1 : s;
 }
 
-int gen_file(char *file, char *ct)
+static void
+gen_attachment(const char *filename)
+{
+	const char *s = filename;
+
+	for (s = (char *) filename; *s; s++)
+		if (*s <= 32 || *s >= 127 || s - filename > 35)
+			goto rfc2231;
+
+	printf("Content-Disposition: attachment; filename=\"%s\"\n", filename);
+	return;
+
+rfc2231:
+
+	printf("Content-Disposition: attachment");
+	int i = 0;
+	int d = 0;
+
+	s = filename;
+
+	while (*s) {
+		i = printf(";\n filename*%d*=", d);
+		if (d++ == 0) {
+			printf("UTF-8''");
+			i += 7;
+		}
+		while (*s && i < 78 - 3) {
+			if (*s <= 32 || *s > 126)
+				i += printf("%%%02x", (uint8_t) *s++);
+			else
+				i += printf("%c", (uint8_t) *s++);
+		}
+	}
+
+	printf("\n");
+}
+
+int
+gen_file(char *file, char *ct)
 {
 	uint8_t *content;
 	off_t size;
@@ -144,8 +182,8 @@ int gen_file(char *file, char *ct)
 			bithigh++;
 	}
 
-	printf("Content-Disposition: attachment; filename=\"%s\"\n",
-	       basenam(file));
+	gen_attachment(basenam(file));
+
 	if (bitlow == 0 && bithigh == 0 &&
 	    maxlinelen <= 78 && content[size-1] == '\n') {
 		if (!ct)