Re: [gmime-devel] Feature request: custom content headers



Try this patch - instead of adding a whitelist of headers to accept, simply accept all headers for non-toplevel mime parts.

Jeff

diff --git a/gmime/gmime-message-part.c b/gmime/gmime-message-part.c
index 2ed9d1c..23b9b58 100644
--- a/gmime/gmime-message-part.c
+++ b/gmime/gmime-message-part.c
@@ -47,13 +47,6 @@ static void g_mime_message_part_init (GMimeMessagePart *message_part, GMimeMessa
 static void g_mime_message_part_finalize (GObject *object);
 
 /* GMimeObject class methods */
-static void message_part_prepend_header (GMimeObject *object, const char *header, const char *value);
-static void message_part_append_header (GMimeObject *object, const char *header, const char *value);
-static void message_part_set_header (GMimeObject *object, const char *header, const char *value);
-static const char *message_part_get_header (GMimeObject *object, const char *header);
-static gboolean message_part_remove_header (GMimeObject *object, const char *header);
-static void message_part_set_content_type (GMimeObject *object, GMimeContentType *content_type);
-static char *message_part_get_headers (GMimeObject *object);
 static ssize_t message_part_write_to_stream (GMimeObject *object, GMimeStream *stream);
 
 
@@ -95,13 +88,6 @@ g_mime_message_part_class_init (GMimeMessagePartClass *klass)
 	
 	gobject_class->finalize = g_mime_message_part_finalize;
 	
-	object_class->prepend_header = message_part_prepend_header;
-	object_class->append_header = message_part_append_header;
-	object_class->remove_header = message_part_remove_header;
-	object_class->set_header = message_part_set_header;
-	object_class->get_header = message_part_get_header;
-	object_class->set_content_type = message_part_set_content_type;
-	object_class->get_headers = message_part_get_headers;
 	object_class->write_to_stream = message_part_write_to_stream;
 }
 
@@ -122,69 +108,6 @@ g_mime_message_part_finalize (GObject *object)
 	G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
-static void
-message_part_prepend_header (GMimeObject *object, const char *header, const char *value)
-{
-	/* RFC 1864 states that you cannot set a Content-MD5 on a message part */
-	if (!g_ascii_strcasecmp ("Content-MD5", header))
-		return;
-	
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a message part */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		GMIME_OBJECT_CLASS (parent_class)->prepend_header (object, header, value);
-}
-
-static void
-message_part_append_header (GMimeObject *object, const char *header, const char *value)
-{
-	/* RFC 1864 states that you cannot set a Content-MD5 on a message part */
-	if (!g_ascii_strcasecmp ("Content-MD5", header))
-		return;
-	
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a message part */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		GMIME_OBJECT_CLASS (parent_class)->append_header (object, header, value);
-}
-
-static void
-message_part_set_header (GMimeObject *object, const char *header, const char *value)
-{
-	/* RFC 1864 states that you cannot set a Content-MD5 on a message part */
-	if (!g_ascii_strcasecmp ("Content-MD5", header))
-		return;
-	
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a message part */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		GMIME_OBJECT_CLASS (parent_class)->set_header (object, header, value);
-}
-
-static const char *
-message_part_get_header (GMimeObject *object, const char *header)
-{
-	return GMIME_OBJECT_CLASS (parent_class)->get_header (object, header);
-}
-
-static gboolean
-message_part_remove_header (GMimeObject *object, const char *header)
-{
-	return GMIME_OBJECT_CLASS (parent_class)->remove_header (object, header);
-}
-
-static void
-message_part_set_content_type (GMimeObject *object, GMimeContentType *content_type)
-{
-	GMIME_OBJECT_CLASS (parent_class)->set_content_type (object, content_type);
-}
-
-static char *
-message_part_get_headers (GMimeObject *object)
-{
-	return GMIME_OBJECT_CLASS (parent_class)->get_headers (object);
-}
-
 static ssize_t
 message_part_write_to_stream (GMimeObject *object, GMimeStream *stream)
 {
diff --git a/gmime/gmime-multipart.c b/gmime/gmime-multipart.c
index a1da42a..415b621 100644
--- a/gmime/gmime-multipart.c
+++ b/gmime/gmime-multipart.c
@@ -55,13 +55,6 @@ static void g_mime_multipart_init (GMimeMultipart *multipart, GMimeMultipartClas
 static void g_mime_multipart_finalize (GObject *object);
 
 /* GMimeObject class methods */
-static void multipart_prepend_header (GMimeObject *object, const char *header, const char *value);
-static void multipart_append_header (GMimeObject *object, const char *header, const char *value);
-static void multipart_set_header (GMimeObject *object, const char *header, const char *value);
-static const char *multipart_get_header (GMimeObject *object, const char *header);
-static gboolean multipart_remove_header (GMimeObject *object, const char *header);
-static void multipart_set_content_type (GMimeObject *object, GMimeContentType *content_type);
-static char *multipart_get_headers (GMimeObject *object);
 static ssize_t multipart_write_to_stream (GMimeObject *object, GMimeStream *stream);
 static void multipart_encode (GMimeObject *object, GMimeEncodingConstraint constraint);
 
@@ -117,13 +110,6 @@ g_mime_multipart_class_init (GMimeMultipartClass *klass)
 	
 	gobject_class->finalize = g_mime_multipart_finalize;
 	
-	object_class->prepend_header = multipart_prepend_header;
-	object_class->append_header = multipart_append_header;
-	object_class->remove_header = multipart_remove_header;
-	object_class->set_header = multipart_set_header;
-	object_class->get_header = multipart_get_header;
-	object_class->set_content_type = multipart_set_content_type;
-	object_class->get_headers = multipart_get_headers;
 	object_class->write_to_stream = multipart_write_to_stream;
 	object_class->encode = multipart_encode;
 	
@@ -165,71 +151,6 @@ g_mime_multipart_finalize (GObject *object)
 	G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
-static void
-multipart_prepend_header (GMimeObject *object, const char *header, const char *value)
-{
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a multipart */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		GMIME_OBJECT_CLASS (parent_class)->prepend_header (object, header, value);
-}
-
-static void
-multipart_append_header (GMimeObject *object, const char *header, const char *value)
-{
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a multipart */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		GMIME_OBJECT_CLASS (parent_class)->append_header (object, header, value);
-}
-
-static void
-multipart_set_header (GMimeObject *object, const char *header, const char *value)
-{
-	/* RFC 1864 states that you cannot set a Content-MD5 on a multipart */
-	if (!g_ascii_strcasecmp ("Content-MD5", header))
-		return;
-	
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a multipart */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		GMIME_OBJECT_CLASS (parent_class)->set_header (object, header, value);
-}
-
-static const char *
-multipart_get_header (GMimeObject *object, const char *header)
-{
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a multipart */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		return GMIME_OBJECT_CLASS (parent_class)->get_header (object, header);
-	else
-		return NULL;
-}
-
-static gboolean
-multipart_remove_header (GMimeObject *object, const char *header)
-{
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a multipart */
-	if (g_ascii_strncasecmp ("Content-", header, 8) != 0)
-		return FALSE;
-	
-	return GMIME_OBJECT_CLASS (parent_class)->remove_header (object, header);
-}
-
-static void
-multipart_set_content_type (GMimeObject *object, GMimeContentType *content_type)
-{
-	GMIME_OBJECT_CLASS (parent_class)->set_content_type (object, content_type);
-}
-
-static char *
-multipart_get_headers (GMimeObject *object)
-{
-	return GMIME_OBJECT_CLASS (parent_class)->get_headers (object);
-}
-
 static ssize_t
 multipart_write_to_stream (GMimeObject *object, GMimeStream *stream)
 {
diff --git a/gmime/gmime-object.c b/gmime/gmime-object.c
index e57d285..d1f42ef 100644
--- a/gmime/gmime-object.c
+++ b/gmime/gmime-object.c
@@ -728,8 +728,11 @@ process_header (GMimeObject *object, const char *header, const char *value)
 	GMimeContentType *content_type;
 	guint i;
 	
+	if (g_ascii_strncasecmp (header, "Content-", 8) != 0)
+		return FALSE;
+	
 	for (i = 0; i < G_N_ELEMENTS (content_headers); i++) {
-		if (!g_ascii_strcasecmp (content_headers[i], header))
+		if (!g_ascii_strcasecmp (content_headers[i] + 8, header + 8))
 			break;
 	}
 	
diff --git a/gmime/gmime-parser.c b/gmime/gmime-parser.c
index 28b66bf..bbbfbc9 100644
--- a/gmime/gmime-parser.c
+++ b/gmime/gmime-parser.c
@@ -92,9 +92,9 @@ static void parser_init (GMimeParser *parser, GMimeStream *stream);
 static void parser_close (GMimeParser *parser);
 
 static GMimeObject *parser_construct_leaf_part (GMimeParser *parser, ContentType *content_type,
-						int *found);
+						gboolean toplevel, int *found);
 static GMimeObject *parser_construct_multipart (GMimeParser *parser, ContentType *content_type,
-						int *found);
+						gboolean toplevel, int *found);
 
 static GObjectClass *parent_class = NULL;
 
@@ -1570,15 +1570,16 @@ parser_scan_message_part (GMimeParser *parser, GMimeMessagePart *mpart, int *fou
 	message = g_mime_message_new (FALSE);
 	header = priv->headers;
 	while (header) {
-		g_mime_object_append_header ((GMimeObject *) message, header->name, header->value);
+		if (g_ascii_strncasecmp (header->name, "Content-", 8) != 0)
+			g_mime_object_append_header ((GMimeObject *) message, header->name, header->value);
 		header = header->next;
 	}
 	
 	content_type = parser_content_type (parser);
 	if (content_type_is_type (content_type, "multipart", "*"))
-		object = parser_construct_multipart (parser, content_type, found);
+		object = parser_construct_multipart (parser, content_type, TRUE, found);
 	else
-		object = parser_construct_leaf_part (parser, content_type, found);
+		object = parser_construct_leaf_part (parser, content_type, TRUE, found);
 	
 	content_type_destroy (content_type);
 	message->mime_part = object;
@@ -1592,7 +1593,7 @@ parser_scan_message_part (GMimeParser *parser, GMimeMessagePart *mpart, int *fou
 }
 
 static GMimeObject *
-parser_construct_leaf_part (GMimeParser *parser, ContentType *content_type, int *found)
+parser_construct_leaf_part (GMimeParser *parser, ContentType *content_type, gboolean toplevel, int *found)
 {
 	struct _GMimeParserPrivate *priv = parser->priv;
 	GMimeObject *object;
@@ -1613,7 +1614,8 @@ parser_construct_leaf_part (GMimeParser *parser, ContentType *content_type, int
 	
 	header = priv->headers;
 	while (header) {
-		g_mime_object_append_header (object, header->name, header->value);
+		if (!toplevel || !g_ascii_strncasecmp (header->name, "Content-", 8))
+			g_mime_object_append_header (object, header->name, header->value);
 		header = header->next;
 	}
 	
@@ -1732,9 +1734,9 @@ parser_scan_multipart_subparts (GMimeParser *parser, GMimeMultipart *multipart)
 		
 		content_type = parser_content_type (parser);
 		if (content_type_is_type (content_type, "multipart", "*"))
-			subpart = parser_construct_multipart (parser, content_type, &found);
+			subpart = parser_construct_multipart (parser, content_type, FALSE, &found);
 		else
-			subpart = parser_construct_leaf_part (parser, content_type, &found);
+			subpart = parser_construct_leaf_part (parser, content_type, FALSE, &found);
 		
 		g_mime_multipart_add (multipart, subpart);
 		content_type_destroy (content_type);
@@ -1745,7 +1747,7 @@ parser_scan_multipart_subparts (GMimeParser *parser, GMimeMultipart *multipart)
 }
 
 static GMimeObject *
-parser_construct_multipart (GMimeParser *parser, ContentType *content_type, int *found)
+parser_construct_multipart (GMimeParser *parser, ContentType *content_type, gboolean toplevel, int *found)
 {
 	struct _GMimeParserPrivate *priv = parser->priv;
 	GMimeMultipart *multipart;
@@ -1760,7 +1762,8 @@ parser_construct_multipart (GMimeParser *parser, ContentType *content_type, int
 	
 	header = priv->headers;
 	while (header) {
-		g_mime_object_append_header (object, header->name, header->value);
+		if (!toplevel || !g_ascii_strncasecmp (header->name, "Content-", 8))
+			g_mime_object_append_header (object, header->name, header->value);
 		header = header->next;
 	}
 	
@@ -1830,9 +1833,9 @@ parser_construct_part (GMimeParser *parser)
 	
 	content_type = parser_content_type (parser);
 	if (content_type_is_type (content_type, "multipart", "*"))
-		object = parser_construct_multipart (parser, content_type, &found);
+		object = parser_construct_multipart (parser, content_type, TRUE, &found);
 	else
-		object = parser_construct_leaf_part (parser, content_type, &found);
+		object = parser_construct_leaf_part (parser, content_type, TRUE, &found);
 	
 	content_type_destroy (content_type);
 	
@@ -1891,7 +1894,8 @@ parser_construct_message (GMimeParser *parser)
 				content_length = ULONG_MAX;
 		}
 		
-		g_mime_object_append_header ((GMimeObject *) message, header->name, header->value);
+		if (g_ascii_strncasecmp (header->name, "Content-", 8) != 0)
+			g_mime_object_append_header ((GMimeObject *) message, header->name, header->value);
 		header = header->next;
 	}
 	
@@ -1903,9 +1907,9 @@ parser_construct_message (GMimeParser *parser)
 	
 	content_type = parser_content_type (parser);
 	if (content_type_is_type (content_type, "multipart", "*"))
-		object = parser_construct_multipart (parser, content_type, &found);
+		object = parser_construct_multipart (parser, content_type, TRUE, &found);
 	else
-		object = parser_construct_leaf_part (parser, content_type, &found);
+		object = parser_construct_leaf_part (parser, content_type, TRUE, &found);
 	
 	content_type_destroy (content_type);
 	message->mime_part = object;
diff --git a/gmime/gmime-part.c b/gmime/gmime-part.c
index 4bcc8f9..f270d89 100644
--- a/gmime/gmime-part.c
+++ b/gmime/gmime-part.c
@@ -60,9 +60,7 @@ static void g_mime_part_finalize (GObject *object);
 static void mime_part_prepend_header (GMimeObject *object, const char *header, const char *value);
 static void mime_part_append_header (GMimeObject *object, const char *header, const char *value);
 static void mime_part_set_header (GMimeObject *object, const char *header, const char *value);
-static const char *mime_part_get_header (GMimeObject *object, const char *header);
 static gboolean mime_part_remove_header (GMimeObject *object, const char *header);
-static char *mime_part_get_headers (GMimeObject *object);
 static ssize_t mime_part_write_to_stream (GMimeObject *object, GMimeStream *stream);
 static void mime_part_encode (GMimeObject *object, GMimeEncodingConstraint constraint);
 
@@ -112,8 +110,6 @@ g_mime_part_class_init (GMimePartClass *klass)
 	object_class->append_header = mime_part_append_header;
 	object_class->remove_header = mime_part_remove_header;
 	object_class->set_header = mime_part_set_header;
-	object_class->get_header = mime_part_get_header;
-	object_class->get_headers = mime_part_get_headers;
 	object_class->write_to_stream = mime_part_write_to_stream;
 	object_class->encode = mime_part_encode;
 	
@@ -169,8 +165,11 @@ process_header (GMimeObject *object, const char *header, const char *value)
 	char *text;
 	guint i;
 	
+	if (g_ascii_strncasecmp (header, "Content-", 8) != 0)
+		return FALSE;
+	
 	for (i = 0; i < G_N_ELEMENTS (content_headers); i++) {
-		if (!g_ascii_strcasecmp (content_headers[i], header))
+		if (!g_ascii_strcasecmp (content_headers[i] + 8, header + 8))
 			break;
 	}
 	
@@ -204,11 +203,6 @@ process_header (GMimeObject *object, const char *header, const char *value)
 static void
 mime_part_prepend_header (GMimeObject *object, const char *header, const char *value)
 {
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a mime part */
-	if (g_ascii_strncasecmp ("Content-", header, 8) != 0)
-		return;
-	
 	if (!process_header (object, header, value))
 		GMIME_OBJECT_CLASS (parent_class)->prepend_header (object, header, value);
 	else
@@ -218,11 +212,6 @@ mime_part_prepend_header (GMimeObject *object, const char *header, const char *v
 static void
 mime_part_append_header (GMimeObject *object, const char *header, const char *value)
 {
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a mime part */
-	if (g_ascii_strncasecmp ("Content-", header, 8) != 0)
-		return;
-	
 	if (!process_header (object, header, value))
 		GMIME_OBJECT_CLASS (parent_class)->append_header (object, header, value);
 	else
@@ -232,69 +221,48 @@ mime_part_append_header (GMimeObject *object, const char *header, const char *va
 static void
 mime_part_set_header (GMimeObject *object, const char *header, const char *value)
 {
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a mime part */
-	if (g_ascii_strncasecmp ("Content-", header, 8) != 0)
-		return;
-	
 	if (!process_header (object, header, value))
 		GMIME_OBJECT_CLASS (parent_class)->set_header (object, header, value);
 	else
 		g_mime_header_list_set (object->headers, header, value);
 }
 
-static const char *
-mime_part_get_header (GMimeObject *object, const char *header)
-{
-	/* Make sure that the header is a Content-* header, else it
-           doesn't belong on a mime part */
-	if (!g_ascii_strncasecmp ("Content-", header, 8))
-		return GMIME_OBJECT_CLASS (parent_class)->get_header (object, header);
-	else
-		return NULL;
-}
-
 static gboolean
 mime_part_remove_header (GMimeObject *object, const char *header)
 {
 	GMimePart *mime_part = (GMimePart *) object;
 	guint i;
 	
-	for (i = 0; i < G_N_ELEMENTS (content_headers); i++) {
-		if (!g_ascii_strcasecmp (content_headers[i], header))
+	if (!g_ascii_strncasecmp (header, "Content-", 8)) {
+		for (i = 0; i < G_N_ELEMENTS (content_headers); i++) {
+			if (!g_ascii_strcasecmp (content_headers[i] + 8, header + 8))
+				break;
+		}
+		
+		switch (i) {
+		case HEADER_CONTENT_TRANSFER_ENCODING:
+			mime_part->encoding = GMIME_CONTENT_ENCODING_DEFAULT;
 			break;
-	}
-	
-	switch (i) {
-	case HEADER_CONTENT_TRANSFER_ENCODING:
-		mime_part->encoding = GMIME_CONTENT_ENCODING_DEFAULT;
-		break;
-	case HEADER_CONTENT_DESCRIPTION:
-		/* FIXME: we should decode this */
-		g_free (mime_part->content_description);
-		mime_part->content_description = NULL;
-		break;
-	case HEADER_CONTENT_LOCATION:
-		g_free (mime_part->content_location);
-		mime_part->content_location = NULL;
-		break;
-	case HEADER_CONTENT_MD5:
-		g_free (mime_part->content_md5);
-		mime_part->content_md5 = NULL;
-		break;
-	default:
-		break;
+		case HEADER_CONTENT_DESCRIPTION:
+			g_free (mime_part->content_description);
+			mime_part->content_description = NULL;
+			break;
+		case HEADER_CONTENT_LOCATION:
+			g_free (mime_part->content_location);
+			mime_part->content_location = NULL;
+			break;
+		case HEADER_CONTENT_MD5:
+			g_free (mime_part->content_md5);
+			mime_part->content_md5 = NULL;
+			break;
+		default:
+			break;
+		}
 	}
 	
 	return GMIME_OBJECT_CLASS (parent_class)->remove_header (object, header);
 }
 
-static char *
-mime_part_get_headers (GMimeObject *object)
-{
-	return GMIME_OBJECT_CLASS (parent_class)->get_headers (object);
-}
-
 static ssize_t
 write_content (GMimePart *part, GMimeStream *stream)
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]