-
Notifications
You must be signed in to change notification settings - Fork 1
/
filetype.c
71 lines (66 loc) · 2.62 KB
/
filetype.c
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
64
65
66
67
68
69
70
71
#include "str.h"
#include "log.h"
#include "case.h"
#include "filetype.h"
int filetype(const char *fn, stralloc *contenttype) {
const char *x;
const char *result;
long long i;
char ch;
int ret = 0;
log_t3("filetype(fn = ", fn, ")");
if (!stralloc_copys(contenttype, "")) goto cleanup;
x = fn + str_rchr(fn, '.');
if (x[str_chr(x, '=')]) {
for (i = 0; (i < 100) && (ch = x[i]); ++i) {
if ((ch != '=') && (ch != '-') && (ch != ':'))
if ((ch < 'a') || (ch > 'z'))
if ((ch < '0') || (ch > '9')) continue;
if (ch == '=') ch = '/';
if (ch == ':') ch = '.';
if (!stralloc_append(contenttype, &ch)) goto cleanup;
}
}
else {
result = "application/octet-stream";
/* web content */
if (case_equals(x, ".html")) result = "text/html";
if (case_equals(x, ".css")) result = "text/css";
if (case_equals(x, ".js")) result = "application/javascript";
if (case_equals(x, ".xml")) result = "text/xml";
/* archive + commpressed */
if (case_equals(x, ".gz")) result = "application/gzip";
if (case_equals(x, ".bz2")) result = "application/x-bzip2";
if (case_equals(x, ".xz")) result = "application/x-xz";
/* documents */
if (case_equals(x, ".dvi")) result = "application/x-dvi";
if (case_equals(x, ".ps")) result = "application/postscript";
if (case_equals(x, ".pdf")) result = "application/pdf";
/* images */
if (case_equals(x, ".ico")) result = "image/x-icon";
if (case_equals(x, ".gif")) result = "image/gif";
if (case_equals(x, ".jpg")) result = "image/jpeg";
if (case_equals(x, ".jpeg")) result = "image/jpeg";
if (case_equals(x, ".png")) result = "image/png";
/* video */
if (case_equals(x, ".mpg")) result = "video/mpeg";
if (case_equals(x, ".mpeg")) result = "video/mpeg";
if (case_equals(x, ".flv")) result = "video/x-flv";
if (case_equals(x, ".avi")) result = "video/x-msvideo";
if (case_equals(x, ".mp4")) result = "video/mp4";
if (case_equals(x, ".ogg")) result = "video/ogg";
if (case_equals(x, ".mov")) result = "video/quicktime";
if (case_equals(x, ".webm")) result = "video/webm";
if (!stralloc_cats(contenttype, result)) goto cleanup;
}
ret = 1;
cleanup:
if (!stralloc_0(contenttype)) {
log_t1("filetype() = 0");
return 0;
}
--contenttype->len;
log_t6("filetype(fn = ", fn, ", contenttype = ", contenttype->s,
") = ", log_num(ret));
return ret;
}