summaryrefslogtreecommitdiff
path: root/sapi/cli
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2012-05-28 16:52:15 +0200
committerOndřej Surý <ondrej@sury.org>2012-05-28 16:52:15 +0200
commit01c525f668ecff08bea21c4ff22745b8f77e8c3a (patch)
tree07ebb675549d7a8ceb905676e4894151122321ac /sapi/cli
parentd4d61a2bcb9975c8aeddbc6603211064174087a9 (diff)
downloadphp-upstream/5.4.4_rc1.tar.gz
Imported Upstream version 5.4.4~rc1upstream/5.4.4_rc1
Diffstat (limited to 'sapi/cli')
-rw-r--r--sapi/cli/php_cli.c11
-rw-r--r--sapi/cli/php_cli_server.c137
-rw-r--r--sapi/cli/tests/bug61546.phpt22
-rw-r--r--sapi/cli/tests/bug61977.phpt157
-rw-r--r--sapi/cli/tests/php_cli_server.inc20
-rw-r--r--sapi/cli/tests/php_cli_server_012.phpt2
-rw-r--r--sapi/cli/tests/php_cli_server_014.phpt4
-rw-r--r--sapi/cli/tests/php_cli_server_016.phpt2
-rw-r--r--sapi/cli/tests/php_cli_server_017.phpt2
9 files changed, 263 insertions, 94 deletions
diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c
index 801e53ba2..205b9db3f 100644
--- a/sapi/cli/php_cli.c
+++ b/sapi/cli/php_cli.c
@@ -662,7 +662,7 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */
int php_optind = 1, orig_optind = 1;
char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
char *arg_free=NULL, **arg_excp=&arg_free;
- char *script_file=NULL;
+ char *script_file=NULL, *translated_path = NULL;
int interactive=0;
int lineno = 0;
const char *param_error=NULL;
@@ -927,8 +927,13 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */
if (script_file) {
if (cli_seek_file_begin(&file_handle, script_file, &lineno TSRMLS_CC) != SUCCESS) {
goto err;
+ } else {
+ char real_path[MAXPATHLEN];
+ if (VCWD_REALPATH(script_file, real_path)) {
+ translated_path = strdup(real_path);
+ }
+ script_filename = script_file;
}
- script_filename = script_file;
} else {
/* We could handle PHP_MODE_PROCESS_STDIN in a different manner */
/* here but this would make things only more complicated. And it */
@@ -947,7 +952,7 @@ static int do_cli(int argc, char **argv TSRMLS_DC) /* {{{ */
SG(request_info).argc=argc-php_optind+1;
arg_excp = argv+php_optind-1;
arg_free = argv[php_optind-1];
- SG(request_info).path_translated = (char*)file_handle.filename;
+ SG(request_info).path_translated = translated_path? translated_path: (char*)file_handle.filename;
argv[php_optind-1] = (char*)file_handle.filename;
SG(request_info).argv=argv+php_optind-1;
diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c
index 79ccea37d..87ab7b48f 100644
--- a/sapi/cli/php_cli_server.c
+++ b/sapi/cli/php_cli_server.c
@@ -98,7 +98,6 @@
#include "ext/standard/html.h"
#include "ext/standard/url.h" /* for php_url_decode() */
#include "ext/standard/php_string.h" /* for php_dirname() */
-#include "ext/standard/info.h" /* for php_info_print_style() */
#include "php_network.h"
#include "php_http_parser.h"
@@ -174,8 +173,6 @@ typedef struct php_cli_server_client {
php_cli_server_request request;
unsigned int content_sender_initialized:1;
php_cli_server_content_sender content_sender;
- php_cli_server_buffer capture_buffer;
- unsigned int capturing:1;
int file_fd;
} php_cli_server_client;
@@ -254,15 +251,17 @@ static php_cli_server_http_reponse_status_code_pair template_map[] = {
};
static php_cli_server_ext_mime_type_pair mime_type_map[] = {
+ { "html", "text/html" },
+ { "htm", "text/html" },
+ { "js", "text/javascript" },
+ { "css", "text/css" },
{ "gif", "image/gif" },
- { "png", "image/png" },
- { "jpe", "image/jpeg" },
{ "jpg", "image/jpeg" },
{ "jpeg", "image/jpeg" },
- { "css", "text/css" },
- { "html", "text/html" },
+ { "png", "image/png" },
+ { "jpe", "image/jpeg" },
+ { "svg", "image/svg+xml" },
{ "txt", "text/plain" },
- { "js", "text/javascript" },
{ NULL, NULL }
};
@@ -276,6 +275,27 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
+/* {{{ static char php_cli_server_css[]
+ * copied from ext/standard/info.c
+ */
+static const char php_cli_server_css[] = "<style type=\"text/css\">\n" \
+ "body {background-color: #ffffff; color: #000000;}\n" \
+ "body, td, th, h1, h2 {font-family: sans-serif;}\n" \
+ ".center {text-align: center;}\n" \
+ ".center table { margin-left: auto; margin-right: auto; text-align: left;}\n" \
+ ".center th { text-align: center !important; }\n" \
+ "h1 {font-size: 150%;}\n" \
+ "h2 {font-size: 125%;}\n" \
+ ".p {text-align: left;}\n" \
+ ".e {background-color: #ccccff; font-weight: bold; color: #000000;}\n" \
+ ".h {background-color: #9999cc; font-weight: bold; color: #000000;}\n" \
+ ".v {background-color: #cccccc; color: #000000;}\n" \
+ ".vr {background-color: #cccccc; text-align: right; color: #000000;}\n" \
+ "img {float: right; border: 0px;}\n" \
+ "hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}\n" \
+ "</style>\n";
+/* }}} */
+
static void char_ptr_dtor_p(char **p) /* {{{ */
{
pefree(*p, 1);
@@ -425,17 +445,7 @@ static int sapi_cli_server_ub_write(const char *str, uint str_length TSRMLS_DC)
if (!client) {
return 0;
}
- if (client->capturing) {
- php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(str_length);
- if (!chunk) {
- zend_bailout();
- }
- memmove(chunk->data.heap.p, str, str_length);
- php_cli_server_buffer_append(&client->capture_buffer, chunk);
- return str_length;
- } else {
- return php_cli_server_client_send_through(client, str, str_length);
- }
+ return php_cli_server_client_send_through(client, str, str_length);
} /* }}} */
static void sapi_cli_server_flush(void *server_context) /* {{{ */
@@ -470,7 +480,7 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers TSRMLS
sapi_header_struct *h;
zend_llist_position pos;
- if (client == NULL || client->capturing || SG(request_info).no_headers) {
+ if (client == NULL || SG(request_info).no_headers) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
@@ -1277,8 +1287,8 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
struct stat sb;
static const char *index_files[] = { "index.php", "index.html", NULL };
char *buf = safe_pemalloc(1, request->vpath_len, 1 + document_root_len + 1 + sizeof("index.html"), 1);
- char *p = buf, *prev_patch = 0, *q, *vpath;
- size_t prev_patch_len;
+ char *p = buf, *prev_path = NULL, *q, *vpath;
+ size_t prev_path_len;
int is_static_file = 0;
if (!buf) {
@@ -1327,8 +1337,8 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
file++;
}
if (!*file || is_static_file) {
- if (prev_patch) {
- pefree(prev_patch, 1);
+ if (prev_path) {
+ pefree(prev_path, 1);
}
pefree(buf, 1);
return;
@@ -1336,25 +1346,25 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
}
break; /* regular file */
}
- if (prev_patch) {
- pefree(prev_patch, 1);
+ if (prev_path) {
+ pefree(prev_path, 1);
*q = DEFAULT_SLASH;
}
while (q > buf && *(--q) != DEFAULT_SLASH);
- prev_patch_len = p - q;
- prev_patch = pestrndup(q, prev_patch_len, 1);
+ prev_path_len = p - q;
+ prev_path = pestrndup(q, prev_path_len, 1);
*q = '\0';
}
- if (prev_patch) {
- request->path_info_len = prev_patch_len;
+ if (prev_path) {
+ request->path_info_len = prev_path_len;
#ifdef PHP_WIN32
- while (prev_patch_len--) {
- if (prev_patch[prev_patch_len] == '\\') {
- prev_patch[prev_patch_len] = '/';
+ while (prev_path_len--) {
+ if (prev_path[prev_path_len] == '\\') {
+ prev_path[prev_path_len] = '/';
}
}
#endif
- request->path_info = prev_patch;
+ request->path_info = prev_path;
pefree(request->vpath, 1);
request->vpath = pestrndup(vpath, q - vpath, 1);
request->vpath_len = q - vpath;
@@ -1677,18 +1687,6 @@ static void destroy_request_info(sapi_request_info *request_info) /* {{{ */
{
} /* }}} */
-static void php_cli_server_client_begin_capture(php_cli_server_client *client) /* {{{ */
-{
- php_cli_server_buffer_ctor(&client->capture_buffer);
- client->capturing = 1;
-} /* }}} */
-
-static void php_cli_server_client_end_capture(php_cli_server_client *client) /* {{{ */
-{
- client->capturing = 0;
- php_cli_server_buffer_dtor(&client->capture_buffer);
-} /* }}} */
-
static int php_cli_server_client_ctor(php_cli_server_client *client, php_cli_server *server, int client_sock, struct sockaddr *addr, socklen_t addr_len TSRMLS_DC) /* {{{ */
{
client->server = server;
@@ -1713,7 +1711,6 @@ static int php_cli_server_client_ctor(php_cli_server_client *client, php_cli_ser
return FAILURE;
}
client->content_sender_initialized = 0;
- client->capturing = 0;
client->file_fd = -1;
return SUCCESS;
} /* }}} */
@@ -1730,9 +1727,6 @@ static void php_cli_server_client_dtor(php_cli_server_client *client) /* {{{ */
if (client->content_sender_initialized) {
php_cli_server_content_sender_dtor(&client->content_sender);
}
- if (client->capturing) {
- php_cli_server_buffer_dtor(&client->capture_buffer);
- }
} /* }}} */
static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
@@ -1768,43 +1762,11 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
{
- int err = 0;
- zval *style = NULL;
- zend_try {
- if (!SG(sapi_started)) {
- php_output_activate(TSRMLS_C);
- }
- php_output_start_user(NULL, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
- php_info_print_style(TSRMLS_C);
- MAKE_STD_ZVAL(style);
- php_output_get_contents(style TSRMLS_CC);
- php_output_discard(TSRMLS_C);
- if (!SG(sapi_started)) {
- static int (*send_header_func)(sapi_headers_struct * TSRMLS_DC);
- send_header_func = sapi_module.send_headers;
- /* we don't want the header to be sent now */
- sapi_module.send_headers = sapi_cli_server_discard_headers;
- php_output_deactivate(TSRMLS_C);
- sapi_module.send_headers = send_header_func;
- }
- if (style && Z_STRVAL_P(style)) {
- char *block = pestrndup(Z_STRVAL_P(style), Z_STRLEN_P(style), 1);
- php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new(block, block, Z_STRLEN_P(style));
- if (!chunk) {
- zval_ptr_dtor(&style);
- goto fail;
- }
- php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
- zval_ptr_dtor(&style);
- } else {
- err = 1;
- }
- } zend_catch {
- err = 1;
- } zend_end_try();
- if (err) {
+ php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(php_cli_server_css, sizeof(php_cli_server_css) - 1);
+ if (!chunk) {
goto fail;
}
+ php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
}
{
static const char template[] = "</head><body>";
@@ -2052,10 +2014,13 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
if (server->router) {
static int (*send_header_func)(sapi_headers_struct * TSRMLS_DC);
send_header_func = sapi_module.send_headers;
- /* we don't want the header to be sent now */
+ /* do not generate default content type header */
+ SG(sapi_headers).send_default_content_type = 0;
+ /* we don't want headers to be sent */
sapi_module.send_headers = sapi_cli_server_discard_headers;
php_request_shutdown(0);
sapi_module.send_headers = send_header_func;
+ SG(sapi_headers).send_default_content_type = 1;
SG(rfc1867_uploaded_files) = NULL;
}
if (SUCCESS != php_cli_server_begin_send_static(server, client TSRMLS_CC)) {
diff --git a/sapi/cli/tests/bug61546.phpt b/sapi/cli/tests/bug61546.phpt
new file mode 100644
index 000000000..2cd690f65
--- /dev/null
+++ b/sapi/cli/tests/bug61546.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Bug #61546 (functions related to current script failed when chdir() in cli sapi)
+--FILE--
+<?php
+$php = getenv("TEST_PHP_EXECUTABLE");
+$test_code = <<<PHP
+<?php
+chdir('..');
+var_dump(get_current_user() != "");
+chdir('..');
+var_dump(getmyinode() != false);
+var_dump(getlastmod() != false);
+PHP;
+
+file_put_contents("bug61546_sub.php", $test_code);
+system($php . ' -n bug61546_sub.php');
+unlink("bug61546_sub.php");
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
diff --git a/sapi/cli/tests/bug61977.phpt b/sapi/cli/tests/bug61977.phpt
new file mode 100644
index 000000000..2f198060f
--- /dev/null
+++ b/sapi/cli/tests/bug61977.phpt
@@ -0,0 +1,157 @@
+--TEST--
+Bug #61977 (Need CLI web-server support for files with .htm & svg extensions)
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+include "php_cli_server.inc";
+php_cli_server_start('<?php ?>', true);
+$doc_root = __DIR__;
+
+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");
+}
+
+file_put_contents($doc_root . '/foo.html', '');
+if(fwrite($fp, <<<HEADER
+GET /foo.html HTTP/1.1
+Host: {$host}
+
+
+HEADER
+)) {
+ while (!feof($fp)) {
+ $text = fgets($fp);
+ if (strncasecmp("Content-type:", $text, 13) == 0) {
+ echo "foo.html => ", $text;
+ }
+ }
+}
+@unlink($doc_root . '/foo.html');
+fclose($fp);
+
+
+$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+if (!$fp) {
+ die("connect failed");
+}
+file_put_contents($doc_root . '/foo.htm', '');
+if(fwrite($fp, <<<HEADER
+GET /foo.htm HTTP/1.1
+Host: {$host}
+
+
+HEADER
+)) {
+ while (!feof($fp)) {
+ $text = fgets($fp);
+ if (strncasecmp("Content-type:", $text, 13) == 0) {
+ echo "foo.htm => ", $text;
+ }
+ }
+}
+@unlink($doc_root . '/foo.htm');
+fclose($fp);
+
+
+$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+if (!$fp) {
+ die("connect failed");
+}
+file_put_contents($doc_root . '/foo.svg', '');
+if(fwrite($fp, <<<HEADER
+GET /foo.svg HTTP/1.1
+Host: {$host}
+
+
+HEADER
+)) {
+ while (!feof($fp)) {
+ $text = fgets($fp);
+ if (strncasecmp("Content-type:", $text, 13) == 0) {
+ echo "foo.svg => ", $text;
+ }
+ }
+}
+@unlink($doc_root . '/foo.svg');
+fclose($fp);
+
+$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+if (!$fp) {
+ die("connect failed");
+}
+file_put_contents($doc_root . '/foo.css', '');
+if(fwrite($fp, <<<HEADER
+GET /foo.css HTTP/1.1
+Host: {$host}
+
+
+HEADER
+)) {
+ while (!feof($fp)) {
+ $text = fgets($fp);
+ if (strncasecmp("Content-type:", $text, 13) == 0) {
+ echo "foo.css => ", $text;
+ }
+ }
+}
+@unlink($doc_root . '/foo.css');
+fclose($fp);
+
+$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+if (!$fp) {
+ die("connect failed");
+}
+file_put_contents($doc_root . '/foo.js', '');
+if(fwrite($fp, <<<HEADER
+GET /foo.js HTTP/1.1
+Host: {$host}
+
+
+HEADER
+)) {
+ while (!feof($fp)) {
+ $text = fgets($fp);
+ if (strncasecmp("Content-type:", $text, 13) == 0) {
+ echo "foo.js => ", $text;
+ }
+ }
+}
+@unlink($doc_root . '/foo.js');
+fclose($fp);
+
+$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
+if (!$fp) {
+ die("connect failed");
+}
+file_put_contents($doc_root . '/foo.png', '');
+if(fwrite($fp, <<<HEADER
+GET /foo.png HTTP/1.1
+Host: {$host}
+
+
+HEADER
+)) {
+ while (!feof($fp)) {
+ $text = fgets($fp);
+ if (strncasecmp("Content-type:", $text, 13) == 0) {
+ echo "foo.png => ", $text;
+ }
+ }
+}
+@unlink($doc_root . '/foo.png');
+fclose($fp);
+?>
+--EXPECTF--
+foo.html => Content-Type: text/html; charset=UTF-8
+foo.htm => Content-Type: text/html; charset=UTF-8
+foo.svg => Content-Type: image/svg+xml
+foo.css => Content-Type: text/css; charset=UTF-8
+foo.js => Content-Type: text/javascript; charset=UTF-8
+foo.png => Content-Type: image/png
diff --git a/sapi/cli/tests/php_cli_server.inc b/sapi/cli/tests/php_cli_server.inc
index 44ee76ea7..3479cd0bd 100644
--- a/sapi/cli/tests/php_cli_server.inc
+++ b/sapi/cli/tests/php_cli_server.inc
@@ -1,5 +1,7 @@
<?php
-define ("PHP_CLI_SERVER_ADDRESS", "localhost:8964");
+define ("PHP_CLI_SERVER_HOSTNAME", "localhost");
+define ("PHP_CLI_SERVER_PORT", 8964);
+define ("PHP_CLI_SERVER_ADDRESS", PHP_CLI_SERVER_HOSTNAME.":".PHP_CLI_SERVER_PORT);
function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE) {
$php_executable = getenv('TEST_PHP_EXECUTABLE');
@@ -32,6 +34,17 @@ function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE)
$handle = proc_open($cmd, $descriptorspec, $pipes, $doc_root);
}
+
+ // note: even when server prints 'Listening on localhost:8964...Press Ctrl-C to quit.'
+ // it might not be listening yet...need to wait until fsockopen() call returns
+ $i = 0;
+ while (($i++ < 5) && !($fp = @fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT))) {
+ usleep(10000);
+ }
+
+ if ($fp) {
+ fclose($fp);
+ }
register_shutdown_function(
function($handle) use($router) {
@@ -40,7 +53,10 @@ function php_cli_server_start($code = 'echo "Hello world";', $no_router = FALSE)
},
$handle
);
- usleep(50000);
+ // don't bother sleeping, server is already up
+ // server can take a variable amount of time to be up, so just sleeping a guessed amount of time
+ // does not work. this is why tests sometimes pass and sometimes fail. to get a reliable pass
+ // sleeping doesn't work.
}
?>
diff --git a/sapi/cli/tests/php_cli_server_012.phpt b/sapi/cli/tests/php_cli_server_012.phpt
index a7d908187..9a1e60c48 100644
--- a/sapi/cli/tests/php_cli_server_012.phpt
+++ b/sapi/cli/tests/php_cli_server_012.phpt
@@ -1,5 +1,5 @@
--TEST--
-Bug #60159 (Router returns false, but POST is not passed to requested resource)
+Bug #60159 (Router returns false, but POST is not passed to requested resource)
--SKIPIF--
<?php
include "skipif.inc";
diff --git a/sapi/cli/tests/php_cli_server_014.phpt b/sapi/cli/tests/php_cli_server_014.phpt
index 4b56caa70..2eca8706b 100644
--- a/sapi/cli/tests/php_cli_server_014.phpt
+++ b/sapi/cli/tests/php_cli_server_014.phpt
@@ -13,6 +13,10 @@ list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
$port = intval($port)?:80;
$output = '';
+// note: select() on Windows (& some other platforms) has historical issues with
+// timeouts less than 1000 millis(0.5). it may be better to increase these
+// timeouts to 1000 millis(1.0) (fsockopen eventually calls select()).
+// see articles like: http://support.microsoft.com/kb/257821
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
if (!$fp) {
die("connect failed");
diff --git a/sapi/cli/tests/php_cli_server_016.phpt b/sapi/cli/tests/php_cli_server_016.phpt
index 3fd065a1c..f15aff124 100644
--- a/sapi/cli/tests/php_cli_server_016.phpt
+++ b/sapi/cli/tests/php_cli_server_016.phpt
@@ -1,7 +1,7 @@
--TEST--
Bug #60591 (Memory leak when access a non-exists file)
--DESCRIPTION--
-this is a indirect test for bug 50691, since mem leak is reproted in the server side
+this is an indirect test for bug 60591, since mem leak is reproted in the server side
and require php compiled with --enable-debug
--SKIPIF--
<?php
diff --git a/sapi/cli/tests/php_cli_server_017.phpt b/sapi/cli/tests/php_cli_server_017.phpt
index 6c414a109..73530af48 100644
--- a/sapi/cli/tests/php_cli_server_017.phpt
+++ b/sapi/cli/tests/php_cli_server_017.phpt
@@ -41,4 +41,4 @@ Connection: close
X-Powered-By: %s
Content-type: text/html
-string(%d) "%s/tests/index.php"
+string(%d) "%sindex.php"