SUBDIRS = lib src doc
-EXTRA_DIST = etc
+EXTRA_DIST = etc examples
if test "$HAS_EPOLL" = yes; then
AC_DEFINE(HAVE_EPOLL)
fi
+AM_CONDITIONAL(USE_EPOLL, [test "$HAS_EPOLL" = yes ])
AH_TEMPLATE(HAVE_XATTR, [define to compile support for filesystem extended attributes])
AC_ARG_WITH(xattr, [ --with-xattr Enable XATTR support])
match
filename *.css
fork sendfile -c text/css
+match
+ filename *.txt
+ fork sendfile -c text/plain
# Image types
match
start() {
export LANG
log_daemon_msg "Starting HTTP server" "ashd"
- ulimit -c unlimited
start-stop-daemon -S -p "$PIDFILE" -qx "$HTPARSER" -- -Sf -p "$PIDFILE" -u nobody -r /var/tmp $PORTSPEC -- $ROOTSPEC
log_end_msg $?
}
--- /dev/null
+This directory contains some examples of more or less simple ashd
+configurations. Each sub-directory contains a self-contained example
+with a shell script called `run'. Simply run that script to start the
+example, and examine it to see how it was made. All of them will start
+an non-SSL HTTP server on port 8080.
+
+Note that ashd, including the default configuration files, must be
+installed on the system to be able to run the examples.
+
+The `python' sub-directory contains examples for using the Python
+module. To run them, the Python module has to be installed and
+accessible as well.
--- /dev/null
+You're accessing via IP address.
--- /dev/null
+This example uses Python to dynamically dispatch requests for
+different vhosts. The Python program dynhosts will look in a directory
+for subdirectories named after the virtual host and start a dirplex
+instance for each such host that is requested.
+
+To try this example, run it on your local machine and request
+<http://localhost:8080/> or <http://127.0.0.1:8080/>.
--- /dev/null
+#!/usr/bin/python
+
+import os, sys, signal
+from ashd import util
+
+children = {}
+root = sys.argv[1]
+
+# Automatically reap all children that die for any reason.
+signal.signal(signal.SIGCHLD, signal.SIG_IGN)
+
+def serve(req):
+ if "Host" in req:
+ # Strip port specification
+ dname = req["Host"].split(':')[0]
+ dname = dname.lower()
+ path = os.path.join(root, dname)
+ if os.path.isdir(path):
+ if dname not in children:
+ children[dname] = util.pchild(["dirplex", path], autorespawn = True)
+ children[dname].passreq(req)
+ return
+ util.respond(req, "No such host in configured.\n", status = "404 Not Found", ctype = "text/plain")
+
+util.serveloop(serve)
--- /dev/null
+You're accessing via hostname.
--- /dev/null
+#!/bin/sh
+
+# Change to the directory containing this script
+set -e
+cd "$(dirname "$0")"
+
+# Start htparser running this dynhosts script. The setsid command
+# ensures that SIGINT is only received by htparser and not by
+# dynhosts; it is not of grave importance, but makes shutdown slightly
+# more clean, and hides the KeyboardInterrupt otherwise raised by
+# Python.
+htparser plain:port=8080 -- setsid ./dynhosts .
--- /dev/null
+To view this example, simply run it on your local machine and point
+your web browser at <http://localhost:8080/>.
--- /dev/null
+<?xml version="1.0" encoding="US-ASCII"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
+<head>
+<title>Test page</title>
+<link rel="stylesheet" title="Default style" type="text/css" href="test.css" />
+</head>
+<body>
+<h1>Test page</h1>
+<p>
+Note how this page is accessible as
+<a href="/"><code>/</code></a>,
+<a href="/index"><code>/index</code></a> and
+<a href="/index.html"><code>/index.html</code></a>.
+</p>
+</body>
+</html>
--- /dev/null
+#!/bin/sh
+
+# Change to the directory containing this script
+set -e
+cd "$(dirname "$0")"
+
+# Simply invoke htparser running dirplex in this directory
+htparser plain:port=8080 -- dirplex .
--- /dev/null
+body {
+ font-family: sans-serif;
+}
+
+h1 {
+ font-size: 1.75em;
+}
+
+p {
+ font-size: 1em;
+}
--- /dev/null
+To view this example, simply run it on your local machine and point
+your web browser at <http://localhost:8080/>.
--- /dev/null
+<?xml version="1.0" encoding="US-ASCII"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
+<head>
+<title>Default site</title>
+<link rel="stylesheet" title="Default style" type="text/css" href="test.css" />
+</head>
+<body>
+<h1>Default site</h1>
+<p>
+This site is called when the requested Host is not exactly
+<code>localhost</code>, such as 127.0.0.1.
+</p><p>
+Try visiting
+<a href="http://localhost:8080/"><code>localhost</code></a>
+instead (and make sure the server is running on your local machine).
+</p>
+</body>
+</html>
--- /dev/null
+body {
+ font-family: sans-serif;
+}
+
+h1 {
+ font-size: 1.75em;
+}
+
+p {
+ font-size: 1em;
+}
--- /dev/null
+<?xml version="1.0" encoding="US-ASCII"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
+<head>
+<title>localhost site</title>
+<link rel="stylesheet" title="Default style" type="text/css" href="test.css" />
+</head>
+<body>
+<h1><code>localhost</code> site</h1>
+<p>
+This site is called when the requested Host is
+exactly <code>localhost</code> (plus any port specification).
+</p><p>
+Try requesting the server via a different hostname, such as
+<a href="http://127.0.0.1:8080/"><code>127.0.0.1</code></a>,
+instead.
+</p>
+</body>
+</html>
--- /dev/null
+body {
+ font-family: sans-serif;
+}
+
+h1 {
+ font-size: 1.75em;
+}
+
+p {
+ font-size: 1em;
+}
--- /dev/null
+# Sample virtual host configuration. See the patplex(1) manual page
+# for further details on the configuration format.
+
+match
+ # Match localhost specifically. Note that the Host header may
+ # include a port specification, which is optionally matched by the
+ # end of the regex.
+ header host ^localhost(:[0-9]+)?$ i
+ handler localhost
+
+child localhost
+ # The child specification called when the localhost pattern
+ # matches. Simply run dirplex(1) in the subdirectory `localhost'.
+ exec dirplex localhost
+
+match
+ # Catch-all pattern for all requests not specifically directed at
+ # localhost.
+ default
+ handler default
+
+child default
+ exec dirplex default-site
--- /dev/null
+#!/bin/sh
+
+# Change to the directory containing this script
+set -e
+cd "$(dirname "$0")"
+
+# Start htparser running patplex; see the patterns.conf file for
+# further details.
+htparser plain:port=8080 -- patplex patterns.conf
-noinst_LIBRARIES = libht.a
+lib_LIBRARIES = libht.a
-libht_a_SOURCES = utils.c mt.c log.c req.c proc.c mtio.c resp.c cf.c \
- utils.h mt.h log.h req.h proc.h mtio.h resp.h cf.h
-libht_a_CFLAGS = -fPIC
-libht_a_CPPFLAGS = -D_GNU_SOURCE
+libht_a_SOURCES = utils.c mt.c log.c req.c proc.c mtio.c resp.c cf.c
+libht_a_CFLAGS = -fPIC
+libht_a_CPPFLAGS = -D_GNU_SOURCE
+if USE_EPOLL
+libht_a_SOURCES += mtio-epoll.c
+else
+libht_a_SOURCES += mtio-select.c
+endif
+
+pkginclude_HEADERS = utils.h mt.h log.h req.h proc.h mtio.h resp.h cf.h
#ifndef _ASHCONF_H
#define _ASHCONF_H
-#include <req.h>
+#include "req.h"
struct cfstate {
struct muth *pf;
--- /dev/null
+/*
+ ashd - A Sane HTTP Daemon
+ Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+#include <fcntl.h>
+#include <string.h>
+#include <sys/epoll.h>
+#include <errno.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <log.h>
+#include <utils.h>
+#include <mt.h>
+#include <mtio.h>
+
+static struct blocker *blockers;
+
+struct blocker {
+ struct blocker *n, *p, *n2, *p2;
+ int fd, reg;
+ int ev;
+ time_t to;
+ struct muth *th;
+};
+
+static int epfd = -1, fdln = 0;
+static struct blocker **fdlist;
+
+static int regfd(struct blocker *bl)
+{
+ struct blocker *o;
+ struct epoll_event evd;
+
+ memset(&evd, 0, sizeof(evd));
+ evd.events = 0;
+ if(bl->ev & EV_READ)
+ evd.events |= EPOLLIN;
+ if(bl->ev & EV_WRITE)
+ evd.events |= EPOLLOUT;
+ evd.data.fd = bl->fd;
+ if(bl->fd >= fdln) {
+ if(fdlist) {
+ fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
+ memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
+ fdln = bl->fd + 1;
+ } else {
+ fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
+ }
+ }
+ if(fdlist[bl->fd] == NULL) {
+ if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
+ /* XXX?! Whatever to do, really? */
+ flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
+ return(-1);
+ }
+ } else {
+ for(o = fdlist[bl->fd]; o; o = o->n2) {
+ if(o->ev & EV_READ)
+ evd.events |= EPOLLIN;
+ if(o->ev & EV_WRITE)
+ evd.events |= EPOLLOUT;
+ }
+ if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
+ /* XXX?! Whatever to do, really? */
+ flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
+ return(-1);
+ }
+ }
+ bl->n2 = fdlist[bl->fd];
+ bl->p2 = NULL;
+ if(fdlist[bl->fd] != NULL)
+ fdlist[bl->fd]->p2 = bl;
+ fdlist[bl->fd] = bl;
+ bl->reg = 1;
+ return(0);
+}
+
+static void remfd(struct blocker *bl)
+{
+ struct blocker *o;
+ struct epoll_event evd;
+
+ if(!bl->reg)
+ return;
+ if(bl->n2)
+ bl->n2->p2 = bl->p2;
+ if(bl->p2)
+ bl->p2->n2 = bl->n2;
+ if(bl == fdlist[bl->fd])
+ fdlist[bl->fd] = bl->n2;
+ if(fdlist[bl->fd] == NULL) {
+ if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
+ flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
+ } else {
+ memset(&evd, 0, sizeof(evd));
+ evd.events = 0;
+ evd.data.fd = bl->fd;
+ for(o = fdlist[bl->fd]; o; o = o->n2) {
+ if(o->ev & EV_READ)
+ evd.events |= EPOLLIN;
+ if(o->ev & EV_WRITE)
+ evd.events |= EPOLLOUT;
+ }
+ if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
+ /* XXX?! Whatever to do, really? */
+ flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
+ }
+ }
+}
+
+int block(int fd, int ev, time_t to)
+{
+ struct blocker *bl;
+ int rv;
+
+ omalloc(bl);
+ bl->fd = fd;
+ bl->ev = ev;
+ if(to > 0)
+ bl->to = time(NULL) + to;
+ bl->th = current;
+ if((epfd >= 0) && regfd(bl)) {
+ free(bl);
+ return(-1);
+ }
+ bl->n = blockers;
+ if(blockers)
+ blockers->p = bl;
+ blockers = bl;
+ rv = yield();
+ if(bl->n)
+ bl->n->p = bl->p;
+ if(bl->p)
+ bl->p->n = bl->n;
+ if(bl == blockers)
+ blockers = bl->n;
+ remfd(bl);
+ free(bl);
+ return(rv);
+}
+
+void ioloop(void)
+{
+ struct blocker *bl, *nbl;
+ struct epoll_event evr[16];
+ int i, fd, nev, ev, toval;
+ time_t now, timeout;
+
+ epfd = epoll_create(128);
+ fcntl(epfd, F_SETFD, FD_CLOEXEC);
+ for(bl = blockers; bl; bl = nbl) {
+ nbl = bl->n;
+ if(regfd(bl))
+ resume(bl->th, -1);
+ }
+ while(blockers != NULL) {
+ timeout = 0;
+ for(bl = blockers; bl; bl = bl->n) {
+ if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
+ timeout = bl->to;
+ }
+ now = time(NULL);
+ if(timeout == 0)
+ toval = -1;
+ else if(timeout > now)
+ toval = (timeout - now) * 1000;
+ else
+ toval = 1000;
+ nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
+ if(nev < 0) {
+ if(errno != EINTR) {
+ flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
+ /* To avoid CPU hogging in case it's bad, which it
+ * probably is. */
+ sleep(1);
+ }
+ continue;
+ }
+ for(i = 0; i < nev; i++) {
+ fd = evr[i].data.fd;
+ ev = 0;
+ if(evr[i].events & EPOLLIN)
+ ev |= EV_READ;
+ if(evr[i].events & EPOLLOUT)
+ ev |= EV_WRITE;
+ if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
+ ev = -1;
+ for(bl = fdlist[fd]; bl; bl = nbl) {
+ nbl = bl->n2;
+ if((ev < 0) || (ev & bl->ev))
+ resume(bl->th, ev);
+ }
+ }
+ now = time(NULL);
+ for(bl = blockers; bl; bl = nbl) {
+ nbl = bl->n;
+ if((bl->to != 0) && (bl->to <= now))
+ resume(bl->th, 0);
+ }
+ }
+ close(epfd);
+ epfd = -1;
+}
--- /dev/null
+/*
+ ashd - A Sane HTTP Daemon
+ Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/select.h>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <log.h>
+#include <utils.h>
+#include <mt.h>
+#include <mtio.h>
+
+static struct blocker *blockers;
+
+struct blocker {
+ struct blocker *n, *p;
+ int fd;
+ int ev;
+ time_t to;
+ struct muth *th;
+};
+
+int block(int fd, int ev, time_t to)
+{
+ struct blocker *bl;
+ int rv;
+
+ omalloc(bl);
+ bl->fd = fd;
+ bl->ev = ev;
+ if(to > 0)
+ bl->to = time(NULL) + to;
+ bl->th = current;
+ bl->n = blockers;
+ if(blockers)
+ blockers->p = bl;
+ blockers = bl;
+ rv = yield();
+ if(bl->n)
+ bl->n->p = bl->p;
+ if(bl->p)
+ bl->p->n = bl->n;
+ if(bl == blockers)
+ blockers = bl->n;
+ free(bl);
+ return(rv);
+}
+
+void ioloop(void)
+{
+ int ret;
+ fd_set rfds, wfds, efds;
+ struct blocker *bl, *nbl;
+ struct timeval toval;
+ time_t now, timeout;
+ int maxfd;
+ int ev;
+
+ while(blockers != NULL) {
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ FD_ZERO(&efds);
+ maxfd = 0;
+ now = time(NULL);
+ timeout = 0;
+ for(bl = blockers; bl; bl = bl->n) {
+ if(bl->ev & EV_READ)
+ FD_SET(bl->fd, &rfds);
+ if(bl->ev & EV_WRITE)
+ FD_SET(bl->fd, &wfds);
+ FD_SET(bl->fd, &efds);
+ if(bl->fd > maxfd)
+ maxfd = bl->fd;
+ if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
+ timeout = bl->to;
+ }
+ toval.tv_sec = timeout - now;
+ toval.tv_usec = 0;
+ ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
+ if(ret < 0) {
+ if(errno != EINTR) {
+ flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
+ /* To avoid CPU hogging in case it's bad, which it
+ * probably is. */
+ sleep(1);
+ }
+ }
+ now = time(NULL);
+ for(bl = blockers; bl; bl = nbl) {
+ nbl = bl->n;
+ ev = 0;
+ if(FD_ISSET(bl->fd, &rfds))
+ ev |= EV_READ;
+ if(FD_ISSET(bl->fd, &wfds))
+ ev |= EV_WRITE;
+ if(FD_ISSET(bl->fd, &efds))
+ ev = -1;
+ if((ev < 0) || (ev & bl->ev))
+ resume(bl->th, ev);
+ else if((bl->to != 0) && (bl->to <= now))
+ resume(bl->th, 0);
+ }
+ }
+}
#include <stdlib.h>
#include <stdio.h>
-#include <fcntl.h>
#include <unistd.h>
+#include <fcntl.h>
#include <string.h>
-#include <time.h>
#include <errno.h>
-#include <sys/select.h>
#include <sys/socket.h>
#ifdef HAVE_CONFIG_H
#include <mt.h>
#include <mtio.h>
-static struct blocker *blockers;
-
-#ifdef HAVE_EPOLL
-
-/*
- * Support for epoll. Optimally, different I/O loops should be split
- * into different files for greater clarity, but I'll save that fun
- * for another day.
- *
- * Scroll down to #else for the normal select loop.
- */
-
-#include <sys/epoll.h>
-
-struct blocker {
- struct blocker *n, *p, *n2, *p2;
- int fd, reg;
- int ev;
- time_t to;
- struct muth *th;
-};
-
-static int epfd = -1, fdln = 0;
-static struct blocker **fdlist;
-
-static int regfd(struct blocker *bl)
-{
- struct blocker *o;
- struct epoll_event evd;
-
- memset(&evd, 0, sizeof(evd));
- evd.events = 0;
- if(bl->ev & EV_READ)
- evd.events |= EPOLLIN;
- if(bl->ev & EV_WRITE)
- evd.events |= EPOLLOUT;
- evd.data.fd = bl->fd;
- if(bl->fd >= fdln) {
- if(fdlist) {
- fdlist = srealloc(fdlist, sizeof(*fdlist) * (bl->fd + 1));
- memset(fdlist + fdln, 0, sizeof(*fdlist) * (bl->fd + 1 - fdln));
- fdln = bl->fd + 1;
- } else {
- fdlist = szmalloc(sizeof(*fdlist) * (fdln = (bl->fd + 1)));
- }
- }
- if(fdlist[bl->fd] == NULL) {
- if(epoll_ctl(epfd, EPOLL_CTL_ADD, bl->fd, &evd)) {
- /* XXX?! Whatever to do, really? */
- flog(LOG_ERR, "epoll_add on fd %i: %s", bl->fd, strerror(errno));
- return(-1);
- }
- } else {
- for(o = fdlist[bl->fd]; o; o = o->n2) {
- if(o->ev & EV_READ)
- evd.events |= EPOLLIN;
- if(o->ev & EV_WRITE)
- evd.events |= EPOLLOUT;
- }
- if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
- /* XXX?! Whatever to do, really? */
- flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
- return(-1);
- }
- }
- bl->n2 = fdlist[bl->fd];
- bl->p2 = NULL;
- if(fdlist[bl->fd] != NULL)
- fdlist[bl->fd]->p2 = bl;
- fdlist[bl->fd] = bl;
- bl->reg = 1;
- return(0);
-}
-
-static void remfd(struct blocker *bl)
-{
- struct blocker *o;
- struct epoll_event evd;
-
- if(!bl->reg)
- return;
- if(bl->n2)
- bl->n2->p2 = bl->p2;
- if(bl->p2)
- bl->p2->n2 = bl->n2;
- if(bl == fdlist[bl->fd])
- fdlist[bl->fd] = bl->n2;
- if(fdlist[bl->fd] == NULL) {
- if(epoll_ctl(epfd, EPOLL_CTL_DEL, bl->fd, NULL))
- flog(LOG_ERR, "epoll_del on fd %i: %s", bl->fd, strerror(errno));
- } else {
- memset(&evd, 0, sizeof(evd));
- evd.events = 0;
- evd.data.fd = bl->fd;
- for(o = fdlist[bl->fd]; o; o = o->n2) {
- if(o->ev & EV_READ)
- evd.events |= EPOLLIN;
- if(o->ev & EV_WRITE)
- evd.events |= EPOLLOUT;
- }
- if(epoll_ctl(epfd, EPOLL_CTL_MOD, bl->fd, &evd)) {
- /* XXX?! Whatever to do, really? */
- flog(LOG_ERR, "epoll_mod on fd %i: %s", bl->fd, strerror(errno));
- }
- }
-}
-
-int block(int fd, int ev, time_t to)
-{
- struct blocker *bl;
- int rv;
-
- omalloc(bl);
- bl->fd = fd;
- bl->ev = ev;
- if(to > 0)
- bl->to = time(NULL) + to;
- bl->th = current;
- if((epfd >= 0) && regfd(bl)) {
- free(bl);
- return(-1);
- }
- bl->n = blockers;
- if(blockers)
- blockers->p = bl;
- blockers = bl;
- rv = yield();
- if(bl->n)
- bl->n->p = bl->p;
- if(bl->p)
- bl->p->n = bl->n;
- if(bl == blockers)
- blockers = bl->n;
- remfd(bl);
- free(bl);
- return(rv);
-}
-
-void ioloop(void)
-{
- struct blocker *bl, *nbl;
- struct epoll_event evr[16];
- int i, fd, nev, ev, toval;
- time_t now, timeout;
-
- epfd = epoll_create(128);
- for(bl = blockers; bl; bl = nbl) {
- nbl = bl->n;
- if(regfd(bl))
- resume(bl->th, -1);
- }
- while(blockers != NULL) {
- timeout = 0;
- for(bl = blockers; bl; bl = bl->n) {
- if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
- timeout = bl->to;
- }
- now = time(NULL);
- if(timeout == 0)
- toval = -1;
- else if(timeout > now)
- toval = (timeout - now) * 1000;
- else
- toval = 1000;
- nev = epoll_wait(epfd, evr, sizeof(evr) / sizeof(*evr), toval);
- if(nev < 0) {
- if(errno != EINTR) {
- flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
- /* To avoid CPU hogging in case it's bad, which it
- * probably is. */
- sleep(1);
- }
- continue;
- }
- for(i = 0; i < nev; i++) {
- fd = evr[i].data.fd;
- ev = 0;
- if(evr[i].events & EPOLLIN)
- ev |= EV_READ;
- if(evr[i].events & EPOLLOUT)
- ev |= EV_WRITE;
- if(evr[i].events & ~(EPOLLIN | EPOLLOUT))
- ev = -1;
- for(bl = fdlist[fd]; bl; bl = nbl) {
- nbl = bl->n2;
- if((ev < 0) || (ev & bl->ev))
- resume(bl->th, ev);
- }
- }
- now = time(NULL);
- for(bl = blockers; bl; bl = nbl) {
- nbl = bl->n;
- if((bl->to != 0) && (bl->to <= now))
- resume(bl->th, 0);
- }
- }
- close(epfd);
- epfd = -1;
-}
-
-#else
-
-struct blocker {
- struct blocker *n, *p;
- int fd;
- int ev;
- time_t to;
- struct muth *th;
-};
-
-int block(int fd, int ev, time_t to)
-{
- struct blocker *bl;
- int rv;
-
- omalloc(bl);
- bl->fd = fd;
- bl->ev = ev;
- if(to > 0)
- bl->to = time(NULL) + to;
- bl->th = current;
- bl->n = blockers;
- if(blockers)
- blockers->p = bl;
- blockers = bl;
- rv = yield();
- if(bl->n)
- bl->n->p = bl->p;
- if(bl->p)
- bl->p->n = bl->n;
- if(bl == blockers)
- blockers = bl->n;
- free(bl);
- return(rv);
-}
-
-void ioloop(void)
-{
- int ret;
- fd_set rfds, wfds, efds;
- struct blocker *bl, *nbl;
- struct timeval toval;
- time_t now, timeout;
- int maxfd;
- int ev;
-
- while(blockers != NULL) {
- FD_ZERO(&rfds);
- FD_ZERO(&wfds);
- FD_ZERO(&efds);
- maxfd = 0;
- now = time(NULL);
- timeout = 0;
- for(bl = blockers; bl; bl = bl->n) {
- if(bl->ev & EV_READ)
- FD_SET(bl->fd, &rfds);
- if(bl->ev & EV_WRITE)
- FD_SET(bl->fd, &wfds);
- FD_SET(bl->fd, &efds);
- if(bl->fd > maxfd)
- maxfd = bl->fd;
- if((bl->to != 0) && ((timeout == 0) || (timeout > bl->to)))
- timeout = bl->to;
- }
- toval.tv_sec = timeout - now;
- toval.tv_usec = 0;
- ret = select(maxfd + 1, &rfds, &wfds, &efds, timeout?(&toval):NULL);
- if(ret < 0) {
- if(errno != EINTR) {
- flog(LOG_CRIT, "ioloop: select errored out: %s", strerror(errno));
- /* To avoid CPU hogging in case it's bad, which it
- * probably is. */
- sleep(1);
- }
- }
- now = time(NULL);
- for(bl = blockers; bl; bl = nbl) {
- nbl = bl->n;
- ev = 0;
- if(FD_ISSET(bl->fd, &rfds))
- ev |= EV_READ;
- if(FD_ISSET(bl->fd, &wfds))
- ev |= EV_WRITE;
- if(FD_ISSET(bl->fd, &efds))
- ev = -1;
- if((ev < 0) || (ev & bl->ev))
- resume(bl->th, ev);
- else if((bl->to != 0) && (bl->to <= now))
- resume(bl->th, 0);
- }
- }
-}
-
-#endif
-
struct stdiofd {
int fd;
int sock;
#include <sys/socket.h>
#include <errno.h>
#include <ctype.h>
+#include <fcntl.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
int stdmkchild(char **argv, void (*chinit)(void *), void *idata)
{
- int i;
pid_t pid;
int fd[2];
if(pid == 0) {
if(chinit != NULL)
chinit(idata);
- for(i = 3; i < FD_SETSIZE; i++) {
- if(i != fd[0])
- close(i);
- }
dup2(fd[0], 0);
close(fd[0]);
+ close(fd[1]);
execvp(argv[0], argv);
flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
exit(127);
}
close(fd[0]);
+ fcntl(fd[1], F_SETFD, FD_CLOEXEC);
return(fd[1]);
}
dup2(fd, 0);
dup2(fd, 1);
- for(i = 3; i < FD_SETSIZE; i++)
- close(i);
+ close(fd);
bufinit(args);
for(i = 0; argv[i]; i++)
#ifndef _LIB_PROC_H
#define _LIB_PROC_H
-#include <req.h>
+#include "req.h"
int stdmkchild(char **argv, void (*chinit)(void *), void *idata);
int sendfd(int sock, int fd, char *data, size_t datalen);
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
+#include <fcntl.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
return(-1);
}
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
buf.s = buf.d;
p = buf.b;
l = buf.d;
#ifndef _LIB_HTRESP_H
#define _LIB_HTRESP_H
-#include <req.h>
+#include "req.h"
char *urlquote(char *text);
char *htmlquote(char *text);
+"""Base module for ashd(7)-related fucntions.
+
+This module implements nothing. Please see the ashd.util or ashd.proto
+modules.
+"""
+"""Low-level protocol module for ashd(7)
+
+This module provides primitive functions that speak the raw ashd(7)
+protocol. Primarily, it implements the `req' class that is used to
+represent ashd requests. The functions it provides can also be used to
+create ashd handlers, but unless you require very precise control, the
+ashd.util module provides an easier-to-use interface.
+"""
+
import os, socket
import htlib
pass
class req(object):
+ """Represents a single ashd request. Normally, you would not
+ create instances of this class manually, but receive them from the
+ recvreq function.
+
+ For the abstract structure of ashd requests, please see the
+ ashd(7) manual page. This class provides access to the HTTP
+ method, raw URL, HTTP version and rest string via the `method',
+ `url', `ver' and `rest' variables respectively. It also implements
+ a dict-like interface for case-independent access to the HTTP
+ headers. The raw headers are available as a list of (name, value)
+ tuples in the `headers' variable.
+
+ For responding, the response socket is available as a standard
+ Python stream object in the `sk' variable. Again, see the ashd(7)
+ manpage for what to receive and transmit on the response socket.
+
+ Note that instances of this class contain a reference to the live
+ socket used for responding to requests, which should be closed
+ when you are done with the request. The socket can be closed
+ manually by calling the close() method on this
+ object. Alternatively, this class implements the resource-manager
+ interface, so that it can be used in `with' statements.
+ """
+
def __init__(self, method, url, ver, rest, headers, fd):
self.method = method
self.url = url
os.close(fd)
def close(self):
+ "Close this request's response socket."
self.sk.close()
def __getitem__(self, header):
+ """Find a HTTP header case-insensitively. For example,
+ req["Content-Type"] returns the value of the content-type
+ header regardlessly of whether the client specified it as
+ "Content-Type", "content-type" or "Content-type".
+ """
header = header.lower()
for key, val in self.headers:
if key.lower() == header:
raise KeyError(header)
def __contains__(self, header):
+ """Works analogously to the __getitem__ method for checking
+ header presence case-insensitively.
+ """
header = header.lower()
for key, val in self.headers:
if key.lower() == header:
return False
def dup(self):
+ """Creates a duplicate of this request, referring to a
+ duplicate of the response socket.
+ """
return req(self.method, self.url, self.ver, self.rest, self.headers, os.dup(self.sk.fileno()))
def match(self, match):
+ """If the `match' argument matches exactly the leading part of
+ the rest string, this method strips that part of the rest
+ string off and returns True. Otherwise, it returns False
+ without doing anything.
+
+ This can be used for simple dispatching. For example:
+ if req.match("foo/"):
+ handle(req)
+ elif req.match("bar/"):
+ handle_otherwise(req)
+ else:
+ util.respond(req, "Not found", status = "404 Not Found", ctype = "text/plain")
+ """
if self.rest[:len(match)] == match:
self.rest = self.rest[len(match):]
return True
return False
def recvreq(sock = 0):
+ """Receive a single ashd request on the specified socket file
+ descriptor (or standard input if unspecified).
+
+ The returned value is an instance of the `req' class. As per its
+ description, care should be taken to close() the request when
+ done, to avoid leaking response sockets. If end-of-file is
+ received on the socket, None is returned.
+
+ This function may either raise on OSError if an error occurs on
+ the socket, or a ashd.proto.protoerr if the incoming request is
+ invalidly encoded.
+ """
data, fd = htlib.recvfd(sock)
if fd is None:
return None
os.close(fd)
def sendreq(sock, req):
+ """Encode and send a single request to the specified socket file
+ descriptor using the ashd protocol. The request should be an
+ instance of the `req' class.
+
+ This function may raise an OSError if an error occurs on the
+ socket.
+ """
data = ""
data += req.method + '\0'
data += req.url + '\0'
+"""High-level utility module for ashd(7)
+
+This module implements a rather convenient interface for writing ashd
+handlers, wrapping the low-level ashd.proto module.
+"""
+
import os, socket
import proto
def stdfork(argv, chinit = None):
+ """Fork a persistent handler process using the `argv' argument
+ list, as per the standard ashd(7) calling convention. For an
+ easier-to-use interface, see the `pchild' class.
+
+ If a callable object of no arguments is provided in the `chinit'
+ argument, it will be called in the child process before exec()'ing
+ the handler program, and can be used to set parameters for the new
+ process, such as working directory, nice level or ulimits.
+
+ Returns the file descriptor of the socket for sending requests to
+ the new child.
+ """
csk, psk = socket.socketpair(socket.AF_UNIX, socket.SOCK_SEQPACKET)
pid = os.fork()
if pid == 0:
psk.close()
return fd
+class pchild(object):
+ """class pchild(argv, autorespawn=False, chinit=None)
+
+ Represents a persistent child handler process, started as per the
+ standard ashd(7) calling convention. It will be called with the
+ `argv' argument lest, which should be a list (or other iterable)
+ of strings.
+
+ If `autorespawn' is specified as True, the child process will be
+ automatically restarted if a request cannot be successfully sent
+ to it.
+
+ For a description of the `chinit' argument, see `stdfork'.
+
+ When this child handler should be disposed of, care should be
+ taken to call the close() method to release its socket and let it
+ exit. This class also implements the resource-manager interface,
+ so that it can be used in `with' statements.
+ """
+
+ def __init__(self, argv, autorespawn = False, chinit = None):
+ self.argv = argv
+ self.chinit = chinit
+ self.fd = -1
+ self.respawn = autorespawn
+ self.spawn()
+
+ def spawn(self):
+ """Start the child handler, or restart it if it is already
+ running. You should not have to call this method manually
+ unless you explicitly want to manage the process' lifecycle.
+ """
+ self.close()
+ self.fd = stdfork(self.argv, self.chinit)
+
+ def close(self):
+ """Close this child handler's socket. For normal child
+ handlers, this will make the program terminate normally.
+ """
+ if self.fd >= 0:
+ os.close(self.fd)
+ self.fd = -1
+
+ def __del__(self):
+ self.close()
+
+ def passreq(self, req):
+ """Pass the specified request (which should be an instance of
+ the ashd.proto.req class) to this child handler. If the child
+ handler fails for some reason, and `autorespawn' was specified
+ as True when creating this handler, one attempt will be made
+ to restart it.
+
+ Note: You still need to close the request normally.
+
+ This method may raise an OSError if the request fails and
+ autorespawning was either not requested, or if the
+ autorespawning fails.
+ """
+ try:
+ proto.sendreq(self.fd, req)
+ except OSError:
+ if self.respawn:
+ self.spawn()
+ proto.sendreq(self.fd, req)
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *excinfo):
+ self.close()
+ return False
+
def respond(req, body, status = ("200 OK"), ctype = "text/html"):
+ """Simple function for conveniently responding to a request.
+
+ Sends the specified body text to the request's response socket,
+ prepending an HTTP header with the appropriate Content-Type and
+ Content-Length headers, and then closes the response socket.
+
+ The `status' argument can be used to specify a non-200 response,
+ and the `ctype' argument can be used to specify a non-HTML
+ MIME-type.
+
+ If `body' is a Unicode object, it will be encoded as UTF-8.
+
+ For example:
+ respond(req, "Not found", status = "404 Not Found", ctype = "text/plain")
+ """
if type(body) == unicode:
body = body.decode("utf-8")
if ctype[:5] == "text/" and ctype.find(';') < 0:
req.close()
def serveloop(handler, sock = 0):
+ """Implements a simple loop for serving requests sequentially, by
+ receiving requests from standard input (or the specified socket),
+ passing them to the specified handler function, and finally making
+ sure to close them. Returns when end-of-file is received on the
+ incoming socket.
+
+ The handler function should be a callable object of one argument,
+ and is called once for each received request.
+ """
while True:
req = proto.recvreq(sock)
if req is None:
#include <Python.h>
#include <errno.h>
-#include <utils.h>
-#include <proc.h>
+#include <ashd/utils.h>
+#include <ashd/proc.h>
static PyObject *p_recvfd(PyObject *self, PyObject *args)
{
from distutils.core import setup, Extension
htlib = Extension("ashd.htlib", ["htp.c"],
- libraries = ["ht"],
- library_dirs = ["../lib/"],
- include_dirs = ["../lib/"])
+ libraries = ["ht"])
setup(name = "ashd-py",
version = "0.1",
}
}
}
+ fcntl(fileno(out), F_SETFD, FD_CLOEXEC);
if((ch = stdmkchild(argv + optind + 1, NULL, NULL)) < 0) {
flog(LOG_ERR, "accesslog: could not fork child: %s", strerror(errno));
exit(1);
} else {
bufaddenv(dst, "SCRIPT_NAME", "%s", url);
}
- free(url);
bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:"");
+ free(url);
if((h = getheader(req, "Host")) != NULL)
bufaddenv(dst, "SERVER_NAME", "%s", h);
if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
} else {
bufaddenv(dst, "SCRIPT_NAME", "%s", url);
}
- free(url);
bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:"");
+ free(url);
if((h = getheader(req, "Host")) != NULL)
bufaddenv(dst, "SERVER_NAME", "%s", h);
if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
name = NULL;
prio = LOG_WARNING;
fac = LOG_DAEMON;
- while((c = getopt(argc, argv, "hn:p:f:")) >= 0) {
+ while((c = getopt(argc, argv, "+hn:p:f:")) >= 0) {
switch(c) {
case 'n':
name = optarg;
{
int c;
- while((c = getopt(argc, argv, "h")) >= 0) {
+ while((c = getopt(argc, argv, "+h")) >= 0) {
switch(c) {
case 'h':
usage(stdout);
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <fcntl.h>
#include <errno.h>
#include <string.h>
close(fd);
return(-1);
}
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
return(fd);
}
close(fd);
return(-1);
}
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
return(fd);
}
{
struct passwd *pwd;
pid_t pid;
- int i, fd[2];
+ int fd[2];
/* XXX: There should be a way for the child to report errors (like
* 404 when htpub doesn't exist), but for now I don't bother with
if((pid = fork()) < 0)
return(-1);
if(pid == 0) {
- for(i = 3; i < FD_SETSIZE; i++) {
- if(i != fd[0])
- close(i);
- }
dup2(fd[0], 0);
close(fd[0]);
+ close(fd[1]);
login(pwd);
execchild(pwd);
exit(127);
}
close(fd[0]);
+ fcntl(fd[1], F_SETFD, FD_CLOEXEC);
return(fd[1]);
}