Redirect directory requests to dirplex to always include a trailing slash.
[ashd.git] / src / patplex.c
CommitLineData
326e08fc
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
23#include <signal.h>
24#include <errno.h>
25#include <ctype.h>
26#include <regex.h>
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <utils.h>
32#include <log.h>
33#include <req.h>
34#include <proc.h>
35#include <resp.h>
06c1a718 36#include <cf.h>
326e08fc
FT
37
38#define PAT_REST 0
39#define PAT_URL 1
40#define PAT_METHOD 2
41#define PAT_HEADER 3
42#define PAT_ALL 4
43#define PAT_DEFAULT 5
44
45#define PATFL_MSS 1
46
47struct config {
48 struct child *children;
49 struct pattern *patterns;
50};
51
326e08fc
FT
52struct rule {
53 int type;
54 int fl;
55 char *header;
56 regex_t *pattern;
57};
58
59struct pattern {
60 struct pattern *next;
61 char *childnm;
62 struct rule **rules;
63 char *restpat;
64};
65
66static struct config *config;
67
68static void freepattern(struct pattern *pat)
69{
70 struct rule **rule;
71
72 for(rule = pat->rules; *rule; rule++) {
73 if((*rule)->header != NULL)
74 free((*rule)->header);
75 if((*rule)->pattern != NULL) {
76 regfree((*rule)->pattern);
77 free((*rule)->pattern);
78 }
79 free(*rule);
80 }
81 if(pat->childnm != NULL)
82 free(pat->childnm);
83 free(pat);
84}
85
326e08fc
FT
86static struct child *getchild(struct config *cf, char *name)
87{
88 struct child *ch;
89
90 for(ch = cf->children; ch; ch = ch->next) {
91 if(!strcmp(ch->name, name))
92 break;
93 }
94 return(ch);
95}
96
97static struct rule *newrule(struct pattern *pat)
98{
99 int i;
100 struct rule *rule;
101
102 for(i = 0; pat->rules[i]; i++);
103 pat->rules = srealloc(pat->rules, sizeof(*pat->rules) * (i + 2));
104 rule = pat->rules[i] = szmalloc(sizeof(*rule));
105 pat->rules[i + 1] = NULL;
106 return(rule);
107}
108
109static struct pattern *newpattern(void)
110{
111 struct pattern *pat;
112
113 omalloc(pat);
114 pat->rules = szmalloc(sizeof(*pat->rules));
115 return(pat);
116}
117
118static regex_t *regalloc(char *regex, int flags)
119{
120 regex_t *ret;
121 int res;
122 char errbuf[256];
123
124 omalloc(ret);
125 if((res = regcomp(ret, regex, flags | REG_EXTENDED)) != 0) {
126 regerror(res, ret, errbuf, sizeof(errbuf));
127 flog(LOG_WARNING, "%s: %s", regex, errbuf);
128 free(ret);
129 return(NULL);
130 }
131 return(ret);
132}
133
06c1a718 134static struct pattern *parsepattern(struct cfstate *s)
326e08fc 135{
326e08fc 136 struct pattern *pat;
06c1a718 137 int sl;
326e08fc
FT
138 struct rule *rule;
139 regex_t *regex;
140 int rxfl;
141
06c1a718
FT
142 if(!strcmp(s->argv[0], "match")) {
143 s->expstart = 1;
144 pat = newpattern();
145 } else {
326e08fc 146 return(NULL);
06c1a718
FT
147 }
148
149 sl = s->lno;
150 while(1) {
151 getcfline(s);
152 if(!strcmp(s->argv[0], "point") ||
153 !strcmp(s->argv[0], "url") ||
154 !strcmp(s->argv[0], "method")) {
155 if(s->argc < 2) {
156 flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
326e08fc 157 continue;
326e08fc 158 }
06c1a718
FT
159 if(s->argc >= 3) {
160 if(strchr(s->argv[2], 'i'))
161 rxfl |= REG_ICASE;
326e08fc 162 }
06c1a718
FT
163 if((regex = regalloc(s->argv[1], rxfl)) == NULL) {
164 flog(LOG_WARNING, "%s:%i: invalid regex for `%s' match", s->file, s->lno, s->argv[0]);
165 continue;
166 }
167 rule = newrule(pat);
168 if(!strcmp(s->argv[0], "point"))
169 rule->type = PAT_REST;
170 else if(!strcmp(s->argv[0], "url"))
171 rule->type = PAT_URL;
172 else if(!strcmp(s->argv[0], "method"))
173 rule->type = PAT_METHOD;
174 rule->pattern = regex;
175 if(s->argc >= 3) {
176 if(strchr(s->argv[2], 's'))
177 rule->fl |= PATFL_MSS;
178 }
179 } else if(!strcmp(s->argv[0], "header")) {
180 if(s->argc < 3) {
181 flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
182 continue;
183 }
184 if(s->argc >= 4) {
185 if(strchr(s->argv[3], 'i'))
186 rxfl |= REG_ICASE;
187 }
188 if((regex = regalloc(s->argv[2], rxfl)) == NULL) {
189 flog(LOG_WARNING, "%s:%i: invalid regex for `header' match", s->file, s->lno);
190 continue;
191 }
192 rule = newrule(pat);
193 rule->type = PAT_HEADER;
194 rule->header = sstrdup(s->argv[1]);
195 rule->pattern = regex;
196 if(s->argc >= 4) {
197 if(strchr(s->argv[3], 's'))
198 rule->fl |= PATFL_MSS;
326e08fc 199 }
06c1a718
FT
200 } else if(!strcmp(s->argv[0], "all")) {
201 newrule(pat)->type = PAT_ALL;
202 } else if(!strcmp(s->argv[0], "default")) {
203 newrule(pat)->type = PAT_DEFAULT;
204 } else if(!strcmp(s->argv[0], "handler")) {
205 if(s->argc < 2) {
206 flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
207 continue;
208 }
209 if(pat->childnm != NULL)
210 free(pat->childnm);
211 pat->childnm = sstrdup(s->argv[1]);
212 } else if(!strcmp(s->argv[0], "restpat")) {
213 if(s->argc < 2) {
214 flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
215 continue;
216 }
217 if(pat->restpat != NULL)
218 free(pat->restpat);
219 pat->restpat = sstrdup(s->argv[1]);
220 } else if(!strcmp(s->argv[0], "end") || !strcmp(s->argv[0], "eof")) {
221 break;
222 } else {
223 flog(LOG_WARNING, "%s:%i: unknown directive `%s' in pattern declaration", s->file, s->lno, s->argv[0]);
326e08fc 224 }
06c1a718
FT
225 }
226
227 if(pat->rules[0] == NULL) {
228 flog(LOG_WARNING, "%s:%i: missing rules in match declaration", s->file, sl);
229 freepattern(pat);
230 return(NULL);
231 }
232 if(pat->childnm == NULL) {
233 flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
234 freepattern(pat);
235 return(NULL);
236 }
237 return(pat);
238}
239
240static struct config *readconfig(char *filename)
241{
242 struct cfstate *s;
243 struct config *cf;
244 struct child *ch;
245 struct pattern *pat;
246 FILE *in;
326e08fc 247
06c1a718
FT
248 if((in = fopen(filename, "r")) == NULL) {
249 flog(LOG_WARNING, "%s: %s", filename, strerror(errno));
250 return(NULL);
251 }
252 s = mkcfparser(in, filename);
253 omalloc(cf);
254
255 while(1) {
256 getcfline(s);
257 if((ch = parsechild(s)) != NULL) {
258 ch->next = cf->children;
259 cf->children = ch;
260 } else if((pat = parsepattern(s)) != NULL) {
261 pat->next = cf->patterns;
262 cf->patterns = pat;
263 } else if(!strcmp(s->argv[0], "eof")) {
264 break;
265 } else {
266 flog(LOG_WARNING, "%s:%i: unknown directive `%s'", s->file, s->lno, s->argv[0]);
267 }
268 }
269
270 freecfparser(s);
271 fclose(in);
326e08fc
FT
272 return(cf);
273}
274
275static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
276{
277 char *p, *p2, *hdr;
278 int mc;
279 struct charbuf buf;
280
281 if(mstr == NULL)
282 mc = 0;
283 else
284 for(mc = 0; mstr[mc]; mc++);
285 bufinit(buf);
286 for(p = pat->restpat; *p; ) {
287 if(*p == '$') {
288 p++;
289 if((*p >= '0') && (*p <= '9')) {
290 if(*p - '0' < mc)
291 bufcatstr(buf, mstr[*p - '0']);
292 p++;
293 } else if(*p == '_') {
294 bufcatstr(buf, req->rest);
295 p++;
296 } else if(*p == '$') {
297 bufadd(buf, '$');
298 p++;
299 } else if(*p == '{') {
06c1a718 300 if((p2 = strchr(p, '}')) == NULL) {
326e08fc
FT
301 p++;
302 } else {
06c1a718 303 hdr = getheader(req, sprintf3("%.*s", p2 - p - 1, p + 1));
326e08fc
FT
304 if(hdr)
305 bufcatstr(buf, hdr);
306 }
307 } else if(!*p) {
308 }
309 } else {
310 bufadd(buf, *(p++));
311 }
312 }
313 bufadd(buf, 0);
314 replrest(req, buf.b);
315 buffree(buf);
316}
317
318static char *findmatch(struct config *cf, struct hthead *req, int trydefault)
319{
320 int i, o;
321 struct pattern *pat;
322 struct rule *rule;
323 int rmo, matched;
324 char *pstr;
325 char **mstr;
326 regmatch_t gr[10];
327
328 mstr = NULL;
329 for(pat = cf->patterns; pat != NULL; pat = pat->next) {
330 rmo = -1;
331 for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
332 matched = 0;
333 if(rule->type == PAT_REST) {
334 if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
335 rmo = gr[0].rm_eo;
336 else
337 break;
338 } else if(rule->type == PAT_URL) {
339 if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
340 break;
341 } else if(rule->type == PAT_METHOD) {
342 if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
343 break;
344 } else if(rule->type == PAT_HEADER) {
345 if(!(pstr = getheader(req, rule->header)))
346 break;
347 if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
348 break;
349 } else if(rule->type == PAT_ALL) {
350 } else if(rule->type == PAT_DEFAULT) {
351 if(!trydefault)
352 break;
353 }
354 if(matched && (rule->fl & PATFL_MSS)) {
355 if(mstr) {
356 flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
357 freeca(mstr);
358 }
359 for(o = 0; o < 10; o++) {
360 if(gr[o].rm_so < 0)
361 break;
362 }
363 mstr = szmalloc((o + 1) * sizeof(*mstr));
364 for(o = 0; o < 10; o++) {
365 if(gr[o].rm_so < 0)
366 break;
367 mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
368 memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
369 mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
370 }
371 }
372 }
373 if(!rule) {
374 if(pat->restpat) {
375 exprestpat(req, pat, mstr);
376 } else if(rmo != -1) {
377 replrest(req, req->rest + rmo);
378 }
379 if(mstr)
380 freeca(mstr);
381 return(pat->childnm);
382 }
383 if(mstr) {
384 freeca(mstr);
385 mstr = NULL;
386 }
387 }
388 return(NULL);
389}
390
326e08fc
FT
391static void serve(struct hthead *req, int fd)
392{
393 char *chnm;
394 struct child *ch;
395
396 if(((chnm = findmatch(config, req, 0)) == NULL) && ((chnm = findmatch(config, req, 1)) == NULL)) {
397 simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
398 return;
399 }
400 if((ch = getchild(config, chnm)) == NULL) {
401 flog(LOG_ERR, "child %s requested, but was not declared", chnm);
402 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", chnm);
403 return;
404 }
405
06c1a718
FT
406 if(childhandle(ch, req, fd))
407 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
326e08fc
FT
408}
409
410int main(int argc, char **argv)
411{
412 struct hthead *req;
413 int fd;
414
415 if(argc < 2) {
416 flog(LOG_ERR, "usage: patplex CONFIGFILE");
417 exit(1);
418 }
419 config = readconfig(argv[1]);
420 signal(SIGCHLD, SIG_IGN);
421 while(1) {
422 if((fd = recvreq(0, &req)) < 0) {
423 if(errno != 0)
424 flog(LOG_ERR, "recvreq: %s", strerror(errno));
425 break;
426 }
427 serve(req, fd);
428 freehthead(req);
429 close(fd);
430 }
431 return(0);
432}