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 handle(struct hthead *req, int fd, char *path, struct pattern *pat)
64 if(!strncmp(path, "./", 2))
67 headappheader(req, "X-Ash-File", path);
68 stdforkserve(pat->fchild, req, fd, NULL, NULL);
70 if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
71 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
72 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
75 if((twd = ccf?ccf->path:NULL) != NULL) {
76 if(!strcmp(twd, ".")) {
78 } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
79 /* Should be an impossible case under the current (and
80 * foreseeable) scheme. */
81 simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
84 path = path + strlen(twd) + 1;
87 headappheader(req, "X-Ash-File", path);
88 if(childhandle(ch, req, fd, chinit, twd))
89 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
93 static void handle404(struct hthead *req, int fd, char *path)
100 ch = findchild(tmp, ".notfound", &ccf);
101 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
102 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
106 static void handlefile(struct hthead *req, int fd, char *path)
110 if((pat = findmatch(path, 0, 0)) == NULL) {
111 handle404(req, fd, path);
114 handle(req, fd, path, pat);
117 static char *findfile(char *path, char *name, struct stat *sb)
126 if((dir = opendir(path)) == NULL)
129 while((dent = readdir(dir)) != NULL) {
130 /* Ignore backup files.
131 * XXX: There is probably a better and more extensible way to
133 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
135 if((p = strchr(dent->d_name, '.')) == NULL)
137 if(p - dent->d_name != strlen(name))
139 if(strncmp(dent->d_name, name, strlen(name)))
141 fp = sprintf3("%s/%s", path, dent->d_name);
144 if(!S_ISREG(sb->st_mode))
153 static void handledir(struct hthead *req, int fd, char *path)
158 char *inm, *ipath, *cpath;
161 cpath = sprintf2("%s/", path);
162 cfs = getconfigs(cpath);
163 for(i = 0; cfs[i] != NULL; i++) {
164 if(cfs[i]->index != NULL) {
165 for(o = 0; cfs[i]->index[o] != NULL; o++) {
166 inm = cfs[i]->index[o];
167 ipath = sprintf2("%s/%s", path, inm);
168 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
169 handlefile(req, fd, ipath);
175 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
176 handlefile(req, fd, ipath);
184 if((pat = findmatch(cpath, 0, 1)) != NULL) {
185 handle(req, fd, cpath, pat);
188 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
194 static int checkpath(struct hthead *req, int fd, char *path, char *rest);
196 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
203 handle404(req, fd, sprintf3("%s/", path));
206 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
207 if(S_ISDIR(sb.st_mode)) {
209 stdredir(req, fd, 301, sprintf3("%s/", el));
212 newpath = sprintf2("%s/%s", path, el);
213 rv = checkpath(req, fd, newpath, rest + 1);
216 } else if(S_ISREG(sb.st_mode)) {
217 newpath = sprintf2("%s/%s", path, el);
219 handlefile(req, fd, newpath);
223 handle404(req, fd, sprintf3("%s/", path));
226 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
228 handlefile(req, fd, newpath);
235 static int checkdir(struct hthead *req, int fd, char *path, char *rest)
238 struct config *cf, *ccf;
241 cf = getconfig(path);
242 if(cf->capture != NULL) {
243 cpath = sprintf2("%s/", path);
244 if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
246 flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
247 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
254 if(childhandle(ch, req, fd, chinit, ccf?ccf->path:NULL))
255 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
261 static int checkpath(struct hthead *req, int fd, char *path, char *rest)
269 if(!strncmp(path, "./", 2))
271 if(checkdir(req, fd, path, rest))
274 if((p = strchr(rest, '/')) == NULL) {
275 el = unquoteurl(rest);
278 char buf[p - rest + 1];
279 memcpy(buf, rest, p - rest);
281 el = unquoteurl(buf);
285 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
289 if(strchr(el, '/') || (!*el && *rest)) {
290 handle404(req, fd, sprintf3("%s/", path));
296 handledir(req, fd, path);
300 rv = checkentry(req, fd, path, rest, el);
308 static void serve(struct hthead *req, int fd)
311 if(!checkpath(req, fd, ".", req->rest))
312 handle404(req, fd, ".");
315 static void chldhandler(int sig)
320 pid = waitpid(-1, NULL, WNOHANG);
324 static void sighandler(int sig)
328 static void usage(FILE *out)
330 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
333 int main(int argc, char **argv)
337 char *gcf, *lcf, *clcf;
343 while((c = getopt(argc, argv, "hNc:")) >= 0) {
359 if(argc - optind < 1) {
364 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
365 gconfig = readconfig(gcf);
370 if(strchr(lcf, '/') == NULL) {
371 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
372 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
375 if((lconfig = readconfig(clcf)) == NULL)
379 if((lconfig = readconfig(lcf)) == NULL)
383 if(chdir(argv[optind])) {
384 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
387 signal(SIGCHLD, chldhandler);
388 signal(SIGPIPE, sighandler);
390 if((fd = recvreq(0, &req)) < 0) {
392 flog(LOG_ERR, "recvreq: %s", strerror(errno));