mirror of
https://github.com/php/php-src.git
synced 2025-08-16 14:08:47 +02:00
improvements to cli server
This commit is contained in:
parent
6ab04a6eef
commit
30019f47a5
1 changed files with 161 additions and 124 deletions
|
@ -209,6 +209,12 @@ static php_cli_server_http_response_status_code_pair template_map[] = {
|
||||||
{ 501, "<h1>%s</h1><p>Request method not supported.</p>" }
|
{ 501, "<h1>%s</h1><p>Request method not supported.</p>" }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define PHP_CLI_SERVER_LOG_PROCESS 1
|
||||||
|
#define PHP_CLI_SERVER_LOG_ERROR 2
|
||||||
|
#define PHP_CLI_SERVER_LOG_MESSAGE 3
|
||||||
|
|
||||||
|
static int php_cli_server_log_level = 3;
|
||||||
|
|
||||||
#if HAVE_UNISTD_H
|
#if HAVE_UNISTD_H
|
||||||
static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
|
static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
|
||||||
#endif
|
#endif
|
||||||
|
@ -218,7 +224,7 @@ static const char php_cli_server_request_error_unexpected_eof[] = "Unexpected EO
|
||||||
static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len);
|
static size_t php_cli_server_client_send_through(php_cli_server_client *client, const char *str, size_t str_len);
|
||||||
static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len);
|
static php_cli_server_chunk *php_cli_server_chunk_heap_new_self_contained(size_t len);
|
||||||
static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk);
|
static void php_cli_server_buffer_append(php_cli_server_buffer *buffer, php_cli_server_chunk *chunk);
|
||||||
static void php_cli_server_logf(const char *format, ...);
|
static void php_cli_server_logf(int type, const char *format, ...);
|
||||||
static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message);
|
static void php_cli_server_log_response(php_cli_server_client *client, int status, const char *message);
|
||||||
|
|
||||||
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
|
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
|
||||||
|
@ -744,10 +750,14 @@ static void sapi_cli_server_register_variables(zval *track_vars_array) /* {{{ */
|
||||||
zend_hash_apply_with_arguments(&client->request.headers, (apply_func_args_t)sapi_cli_server_register_entry_cb, 1, track_vars_array);
|
zend_hash_apply_with_arguments(&client->request.headers, (apply_func_args_t)sapi_cli_server_register_entry_cb, 1, track_vars_array);
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
static void sapi_cli_server_log_message(char *msg, int syslog_type_int) /* {{{ */
|
static void sapi_cli_server_log_write(int type, char *msg) /* {{{ */
|
||||||
{
|
{
|
||||||
char buf[52];
|
char buf[52];
|
||||||
|
|
||||||
|
if (php_cli_server_log_level < type) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (php_cli_server_get_system_time(buf) != 0) {
|
if (php_cli_server_get_system_time(buf) != 0) {
|
||||||
memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
|
memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -769,6 +779,11 @@ static void sapi_cli_server_log_message(char *msg, int syslog_type_int) /* {{{ *
|
||||||
#endif
|
#endif
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
static void sapi_cli_server_log_message(char *msg, int syslog_type_int) /* {{{ */
|
||||||
|
{
|
||||||
|
sapi_cli_server_log_write(PHP_CLI_SERVER_LOG_MESSAGE, msg);
|
||||||
|
} /* }}} */
|
||||||
|
|
||||||
/* {{{ sapi_module_struct cli_server_sapi_module
|
/* {{{ sapi_module_struct cli_server_sapi_module
|
||||||
*/
|
*/
|
||||||
sapi_module_struct cli_server_sapi_module = {
|
sapi_module_struct cli_server_sapi_module = {
|
||||||
|
@ -1121,9 +1136,11 @@ static int php_cli_server_content_sender_pull(php_cli_server_content_sender *sen
|
||||||
_nbytes_read = read(fd, chunk->data.heap.p, chunk->data.heap.len);
|
_nbytes_read = read(fd, chunk->data.heap.p, chunk->data.heap.len);
|
||||||
#endif
|
#endif
|
||||||
if (_nbytes_read < 0) {
|
if (_nbytes_read < 0) {
|
||||||
|
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
|
||||||
char *errstr = get_last_error();
|
char *errstr = get_last_error();
|
||||||
php_cli_server_logf("%s", errstr);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "%s", errstr);
|
||||||
pefree(errstr, 1);
|
pefree(errstr, 1);
|
||||||
|
}
|
||||||
php_cli_server_chunk_dtor(chunk);
|
php_cli_server_chunk_dtor(chunk);
|
||||||
pefree(chunk, 1);
|
pefree(chunk, 1);
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -1210,9 +1227,9 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
|
||||||
}
|
}
|
||||||
|
|
||||||
if (color) {
|
if (color) {
|
||||||
php_cli_server_logf("\x1b[3%dm%s%s%s\x1b[0m", color, basic_buf, message_buf, error_buf);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE, "\x1b[3%dm%s%s%s\x1b[0m", color, basic_buf, message_buf, error_buf);
|
||||||
} else {
|
} else {
|
||||||
php_cli_server_logf("%s%s%s", basic_buf, message_buf, error_buf);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE, "%s%s%s", basic_buf, message_buf, error_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
efree(basic_buf);
|
efree(basic_buf);
|
||||||
|
@ -1224,11 +1241,15 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
|
||||||
}
|
}
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
static void php_cli_server_logf(const char *format, ...) /* {{{ */
|
static void php_cli_server_logf(int type, const char *format, ...) /* {{{ */
|
||||||
{
|
{
|
||||||
char *buf = NULL;
|
char *buf = NULL;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
||||||
|
if (php_cli_server_log_level < type) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
va_start(ap, format);
|
va_start(ap, format);
|
||||||
vspprintf(&buf, 0, format, ap);
|
vspprintf(&buf, 0, format, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
@ -1237,9 +1258,7 @@ static void php_cli_server_logf(const char *format, ...) /* {{{ */
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sapi_module.log_message) {
|
sapi_cli_server_log_write(type, buf);
|
||||||
sapi_module.log_message(buf, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
efree(buf);
|
efree(buf);
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
@ -1798,20 +1817,30 @@ static int php_cli_server_client_read_request(php_cli_server_client *client, cha
|
||||||
if (err == SOCK_EAGAIN) {
|
if (err == SOCK_EAGAIN) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
|
||||||
*errstr = php_socket_strerror(err, NULL, 0);
|
*errstr = php_socket_strerror(err, NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
} else if (nbytes_read == 0) {
|
} else if (nbytes_read == 0) {
|
||||||
|
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
|
||||||
*errstr = estrdup(php_cli_server_request_error_unexpected_eof);
|
*errstr = estrdup(php_cli_server_request_error_unexpected_eof);
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
client->parser.data = client;
|
client->parser.data = client;
|
||||||
nbytes_consumed = php_http_parser_execute(&client->parser, &settings, buf, nbytes_read);
|
nbytes_consumed = php_http_parser_execute(&client->parser, &settings, buf, nbytes_read);
|
||||||
if (nbytes_consumed != (size_t)nbytes_read) {
|
if (nbytes_consumed != (size_t)nbytes_read) {
|
||||||
|
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
|
||||||
if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
|
if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
|
||||||
*errstr = estrdup("Unsupported SSL request");
|
*errstr = estrdup("Unsupported SSL request");
|
||||||
} else {
|
} else {
|
||||||
*errstr = estrdup("Malformed HTTP request");
|
*errstr = estrdup("Malformed HTTP request");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (client->current_header_name) {
|
if (client->current_header_name) {
|
||||||
|
@ -1935,9 +1964,8 @@ static void php_cli_server_client_dtor(php_cli_server_client *client) /* {{{ */
|
||||||
|
|
||||||
static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client) /* {{{ */
|
static void php_cli_server_close_connection(php_cli_server *server, php_cli_server_client *client) /* {{{ */
|
||||||
{
|
{
|
||||||
#if PHP_DEBUG
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE, "%s Closing", client->addr_str);
|
||||||
php_cli_server_logf("%s Closing", client->addr_str);
|
|
||||||
#endif
|
|
||||||
zend_hash_index_del(&server->clients, client->sock);
|
zend_hash_index_del(&server->clients, client->sock);
|
||||||
} /* }}} */
|
} /* }}} */
|
||||||
|
|
||||||
|
@ -2321,6 +2349,7 @@ static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */
|
||||||
{
|
{
|
||||||
php_cli_server_client *p = Z_PTR_P(zv);
|
php_cli_server_client *p = Z_PTR_P(zv);
|
||||||
|
|
||||||
|
shutdown(p->sock, SHUT_RDWR);
|
||||||
closesocket(p->sock);
|
closesocket(p->sock);
|
||||||
php_cli_server_poller_remove(&p->server->poller, POLLIN | POLLOUT, p->sock);
|
php_cli_server_poller_remove(&p->server->poller, POLLIN | POLLOUT, p->sock);
|
||||||
php_cli_server_client_dtor(p);
|
php_cli_server_client_dtor(p);
|
||||||
|
@ -2378,7 +2407,7 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
|
||||||
|
|
||||||
server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr);
|
server_sock = php_network_listen_socket(host, &port, SOCK_STREAM, &server->address_family, &server->socklen, &errstr);
|
||||||
if (server_sock == SOCK_ERR) {
|
if (server_sock == SOCK_ERR) {
|
||||||
php_cli_server_logf("Failed to listen on %s:%d (reason: %s)", host, port, errstr ? ZSTR_VAL(errstr) : "?");
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to listen on %s:%d (reason: %s)", host, port, errstr ? ZSTR_VAL(errstr) : "?");
|
||||||
if (errstr) {
|
if (errstr) {
|
||||||
zend_string_release_ex(errstr, 0);
|
zend_string_release_ex(errstr, 0);
|
||||||
}
|
}
|
||||||
|
@ -2453,14 +2482,15 @@ static int php_cli_server_recv_event_read_request(php_cli_server *server, php_cl
|
||||||
char *errstr = NULL;
|
char *errstr = NULL;
|
||||||
int status = php_cli_server_client_read_request(client, &errstr);
|
int status = php_cli_server_client_read_request(client, &errstr);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
|
if (errstr) {
|
||||||
if (strcmp(errstr, php_cli_server_request_error_unexpected_eof) == 0 && client->parser.state == s_start_req) {
|
if (strcmp(errstr, php_cli_server_request_error_unexpected_eof) == 0 && client->parser.state == s_start_req) {
|
||||||
#if PHP_DEBUG
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE,
|
||||||
php_cli_server_logf("%s Closed without sending a request; it was probably just an unused speculative preconnection", client->addr_str);
|
"%s Closed without sending a request; it was probably just an unused speculative preconnection", client->addr_str);
|
||||||
#endif
|
|
||||||
} else {
|
} else {
|
||||||
php_cli_server_logf("%s Invalid request (%s)", client->addr_str, errstr);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "%s Invalid request (%s)", client->addr_str, errstr);
|
||||||
}
|
}
|
||||||
efree(errstr);
|
efree(errstr);
|
||||||
|
}
|
||||||
php_cli_server_close_connection(server, client);
|
php_cli_server_close_connection(server, client);
|
||||||
return FAILURE;
|
return FAILURE;
|
||||||
} else if (status == 1 && client->request.request_method == PHP_HTTP_NOT_IMPLEMENTED) {
|
} else if (status == 1 && client->request.request_method == PHP_HTTP_NOT_IMPLEMENTED) {
|
||||||
|
@ -2522,10 +2552,12 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socke
|
||||||
struct sockaddr *sa = pemalloc(server->socklen, 1);
|
struct sockaddr *sa = pemalloc(server->socklen, 1);
|
||||||
client_sock = accept(server->server_sock, sa, &socklen);
|
client_sock = accept(server->server_sock, sa, &socklen);
|
||||||
if (!ZEND_VALID_SOCKET(client_sock)) {
|
if (!ZEND_VALID_SOCKET(client_sock)) {
|
||||||
char *errstr;
|
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
|
||||||
errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
|
char *errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
|
||||||
php_cli_server_logf("Failed to accept a client (reason: %s)", errstr);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR,
|
||||||
|
"Failed to accept a client (reason: %s)", errstr);
|
||||||
efree(errstr);
|
efree(errstr);
|
||||||
|
}
|
||||||
pefree(sa, 1);
|
pefree(sa, 1);
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -2536,16 +2568,17 @@ static int php_cli_server_do_event_for_each_fd_callback(void *_params, php_socke
|
||||||
}
|
}
|
||||||
client = pemalloc(sizeof(php_cli_server_client), 1);
|
client = pemalloc(sizeof(php_cli_server_client), 1);
|
||||||
if (FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen)) {
|
if (FAILURE == php_cli_server_client_ctor(client, server, client_sock, sa, socklen)) {
|
||||||
php_cli_server_logf("Failed to create a new request object");
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to create a new request object");
|
||||||
pefree(sa, 1);
|
pefree(sa, 1);
|
||||||
closesocket(client_sock);
|
closesocket(client_sock);
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
#if PHP_DEBUG
|
|
||||||
php_cli_server_logf("%s Accepted", client->addr_str);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE, "%s Accepted", client->addr_str);
|
||||||
#endif
|
|
||||||
zend_hash_index_update_ptr(&server->clients, client_sock, client);
|
zend_hash_index_update_ptr(&server->clients, client_sock, client);
|
||||||
php_cli_server_recv_event_read_request(server, client);
|
|
||||||
|
php_cli_server_poller_add(&server->poller, POLLIN, client->sock);
|
||||||
} else {
|
} else {
|
||||||
php_cli_server_client *client;
|
php_cli_server_client *client;
|
||||||
if (NULL != (client = zend_hash_index_find_ptr(&server->clients, fd))) {
|
if (NULL != (client = zend_hash_index_find_ptr(&server->clients, fd))) {
|
||||||
|
@ -2586,9 +2619,11 @@ static int php_cli_server_do_event_loop(php_cli_server *server) /* {{{ */
|
||||||
} else {
|
} else {
|
||||||
int err = php_socket_errno();
|
int err = php_socket_errno();
|
||||||
if (err != SOCK_EINTR) {
|
if (err != SOCK_EINTR) {
|
||||||
|
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
|
||||||
char *errstr = php_socket_strerror(err, NULL, 0);
|
char *errstr = php_socket_strerror(err, NULL, 0);
|
||||||
php_cli_server_logf("%s", errstr);
|
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "%s", errstr);
|
||||||
efree(errstr);
|
efree(errstr);
|
||||||
|
}
|
||||||
retval = FAILURE;
|
retval = FAILURE;
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
@ -2645,6 +2680,11 @@ int do_cli_server(int argc, char **argv) /* {{{ */
|
||||||
document_root = document_root_tmp;
|
document_root = document_root_tmp;
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
case 'q':
|
||||||
|
if (php_cli_server_log_level > 1) {
|
||||||
|
php_cli_server_log_level--;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2683,25 +2723,22 @@ int do_cli_server(int argc, char **argv) /* {{{ */
|
||||||
sapi_module.phpinfo_as_text = 0;
|
sapi_module.phpinfo_as_text = 0;
|
||||||
|
|
||||||
{
|
{
|
||||||
char buf[52];
|
php_cli_server_logf(
|
||||||
|
PHP_CLI_SERVER_LOG_PROCESS,
|
||||||
if (php_cli_server_get_system_time(buf) != 0) {
|
"PHP %s Development Server (http://%s) started",
|
||||||
memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
|
PHP_VERSION, server_bind_address);
|
||||||
}
|
|
||||||
|
|
||||||
#if HAVE_FORK
|
|
||||||
printf("[%ld] PHP %s Development Server (http://%s) started at %s",
|
|
||||||
(long) getpid(), PHP_VERSION, server_bind_address, buf);
|
|
||||||
#else
|
|
||||||
printf("PHP %s Development Server (http://%s) started at %s",
|
|
||||||
PHP_VERSION, server_bind_address, buf);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(SIGINT)
|
#if defined(SIGINT)
|
||||||
signal(SIGINT, php_cli_server_sigint_handler);
|
signal(SIGINT, php_cli_server_sigint_handler);
|
||||||
zend_signal_init();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(SIGPIPE)
|
||||||
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
zend_signal_init();
|
||||||
|
|
||||||
php_cli_server_do_event_loop(&server);
|
php_cli_server_do_event_loop(&server);
|
||||||
php_cli_server_dtor(&server);
|
php_cli_server_dtor(&server);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue