Skip to content

Commit cc58373

Browse files
committed
Improved performance while sending error page
this also fixed bug #61785 (Memory leak when access a non-exists file without router)
1 parent bca2d6b commit cc58373

2 files changed

Lines changed: 33 additions & 67 deletions

File tree

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ PHP NEWS
33
?? ??? 2012, PHP 5.4.1 RC1
44

55
- CLI Server:
6+
. Improved performance while sending error page, this also fixed
7+
bug #61785 (Memory leak when access a non-exists file without router).
8+
(Laruence)
69
. Fixed bug #61461 (missing checks around malloc() calls). (Ilia)
710
. Implemented FR #60850 (Built in web server does not set
811
$_SERVER['SCRIPT_FILENAME'] when using router). (Laruence)

sapi/cli/php_cli_server.c

Lines changed: 30 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
#include "ext/standard/html.h"
9999
#include "ext/standard/url.h" /* for php_url_decode() */
100100
#include "ext/standard/php_string.h" /* for php_dirname() */
101-
#include "ext/standard/info.h" /* for php_info_print_style() */
102101
#include "php_network.h"
103102

104103
#include "php_http_parser.h"
@@ -174,8 +173,6 @@ typedef struct php_cli_server_client {
174173
php_cli_server_request request;
175174
unsigned int content_sender_initialized:1;
176175
php_cli_server_content_sender content_sender;
177-
php_cli_server_buffer capture_buffer;
178-
unsigned int capturing:1;
179176
int file_fd;
180177
} php_cli_server_client;
181178

@@ -276,6 +273,27 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
276273

277274
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
278275

276+
/* {{{ static char php_cli_server_css[]
277+
* copied from ext/standard/info.c
278+
*/
279+
static char php_cli_server_css[] = "<style type=\"text/css\">\n" \
280+
"body {background-color: #ffffff; color: #000000;}\n" \
281+
"body, td, th, h1, h2 {font-family: sans-serif;}\n" \
282+
".center {text-align: center;}\n" \
283+
".center table { margin-left: auto; margin-right: auto; text-align: left;}\n" \
284+
".center th { text-align: center !important; }\n" \
285+
"h1 {font-size: 150%;}\n" \
286+
"h2 {font-size: 125%;}\n" \
287+
".p {text-align: left;}\n" \
288+
".e {background-color: #ccccff; font-weight: bold; color: #000000;}\n" \
289+
".h {background-color: #9999cc; font-weight: bold; color: #000000;}\n" \
290+
".v {background-color: #cccccc; color: #000000;}\n" \
291+
".vr {background-color: #cccccc; text-align: right; color: #000000;}\n" \
292+
"img {float: right; border: 0px;}\n" \
293+
"hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}\n" \
294+
"</style>\n";
295+
/* }}} */
296+
279297
static void char_ptr_dtor_p(char **p) /* {{{ */
280298
{
281299
pefree(*p, 1);
@@ -425,17 +443,7 @@ static int sapi_cli_server_ub_write(const char *str, uint str_length TSRMLS_DC)
425443
if (!client) {
426444
return 0;
427445
}
428-
if (client->capturing) {
429-
php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(str_length);
430-
if (!chunk) {
431-
zend_bailout();
432-
}
433-
memmove(chunk->data.heap.p, str, str_length);
434-
php_cli_server_buffer_append(&client->capture_buffer, chunk);
435-
return str_length;
436-
} else {
437-
return php_cli_server_client_send_through(client, str, str_length);
438-
}
446+
return php_cli_server_client_send_through(client, str, str_length);
439447
} /* }}} */
440448

441449
static void sapi_cli_server_flush(void *server_context) /* {{{ */
@@ -470,7 +478,7 @@ static int sapi_cli_server_send_headers(sapi_headers_struct *sapi_headers TSRMLS
470478
sapi_header_struct *h;
471479
zend_llist_position pos;
472480

473-
if (client == NULL || client->capturing || SG(request_info).no_headers) {
481+
if (client == NULL || SG(request_info).no_headers) {
474482
return SAPI_HEADER_SENT_SUCCESSFULLY;
475483
}
476484

@@ -1677,18 +1685,6 @@ static void destroy_request_info(sapi_request_info *request_info) /* {{{ */
16771685
{
16781686
} /* }}} */
16791687

1680-
static void php_cli_server_client_begin_capture(php_cli_server_client *client) /* {{{ */
1681-
{
1682-
php_cli_server_buffer_ctor(&client->capture_buffer);
1683-
client->capturing = 1;
1684-
} /* }}} */
1685-
1686-
static void php_cli_server_client_end_capture(php_cli_server_client *client) /* {{{ */
1687-
{
1688-
client->capturing = 0;
1689-
php_cli_server_buffer_dtor(&client->capture_buffer);
1690-
} /* }}} */
1691-
16921688
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) /* {{{ */
16931689
{
16941690
client->server = server;
@@ -1713,7 +1709,6 @@ static int php_cli_server_client_ctor(php_cli_server_client *client, php_cli_ser
17131709
return FAILURE;
17141710
}
17151711
client->content_sender_initialized = 0;
1716-
client->capturing = 0;
17171712
client->file_fd = -1;
17181713
return SUCCESS;
17191714
} /* }}} */
@@ -1730,9 +1725,6 @@ static void php_cli_server_client_dtor(php_cli_server_client *client) /* {{{ */
17301725
if (client->content_sender_initialized) {
17311726
php_cli_server_content_sender_dtor(&client->content_sender);
17321727
}
1733-
if (client->capturing) {
1734-
php_cli_server_buffer_dtor(&client->capture_buffer);
1735-
}
17361728
} /* }}} */
17371729

17381730
static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
@@ -1768,43 +1760,11 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
17681760
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
17691761
}
17701762
{
1771-
int err = 0;
1772-
zval *style = NULL;
1773-
zend_try {
1774-
if (!SG(sapi_started)) {
1775-
php_output_activate(TSRMLS_C);
1776-
}
1777-
php_output_start_user(NULL, 0, PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
1778-
php_info_print_style(TSRMLS_C);
1779-
MAKE_STD_ZVAL(style);
1780-
php_output_get_contents(style TSRMLS_CC);
1781-
php_output_discard(TSRMLS_C);
1782-
if (!SG(sapi_started)) {
1783-
static int (*send_header_func)(sapi_headers_struct * TSRMLS_DC);
1784-
send_header_func = sapi_module.send_headers;
1785-
/* we don't want the header to be sent now */
1786-
sapi_module.send_headers = sapi_cli_server_discard_headers;
1787-
php_output_deactivate(TSRMLS_C);
1788-
sapi_module.send_headers = send_header_func;
1789-
}
1790-
if (style && Z_STRVAL_P(style)) {
1791-
char *block = pestrndup(Z_STRVAL_P(style), Z_STRLEN_P(style), 1);
1792-
php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new(block, block, Z_STRLEN_P(style));
1793-
if (!chunk) {
1794-
zval_ptr_dtor(&style);
1795-
goto fail;
1796-
}
1797-
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
1798-
zval_ptr_dtor(&style);
1799-
} else {
1800-
err = 1;
1801-
}
1802-
} zend_catch {
1803-
err = 1;
1804-
} zend_end_try();
1805-
if (err) {
1763+
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(php_cli_server_css, sizeof(php_cli_server_css) - 1);
1764+
if (!chunk) {
18061765
goto fail;
18071766
}
1767+
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
18081768
}
18091769
{
18101770
static const char template[] = "</head><body>";
@@ -2052,10 +2012,13 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
20522012
if (server->router) {
20532013
static int (*send_header_func)(sapi_headers_struct * TSRMLS_DC);
20542014
send_header_func = sapi_module.send_headers;
2055-
/* we don't want the header to be sent now */
2015+
/* do not generate default content type header */
2016+
SG(sapi_headers).send_default_content_type = 0;
2017+
/* we don't want headers to be sent */
20562018
sapi_module.send_headers = sapi_cli_server_discard_headers;
20572019
php_request_shutdown(0);
20582020
sapi_module.send_headers = send_header_func;
2021+
SG(sapi_headers).send_default_content_type = 1;
20592022
SG(rfc1867_uploaded_files) = NULL;
20602023
}
20612024
if (SUCCESS != php_cli_server_begin_send_static(server, client TSRMLS_CC)) {

0 commit comments

Comments
 (0)