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_X_ASH_REMOTE_USER"))
131 putenv(sprintf2("REMOTE_USER=%s", getenv("REQ_X_ASH_REMOTE_USER")));
132 if(getenv("REQ_CONTENT_TYPE"))
133 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
134 if(getenv("REQ_CONTENT_LENGTH"))
135 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
136 for(env = environ; *env; env++) {
137 if(!strncmp(*env, "REQ_", 4))
138 putenv(sprintf2("HTTP_%s", (*env) + 4));
141 * This is (understandably) missing from the CGI
142 * specification, but PHP seems to require it.
144 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
146 execlp(prog, prog, file, NULL);
148 execl(prog, prog, file, NULL);
158 static void trim(struct charbuf *buf)
162 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
163 memmove(buf->b, p, buf->d -= (p - buf->b));
164 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
167 static char **parseheaders(FILE *s)
170 struct charvbuf hbuf;
181 } else if(c == '\n') {
183 } else if(c == EOF) {
189 } else if(state == 1) {
196 } else if(c == '\r') {
197 } else if(c == '\n') {
199 } else if(c == EOF) {
204 } else if(state == 2) {
206 } else if(c == '\n') {
212 } else if(c == EOF) {
228 static char *defstatus(int code)
237 return("No Content");
239 return("Multiple Choices");
241 return("Moved Permanently");
247 return("Not Modified");
249 return("Moved Temporarily");
251 return("Bad Request");
253 return("Unauthorized");
259 return("Internal Server Error");
261 return("Not Implemented");
263 return("Service Unavailable");
265 return("Unknown status");
268 static void sendstatus(char **headers, FILE *out)
271 char *status, *location;
274 status = location = NULL;
276 if(!strcasecmp(hp[0], "status")) {
278 /* Clear this header, so that it is not transmitted by sendheaders. */
280 } else if(!strcasecmp(hp[0], "location")) {
288 if(strchr(status, ' '))
289 fprintf(out, "HTTP/1.1 %s\n", status);
291 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
292 } else if(location) {
293 fprintf(out, "HTTP/1.1 303 See Other\n");
295 fprintf(out, "HTTP/1.1 200 OK\n");
299 static void sendheaders(char **headers, FILE *out)
303 fprintf(out, "%s: %s\n", headers[0], headers[1]);
308 static void usage(void)
310 flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST");
313 int main(int argc, char **argv, char **envp)
316 char *file, *prog, *sp;
324 signal(SIGPIPE, SIG_IGN);
329 while((c = getopt(argc, argv, "cp:")) >= 0) {
344 if(argc - optind < 3) {
348 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
349 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
354 /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2),
355 * but not strictly required, and I get the feeling it might break some
356 * relative paths here or there, so it's not the default for now. */
357 if((sp = strrchr(file, '/')) != NULL) {
369 child = forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
370 in = fdopen(infd, "w");
371 passdata(stdin, in); /* Ignore errors, perhaps? */
373 out = fdopen(outfd, "r");
374 if((headers = parseheaders(out)) == NULL) {
375 flog(LOG_WARNING, "CGI handler returned invalid headers");
378 sendstatus(headers, stdout);
379 sendheaders(headers, stdout);
381 if(passdata(out, stdout))