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)
88 putenv(sprintf2("SERVER_SOFTWARE=ashd/%s", VERSION));
89 putenv("GATEWAY_INTERFACE=CGI/1.1");
90 if(getenv("HTTP_VERSION"))
91 putenv(sprintf2("SERVER_PROTOCOL=%s", getenv("HTTP_VERSION")));
92 putenv(sprintf2("REQUEST_METHOD=%s", method));
93 putenv(sprintf2("PATH_INFO=%s", rest));
94 putenv(sprintf2("SCRIPT_NAME=%s", url));
95 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
96 if(getenv("REQ_HOST"))
97 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
98 if(getenv("REQ_X_ASH_SERVER_PORT"))
99 putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
100 if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
102 if(getenv("REQ_X_ASH_ADDRESS"))
103 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
104 if(getenv("REQ_CONTENT_TYPE"))
105 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
106 if(getenv("REQ_CONTENT_LENGTH"))
107 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
108 for(env = environ; *env; env++) {
109 if(!strncmp(*env, "REQ_", 4))
110 putenv(sprintf2("HTTP_%s", (*env) + 4));
113 * This is (understandably) missing from the CGI
114 * specification, but PHP seems to require it.
116 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
118 execlp(prog, prog, file, NULL);
120 execl(prog, prog, file, NULL);
129 static void trim(struct charbuf *buf)
133 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
134 memmove(buf->b, p, buf->d -= (p - buf->b));
135 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
138 static char **parseheaders(FILE *s)
141 struct charvbuf hbuf;
152 } else if(c == '\n') {
154 } else if(c == EOF) {
160 } else if(state == 1) {
167 } else if(c == '\r') {
168 } else if(c == '\n') {
170 } else if(c == EOF) {
175 } else if(state == 2) {
177 } else if(c == '\n') {
183 } else if(c == EOF) {
199 static char *defstatus(int code)
208 return("No Content");
210 return("Multiple Choices");
212 return("Moved Permanently");
218 return("Not Modified");
220 return("Moved Temporarily");
222 return("Bad Request");
224 return("Unauthorized");
230 return("Internal Server Error");
232 return("Not Implemented");
234 return("Service Unavailable");
236 return("Unknown status");
239 static void sendstatus(char **headers, FILE *out)
242 char *status, *location;
245 status = location = NULL;
247 if(!strcasecmp(hp[0], "status")) {
249 /* Clear this header, so that it is not transmitted by sendheaders. */
251 } else if(!strcasecmp(hp[0], "location")) {
259 if(strchr(status, ' '))
260 fprintf(out, "HTTP/1.1 %s\n", status);
262 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
263 } else if(location) {
264 fprintf(out, "HTTP/1.1 303 See Other\n");
266 fprintf(out, "HTTP/1.1 200 OK\n");
270 static void sendheaders(char **headers, FILE *out)
274 fprintf(out, "%s: %s\n", headers[0], headers[1]);
279 static void usage(void)
281 flog(LOG_ERR, "usage: callcgi [-p PROGRAM] METHOD URL REST");
284 int main(int argc, char **argv, char **envp)
294 signal(SIGPIPE, SIG_IGN);
298 while((c = getopt(argc, argv, "p:")) >= 0) {
310 if(argc - optind < 3) {
314 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
315 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
320 forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
321 in = fdopen(infd, "w");
324 out = fdopen(outfd, "r");
325 if((headers = parseheaders(out)) == NULL) {
326 flog(LOG_WARNING, "CGI handler returned invalid headers");
329 sendstatus(headers, stdout);
330 sendheaders(headers, stdout);
332 passdata(out, stdout);