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/>.
35 static char **environ;
37 static int passdata(FILE *in, FILE *out)
41 struct pollfd pfds[2];
44 memset(pfds, 0, sizeof(struct pollfd) * 2);
45 pfds[0].fd = fileno(in);
46 pfds[0].events = POLLIN;
47 pfds[1].fd = fileno(out);
48 pfds[1].events = POLLHUP;
49 ret = poll(pfds, 2, -1);
52 flog(LOG_ERR, "callcgi: error in poll: %s", strerror(errno));
57 if(pfds[0].revents & (POLLIN | POLLERR | POLLHUP)) {
58 ret = fread(buf, 1, 65536, in);
60 flog(LOG_ERR, "callcgi: could not read input: %s", strerror(errno));
63 if(fwrite(buf, 1, ret, out) != ret) {
64 flog(LOG_ERR, "callcgi: could not write output: %s", strerror(errno));
68 if(pfds[1].revents & POLLHUP)
75 static char *absolutify(char *file)
80 getcwd(cwd, sizeof(cwd));
81 return(sprintf2("%s/%s", cwd, file));
83 return(sstrdup(file));
86 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");
105 for(i = 3; i < FD_SETSIZE; i++)
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));
114 unqr = unquoteurl(rest);
115 putenv(sprintf2("PATH_INFO=%s", unqr?unqr:rest));
117 /* XXX: This is an ugly hack (I think), but though I can think
118 * of several alternatives, none seem to be better. */
119 if(*rest && (strlen(url) >= strlen(rest)) &&
120 !strcmp(rest, url + strlen(url) - strlen(rest))) {
121 name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest)), url);
123 putenv(sprintf2("SCRIPT_NAME=%s", name));
124 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
125 if(getenv("REQ_HOST"))
126 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
127 if(getenv("REQ_X_ASH_SERVER_PORT"))
128 putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
129 if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
131 if(getenv("REQ_X_ASH_ADDRESS"))
132 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
133 if(getenv("REQ_X_ASH_REMOTE_USER"))
134 putenv(sprintf2("REMOTE_USER=%s", getenv("REQ_X_ASH_REMOTE_USER")));
135 if(getenv("REQ_CONTENT_TYPE"))
136 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
137 if(getenv("REQ_CONTENT_LENGTH"))
138 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
139 for(env = environ; *env; env++) {
140 if(!strncmp(*env, "REQ_", 4))
141 putenv(sprintf2("HTTP_%s", (*env) + 4));
144 * This is (understandably) missing from the CGI
145 * specification, but PHP seems to require it.
147 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
149 execlp(prog, prog, file, NULL);
151 execl(prog, prog, file, NULL);
161 static void trim(struct charbuf *buf)
165 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
166 memmove(buf->b, p, buf->d -= (p - buf->b));
167 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
170 static char **parsecgiheaders(FILE *s)
173 struct charvbuf hbuf;
184 } else if(c == '\n') {
186 } else if(c == EOF) {
192 } else if(state == 1) {
199 } else if(c == '\r') {
200 } else if(c == '\n') {
202 } else if(c == EOF) {
207 } else if(state == 2) {
209 } else if(c == '\n') {
215 } else if(c == EOF) {
231 static char *defstatus(int code)
240 return("No Content");
242 return("Multiple Choices");
244 return("Moved Permanently");
250 return("Not Modified");
252 return("Moved Temporarily");
254 return("Bad Request");
256 return("Unauthorized");
262 return("Internal Server Error");
264 return("Not Implemented");
266 return("Service Unavailable");
268 return("Unknown status");
271 static void sendstatus(char **headers, FILE *out)
274 char *status, *location;
277 status = location = NULL;
279 if(!strcasecmp(hp[0], "status")) {
281 /* Clear this header, so that it is not transmitted by sendheaders. */
283 } else if(!strcasecmp(hp[0], "location")) {
291 if(strchr(status, ' '))
292 fprintf(out, "HTTP/1.1 %s\n", status);
294 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
295 } else if(location) {
296 fprintf(out, "HTTP/1.1 303 See Other\n");
298 fprintf(out, "HTTP/1.1 200 OK\n");
302 static void sendheaders(char **headers, FILE *out)
306 fprintf(out, "%s: %s\n", headers[0], headers[1]);
311 static void usage(void)
313 flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST");
316 int main(int argc, char **argv, char **envp)
319 char *file, *prog, *sp;
327 signal(SIGPIPE, SIG_IGN);
332 while((c = getopt(argc, argv, "cp:")) >= 0) {
347 if(argc - optind < 3) {
351 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
352 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
357 /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2),
358 * but not strictly required, and I get the feeling it might break some
359 * relative paths here or there, so it's not the default for now. */
360 if((sp = strrchr(file, '/')) != NULL) {
372 child = forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
373 in = fdopen(infd, "w");
374 passdata(stdin, in); /* Ignore errors, perhaps? */
376 out = fdopen(outfd, "r");
377 if((headers = parsecgiheaders(out)) == NULL) {
378 flog(LOG_WARNING, "CGI handler returned invalid headers");
381 sendstatus(headers, stdout);
382 sendheaders(headers, stdout);
384 if(passdata(out, stdout))