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/>.
36 static char **environ;
38 static int passdata(FILE *in, FILE *out)
42 struct pollfd pfds[2];
45 memset(pfds, 0, sizeof(struct pollfd) * 2);
46 pfds[0].fd = fileno(in);
47 pfds[0].events = POLLIN;
48 pfds[1].fd = fileno(out);
49 pfds[1].events = POLLHUP;
50 ret = poll(pfds, 2, -1);
53 flog(LOG_ERR, "callcgi: error in poll: %s", strerror(errno));
58 if(pfds[0].revents & (POLLIN | POLLERR | POLLHUP)) {
59 ret = fread(buf, 1, 65536, in);
61 flog(LOG_ERR, "callcgi: could not read input: %s", strerror(errno));
64 if(fwrite(buf, 1, ret, out) != ret) {
65 flog(LOG_ERR, "callcgi: could not write output: %s", strerror(errno));
69 if(pfds[1].revents & POLLHUP)
76 static char *absolutify(char *file)
81 getcwd(cwd, sizeof(cwd));
82 return(sprintf2("%s/%s", cwd, file));
84 return(sstrdup(file));
87 static pid_t forkchild(int inpath, char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
89 char *qp, **env, *name;
96 if((pid = fork()) < 0) {
97 flog(LOG_ERR, "callcgi: could not fork");
107 if((qp = strchr(url, '?')) != NULL)
109 putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
110 putenv("GATEWAY_INTERFACE=CGI/1.1");
111 if(getenv("HTTP_VERSION"))
112 putenv(sprintf2("SERVER_PROTOCOL=%s", getenv("HTTP_VERSION")));
113 putenv(sprintf2("REQUEST_METHOD=%s", method));
115 /* XXX: This is an ugly hack (I think), but though I can think
116 * of several alternatives, none seem to be better. */
117 if(*rest && (strlen(url) >= strlen(rest)) &&
118 !strcmp(rest, url + strlen(url) - strlen(rest))) {
119 name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest)), url);
121 if((pi = unquoteurl(rest)) == NULL)
123 if(!strcmp(name, "/")) {
125 * Normal CGI behavior appears to be to always let
126 * PATH_INFO begin with a slash and never let SCRIPT_NAME
127 * end with one. That conflicts, however, with some
128 * behaviors, such as "mounting" CGI applications on a
129 * directory element of the URI space -- a handler
130 * responding to "/foo/" would not be able to tell that it
131 * is not called "/foo", which makes a large difference,
132 * not least in relation to URI reconstruction and
133 * redirections. A common practical case is CGI-handled
134 * index files in directories. Therefore, this only
135 * handles the nonconditional case of the root directory
136 * and leaves other decisions to the previous handler
137 * handing over the request to callcgi. It is unclear if
138 * there is a better way to handle the problem.
141 pi = sprintf2("/%s", pi);
143 putenv(sprintf2("PATH_INFO=%s", pi));
144 putenv(sprintf2("SCRIPT_NAME=%s", name));
145 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
146 if(getenv("REQ_HOST"))
147 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
148 if(getenv("REQ_X_ASH_SERVER_ADDRESS"))
149 putenv(sprintf2("SERVER_ADDR=%s", getenv("REQ_X_ASH_SERVER_ADDRESS")));
150 if(getenv("REQ_X_ASH_SERVER_PORT"))
151 putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
152 if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
154 if(getenv("REQ_X_ASH_ADDRESS"))
155 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
156 if(getenv("REQ_X_ASH_PORT"))
157 putenv(sprintf2("REMOTE_PORT=%s", getenv("REQ_X_ASH_PORT")));
158 if(getenv("REQ_X_ASH_REMOTE_USER"))
159 putenv(sprintf2("REMOTE_USER=%s", getenv("REQ_X_ASH_REMOTE_USER")));
160 if(getenv("REQ_CONTENT_TYPE"))
161 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
162 if(getenv("REQ_CONTENT_LENGTH"))
163 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
164 for(env = environ; *env; env++) {
165 if(!strncmp(*env, "REQ_", 4))
166 putenv(sprintf2("HTTP_%s", (*env) + 4));
169 * This is (understandably) missing from the CGI
170 * specification, but PHP seems to require it.
172 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
174 execlp(prog, prog, file, NULL);
176 execl(prog, prog, file, NULL);
186 static void trim(struct charbuf *buf)
190 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
191 memmove(buf->b, p, buf->d -= (p - buf->b));
192 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
195 static char **parsecgiheaders(FILE *s)
198 struct charvbuf hbuf;
209 } else if(c == '\n') {
211 } else if(c == EOF) {
217 } else if(state == 1) {
224 } else if(c == '\r') {
225 } else if(c == '\n') {
227 } else if(c == EOF) {
232 } else if(state == 2) {
234 } else if(c == '\n') {
240 } else if(c == EOF) {
256 static char *defstatus(int code)
265 return("No Content");
267 return("Multiple Choices");
269 return("Moved Permanently");
275 return("Not Modified");
277 return("Moved Temporarily");
279 return("Bad Request");
281 return("Unauthorized");
287 return("Internal Server Error");
289 return("Not Implemented");
291 return("Service Unavailable");
293 return("Unknown status");
296 static void sendstatus(char **headers, FILE *out)
299 char *status, *location;
302 status = location = NULL;
304 if(!strcasecmp(hp[0], "status")) {
306 /* Clear this header, so that it is not transmitted by sendheaders. */
308 } else if(!strcasecmp(hp[0], "location")) {
316 if(strchr(status, ' '))
317 fprintf(out, "HTTP/1.1 %s\n", status);
319 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
320 } else if(location) {
321 fprintf(out, "HTTP/1.1 303 See Other\n");
323 fprintf(out, "HTTP/1.1 200 OK\n");
327 static void sendheaders(char **headers, FILE *out)
331 fprintf(out, "%s: %s\n", headers[0], headers[1]);
336 static void usage(void)
338 flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST");
341 int main(int argc, char **argv, char **envp)
344 char *file, *prog, *sp;
353 signal(SIGPIPE, SIG_IGN);
358 while((c = getopt(argc, argv, "cp:")) >= 0) {
373 if(argc - optind < 3) {
377 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
378 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
383 /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2),
384 * but not strictly required, and I get the feeling it might break some
385 * relative paths here or there, so it's not the default for now. */
386 if((sp = strrchr(file, '/')) != NULL) {
398 child = forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
399 in = fdopen(infd, "w");
400 passdata(stdin, in); /* Ignore errors, perhaps? */
402 out = fdopen(outfd, "r");
403 if((headers = parsecgiheaders(out)) == NULL) {
404 flog(LOG_WARNING, "CGI handler returned invalid headers");
407 sendstatus(headers, stdout);
408 sendheaders(headers, stdout);
410 if(passdata(out, stdout))
412 if(waitpid(child, &estat, 0) == child) {
414 flog(LOG_WARNING, "CGI handler `%s' dumped core", prog);
415 if(WIFEXITED(estat) && !WEXITSTATUS(estat))
420 flog(LOG_WARNING, "could not wait for CGI handler: %s", strerror(errno));