File: filetype.patch

package info (click to toggle)
publicfile 0.52-14
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 900 kB
  • sloc: ansic: 3,903; makefile: 472; sh: 296
file content (63 lines) | stat: -rw-r--r-- 2,680 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
X-Comment: see https://dep.debian.net/deps/dep3/ for format
Description: File Types from the Environment
 Out of the box, publicfile serves content using a hardcoded mapping from file
 extension to MIME type. The mapping was chosen in the late 90s, and is missing
 most modern content types. (It also has some idiosyncracies, such as requiring
 JPEG files to be named .jpeg instead of the more common .jpg.)
 .
 Uwe Ohse fixed this with a patch that teaches publicfile to consult its
 environment, instead. If it finds an unknown file extension .ext, it looks for
 an environment variable called CT_ext. If it exists, publicflie will use its
 contents as the MIME type.
 .
 Since djb’s daemontools provide a program, envdir, that configures a program’s
 environment using files in a directory, you can now create your content mapping
 as a directory of small text files instead of having to modify publicfile
 itself.
 .
 (taken verbatim from https://cliffle.com/blog/publicfile-patches/, by Cliff L.
 Biffle.)
 .
 The patch is linked to from https://www.ohse.de/uwe/patches.html , on that page
 it says: "Allows publicfile to guess the MIME content type of a file.
 Description: If the file extension is ".xY", then the content of the
 environment variable "CT_xY" be used to determine the file type. If this is not
 found then "text/plain" will be used.  1999-12-06. Seems to work."
Origin: https://www.ohse.de/uwe/patches/publicfile-0.52-filetype-diff
Author: Uwe Ohse

diff -u publicfile-0.52.old/filetype.c publicfile-0.52/filetype.c
--- publicfile-0.52.old/filetype.c Mon Dec  6 10:43:36 1999
+++ publicfile-0.52/filetype.c     Mon Dec  6 10:50:36 1999
@@ -1,5 +1,6 @@
 #include "filetype.h"
 #include "str.h"
+#include "env.h"
 
 void filetype(char *fn,stralloc *contenttype)
 {
@@ -22,7 +23,7 @@
       if (!stralloc_append(contenttype,&ch)) _exit(21);
     }
   else {
-    result = "text/plain";
+    result = 0;
     if (str_equal(x,".html")) result = "text/html";
     else if (str_equal(x,".gz")) result = "application/x-gzip";
     else if (str_equal(x,".dvi")) result = "application/x-dvi";
@@ -32,6 +33,15 @@
     else if (str_equal(x,".jpeg")) result = "image/jpeg";
     else if (str_equal(x,".png")) result = "image/png";
     else if (str_equal(x,".mpeg")) result = "video/mpeg";
+	if (!result) {
+		stralloc envname = {0};
+		if (!stralloc_copys(&envname,"CT_")) _exit(21);
+		if (!stralloc_cats(&envname,x+1)) _exit(21);
+		if (!stralloc_0(&envname)) _exit(21);
+		result=env_get(envname.s);
+		alloc_free(envname.s); /* is this the right function */
+	}
+	if (!result) result="text/plain";
 
     if (!stralloc_cats(contenttype,result)) _exit(21);
   }