mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-05 22:08:23 +00:00
CGI script execution fixed for windows
This commit is contained in:
1 parent
7865ed7bad
commit
66ba1259f2
1 file changed
+21
-23
+21
-23
@@ -1098,7 +1098,7 @@ static pid_t spawn_process(struct mg_connection *conn, const char *prog,
|
|||||||
char *envblk, char *envp[], int fd_stdin,
|
char *envblk, char *envp[], int fd_stdin,
|
||||||
int fd_stdout, const char *dir) {
|
int fd_stdout, const char *dir) {
|
||||||
HANDLE me;
|
HANDLE me;
|
||||||
char *p, *interp, cmdline[PATH_MAX], line[PATH_MAX];
|
char *p, *interp, cmdline[PATH_MAX], buf[PATH_MAX];
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
STARTUPINFOA si;
|
STARTUPINFOA si;
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
@@ -1122,36 +1122,29 @@ static pid_t spawn_process(struct mg_connection *conn, const char *prog,
|
|||||||
// If CGI file is a script, try to read the interpreter line
|
// If CGI file is a script, try to read the interpreter line
|
||||||
interp = conn->ctx->config[CGI_INTERPRETER];
|
interp = conn->ctx->config[CGI_INTERPRETER];
|
||||||
if (interp == NULL) {
|
if (interp == NULL) {
|
||||||
line[2] = '\0';
|
buf[2] = '\0';
|
||||||
(void) mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%c%s",
|
|
||||||
dir, DIRSEP, prog);
|
|
||||||
if ((fp = fopen(cmdline, "r")) != NULL) {
|
if ((fp = fopen(cmdline, "r")) != NULL) {
|
||||||
(void) fgets(line, sizeof(line), fp);
|
(void) fgets(buf, sizeof(buf), fp);
|
||||||
if (memcmp(line, "#!", 2) != 0)
|
if (buf[0] != '#' || buf[1] != '!') {
|
||||||
line[2] = '\0';
|
// First line does not start with "#!". Do not set interpreter.
|
||||||
// Trim whitespaces from interpreter name
|
buf[2] = '\0';
|
||||||
for (p = &line[strlen(line) - 1]; p > line &&
|
} else {
|
||||||
isspace(*p); p--) {
|
// Trim whitespaces in interpreter name
|
||||||
*p = '\0';
|
for (p = &buf[strlen(buf) - 1]; p > buf && isspace(*p); p--) {
|
||||||
|
*p = '\0';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
(void) fclose(fp);
|
(void) fclose(fp);
|
||||||
}
|
}
|
||||||
interp = line + 2;
|
interp = buf + 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((p = (char *) strrchr(prog, '/')) != NULL) {
|
(void) mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%s%s%c%s",
|
||||||
prog = p + 1;
|
interp, interp[0] == '\0' ? "" : " ", dir, DIRSEP, prog);
|
||||||
}
|
|
||||||
|
|
||||||
(void) mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%s%s",
|
|
||||||
interp, interp[0] == '\0' ? "" : " ", prog);
|
|
||||||
|
|
||||||
(void) mg_snprintf(conn, line, sizeof(line), "%s", dir);
|
|
||||||
change_slashes_to_backslashes(line);
|
|
||||||
|
|
||||||
DEBUG_TRACE(("Running [%s]", cmdline));
|
DEBUG_TRACE(("Running [%s]", cmdline));
|
||||||
if (CreateProcessA(NULL, cmdline, NULL, NULL, TRUE,
|
if (CreateProcessA(NULL, cmdline, NULL, NULL, TRUE,
|
||||||
CREATE_NEW_PROCESS_GROUP, envblk, line, &si, &pi) == 0) {
|
CREATE_NEW_PROCESS_GROUP, envblk, dir, &si, &pi) == 0) {
|
||||||
cry(conn, "%s: CreateProcess(%s): %d",
|
cry(conn, "%s: CreateProcess(%s): %d",
|
||||||
__func__, cmdline, ERRNO);
|
__func__, cmdline, ERRNO);
|
||||||
pi.hProcess = (pid_t) -1;
|
pi.hProcess = (pid_t) -1;
|
||||||
@@ -2849,10 +2842,15 @@ static void handle_cgi_request(struct mg_connection *conn, const char *prog) {
|
|||||||
|
|
||||||
prepare_cgi_environment(conn, prog, &blk);
|
prepare_cgi_environment(conn, prog, &blk);
|
||||||
|
|
||||||
// CGI must be executed in its own directory
|
// CGI must be executed in its own directory. 'dir' must point to the
|
||||||
|
// directory containing executable program, 'p' must point to the
|
||||||
|
// executable program name relative to 'dir'.
|
||||||
(void) mg_snprintf(conn, dir, sizeof(dir), "%s", prog);
|
(void) mg_snprintf(conn, dir, sizeof(dir), "%s", prog);
|
||||||
if ((p = strrchr(dir, DIRSEP)) != NULL) {
|
if ((p = strrchr(dir, DIRSEP)) != NULL) {
|
||||||
*p++ = '\0';
|
*p++ = '\0';
|
||||||
|
} else {
|
||||||
|
dir[0] = '.', dir[1] = '\0';
|
||||||
|
p = (char *) prog;
|
||||||
}
|
}
|
||||||
|
|
||||||
pid = (pid_t) -1;
|
pid = (pid_t) -1;
|
||||||
|
|||||||
Reference in new issue
Block a user