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/>.
28 #include <sys/signal.h>
44 static void chinit(void *idata)
49 /* This should never be able to fail other than for critical
50 * I/O errors or some such, since the path has already been
57 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
64 headappheader(req, "X-Ash-File", path);
65 stdforkserve(pat->fchild, req, fd, NULL, NULL);
67 if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
68 flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
69 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
73 if((twd = ccf->path) != NULL) {
74 if(!strcmp(twd, ".")) {
76 } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
77 /* Should be an impossible case under the current (and
78 * foreseeable) scheme. */
79 simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
82 path = path + strlen(twd) + 1;
85 headappheader(req, "X-Ash-File", path);
86 if(childhandle(ch, req, fd, chinit, twd))
87 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
91 static void handlefile(struct hthead *req, int fd, char *path)
95 if((pat = findmatch(path, 0, 0)) == NULL) {
96 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
99 handle(req, fd, path, pat);
102 static char *findfile(char *path, char *name, struct stat *sb)
111 if((dir = opendir(path)) == NULL)
114 while((dent = readdir(dir)) != NULL) {
115 /* Ignore backup files.
116 * XXX: There is probably a better and more extensible way to
118 if(dent->d_name[strlen(dent->d_name) - 1] == '~')
120 if((p = strchr(dent->d_name, '.')) == NULL)
122 if(p - dent->d_name != strlen(name))
124 if(strncmp(dent->d_name, name, strlen(name)))
126 fp = sprintf3("%s/%s", path, dent->d_name);
129 if(!S_ISREG(sb->st_mode))
138 static void handledir(struct hthead *req, int fd, char *path)
143 char *inm, *ipath, *cpath;
146 cpath = sprintf2("%s/", path);
147 cfs = getconfigs(cpath);
148 for(i = 0; cfs[i] != NULL; i++) {
149 if(cfs[i]->index != NULL) {
150 for(o = 0; cfs[i]->index[o] != NULL; o++) {
151 inm = cfs[i]->index[o];
152 ipath = sprintf2("%s/%s", path, inm);
153 if(!stat(ipath, &sb) && S_ISREG(sb.st_mode)) {
154 handlefile(req, fd, ipath);
160 if(!strchr(inm, '.') && ((ipath = findfile(path, inm, NULL)) != NULL)) {
161 handlefile(req, fd, ipath);
169 if((pat = findmatch(cpath, 0, 1)) != NULL) {
170 handle(req, fd, cpath, pat);
173 simpleerror(fd, 403, "Not Authorized", "Will not send listings for this directory.");
179 static int checkpath(struct hthead *req, int fd, char *path, char *rest);
181 static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *el)
188 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
191 if(!stat(sprintf3("%s/%s", path, el), &sb)) {
192 if(S_ISDIR(sb.st_mode)) {
194 stdredir(req, fd, 301, sprintf3("%s/", el));
197 newpath = sprintf2("%s/%s", path, el);
198 rv = checkpath(req, fd, newpath, rest + 1);
201 } else if(S_ISREG(sb.st_mode)) {
202 newpath = sprintf2("%s/%s", path, el);
204 handlefile(req, fd, newpath);
208 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
211 if(!strchr(el, '.') && ((newpath = findfile(path, el, NULL)) != NULL)) {
213 handlefile(req, fd, newpath);
220 static int checkdir(struct hthead *req, int fd, char *path, char *rest)
223 struct config *cf, *ccf;
226 cf = getconfig(path);
227 if(cf->capture != NULL) {
228 cpath = sprintf2("%s/", path);
229 if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
231 flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
232 simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
239 if(childhandle(ch, req, fd, chinit, ccf->path))
240 simpleerror(fd, 500, "Server Error", "The request handler crashed.");
246 static int checkpath(struct hthead *req, int fd, char *path, char *rest)
254 if(!strncmp(path, "./", 2))
256 if(checkdir(req, fd, path, rest))
259 if((p = strchr(rest, '/')) == NULL) {
260 el = unquoteurl(rest);
263 char buf[p - rest + 1];
264 memcpy(buf, rest, p - rest);
266 el = unquoteurl(buf);
270 simpleerror(fd, 400, "Bad Request", "The requested URL contains an invalid escape sequence.");
274 if(strchr(el, '/') || (!*el && *rest)) {
275 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
281 handledir(req, fd, path);
284 rv = checkentry(req, fd, path, rest, el);
292 static void serve(struct hthead *req, int fd)
295 if(!checkpath(req, fd, ".", req->rest))
296 simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
299 static void sighandler(int sig)
303 static void usage(FILE *out)
305 fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
308 int main(int argc, char **argv)
312 char *gcf, *lcf, *clcf;
318 while((c = getopt(argc, argv, "hNc:")) >= 0) {
334 if(argc - optind < 1) {
339 if((gcf = findstdconf("ashd/dirplex.rc")) != NULL) {
340 gconfig = readconfig(gcf);
345 if(strchr(lcf, '/') == NULL) {
346 if((clcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
347 flog(LOG_ERR, "could not find requested configuration `%s'", lcf);
350 if((lconfig = readconfig(clcf)) == NULL)
354 if((lconfig = readconfig(lcf)) == NULL)
358 if(chdir(argv[optind])) {
359 flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
362 signal(SIGCHLD, SIG_IGN);
363 signal(SIGPIPE, sighandler);
365 if((fd = recvreq(0, &req)) < 0) {
367 flog(LOG_ERR, "recvreq: %s", strerror(errno));