[prev in list] [next in list] [prev in thread] [next in thread] 

List:       thttpd
Subject:    [THTTPD] [patch] allow externally configurable content type/encoding
From:       Davide Libenzi <davidel () xmailserver ! org>
Date:       2007-11-13 2:39:22
Message-ID: 200711130239.lAD2dTBV076262 () gate ! acme ! com
[Download RAW message or body]

I needed a way to allow thttpd to be made aware of new content type and 
encodings w/out rebuilding it. I cooked a quick patch that allows it to 
read, at boot time, files with the same format of the mime_* ones present 
inside the vanilla tarbal.
Two environment variables THTTPD_MIME_ENC_PATH and THTTPD_MIME_TYP_PATH 
tell thttpd the path of the two files to load (ENCodings, TYPes).
Those paths are post-chroot paths, in case ppl enabled that in their 
configs.
Only briefly tested on my server, and seems to be doing it.



- Davide


---
 libhttpd.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 52 insertions(+), 7 deletions(-)

Index: thttpd-2.25b/libhttpd.c
===================================================================
--- thttpd-2.25b.orig/libhttpd.c	2007-11-12 12:57:50.000000000 -0800
+++ thttpd-2.25b/libhttpd.c	2007-11-12 18:25:35.000000000 -0800
@@ -2482,14 +2482,19 @@
     char* val;
     size_t val_len;
     };
-static struct mime_entry enc_tab[] = {
+static struct mime_entry senc_tab[] = {
 #include "mime_encodings.h"
     };
-static const int n_enc_tab = sizeof(enc_tab) / sizeof(*enc_tab);
-static struct mime_entry typ_tab[] = {
+static const int n_senc_tab = sizeof(senc_tab) / sizeof(*senc_tab);
+static struct mime_entry styp_tab[] = {
 #include "mime_types.h"
     };
-static const int n_typ_tab = sizeof(typ_tab) / sizeof(*typ_tab);
+static const int n_styp_tab = sizeof(styp_tab) / sizeof(*styp_tab);
+
+static struct mime_entry *enc_tab;
+static int n_enc_tab;
+static struct mime_entry *typ_tab;
+static int n_typ_tab;
 
 
 /* qsort comparison routine - declared old-style on purpose, for portability. */
@@ -2501,15 +2506,55 @@
     return strcmp( a->ext, b->ext );
     }
 
+static struct mime_entry *load_table(const char *fname,
+				     struct mime_entry *fixed, int nfixed,
+				     int *nents) {
+	int n;
+	FILE *file;
+	struct mime_entry *mtab;
+	char *ext, *val;
+	char buf[512];
+
+	mtab = fixed;
+	*nents = nfixed;
+	if (fname != NULL && (file = fopen(fname, "r")) != NULL) {
+		syslog(LOG_NOTICE, "loading table '%s'\n", fname);
+		for (n = 0; fgets(buf, sizeof(buf), file) != NULL; n++);
+		rewind(file);
+		if ((mtab = (struct mime_entry *)
+		     malloc((n + nfixed) * sizeof(struct mime_entry))) == NULL) {
+			fclose(file);
+			mtab = fixed;
+			goto sort_ents;
+		}
+		memcpy(mtab, fixed, nfixed * sizeof(struct mime_entry));
+		while (fgets(buf, sizeof(buf), file) != NULL) {
+			if ((ext = strtok(buf, " \t\r\n")) == NULL ||
+			    (val = strtok(NULL, " \t\r\n")) == NULL ||
+			    *ext == '#')
+				continue;
+			mtab[*nents].ext = strdup(ext);
+			mtab[*nents].val = strdup(val);
+			(*nents)++;
+		}
+		fclose(file);
+	} else if (fname != NULL)
+		syslog(LOG_NOTICE, "load_table file open failed with '%s'\n", fname);
+
+	sort_ents:
+	qsort(mtab, *nents, sizeof(*mtab), ext_compare);
+	return mtab;
+}
 
 static void
 init_mime( void )
     {
     int i;
 
-    /* Sort the tables so we can do binary search. */
-    qsort( enc_tab, n_enc_tab, sizeof(*enc_tab), ext_compare );
-    qsort( typ_tab, n_typ_tab, sizeof(*typ_tab), ext_compare );
+    enc_tab = load_table(getenv("THTTPD_MIME_ENC_PATH"), senc_tab, n_senc_tab,
+			 &n_enc_tab);
+    typ_tab = load_table(getenv("THTTPD_MIME_TYP_PATH"), styp_tab, n_styp_tab,
+			 &n_typ_tab);
 
     /* Fill in the lengths. */
     for ( i = 0; i < n_enc_tab; ++i )
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic