From ddbe6906984b8f2b51bf6e575719a930070644f5 Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Wed, 29 Jan 2014 11:42:14 +0000
Subject: [PATCH] CGI directory logic fixed

---
 mongoose.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 44586e824..e53b26d27 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -1131,16 +1131,17 @@ static const char cgi_status[] = "HTTP/1.1 200 OK\r\n";
 
 static void open_cgi_endpoint(struct connection *conn, const char *prog) {
   struct cgi_env_block blk;
-  char dir[MAX_PATH_SIZE];
+  char dir[MAX_PATH_SIZE], *p;
   sock_t fds[2];
 
   prepare_cgi_environment(conn, prog, &blk);
   // 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'.
-  mg_snprintf(dir, sizeof(dir), "%s", prog);
-  if (strrchr(dir, '/') == NULL) {
+  if ((p = strrchr(prog, '/')) == NULL) {
     mg_snprintf(dir, sizeof(dir), "%s", ".");
+  } else {
+    mg_snprintf(dir, sizeof(dir), "%.*s", p - prog, prog);
   }
 
   // Try to create socketpair in a loop until success. mg_socketpair()
-- 
GitLab