summaryrefslogtreecommitdiff
path: root/appl/svc/httpd/contents.b
blob: 5e57dd250c1f786163ea49be2f297a9e404a2971 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
implement Contents;

include "sys.m";
	sys: Sys;
	dbg_log : ref Sys->FD;
include "draw.m";

include "bufio.m";
	bufio: Bufio;
Iobuf : import bufio;
	
include "contents.m";

include "cache.m";

include "httpd.m";

include "string.m";
	str : String;

Suffix: adt{
	suffix : string;
	generic : string;
	specific : string;
	encoding : string;
};

suffixes: list of Suffix;

#internal functions...
parsesuffix : fn(nil:string): (int,Suffix);

mkcontent(generic,specific : string): ref Content
{
	c:= ref Content; 	
	c.generic = generic;
	c.specific = specific;
	c.q = real 1;
	return c;
}

badmod(m: string)
{
	sys->fprint(stderr(), "contents: cannot load %s: %r\n", m);
	raise "fail:bad module";
}

contentinit(log: ref Sys->FD)
{
	if(suffixes != nil)
		return;

	sys = load Sys Sys->PATH;

	bufio = load Bufio Bufio->PATH;
	if (bufio == nil) badmod(Bufio->PATH);

	str = load String String->PATH;
	if (str == nil) badmod(String->PATH);

	iob := bufio->open(Httpd->HTTP_SUFF, bufio->OREAD);
	if (iob==nil) {
		sys->fprint(stderr(), "contents: cannot open %s: %r\n", Httpd->HTTP_SUFF);
		raise "fail:no suffix file";;
	}
	while((s := iob.gets('\n'))!=nil) {
		(i, su) := parsesuffix(s);
		if (i != 0)
			suffixes =  su :: suffixes;
	}
	dbg_log = log;
}

# classify by file name extensions

uriclass(name : string): (ref Content, ref Content)
{
	s : Suffix;
	typ, enc: ref Content;
	p : string;
	lis := suffixes;
	typ=nil;
	enc=nil;
	uri:=name;
	(nil,p) = str->splitr(name,"/");
	if (p!=nil) name=p;

	if(str->in('.',name)){
		(nil,p) = str->splitl(name,".");
		for(s = hd lis; lis!=nil; lis = tl lis){
			if(p == s.suffix){	
				if(s.generic != nil && typ==nil)
					typ = mkcontent(s.generic, s.specific);
				if(s.encoding != nil && enc==nil)
					enc = mkcontent(s.encoding, "");
			}
		s = hd lis;
		}
	}
	if(typ == nil && enc == nil){
		buff := array[64] of byte;
		fd := sys->open(uri, sys->OREAD);
		n := sys->read(fd, buff, len buff);
		if(n > 0){
			tmp := string buff[0:n];
			(typ, enc) = dataclass(tmp);
		}
	}
	return (typ, enc);
}


parsesuffix(line: string): (int, Suffix)
{
	s : Suffix;	
	if (str->in('#',line))
		(line,nil) = str->splitl(line, "#");
	if (line!=nil){
		(n,slist):=sys->tokenize(line,"\n\t ");
		if (n!=4 && n!=0){
			if (dbg_log!=nil)
				sys->fprint(dbg_log,"Error in suffixes file!, n=%d\n",n);
			sys->print("Error in suffixes file!, n=%d\n",n);
			exit;
		}
		s.suffix = hd slist;
		slist = tl slist;
		s.generic = hd slist;
		if (s.generic == "-") s.generic="";	
		slist = tl slist;
		s.specific = hd slist;
		if (s.specific == "-") s.specific="";	
		slist = tl slist;
		s.encoding = hd slist;
		if (s.encoding == "-") s.encoding="";
		
	}
	if (((s.generic ==  "")||(s.specific ==  "")) && s.encoding=="")
		return (0,s);
	return (1,s);
}

#classify by initial contents of file
dataclass(buf : string): (ref Content,ref Content)
{
	c,n : int;
	c=0;
	n = len buf;
	for(; n > 0; n --){		
		if(buf[c] < 16r80)
			if(buf[c] < 32 && buf[c] != '\n' && buf[c] != '\r' 
					&& buf[c] != '\t' && buf[c] != '\v')
				return (nil,nil);
		c++;		
	}
	return (mkcontent("text", "plain"),nil);
}

checkcontent(me: ref Content,oks :list of ref Content, clist : string): int
{
	ok:=oks;
	try : ref Content;
	if(oks == nil || me == nil)
		return 1;
	for(; ok != nil; ok = tl ok){
		try = hd ok;
		if((try.generic==me.generic || try.generic=="*")
		&& (try.specific==me.specific || try.specific=="*")){
			return 1;
		}
	}

	sys->fprint(dbg_log,"%s/%s not found", 
				me.generic, me.specific);
	logcontent(clist, oks);
	return 1;
}

logcontent(name : string, c : list of ref Content)
{
	buf : string;
	if (dbg_log!=nil){
		for(; c!=nil; c = tl c)
			buf+=sys->sprint("%s/%s ", (hd c).generic,(hd c).specific);
		sys->fprint(dbg_log,"%s: %s: %s", "client", name, buf);
	}
}

stderr(): ref Sys->FD
{
	return sys->fildes(2);
}