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/>.
33 static char **environ;
35 static void passdata(FILE *in, FILE *out)
42 ret = fread(buf, 1, 65536, in);
44 flog(LOG_ERR, "sendfile: could not read input: %s", strerror(errno));
47 if(fwrite(buf, 1, ret, out) != ret) {
48 flog(LOG_ERR, "sendfile: could not write output: %s", strerror(errno));
55 static char *absolutify(char *file)
60 getcwd(cwd, sizeof(cwd));
61 return(sprintf2("%s/%s", cwd, file));
63 return(sstrdup(file));
66 static void forkchild(int inpath, char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
75 if((pid = fork()) < 0) {
76 flog(LOG_ERR, "callcgi: could not fork");
84 for(i = 3; i < FD_SETSIZE; i++)
86 if((qp = strchr(url, '?')) != NULL)
89 * XXX: Currently missing:
90 * SERVER_NAME (Partially)
93 putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
94 putenv("GATEWAY_INTERFACE=CGI/1.1");
95 if(getenv("HTTP_VERSION"))
96 putenv(sprintf2("SERVER_PROTOCOL=HTTP/%s", getenv("HTTP_VERSION")));
97 putenv(sprintf2("REQUEST_METHOD=%s", method));
98 putenv(sprintf2("PATH_INFO=%s", rest));
99 putenv(sprintf2("SCRIPT_NAME=%s", url));
100 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
101 if(getenv("REQ_HOST"))
102 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
103 if(getenv("REQ_X_ASH_ADDRESS"))
104 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
105 if(getenv("REQ_CONTENT_TYPE"))
106 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
107 if(getenv("REQ_CONTENT_LENGTH"))
108 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
109 for(env = environ; *env; env++) {
110 if(!strncmp(*env, "REQ_", 4))
111 putenv(sprintf2("HTTP_%s", (*env) + 4));
114 * This is (understandably) missing from the CGI
115 * specification, but PHP seems to require it.
117 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
119 execlp(prog, prog, file, NULL);
121 execl(prog, prog, file, NULL);
130 static void trim(struct charbuf *buf)
134 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
135 memmove(buf->b, p, buf->d -= (p - buf->b));
136 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
139 static char **parseheaders(FILE *s)
142 struct charvbuf hbuf;
153 } else if(c == '\n') {
155 } else if(c == EOF) {
161 } else if(state == 1) {
168 } else if(c == '\r') {
169 } else if(c == '\n') {
171 } else if(c == EOF) {
176 } else if(state == 2) {
178 } else if(c == '\n') {
184 } else if(c == EOF) {
200 static char *defstatus(int code)
209 return("No Content");
211 return("Multiple Choices");
213 return("Moved Permanently");
219 return("Not Modified");
221 return("Moved Temporarily");
223 return("Bad Request");
225 return("Unauthorized");
231 return("Internal Server Error");
233 return("Not Implemented");
235 return("Service Unavailable");
237 return("Unknown status");
240 static void sendstatus(char **headers, FILE *out)
243 char *status, *location;
246 status = location = NULL;
248 if(!strcasecmp(hp[0], "status")) {
250 /* Clear this header, so that it is not transmitted by sendheaders. */
252 } else if(!strcasecmp(hp[0], "location")) {
260 if(strchr(status, ' '))
261 fprintf(out, "HTTP/1.1 %s\n", status);
263 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
264 } else if(location) {
265 fprintf(out, "HTTP/1.1 303 See Other\n");
267 fprintf(out, "HTTP/1.1 200 OK\n");
271 static void sendheaders(char **headers, FILE *out)
275 fprintf(out, "%s: %s\n", headers[0], headers[1]);
280 static void usage(void)
282 flog(LOG_ERR, "usage: callcgi [-p PROGRAM] METHOD URL REST");
285 int main(int argc, char **argv, char **envp)
295 signal(SIGPIPE, SIG_IGN);
299 while((c = getopt(argc, argv, "p:")) >= 0) {
311 if(argc - optind < 3) {
315 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
316 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
321 forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
322 in = fdopen(infd, "w");
325 out = fdopen(outfd, "r");
326 if((headers = parseheaders(out)) == NULL) {
327 flog(LOG_WARNING, "CGI handler returned invalid headers");
330 sendstatus(headers, stdout);
331 sendheaders(headers, stdout);
333 passdata(out, stdout);