Skip to content

Commit 2fb7e1e

Browse files
committed
cli server addressing few todos.
1 parent 6d9d2eb commit 2fb7e1e

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

sapi/cli/php_cli_server.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ static bool php_cli_server_get_system_time(char *buf) {
270270

271271
gettimeofday(&tv, NULL);
272272

273-
/* TODO: should be checked for NULL tm/return value */
274-
php_localtime_r(&tv.tv_sec, &tm);
275-
php_asctime_r(&tm, buf);
276-
return true;
273+
if (!php_localtime_r(&tv.tv_sec, &tm)) {
274+
return false;
275+
}
276+
return php_asctime_r(&tm, buf) != NULL;
277277
}
278278
#endif
279279

@@ -865,7 +865,6 @@ static int php_cli_server_poller_poll(php_cli_server_poller *poller, struct time
865865
return php_select(poller->max_fd + 1, &poller->active.rfds, &poller->active.wfds, NULL, tv);
866866
} /* }}} */
867867

868-
// TODO Return value is unused, refactor?
869868
static zend_result php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, void *opaque, zend_result(*callback)(void *, php_socket_t fd, int events)) /* {{{ */
870869
{
871870
zend_result retval = SUCCESS;
@@ -2214,10 +2213,9 @@ static void php_cli_server_request_shutdown(php_cli_server *server, php_cli_serv
22142213
}
22152214
/* }}} */
22162215

2217-
// TODO Use bool, return value is strange
2218-
static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client) /* {{{ */
2216+
static zend_result php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client) /* {{{ */
22192217
{
2220-
int decline = 0;
2218+
zend_result decline;
22212219
zend_file_handle zfd;
22222220
char *old_cwd;
22232221

@@ -2228,14 +2226,17 @@ static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server
22282226

22292227
zend_stream_init_filename(&zfd, server->router);
22302228
zfd.primary_script = 1;
2229+
decline = SUCCESS;
22312230

22322231
zend_try {
22332232
zval retval;
22342233

22352234
ZVAL_UNDEF(&retval);
22362235
if (SUCCESS == zend_execute_scripts(ZEND_REQUIRE, &retval, 1, &zfd)) {
22372236
if (Z_TYPE(retval) != IS_UNDEF) {
2238-
decline = Z_TYPE(retval) == IS_FALSE;
2237+
if (Z_TYPE(retval) == IS_FALSE) {
2238+
decline = FAILURE;
2239+
}
22392240
zval_ptr_dtor(&retval);
22402241
}
22412242
}
@@ -2269,14 +2270,14 @@ static zend_result php_cli_server_dispatch(php_cli_server *server, php_cli_serve
22692270
if (server->router || !is_static_file) {
22702271
if (FAILURE == php_cli_server_request_startup(server, client)) {
22712272
php_cli_server_request_shutdown(server, client);
2272-
return SUCCESS;
2273+
return FAILURE;
22732274
}
22742275
}
22752276

22762277
if (server->router) {
2277-
if (!php_cli_server_dispatch_router(server, client)) {
2278+
if (FAILURE == php_cli_server_dispatch_router(server, client)) {
22782279
php_cli_server_request_shutdown(server, client);
2279-
return SUCCESS;
2280+
return FAILURE;
22802281
}
22812282
}
22822283

@@ -2610,8 +2611,7 @@ static zend_result php_cli_server_recv_event_read_request(php_cli_server *server
26102611
return php_cli_server_send_error_page(server, client, 501);
26112612
}
26122613
php_cli_server_poller_remove(&server->poller, POLLIN, client->sock);
2613-
php_cli_server_dispatch(server, client);
2614-
return SUCCESS;
2614+
return php_cli_server_dispatch(server, client);
26152615
case 0:
26162616
php_cli_server_poller_add(&server->poller, POLLIN, client->sock);
26172617
return SUCCESS;
@@ -2657,7 +2657,6 @@ typedef struct php_cli_server_do_event_for_each_fd_callback_params {
26572657
zend_result(*whandler)(php_cli_server*, php_cli_server_client*);
26582658
} php_cli_server_do_event_for_each_fd_callback_params;
26592659

2660-
// TODO return FAILURE on failure???
26612660
static zend_result php_cli_server_do_event_for_each_fd_callback(void *_params, php_socket_t fd, int event) /* {{{ */
26622661
{
26632662
php_cli_server_do_event_for_each_fd_callback_params *params = _params;
@@ -2677,12 +2676,12 @@ static zend_result php_cli_server_do_event_for_each_fd_callback(void *_params, p
26772676
efree(errstr);
26782677
}
26792678
pefree(sa, 1);
2680-
return SUCCESS;
2679+
return FAILURE;
26812680
}
26822681
if (SUCCESS != php_set_sock_blocking(client_sock, 0)) {
26832682
pefree(sa, 1);
26842683
closesocket(client_sock);
2685-
return SUCCESS;
2684+
return FAILURE;
26862685
}
26872686
client = pemalloc(sizeof(php_cli_server_client), 1);
26882687

@@ -2717,10 +2716,11 @@ static void php_cli_server_do_event_for_each_fd(php_cli_server *server,
27172716
whandler
27182717
};
27192718

2720-
php_cli_server_poller_iter_on_active(&server->poller, &params, php_cli_server_do_event_for_each_fd_callback);
2719+
if (SUCCESS != php_cli_server_poller_iter_on_active(&server->poller, &params, php_cli_server_do_event_for_each_fd_callback)) {
2720+
php_cli_server_logf(PHP_CLI_SERVER_LOG_ERROR, "Failed to poll event");
2721+
}
27212722
} /* }}} */
27222723

2723-
// TODO Return value of function is not used
27242724
static zend_result php_cli_server_do_event_loop(php_cli_server *server) /* {{{ */
27252725
{
27262726
zend_result retval = SUCCESS;
@@ -2763,7 +2763,7 @@ int do_cli_server(int argc, char **argv) /* {{{ */
27632763
{
27642764
char *php_optarg = NULL;
27652765
int php_optind = 1;
2766-
int c;
2766+
int c, r;
27672767
const char *server_bind_address = NULL;
27682768
extern const opt_struct OPTIONS[];
27692769
const char *document_root = NULL;
@@ -2841,6 +2841,7 @@ int do_cli_server(int argc, char **argv) /* {{{ */
28412841
sapi_module.phpinfo_as_text = 0;
28422842

28432843
{
2844+
r = 0;
28442845
bool ipv6 = strchr(server.host, ':');
28452846
php_cli_server_logf(
28462847
PHP_CLI_SERVER_LOG_PROCESS,
@@ -2859,7 +2860,9 @@ int do_cli_server(int argc, char **argv) /* {{{ */
28592860

28602861
zend_signal_init();
28612862

2862-
php_cli_server_do_event_loop(&server);
2863+
if (SUCCESS != php_cli_server_do_event_loop(&server)) {
2864+
r = 1;
2865+
}
28632866
php_cli_server_dtor(&server);
2864-
return 0;
2867+
return r;
28652868
} /* }}} */

0 commit comments

Comments
 (0)