Default charset for text mime types

Kuba Winnicki <[email protected]> Wed, 02 Feb 2005 15:25:52 +0100
Newsgroups gmane.comp.web.fnord
Organization Yn.pl
Message-ID <1107354352.13941.20.camel@yn>
Here's a simple patch to set default charset for text mime types.

Setting charset in mime headers is highly recommended by couple of
specs. Here's the one of them:

http://www.w3.org/TR/xhtml-media-types/

This is maybe not the most complete solution, as it doesn't force
charset on cgi generated content-type headers, nor does it parse static
files for charset recognition, but it works for me ;)

Additionally it defines a mime type for files with .xhtml extension.

==== 8< ====
--- fnord-1.9/httpd.c	Wed Feb  2 14:58:14 2005
+++ httpd~/httpd.c	Wed Feb  2 14:32:07 2005
@@ -24,6 +24,18 @@
 #include "byte.h"
 #include "scan.h"
 
+/* set default charset for text mime types here */
+
+#ifndef DEFAULT_TEXT_CHARSET
+#define DEFAULT_TEXT_CHARSET "iso-8859-1"
+#endif
+
+/* set default charset for xml mime types here */
+
+#ifndef DEFAULT_XML_CHARSET
+#define DEFAULT_XML_CHARSET "utf-8"
+#endif
+
 /* uncomment the following line to enable support for CGI */
 // #define CGI
 
@ -660,11 +685,11 @@
 }
 
 static char* encoding=0;
-static char* mimetype="text/plain";
+static char* mimetype="text/plain; charset=" DEFAULT_TEXT_CHARSET;
 
 static struct mimeentry { const char* name, *type; } mimetab[] = {
-  { "html",	"text/html" },
-  { "css",	"text/css" },
+  { "html",	"text/html; charset=" DEFAULT_TEXT_CHARSET },
+  { "css",	"text/css; charset=" DEFAULT_TEXT_CHARSET },
   { "dvi",	"application/x-dvi" },
   { "ps",	"application/postscript" },
   { "pdf",	"application/pdf" },
@@ -687,8 +712,9 @@
   { "js",	"application/x-javascript" },
   { "tar",	"application/x-tar" },
   { "zip",	"application/zip" },
-  { "dtd",	"text/xml" },
-  { "xml",	"text/xml" },
+  { "dtd",	"text/xml; charset=" DEFAULT_XML_CHARSET },
+  { "xml",	"text/xml; charset=" DEFAULT_XML_CHARSET },
+  { "xhtml",	"application/xhtml+xml; charset=" DEFAULT_XML_CHARSET },
   { "xbm",	"image/x-xbitmap" },
   { "xpm",	"image/x-xpixmap" },
   { "xwd",	"image/x-xwindowdump" },