X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcallcgi.c;h=e51a06a71e3fd7404b9dcf745444230b2cb6c3a8;hb=600a1ce79471493f8cad5fcf118dc9797331d5aa;hp=10bb87c9af47e6223dc35fd237a40fad51d2d98b;hpb=f0a758cc8c1337606a392db67e63cec6601d2730;p=ashd.git diff --git a/src/callcgi.c b/src/callcgi.c index 10bb87c..e51a06a 100644 --- a/src/callcgi.c +++ b/src/callcgi.c @@ -90,14 +90,13 @@ static void forkchild(int inpath, char *prog, char *file, char *method, char *ur if(getenv("HTTP_VERSION")) putenv(sprintf2("SERVER_PROTOCOL=%s", getenv("HTTP_VERSION"))); putenv(sprintf2("REQUEST_METHOD=%s", method)); - putenv(sprintf2("PATH_INFO=/%s", rest)); + putenv(sprintf2("PATH_INFO=%s", rest)); name = url; /* XXX: This is an ugly hack (I think), but though I can think * of several alternatives, none seem to be better. */ - if(*rest && (strlen(url) > strlen(rest)) && - !strcmp(rest, url + strlen(url) - strlen(rest)) && - (url[strlen(url) - strlen(rest) - 1] == '/')) { - name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest) - 1), url); + if(*rest && (strlen(url) >= strlen(rest)) && + !strcmp(rest, url + strlen(url) - strlen(rest))) { + name = sprintf2("%.*s", (int)(strlen(url) - strlen(rest)), url); } putenv(sprintf2("SCRIPT_NAME=%s", name)); putenv(sprintf2("QUERY_STRING=%s", qp?qp:"")); @@ -286,14 +285,14 @@ static void sendheaders(char **headers, FILE *out) static void usage(void) { - flog(LOG_ERR, "usage: callcgi [-p PROGRAM] METHOD URL REST"); + flog(LOG_ERR, "usage: callcgi [-c] [-p PROGRAM] METHOD URL REST"); } int main(int argc, char **argv, char **envp) { int c; - char *file, *prog; - int inpath; + char *file, *prog, *sp; + int inpath, cd; int infd, outfd; FILE *in, *out; char **headers; @@ -303,8 +302,12 @@ int main(int argc, char **argv, char **envp) prog = NULL; inpath = 0; - while((c = getopt(argc, argv, "p:")) >= 0) { + cd = 0; + while((c = getopt(argc, argv, "cp:")) >= 0) { switch(c) { + case 'c': + cd = 1; + break; case 'p': prog = optarg; inpath = 1; @@ -323,6 +326,21 @@ int main(int argc, char **argv, char **envp) flog(LOG_ERR, "callcgi: needs to be called with the X-Ash-File header"); exit(1); } + + if(cd) { + /* This behavior is encouraged by the CGI specification (RFC 3875, 7.2), + * but not strictly required, and I get the feeling it might break some + * relative paths here or there, so it's not the default for now. */ + if((sp = strrchr(file, '/')) != NULL) { + *sp = 0; + if(chdir(file)) { + *sp = '/'; + } else { + file = sp + 1; + } + } + } + if(prog == NULL) prog = file; forkchild(inpath, prog, file, argv[optind], argv[optind + 1], argv[optind + 2], &infd, &outfd);