@@ -2353,6 +2353,65 @@ static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */
2353
2353
pefree (p , 1 );
2354
2354
} /* }}} */
2355
2355
2356
+ /**
2357
+ * Parse the host and port portions of an address specifier in
2358
+ * one of the following forms:
2359
+ * - hostOrIP:port
2360
+ * - [hostOrIP]:port
2361
+ */
2362
+ static char * php_cli_server_parse_addr (const char * addr , int * pport ) {
2363
+ const char * p , * end ;
2364
+ long port ;
2365
+
2366
+ if (addr [0 ] == '[' ) {
2367
+ /* Encapsulated [hostOrIP]:port */
2368
+ const char * start = addr + 1 ;
2369
+ end = strchr (start , ']' );
2370
+ if (!end ) {
2371
+ /* No ending ] delimiter to match [ */
2372
+ return NULL ;
2373
+ }
2374
+
2375
+ p = end + 1 ;
2376
+ if (* p != ':' ) {
2377
+ /* Invalid char following address/missing port */
2378
+ return NULL ;
2379
+ }
2380
+
2381
+ port = strtol (p + 1 , (char * * )& p , 10 );
2382
+ if (p && * p ) {
2383
+ /* Non-numeric in port */
2384
+ return NULL ;
2385
+ }
2386
+ if (port < 0 || port > 65535 ) {
2387
+ /* Invalid port */
2388
+ return NULL ;
2389
+ }
2390
+
2391
+ /* Full [hostOrIP]:port provided */
2392
+ * pport = (int )port ;
2393
+ return pestrndup (start , end - start , 1 );
2394
+ }
2395
+
2396
+ end = strchr (addr , ':' );
2397
+ if (!end ) {
2398
+ /* Missing port */
2399
+ return NULL ;
2400
+ }
2401
+
2402
+ port = strtol (end + 1 , (char * * )& p , 10 );
2403
+ if (p && * p ) {
2404
+ /* Non-numeric port */
2405
+ return NULL ;
2406
+ }
2407
+ if (port < 0 || port > 65535 ) {
2408
+ /* Invalid port */
2409
+ return NULL ;
2410
+ }
2411
+ * pport = (int )port ;
2412
+ return pestrndup (addr , end - addr , 1 );
2413
+ }
2414
+
2356
2415
static int php_cli_server_ctor (php_cli_server * server , const char * addr , const char * document_root , const char * router ) /* {{{ */
2357
2416
{
2358
2417
int retval = SUCCESS ;
@@ -2363,40 +2422,9 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
2363
2422
int err = 0 ;
2364
2423
int port = 3000 ;
2365
2424
php_socket_t server_sock = SOCK_ERR ;
2366
- char * p = NULL ;
2367
2425
2368
- if (addr [0 ] == '[' ) {
2369
- host = pestrdup (addr + 1 , 1 );
2370
- if (!host ) {
2371
- return FAILURE ;
2372
- }
2373
- p = strchr (host , ']' );
2374
- if (p ) {
2375
- * p ++ = '\0' ;
2376
- if (* p == ':' ) {
2377
- port = strtol (p + 1 , & p , 10 );
2378
- if (port <= 0 || port > 65535 ) {
2379
- p = NULL ;
2380
- }
2381
- } else if (* p != '\0' ) {
2382
- p = NULL ;
2383
- }
2384
- }
2385
- } else {
2386
- host = pestrdup (addr , 1 );
2387
- if (!host ) {
2388
- return FAILURE ;
2389
- }
2390
- p = strchr (host , ':' );
2391
- if (p ) {
2392
- * p ++ = '\0' ;
2393
- port = strtol (p , & p , 10 );
2394
- if (port <= 0 || port > 65535 ) {
2395
- p = NULL ;
2396
- }
2397
- }
2398
- }
2399
- if (!p ) {
2426
+ host = php_cli_server_parse_addr (addr , & port );
2427
+ if (!host ) {
2400
2428
fprintf (stderr , "Invalid address: %s\n" , addr );
2401
2429
retval = FAILURE ;
2402
2430
goto out ;
@@ -2720,10 +2748,12 @@ int do_cli_server(int argc, char **argv) /* {{{ */
2720
2748
sapi_module .phpinfo_as_text = 0 ;
2721
2749
2722
2750
{
2751
+ zend_bool ipv6 = strchr (server .host , ':' );
2723
2752
php_cli_server_logf (
2724
2753
PHP_CLI_SERVER_LOG_PROCESS ,
2725
- "PHP %s Development Server (http://%s) started" ,
2726
- PHP_VERSION , server_bind_address );
2754
+ "PHP %s Development Server (http://%s%s%s:%d) started" ,
2755
+ PHP_VERSION , ipv6 ? "[" : "" , server .host ,
2756
+ ipv6 ? "]" : "" , server .port );
2727
2757
}
2728
2758
2729
2759
#if defined(SIGINT )
0 commit comments