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/>.
34 static char **environ;
36 static int passdata(FILE *in, FILE *out)
40 struct pollfd pfds[2];
43 memset(pfds, 0, sizeof(struct pollfd) * 2);
44 pfds[0].fd = fileno(in);
45 pfds[0].events = POLLIN;
46 pfds[1].fd = fileno(out);
47 pfds[1].events = POLLHUP;
48 ret = poll(pfds, 2, -1);
51 flog(LOG_ERR, "callcgi: error in poll: %s", strerror(errno));
56 if(pfds[0].revents & (POLLIN | POLLERR | POLLHUP)) {
57 ret = fread(buf, 1, 65536, in);
59 flog(LOG_ERR, "callcgi: could not read input: %s", strerror(errno));
62 if(fwrite(buf, 1, ret, out) != ret) {
63 flog(LOG_ERR, "callcgi: could not write output: %s", strerror(errno));
67 if(pfds[1].revents & POLLHUP)
74 static char *absolutify(char *file)
79 getcwd(cwd, sizeof(cwd));
80 return(sprintf2("%s/%s", cwd, file));
82 return(sstrdup(file));
85 static pid_t forkchild(int inpath, char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
88 char *qp, **env, *name;
94 if((pid = fork()) < 0) {
95 flog(LOG_ERR, "callcgi: could not fork");
103 for(i = 3; i < FD_SETSIZE; i++)
105 if((qp = strchr(url, '?')) != NULL)
107 putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
108 putenv("GATEWAY_INTERFACE=CGI/1.1");
109 if(getenv("HTTP_VERSION"))
110 putenv(sprintf2("SERVER_PROTOCOL=%s", getenv("HTTP_VERSION")));
111 putenv(sprintf2("REQUEST_METHOD=%s", method));
112 putenv(sprintf2("PATH_INFO=%s", rest));
114 /* XXX: This is an ugly hack (I think), but though I can think
115 * of several alternatives, none seem to be better. */
116 if(*rest && (strlen(url) >= strlen(rest)) &&
117 !strcmp(rest, url + strlen(url) - strlen(rest))) {
118 name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest)), url);
120 putenv(sprintf2("SCRIPT_NAME=%s", name));
121 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
122 if(getenv("REQ_HOST"))
123 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
124 if(getenv("REQ_X_ASH_SERVER_PORT"))
125 putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
126 if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
128 if(getenv("REQ_X_ASH_ADDRESS"))
129 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
130 if(getenv("REQ_CONTENT_TYPE"))
131 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
132 if(getenv("REQ_CONTENT_LENGTH"))
133 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
134 for(env = environ; *env; env++) {
135 if(!strncmp(*env, "REQ_", 4))
136 putenv(sprintf2("HTTP_%s", (*env) + 4));
139 * This is (understandably) missing from the CGI
140 * specification, but PHP seems to require it.
142 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
144 execlp(prog, prog, file, NULL);
146 execl(prog, prog, file, NULL);
156 static void trim(struct charbuf *buf)
160 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
161 memmove(buf->b, p, buf->d -= (p - buf->b));
162 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
165 static char **parseheaders(FILE *s)
168 struct charvbuf hbuf;
179 } else if(c == '\n') {
181 } else if(c == EOF) {
187 } else if(state == 1) {
194 } else if(c == '\r') {
195 } else if(c == '\n') {
197 } else if(c == EOF) {
202 } else if(state == 2) {
204 } else if(c == '\n') {
210 } else if(c == EOF) {
226 static char *defstatus(int code)
235 return("No Content");
237 return("Multiple Choices");
239 return("Moved Permanently");
245 return("Not Modified");
247 return("Moved Temporarily");
249 return("Bad Request");
251 return("Unauthorized");
257 return("Internal Server Error");
259 return("Not Implemented");
261 return("Service Unavailable");
263 return("Unknown status");
266 static void sendstatus(char **headers, FILE *out)
269 char *status, *location;
272 status = location = NULL;
274 if(!strcasecmp(hp[0], "status")) {
276 /* Clear this header, so that it is not transmitted by sendheaders. */
278 } else if(!strcasecmp(hp[0], "location")) {
286 if(strchr(status, ' '))
287 fprintf(out, "HTTP/1.1 %s\n", status);
289 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
290 } else if(location) {
291 fprintf(out, "HTTP/1.1 303 See Other\n");
293 fprintf(out, "HTTP/1.1 200 OK\n");
297 static void sendheaders(char **headers, FILE *out)
301 fprintf(out, "%s: %s\n", headers[0], headers[1]);
306 static void usage(void)
308 flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST");
311 int main(int argc, char **argv, char **envp)
314 char *file, *prog, *sp;
322 signal(SIGPIPE, SIG_IGN);
327 while((c = getopt(argc, argv, "cp:")) >= 0) {
342 if(argc - optind < 3) {
346 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
347 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
352 /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2),
353 * but not strictly required, and I get the feeling it might break some
354 * relative paths here or there, so it's not the default for now. */
355 if((sp = strrchr(file, '/')) != NULL) {
367 child = forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
368 in = fdopen(infd, "w");
369 passdata(stdin, in); /* Ignore errors, perhaps? */
371 out = fdopen(outfd, "r");
372 if((headers = parseheaders(out)) == NULL) {
373 flog(LOG_WARNING, "CGI handler returned invalid headers");
376 sendstatus(headers, stdout);
377 sendheaders(headers, stdout);
379 if(passdata(out, stdout))