improvements to cli server

This commit is contained in:
Joe Watkins 2019-07-03 16:30:51 +02:00
parent 6ab04a6eef
commit 30019f47a5
No known key found for this signature in database
GPG key ID: F9BA0ADA31CBD89E

View file

@ -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>" }
};
#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
static int php_cli_output_is_tty = OUTPUT_NOT_CHECKED;
#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 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_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);
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);
} /* }}} */
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];
if (php_cli_server_log_level < type) {
return;
}
if (php_cli_server_get_system_time(buf) != 0) {
memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
} else {
@ -769,6 +779,11 @@ static void sapi_cli_server_log_message(char *msg, int syslog_type_int) /* {{{ *
#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 = {
@ -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);
#endif
if (_nbytes_read < 0) {
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_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);
}
php_cli_server_chunk_dtor(chunk);
pefree(chunk, 1);
return 1;
@ -1210,9 +1227,9 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
}
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 {
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);
@ -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;
va_list ap;
if (php_cli_server_log_level < type) {
return;
}
va_start(ap, format);
vspprintf(&buf, 0, format, ap);
va_end(ap);
@ -1237,9 +1258,7 @@ static void php_cli_server_logf(const char *format, ...) /* {{{ */
return;
}
if (sapi_module.log_message) {
sapi_module.log_message(buf, -1);
}
sapi_cli_server_log_write(type, 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) {
return 0;
}
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
*errstr = php_socket_strerror(err, NULL, 0);
}
return -1;
} 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);
}
return -1;
}
client->parser.data = client;
nbytes_consumed = php_http_parser_execute(&client->parser, &settings, buf, 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 */) {
*errstr = estrdup("Unsupported SSL request");
} else {
*errstr = estrdup("Malformed HTTP request");
}
}
return -1;
}
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) /* {{{ */
{
#if PHP_DEBUG
php_cli_server_logf("%s Closing", client->addr_str);
#endif
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE, "%s Closing", client->addr_str);
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);
shutdown(p->sock, SHUT_RDWR);
closesocket(p->sock);
php_cli_server_poller_remove(&p->server->poller, POLLIN | POLLOUT, p->sock);
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);
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) {
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;
int status = php_cli_server_client_read_request(client, &errstr);
if (status < 0) {
if (errstr) {
if (strcmp(errstr, php_cli_server_request_error_unexpected_eof) == 0 && client->parser.state == s_start_req) {
#if PHP_DEBUG
php_cli_server_logf("%s Closed without sending a request; it was probably just an unused speculative preconnection", client->addr_str);
#endif
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE,
"%s Closed without sending a request; it was probably just an unused speculative preconnection", client->addr_str);
} 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);
}
php_cli_server_close_connection(server, client);
return FAILURE;
} 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);
client_sock = accept(server->server_sock, sa, &socklen);
if (!ZEND_VALID_SOCKET(client_sock)) {
char *errstr;
errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
php_cli_server_logf("Failed to accept a client (reason: %s)", errstr);
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
char *errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR,
"Failed to accept a client (reason: %s)", errstr);
efree(errstr);
}
pefree(sa, 1);
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);
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);
closesocket(client_sock);
return SUCCESS;
}
#if PHP_DEBUG
php_cli_server_logf("%s Accepted", client->addr_str);
#endif
php_cli_server_logf(PHP_CLI_SERVER_LOG_MESSAGE, "%s Accepted", client->addr_str);
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 {
php_cli_server_client *client;
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 {
int err = php_socket_errno();
if (err != SOCK_EINTR) {
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
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);
}
retval = FAILURE;
goto out;
}
@ -2645,6 +2680,11 @@ int do_cli_server(int argc, char **argv) /* {{{ */
document_root = document_root_tmp;
#endif
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;
{
char buf[52];
if (php_cli_server_get_system_time(buf) != 0) {
memmove(buf, "unknown time, can't be fetched", sizeof("unknown time, can't be fetched"));
}
#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
php_cli_server_logf(
PHP_CLI_SERVER_LOG_PROCESS,
"PHP %s Development Server (http://%s) started",
PHP_VERSION, server_bind_address);
}
#if defined(SIGINT)
signal(SIGINT, php_cli_server_sigint_handler);
zend_signal_init();
#endif
#if defined(SIGPIPE)
signal(SIGPIPE, SIG_IGN);
#endif
zend_signal_init();
php_cli_server_do_event_loop(&server);
php_cli_server_dtor(&server);
return 0;