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)
69 char *qp, **env, *name;
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));
95 /* XXX: This is an ugly hack (I think), but though I can think
96 * of several alternatives, none seem to be better. */
97 if(*rest && (strlen(url) >= strlen(rest)) &&
98 !strcmp(rest, url + strlen(url) - strlen(rest))) {
99 name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest)), url);
101 putenv(sprintf2("SCRIPT_NAME=%s", name));
102 putenv(sprintf2("QUERY_STRING=%s", qp?qp:""));
103 if(getenv("REQ_HOST"))
104 putenv(sprintf2("SERVER_NAME=%s", getenv("REQ_HOST")));
105 if(getenv("REQ_X_ASH_SERVER_PORT"))
106 putenv(sprintf2("SERVER_PORT=%s", getenv("REQ_X_ASH_SERVER_PORT")));
107 if(getenv("REQ_X_ASH_PROTOCOL") && !strcmp(getenv("REQ_X_ASH_PROTOCOL"), "https"))
109 if(getenv("REQ_X_ASH_ADDRESS"))
110 putenv(sprintf2("REMOTE_ADDR=%s", getenv("REQ_X_ASH_ADDRESS")));
111 if(getenv("REQ_CONTENT_TYPE"))
112 putenv(sprintf2("CONTENT_TYPE=%s", getenv("REQ_CONTENT_TYPE")));
113 if(getenv("REQ_CONTENT_LENGTH"))
114 putenv(sprintf2("CONTENT_LENGTH=%s", getenv("REQ_CONTENT_LENGTH")));
115 for(env = environ; *env; env++) {
116 if(!strncmp(*env, "REQ_", 4))
117 putenv(sprintf2("HTTP_%s", (*env) + 4));
120 * This is (understandably) missing from the CGI
121 * specification, but PHP seems to require it.
123 putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
125 execlp(prog, prog, file, NULL);
127 execl(prog, prog, file, NULL);
136 static void trim(struct charbuf *buf)
140 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
141 memmove(buf->b, p, buf->d -= (p - buf->b));
142 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
145 static char **parseheaders(FILE *s)
148 struct charvbuf hbuf;
159 } else if(c == '\n') {
161 } else if(c == EOF) {
167 } else if(state == 1) {
174 } else if(c == '\r') {
175 } else if(c == '\n') {
177 } else if(c == EOF) {
182 } else if(state == 2) {
184 } else if(c == '\n') {
190 } else if(c == EOF) {
206 static char *defstatus(int code)
215 return("No Content");
217 return("Multiple Choices");
219 return("Moved Permanently");
225 return("Not Modified");
227 return("Moved Temporarily");
229 return("Bad Request");
231 return("Unauthorized");
237 return("Internal Server Error");
239 return("Not Implemented");
241 return("Service Unavailable");
243 return("Unknown status");
246 static void sendstatus(char **headers, FILE *out)
249 char *status, *location;
252 status = location = NULL;
254 if(!strcasecmp(hp[0], "status")) {
256 /* Clear this header, so that it is not transmitted by sendheaders. */
258 } else if(!strcasecmp(hp[0], "location")) {
266 if(strchr(status, ' '))
267 fprintf(out, "HTTP/1.1 %s\n", status);
269 fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
270 } else if(location) {
271 fprintf(out, "HTTP/1.1 303 See Other\n");
273 fprintf(out, "HTTP/1.1 200 OK\n");
277 static void sendheaders(char **headers, FILE *out)
281 fprintf(out, "%s: %s\n", headers[0], headers[1]);
286 static void usage(void)
288 flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST");
291 int main(int argc, char **argv, char **envp)
294 char *file, *prog, *sp;
301 signal(SIGPIPE, SIG_IGN);
306 while((c = getopt(argc, argv, "cp:")) >= 0) {
321 if(argc - optind < 3) {
325 if((file = getenv("REQ_X_ASH_FILE")) == NULL) {
326 flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header");
331 /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2),
332 * but not strictly required, and I get the feeling it might break some
333 * relative paths here or there, so it's not the default for now. */
334 if((sp = strrchr(file, '/')) != NULL) {
346 forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);
347 in = fdopen(infd, "w");
350 out = fdopen(outfd, "r");
351 if((headers = parseheaders(out)) == NULL) {
352 flog(LOG_WARNING, "CGI handler returned invalid headers");
355 sendstatus(headers, stdout);
356 sendheaders(headers, stdout);
358 passdata(out, stdout);