free(buf);
}
+static char *absolutify(char *file)
+{
+ char cwd[1024];
+
+ if(*file != '/') {
+ getcwd(cwd, sizeof(cwd));
+ return(sprintf2("%s/%s", cwd, file));
+ }
+ return(sstrdup(file));
+}
+
static void forkchild(int inpath, char *prog, char *file, char *method, char *url, char *rest, int *infd, int *outfd)
{
int i;
* This is (understandably) missing from the CGI
* specification, but PHP seems to require it.
*/
- putenv(sprintf2("SCRIPT_FILENAME=%s", file));
+ putenv(sprintf2("SCRIPT_FILENAME=%s", absolutify(file)));
if(inpath)
execlp(prog, prog, file, NULL);
else
return(NULL);
}
+static char *defstatus(int code)
+{
+ if(code == 200)
+ return("OK");
+ else if(code == 201)
+ return("Created");
+ else if(code == 202)
+ return("Accepted");
+ else if(code == 204)
+ return("No Content");
+ else if(code == 300)
+ return("Multiple Choices");
+ else if(code == 301)
+ return("Moved Permanently");
+ else if(code == 302)
+ return("Found");
+ else if(code == 303)
+ return("See Other");
+ else if(code == 304)
+ return("Not Modified");
+ else if(code == 307)
+ return("Moved Temporarily");
+ else if(code == 400)
+ return("Bad Request");
+ else if(code == 401)
+ return("Unauthorized");
+ else if(code == 403)
+ return("Forbidden");
+ else if(code == 404)
+ return("Not Found");
+ else if(code == 500)
+ return("Internal Server Error");
+ else if(code == 501)
+ return("Not Implemented");
+ else if(code == 503)
+ return("Service Unavailable");
+ else
+ return("Unknown status");
+}
+
static void sendstatus(char **headers, FILE *out)
{
char **hp;
**hp = 0;
} else if(!strcasecmp(hp[0], "location")) {
location = hp[1];
+ hp += 2;
} else {
hp += 2;
}
}
if(status) {
- fprintf(out, "HTTP/1.1 %s\r\n", status);
+ if(strchr(status, ' '))
+ fprintf(out, "HTTP/1.1 %s\n", status);
+ else
+ fprintf(out, "HTTP/1.1 %i %s\n", atoi(status), defstatus(atoi(status)));
} else if(location) {
- fprintf(out, "HTTP/1.1 303 See Other\r\n");
+ fprintf(out, "HTTP/1.1 303 See Other\n");
} else {
- fprintf(out, "HTTP/1.1 200 OK\r\n");
+ fprintf(out, "HTTP/1.1 200 OK\n");
}
}
{
while(*headers) {
if(**headers)
- fprintf(out, "%s: %s\r\n", headers[0], headers[1]);
+ fprintf(out, "%s: %s\n", headers[0], headers[1]);
headers += 2;
}
}
}
sendstatus(headers, stdout);
sendheaders(headers, stdout);
- printf("\r\n");
+ printf("\n");
passdata(out, stdout);
return(0);
}