Skip to content

Commit a61a9fe

Browse files
committed
Support ephemeral ports in debug server
1 parent 95f2583 commit a61a9fe

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.0.0RC1
44

5+
- CLI:
6+
. Allow debug server binding to an ephemeral port via `-S localhost:0`. (Sara)
57
- Core:
68
. Fixed bug #80109 (Cannot skip arguments when extended debug is enabled).
79
(Nikita)

sapi/cli/php_cli_server.c

Lines changed: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,65 @@ static void php_cli_server_client_dtor_wrapper(zval *zv) /* {{{ */
23532353
pefree(p, 1);
23542354
} /* }}} */
23552355

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+
23562415
static int php_cli_server_ctor(php_cli_server *server, const char *addr, const char *document_root, const char *router) /* {{{ */
23572416
{
23582417
int retval = SUCCESS;
@@ -2363,40 +2422,9 @@ static int php_cli_server_ctor(php_cli_server *server, const char *addr, const c
23632422
int err = 0;
23642423
int port = 3000;
23652424
php_socket_t server_sock = SOCK_ERR;
2366-
char *p = NULL;
23672425

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) {
24002428
fprintf(stderr, "Invalid address: %s\n", addr);
24012429
retval = FAILURE;
24022430
goto out;
@@ -2720,10 +2748,12 @@ int do_cli_server(int argc, char **argv) /* {{{ */
27202748
sapi_module.phpinfo_as_text = 0;
27212749

27222750
{
2751+
zend_bool ipv6 = strchr(server.host, ':');
27232752
php_cli_server_logf(
27242753
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);
27272757
}
27282758

27292759
#if defined(SIGINT)

0 commit comments

Comments
 (0)