2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
29 #include <sys/signal.h>
45 static void chinit(void *idata)
50 /* This should never be able to fail other than for critical
51 * I/O errors or some such, since the path has already been
58 static void childerror(struct hthead *req, int fd)
61 simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
63 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
66 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
73 for(head = pat->headers; head != NULL; head = head->next) {
74 headrmheader(req, head->name);
75 headappheader(req, head->name, head->value);
77 if(!strncmp(path, "./", 2) && path[2])
80 headappheader(req, "X-Ash-File", path);
81 stdforkserve(pat->fchild, req, fd, NULL, NULL);
83 if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
84 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
85 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
88 if((twd = ccf?ccf->path:NULL) != NULL) {
89 if(!strcmp(twd, ".")) {
91 } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
92 /* Should be an impossible case under the current (and
93 * foreseeable) scheme. */
94 simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
97 path = path + strlen(twd) + 1;
100 headappheader(req, "X-Ash-File", path);
101 if(childhandle(ch, req, fd, chinit, twd))
106 static void handle404(struct hthead *req, int fd, char *path)
112 char tmp[strlen(path) + 1];
114 if((pat = findmatch(tmp, 0, PT_NOTFOUND)) != NULL) {
115 handle(req, fd, tmp, pat);
117 ch = findchild(tmp, ".notfound", &ccf);
118 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
123 static void handlefile(struct hthead *req, int fd, char *path)
127 if((pat = findmatch(path, 0, PT_FILE)) == NULL) {
128 handle404(req, fd, path);
131 handle(req, fd, path, pat);
134 static char *findfile(char *path, char *name, struct stat *sb)
143 if((dir = opendir(path)) == NULL)
146 while((dent = readdir(dir)) != NULL) {
147 /* Ignore backup files.
148 * XXX: There is probably a better and more extensible way to
150 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
152 if((p = strchr(dent->d_name, '.')) == NULL)
154 if(p - dent->d_name != strlen(name))
156 if(strncmp(dent->d_name, name, strlen(name)))
158 fp = sprintf3("%s/%s", path, dent->d_name);
161 if(!S_ISREG(sb->st_mode))
170 static void handledir(struct hthead *req, int fd, char *path)
175 char *inm, *ipath, *cpath;
178 cpath = sprintf2("%s/", path);
179 cfs = getconfigs(cpath);
180 for(i = 0; cfs[i] != NULL; i++) {
181 if(cfs[i]->index != NULL) {
182 for(o = 0; cfs[i]->index[o] != NULL; o++) {
183 inm = cfs[i]->index[o];
184 ipath = sprintf2("%s/%s", path, inm);
185 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
186 handlefile(req, fd, ipath);
192 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
193 handlefile(req, fd, ipath);
201 if((pat = findmatch(cpath, 0, PT_DIR)) != NULL) {
202 handle(req, fd, cpath, pat);
205 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
211 static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final);
213 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el, int final)
221 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
222 if(S_ISDIR(sb.st_mode)) {
224 stdredir(req, fd, 301, sprintf3("%s/", el));
227 newpath = sprintf2("%s/%s", path, el);
228 rv = checkpath(req, fd, newpath, rest + 1, final);
231 } else if(S_ISREG(sb.st_mode)) {
232 newpath = sprintf2("%s/%s", path, el);
234 handlefile(req, fd, newpath);
238 handle404(req, fd, sprintf3("%s/", path));
241 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
243 handlefile(req, fd, newpath);
250 static int checkdir(struct hthead *req, int fd, char *path, char *rest)
252 char *cpath, *newpath;
253 struct config *cf, *ccf;
258 cf = getconfig(path);
259 if((cf->capture != NULL) && (cf->caproot || !cf->path || strcmp(cf->path, "."))) {
260 cpath = sprintf2("%s/", path);
261 if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
263 flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
264 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
271 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
275 if(cf->reparse != NULL) {
276 newpath = (cf->reparse[0] == '/')?sstrdup(cf->reparse):sprintf2("%s/%s", path, cf->reparse);
277 rv = stat(newpath, &sb);
278 if(!rv && S_ISDIR(sb.st_mode)) {
279 rv = checkpath(req, fd, newpath, rest, !cf->parsecomb);
280 } else if(!rv && S_ISREG(sb.st_mode)) {
282 handlefile(req, fd, newpath);
294 static int checkpath(struct hthead *req, int fd, char *path, char *rest, int final)
302 if(!strncmp(path, "./", 2))
304 if(checkdir(req, fd, path, rest))
307 if((p = strchr(rest, '/')) == NULL) {
308 el = unquoteurl(rest);
311 char buf[p - rest + 1];
312 memcpy(buf, rest, p - rest);
314 el = unquoteurl(buf);
318 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
322 if(strchr(el, '/') || (!*el && *rest)) {
328 handledir(req, fd, path);
332 rv = checkentry(req, fd, path, rest, el, final);
336 handle404(req, fd, sprintf3("%s/", path));
344 static void serve(struct hthead *req, int fd)
347 checkpath(req, fd, ".", req->rest, 1);
350 static void chldhandler(int sig)
355 while((pid = waitpid(-1, &st, WNOHANG)) > 0) {
357 flog(LOG_WARNING, "child process %i dumped core", pid);
361 static void sighandler(int sig)
365 static void usage(FILE *out)
367 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
370 int main(int argc, char **argv)
374 char *gcf, *lcf, *clcf;
380 while((c = getopt(argc, argv, "hNc:")) >= 0) {
396 if(argc - optind < 1) {
401 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
402 gconfig = readconfig(gcf);
407 if(strchr(lcf, '/') == NULL) {
408 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
409 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
412 if((lconfig = readconfig(clcf)) == NULL)
416 if((lconfig = readconfig(lcf)) == NULL)
420 if(chdir(argv[optind])) {
421 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
424 signal(SIGCHLD, chldhandler);
425 signal(SIGPIPE, sighandler);
427 if((fd = recvreq(0, &req)) < 0) {
429 flog(LOG_ERR, "recvreq: %s", strerror(errno));