File: filetype.c

package info (click to toggle)
publicfile 0.52-13
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 896 kB
  • sloc: ansic: 3,903; makefile: 468; sh: 296
file content (50 lines) | stat: -rw-r--r-- 1,558 bytes parent folder | download | duplicates (2)
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
#include "filetype.h"
#include "str.h"
#include "env.h"

void filetype(char *fn,stralloc *contenttype)
{
  char *x;
  char *result;
  int i;
  char ch;

  if (!stralloc_copys(contenttype,"Content-Type: ")) _exit(21);

  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)) _exit(21);
    }
  else {
    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";
    else if (str_equal(x,".ps")) result = "application/postscript";
    else if (str_equal(x,".pdf")) result = "application/pdf";
    else if (str_equal(x,".gif")) result = "image/gif";
    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);
  }

  if (!stralloc_cats(contenttype,"\r\n")) _exit(21);
}