Skip to content

Commit f8386e3

Browse files
committed
Fixed bug #60159 (Router returns false, but POST is not passed to requested
resource) and bug #55759 (mem leak when use built-in server)
1 parent 0f4ebd9 commit f8386e3

File tree

4 files changed

+225
-59
lines changed

4 files changed

+225
-59
lines changed

NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ PHP NEWS
1919
. Fixed bug #60282 (Segfault when using ob_gzhandler() with open buffers).
2020
(Laruence)
2121

22+
-CLI SAPI:
23+
. Fixed bug #60159 (Router returns false, but POST is not passed to requested
24+
resource). (Laruence)
25+
. Fixed bug #55759 (mem leak when use built-in server). (Laruence)
26+
2227
11 Nov 2011, PHP 5.4.0 RC1
2328
- General improvements:
2429
. Changed silent conversion of array to string to produce a notice. (Patrick)

sapi/cli/php_cli_server.c

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,22 +1815,8 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
18151815

18161816
static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
18171817
{
1818-
php_cli_server_client_populate_request_info(client, &SG(request_info));
1819-
{
1820-
char **auth;
1821-
if (SUCCESS == zend_hash_find(&client->request.headers, "Authorization", sizeof("Authorization"), (void**)&auth)) {
1822-
php_handle_auth_data(*auth TSRMLS_CC);
1823-
}
1824-
}
1825-
SG(sapi_headers).http_response_code = 200;
1826-
if (FAILURE == php_request_startup(TSRMLS_C)) {
1827-
/* should never be happen */
1828-
destroy_request_info(&SG(request_info));
1829-
return FAILURE;
1830-
}
18311818
if (strlen(client->request.path_translated) != client->request.path_translated_len) {
18321819
/* can't handle paths that contain nul bytes */
1833-
destroy_request_info(&SG(request_info));
18341820
return php_cli_server_send_error_page(server, client, 400 TSRMLS_CC);
18351821
}
18361822
{
@@ -1846,9 +1832,6 @@ static int php_cli_server_dispatch_script(php_cli_server *server, php_cli_server
18461832
}
18471833

18481834
php_cli_server_log_response(client, SG(sapi_headers).http_response_code, NULL TSRMLS_CC);
1849-
php_request_shutdown(0);
1850-
php_cli_server_close_connection(server, client TSRMLS_CC);
1851-
destroy_request_info(&SG(request_info));
18521835
return SUCCESS;
18531836
} /* }}} */
18541837

@@ -1910,27 +1893,35 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv
19101893
}
19111894
/* }}} */
19121895

1913-
static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
1914-
{
1915-
int decline = 0;
1916-
1917-
if (!server->router) {
1918-
return 1;
1919-
}
1920-
1896+
static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
1897+
char **auth;
19211898
php_cli_server_client_populate_request_info(client, &SG(request_info));
1922-
{
1923-
char **auth;
1924-
if (SUCCESS == zend_hash_find(&client->request.headers, "Authorization", sizeof("Authorization"), (void**)&auth)) {
1925-
php_handle_auth_data(*auth TSRMLS_CC);
1926-
}
1899+
if (SUCCESS == zend_hash_find(&client->request.headers, "Authorization", sizeof("Authorization"), (void**)&auth)) {
1900+
php_handle_auth_data(*auth TSRMLS_CC);
19271901
}
19281902
SG(sapi_headers).http_response_code = 200;
19291903
if (FAILURE == php_request_startup(TSRMLS_C)) {
19301904
/* should never be happen */
19311905
destroy_request_info(&SG(request_info));
1932-
return -1;
1906+
return FAILURE;
19331907
}
1908+
1909+
return SUCCESS;
1910+
}
1911+
/* }}} */
1912+
1913+
static int php_cli_server_request_shutdown(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
1914+
php_request_shutdown(0);
1915+
php_cli_server_close_connection(server, client TSRMLS_CC);
1916+
destroy_request_info(&SG(request_info));
1917+
SG(server_context) = NULL;
1918+
return SUCCESS;
1919+
}
1920+
/* }}} */
1921+
1922+
static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
1923+
{
1924+
int decline = 0;
19341925
if (!php_handle_special_queries(TSRMLS_C)) {
19351926
zend_file_handle zfd;
19361927
char *old_cwd;
@@ -1965,44 +1956,51 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server
19651956
free_alloca(old_cwd, use_heap);
19661957
}
19671958

1968-
if (decline) {
1969-
php_request_shutdown_for_hook(0);
1970-
} else {
1971-
php_request_shutdown(0);
1972-
php_cli_server_close_connection(server, client TSRMLS_CC);
1973-
}
1974-
destroy_request_info(&SG(request_info));
1975-
1976-
return decline ? 1: 0;
1959+
return decline;
19771960
}
19781961
/* }}} */
19791962

19801963
static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
19811964
{
1982-
int status;
1965+
int is_static_file = 0;
19831966

19841967
SG(server_context) = client;
1985-
status = php_cli_server_dispatch_router(server, client TSRMLS_CC);
1968+
if (client->request.ext_len != 3 || memcmp(client->request.ext, "php", 3) || !client->request.path_translated) {
1969+
is_static_file = 1;
1970+
}
19861971

1987-
if (status < 0) {
1988-
goto fail;
1989-
} else if (status > 0) {
1990-
if (client->request.ext_len == 3 && memcmp(client->request.ext, "php", 3) == 0 && client->request.path_translated) {
1991-
if (SUCCESS != php_cli_server_dispatch_script(server, client TSRMLS_CC) &&
1992-
SUCCESS != php_cli_server_send_error_page(server, client, 500 TSRMLS_CC)) {
1993-
goto fail;
1994-
}
1995-
} else {
1996-
if (SUCCESS != php_cli_server_begin_send_static(server, client TSRMLS_CC)) {
1997-
goto fail;
1998-
}
1972+
if (server->router || !is_static_file) {
1973+
if (FAILURE == php_cli_server_request_startup(server, client TSRMLS_CC)) {
1974+
SG(server_context) = NULL;
1975+
php_cli_server_close_connection(server, client TSRMLS_CC);
1976+
destroy_request_info(&SG(request_info));
1977+
return SUCCESS;
1978+
}
1979+
}
1980+
1981+
if (server->router) {
1982+
if (!php_cli_server_dispatch_router(server, client TSRMLS_CC)) {
1983+
php_cli_server_request_shutdown(server, client TSRMLS_CC);
1984+
return SUCCESS;
19991985
}
20001986
}
2001-
SG(server_context) = 0;
2002-
return SUCCESS;
2003-
fail:
2004-
SG(server_context) = 0;
2005-
php_cli_server_close_connection(server, client TSRMLS_CC);
1987+
1988+
if (!is_static_file) {
1989+
if (SUCCESS == php_cli_server_dispatch_script(server, client TSRMLS_CC)
1990+
|| SUCCESS != php_cli_server_send_error_page(server, client, 500 TSRMLS_CC)) {
1991+
php_cli_server_request_shutdown(server, client TSRMLS_CC);
1992+
return SUCCESS;
1993+
}
1994+
} else {
1995+
if (SUCCESS != php_cli_server_begin_send_static(server, client TSRMLS_CC)) {
1996+
php_cli_server_close_connection(server, client TSRMLS_CC);
1997+
}
1998+
SG(server_context) = NULL;
1999+
return SUCCESS;
2000+
}
2001+
2002+
SG(server_context) = NULL;
2003+
destroy_request_info(&SG(request_info));
20062004
return SUCCESS;
20072005
}
20082006
/* }}} */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
--TEST--
2+
Bug #60159 (Router returns false, but POST is not passed to requested resource)
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
include "php_cli_server.inc";
10+
php_cli_server_start('print_r($_REQUEST); $_REQUEST["foo"] = "bar"; return FALSE;');
11+
$doc_root = __DIR__;
12+
file_put_contents($doc_root . '/request.php', '<?php print_r($_REQUEST); ?>');
13+
14+
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
15+
$port = intval($port)?:80;
16+
17+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
18+
if (!$fp) {
19+
die("connect failed");
20+
}
21+
22+
if(fwrite($fp, <<<HEADER
23+
POST /request.php HTTP/1.1
24+
Host: {$host}
25+
Content-Type: application/x-www-form-urlencoded
26+
Content-Length: 3
27+
28+
a=b
29+
HEADER
30+
)) {
31+
while (!feof($fp)) {
32+
echo fgets($fp);
33+
}
34+
}
35+
36+
fclose($fp);
37+
@unlink($doc_root . '/request.php');
38+
39+
?>
40+
--EXPECTF--
41+
HTTP/1.1 200 OK
42+
Host: %s
43+
Connection: closed
44+
X-Powered-By: PHP/%s
45+
Content-type: text/html
46+
47+
Array
48+
(
49+
[a] => b
50+
)
51+
Array
52+
(
53+
[a] => b
54+
[foo] => bar
55+
)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
--TEST--
2+
No router, no script
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
include "php_cli_server.inc";
10+
php_cli_server_start(NULL, TRUE);
11+
12+
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
13+
$port = intval($port)?:80;
14+
$output = '';
15+
16+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
17+
if (!$fp) {
18+
die("connect failed");
19+
}
20+
21+
22+
if(fwrite($fp, <<<HEADER
23+
POST / HTTP/1.1
24+
Host: {$host}
25+
Content-Type: application/x-www-form-urlencoded
26+
Content-Length: 3
27+
28+
a=b
29+
HEADER
30+
)) {
31+
while (!feof($fp)) {
32+
$output .= fgets($fp);
33+
}
34+
}
35+
36+
echo preg_replace("/<style type=\"text\/css\">(.*?)<\/style>/s", "<style type=\"text/css\">AAA</style>", $output), "\n";
37+
fclose($fp);
38+
39+
40+
$output = '';
41+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
42+
if (!$fp) {
43+
die("connect failed");
44+
}
45+
46+
if(fwrite($fp, <<<HEADER
47+
GET /main/style.css HTTP/1.1
48+
Host: {$host}
49+
50+
51+
HEADER
52+
)) {
53+
while (!feof($fp)) {
54+
$output .= fgets($fp);
55+
}
56+
}
57+
58+
echo preg_replace("/<style type=\"text\/css\">(.*?)<\/style>/s", "<style type=\"text/css\">AAA</style>", $output), "\n";
59+
fclose($fp);
60+
61+
$output = '';
62+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
63+
if (!$fp) {
64+
die("connect failed");
65+
}
66+
67+
if(fwrite($fp, <<<HEADER
68+
HEAD /main/foo/bar HTTP/1.1
69+
Host: {$host}
70+
71+
72+
HEADER
73+
)) {
74+
while (!feof($fp)) {
75+
$output .= fgets($fp);
76+
}
77+
}
78+
79+
echo preg_replace("/<style type=\"text\/css\">(.*?)<\/style>/s", "<style type=\"text/css\">AAA</style>", $output), "\n";
80+
fclose($fp);
81+
?>
82+
--EXPECTF--
83+
84+
HTTP/1.1 404 Not Found
85+
Host: %s
86+
Connection: closed
87+
Content-Type: text/html; charset=UTF-8
88+
Content-Length: %d
89+
90+
<html><head><title>404 Not Found</title><style type="text/css">AAA</style>
91+
</head><body><h1 class="h">Not Found</h1><p>The requested resource / was not found on this server.</p></body></html>
92+
HTTP/1.1 404 Not Found
93+
Host: %s
94+
Connection: closed
95+
Content-Type: text/html; charset=UTF-8
96+
Content-Length: %d
97+
98+
<html><head><title>404 Not Found</title><style type="text/css">AAA</style>
99+
</head><body><h1 class="h">Not Found</h1><p>The requested resource /main/style.css was not found on this server.</p></body></html>
100+
HTTP/1.1 404 Not Found
101+
Host: %s
102+
Connection: closed
103+
Content-Type: text/html; charset=UTF-8
104+
Content-Length: %d
105+
106+
<html><head><title>404 Not Found</title><style type="text/css">AAA</style>
107+
</head><body><h1 class="h">Not Found</h1><p>The requested resource /main/foo/bar was not found on this server.</p></body></html>
108+

0 commit comments

Comments
 (0)