Skip to content

Fixed bug #62060 (Built-in web server mismatch index files) #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/

/* $Id: php_cli.c 306938 2011-01-01 02:17:06Z felipe $ */
/* $Id$ */

#include <stdio.h>
#include <fcntl.h>
Expand Down Expand Up @@ -1289,7 +1289,8 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
char *buf = safe_pemalloc(1, request->vpath_len, 1 + document_root_len + 1 + sizeof("index.html"), 1);
char *p = buf, *prev_path = NULL, *q, *vpath;
size_t prev_path_len;
int is_static_file = 0;
int is_static_file = 0;
int is_index_file_matched = 0;

if (!buf) {
return;
Expand Down Expand Up @@ -1322,7 +1323,8 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
q = p;
while (q > buf) {
if (!stat(buf, &sb)) {
if (sb.st_mode & S_IFDIR) {
if (!is_index_file_matched && (sb.st_mode & S_IFDIR)) {
is_index_file_matched = 1;
const char **file = index_files;
if (q[-1] != DEFAULT_SLASH) {
*q++ = DEFAULT_SLASH;
Expand All @@ -1345,7 +1347,10 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
}
}
break; /* regular file */
}
} else {
is_index_file_matched = 1;
}

if (prev_path) {
pefree(prev_path, 1);
*q = DEFAULT_SLASH;
Expand All @@ -1355,7 +1360,10 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
prev_path = pestrndup(q, prev_path_len, 1);
*q = '\0';
}
if (prev_path) {
if (!(sb.st_mode & S_IFREG)) {
request->path_translated = NULL;
return;
} else if (prev_path) {
request->path_info_len = prev_path_len;
#ifdef PHP_WIN32
while (prev_path_len--) {
Expand Down
35 changes: 2 additions & 33 deletions sapi/cli/tests/php_cli_server_009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,6 @@ if (!$fp) {
die("connect failed");
}

if(fwrite($fp, <<<HEADER
GET /foo/bar HTTP/1.1
Host: {$host}


HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
}
}

fclose($fp);

$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}


if(fwrite($fp, <<<HEADER
GET /foo/bar/ HTTP/1.0
Expand All @@ -49,6 +30,7 @@ HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
break;
}
}

Expand Down Expand Up @@ -76,18 +58,5 @@ HEADER
fclose($fp);
?>
--EXPECTF--
HTTP/1.1 200 OK
Host: %s
Connection: close
X-Powered-By: PHP/%s
Content-type: text/html

string(8) "/foo/bar"
HTTP/1.0 200 OK
Host: %s
Connection: close
X-Powered-By: PHP/%s
Content-type: text/html

string(9) "/foo/bar/"
HTTP/1.0 404 Not Found
HTTP/1.0 404 Not Found
12 changes: 2 additions & 10 deletions sapi/cli/tests/php_cli_server_010.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
break;
}
}

Expand Down Expand Up @@ -53,16 +54,7 @@ fclose($fp);

?>
--EXPECTF--
HTTP/1.1 200 OK
Host: %s
Connection: close
X-Powered-By: PHP/%s
Content-type: text/html

string(18) "/index.php/foo/bar"
string(10) "/index.php"
string(8) "/foo/bar"
string(7) "foo=bar"
HTTP/1.1 404 Not Found
HTTP/1.0 200 OK
Host: %s
Connection: close
Expand Down
121 changes: 121 additions & 0 deletions sapi/cli/tests/php_cli_server_018.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
--TEST--
Fixed default index files matching
--SKIPIF--
<?php
include "skipif.inc";
?>
--FILE--
<?php
include "php_cli_server.inc";
php_cli_server_start('var_dump($_SERVER["SCRIPT_FILENAME"]);', TRUE);

list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
$port = intval($port)?:80;

$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}

if(fwrite($fp, <<<HEADER
POST /not/such/path HTTP/1.1
Host: {$host}


HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
break;
}
}

fclose($fp);

$exits_dir = __DIR__ . "/real_dir";
mkdir($exits_dir);

file_put_contents($exits_dir . "/index.php", '<?php var_dump($_SERVER["PHP_SELF"], $_SERVER["SCRIPT_NAME"], $_SERVER["PATH_INFO"]); ?>');

$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}

if(fwrite($fp, <<<HEADER
POST /real_dir HTTP/1.1
Host: {$host}


HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
}
}

fclose($fp);

$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}

if(fwrite($fp, <<<HEADER
POST /real_dir/not-exits HTTP/1.1
Host: {$host}


HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
break;
}
}

fclose($fp);

$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
}

if(fwrite($fp, <<<HEADER
POST /real_dir/index.php/path/info HTTP/1.1
Host: {$host}


HEADER
)) {
while (!feof($fp)) {
echo fgets($fp);
}
}

fclose($fp);

@unlink($exits_dir . "/index.php");
@rmdir($exits_dir);
?>
--EXPECTF--
HTTP/1.1 404 Not Found
HTTP/1.1 200 OK
Host: %s
Connection: close
X-Powered-By: PHP/%s
Content-type: text/html

string(19) "/real_dir/index.php"
string(19) "/real_dir/index.php"
NULL
HTTP/1.1 404 Not Found
HTTP/1.1 200 OK
Host: %s
Connection: close
X-Powered-By: PHP/%s
Content-type: text/html

string(29) "/real_dir/index.php/path/info"
string(19) "/real_dir/index.php"
string(10) "/path/info"