@@ -2353,7 +2353,81 @@ static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */
2353
2353
pefree (p , 1 );
2354
2354
} /* }}} */
2355
2355
2356
- static int php_cli_server_ctor (php_cli_server * server , const char * addr , const char * document_root , const char * router ) /* {{{ */
2356
+ /**
2357
+ * Parse the host and port portions of an address specifier in
2358
+ * one of the following forms:
2359
+ * - hostOrIP
2360
+ * - [hostOrIp]
2361
+ * - hostOrIP:port
2362
+ * - [hostOrIP]:port
2363
+ */
2364
+ static char * php_cli_server_parse_addr (const char * addr , int * pport ) {
2365
+ const char * p , * end ;
2366
+ long port ;
2367
+
2368
+ if (addr [0 ] == '[' ) {
2369
+ /* Encapsulated [hostOrIP]:port or just [hostOrIP] */
2370
+ const char * start = addr + 1 ;
2371
+ end = strchr (start , ']' );
2372
+ if (!end ) {
2373
+ /* No ending ] delimiter to match [ */
2374
+ return NULL ;
2375
+ }
2376
+
2377
+ p = end + 1 ;
2378
+ if (* p == '\0' ) {
2379
+ /* host only, implied port 0 */
2380
+ * pport = 0 ;
2381
+ return pestrndup (start , end - start , 1 );
2382
+ }
2383
+
2384
+ if (* p != ':' ) {
2385
+ /* Invalid char following address */
2386
+ return NULL ;
2387
+ }
2388
+
2389
+ port = strtol (p + 1 , (char * * )& p , 10 );
2390
+ if (p && * p ) {
2391
+ /* Non-numeric in port */
2392
+ return NULL ;
2393
+ }
2394
+ if (port < 0 || port > 65535 ) {
2395
+ /* Invalid port */
2396
+ return NULL ;
2397
+ }
2398
+
2399
+ /* Full [hostOrIP]:port provided */
2400
+ * pport = (int )port ;
2401
+ return pestrndup (start , end - start , 1 );
2402
+ }
2403
+
2404
+ end = strchr (addr , ':' );
2405
+ if (!end ) {
2406
+ /* host only, implied port 0 */
2407
+ * pport = 0 ;
2408
+ return pestrdup (addr , 1 );
2409
+ }
2410
+
2411
+ if (strchr (end + 1 , ':' )) {
2412
+ /* Probably a bare IPv6 */
2413
+ * pport = 0 ;
2414
+ return pestrdup (addr , 1 );
2415
+ }
2416
+
2417
+ port = strtol (end + 1 , (char * * )& p , 10 );
2418
+ if (p && * p ) {
2419
+ /* Non-numeric port */
2420
+ return NULL ;
2421
+ }
2422
+ if (port < 0 || port > 65535 ) {
2423
+ /* Invalid port */
2424
+ return NULL ;
2425
+ }
2426
+ * pport = (int )port ;
2427
+ return pestrndup (addr , end - addr , 1 );
2428
+ }
2429
+
2430
+ static int php_cli_server_ctor (php_cli_server * server , const char * addr , const char * document_root , const char * router , const char * * phost , int * pport ) /* {{{ */
2357
2431
{
2358
2432
int retval = SUCCESS ;
2359
2433
char * host = NULL ;
@@ -2363,40 +2437,9 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
2363
2437
int err = 0 ;
2364
2438
int port = 3000 ;
2365
2439
php_socket_t server_sock = SOCK_ERR ;
2366
- char * p = NULL ;
2367
2440
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 ) {
2441
+ host = php_cli_server_parse_addr (addr , & port );
2442
+ if (!host ) {
2400
2443
fprintf (stderr , "Invalid address: %s\n" , addr );
2401
2444
retval = FAILURE ;
2402
2445
goto out ;
@@ -2457,7 +2500,14 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
2457
2500
2458
2501
server -> is_running = 1 ;
2459
2502
out :
2460
- if (retval != SUCCESS ) {
2503
+ if (retval == SUCCESS ) {
2504
+ if (phost ) {
2505
+ * phost = host ;
2506
+ }
2507
+ if (pport ) {
2508
+ * pport = port ;
2509
+ }
2510
+ } else {
2461
2511
if (host ) {
2462
2512
pefree (host , 1 );
2463
2513
}
@@ -2644,6 +2694,8 @@ int do_cli_server(int argc, char **argv) /* {{{ */
2644
2694
int php_optind = 1 ;
2645
2695
int c ;
2646
2696
const char * server_bind_address = NULL ;
2697
+ const char * server_bound_host = NULL ;
2698
+ int server_bound_port = 0 ;
2647
2699
extern const opt_struct OPTIONS [];
2648
2700
const char * document_root = NULL ;
2649
2701
#ifdef PHP_WIN32
@@ -2714,16 +2766,19 @@ int do_cli_server(int argc, char **argv) /* {{{ */
2714
2766
router = argv [php_optind ];
2715
2767
}
2716
2768
2717
- if (FAILURE == php_cli_server_ctor (& server , server_bind_address , document_root , router )) {
2769
+ if (FAILURE == php_cli_server_ctor (& server , server_bind_address , document_root , router ,
2770
+ & server_bound_host , & server_bound_port )) {
2718
2771
return 1 ;
2719
2772
}
2720
2773
sapi_module .phpinfo_as_text = 0 ;
2721
2774
2722
2775
{
2776
+ zend_bool ipv6 = strchr (server_bound_host , ':' );
2723
2777
php_cli_server_logf (
2724
2778
PHP_CLI_SERVER_LOG_PROCESS ,
2725
- "PHP %s Development Server (http://%s) started" ,
2726
- PHP_VERSION , server_bind_address );
2779
+ "PHP %s Development Server (http://%s%s%s:%d) started" ,
2780
+ PHP_VERSION , ipv6 ? "[" : "" , server_bound_host ,
2781
+ ipv6 ? "]" : "" , server_bound_port );
2727
2782
}
2728
2783
2729
2784
#if defined(SIGINT )
0 commit comments