From d864d7247073cc0b2fdc018b1148d7961a5d9ada Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Wed, 17 Jul 2013 08:24:52 +0100
Subject: [PATCH] On windows, enclosing path to CGI script in quotes to allow
 paths with spaces

---
 mongoose.c                     |  2 +-
 test/dir with spaces/hello.cgi |  3 +++
 test/test.pl                   | 24 +++++++++++++-----------
 3 files changed, 17 insertions(+), 12 deletions(-)
 create mode 100755 test/dir with spaces/hello.cgi

diff --git a/mongoose.c b/mongoose.c
index 27d271eae..30de4f161 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -1341,7 +1341,7 @@ static pid_t spawn_process(struct mg_connection *conn, const char *prog,
   }
   GetFullPathNameA(dir, sizeof(full_dir), full_dir, NULL);
 
-  mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%s%s\\%s",
+  mg_snprintf(conn, cmdline, sizeof(cmdline), "%s%s\"%s\\%s\"",
               interp, interp[0] == '\0' ? "" : " ", full_dir, prog);
 
   DEBUG_TRACE(("Running [%s]", cmdline));
diff --git a/test/dir with spaces/hello.cgi b/test/dir with spaces/hello.cgi
new file mode 100755
index 000000000..39decbeb1
--- /dev/null
+++ b/test/dir with spaces/hello.cgi	
@@ -0,0 +1,3 @@
+#!/usr/bin/env perl
+
+print "Content-Type: text/plain\n\nhello\n";
diff --git a/test/test.pl b/test/test.pl
index f6bda36cb..45d1865b4 100644
--- a/test/test.pl
+++ b/test/test.pl
@@ -19,9 +19,10 @@ my $test_dir_uri = "test_dir";
 my $root = 'test';
 my $test_dir = $root . $dir_separator. $test_dir_uri;
 my $config = 'mongoose.conf';
-my $exe = '.' . $dir_separator . 'mongoose';
-my $embed_exe = '.' . $dir_separator . 'embed';
-my $unit_test_exe = '.' . $dir_separator . 'unit_test';
+my $exe_ext = on_windows() ? '.exe' : '';
+my $mongoose_exe = '.' . $dir_separator . 'mongoose' . $exe_ext;
+my $embed_exe = '.' . $dir_separator . 'embed' . $exe_ext;
+my $unit_test_exe = '.' . $dir_separator . 'unit_test' . $exe_ext;
 my $exit_code = 0;
 
 my @files_to_delete = ('debug.log', 'access.log', $config, "$root/a/put.txt",
@@ -51,9 +52,9 @@ sub get_num_of_log_entries {
 # Send the request to the 127.0.0.1:$port and return the reply
 sub req {
   my ($request, $inc, $timeout) = @_;
-  my $sock = IO::Socket::INET->new(Proto=>"tcp",
-    PeerAddr=>'127.0.0.1', PeerPort=>$port);
-  fail("Cannot connect: $!") unless $sock;
+  my $sock = IO::Socket::INET->new(Proto => 6,
+    PeerAddr => '127.0.0.1', PeerPort => $port);
+  fail("Cannot connect to http://127.0.0.1:$port : $!") unless $sock;
   $sock->autoflush(1);
   foreach my $byte (split //, $request) {
     last unless print $sock $byte;
@@ -97,7 +98,6 @@ sub spawn {
   if (on_windows()) {
     my @args = split /\s+/, $cmdline;
     my $executable = $args[0];
-    $executable .= '.exe';
     Win32::Spawn($executable, $cmdline, $pid);
     die "Cannot spawn @_: $!" unless $pid;
   } else {
@@ -155,13 +155,13 @@ if (scalar(@ARGV) > 0 and $ARGV[0] eq 'unit') {
 # Command line options override config files settings
 write_file($config, "access_log_file access.log\n" .
            "listening_ports 127.0.0.1:12345\n");
-spawn("$exe -listening_ports 127.0.0.1:$port");
+spawn("$mongoose_exe -listening_ports 127.0.0.1:$port");
 o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file');
 unlink $config;
 kill_spawned_child();
 
 # Spawn the server on port $port
-my $cmd = "$exe ".
+my $cmd = "$mongoose_exe ".
   "-listening_ports 127.0.0.1:$port ".
   "-access_log_file access.log ".
   "-error_log_file debug.log ".
@@ -352,6 +352,8 @@ unless (scalar(@ARGV) > 0 and $ARGV[0] eq "basic_tests") {
   unlink "$root/.htpasswd";
 
 
+  o("GET /dir%20with%20spaces/hello.cgi HTTP/1.0\n\r\n",
+      'HTTP/1.1 200 OK.+hello', 'CGI script with spaces in path');
   o("GET /env.cgi HTTP/1.0\n\r\n", 'HTTP/1.1 200 OK', 'GET CGI file');
   o("GET /bad2.cgi HTTP/1.0\n\n", "HTTP/1.1 123 Please pass me to the client\r",
     'CGI Status code text');
@@ -410,9 +412,9 @@ unless (scalar(@ARGV) > 0 and $ARGV[0] eq "basic_tests") {
   # Manipulate the passwords file
   my $path = 'test_htpasswd';
   unlink $path;
-  system("$exe -A $path a b c") == 0
+  system("$mongoose_exe -A $path a b c") == 0
     or fail("Cannot add user in a passwd file");
-  system("$exe -A $path a b c2") == 0
+  system("$mongoose_exe -A $path a b c2") == 0
     or fail("Cannot edit user in a passwd file");
   my $content = read_file($path);
   $content =~ /^b:a:\w+$/gs or fail("Bad content of the passwd file");
-- 
GitLab