Skip to content

Commit 30929c9

Browse files
committed
Support index.php fallback for files in built-in server
If no router script is used, the built-in webserver will now look for a fallback index file recursively in all cases, including URLs with a period. Furthermore, if a router script returns false, we will no longer perform a recursive fallback. Previously, recursive fallback was performed when the URL contained no period. The rationale for this is that it avoids serving the root index.php file when the router has indicated that the file is a static file. The existing behavior essentially assumes that virtual routes don't contain periods, and that real files do. Both of those assumptions are incorrect. Fixes GH-12604
1 parent bb6ceec commit 30929c9

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

sapi/cli/php_cli_server.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,14 +1437,13 @@ static void php_cli_server_request_dtor(php_cli_server_request *req) /* {{{ */
14371437
}
14381438
} /* }}} */
14391439

1440-
static void php_cli_server_request_translate_vpath(php_cli_server_request *request, const char *document_root, size_t document_root_len) /* {{{ */
1440+
static void php_cli_server_request_translate_vpath(php_cli_server *server, php_cli_server_request *request, const char *document_root, size_t document_root_len) /* {{{ */
14411441
{
14421442
zend_stat_t sb = {0};
14431443
static const char *index_files[] = { "index.php", "index.html", NULL };
14441444
char *buf = safe_pemalloc(1, request->vpath_len, 1 + document_root_len + 1 + sizeof("index.html"), 1);
14451445
char *p = buf, *prev_path = NULL, *q, *vpath;
14461446
size_t prev_path_len = 0;
1447-
int is_static_file = 0;
14481447

14491448
memmove(p, document_root, document_root_len);
14501449
p += document_root_len;
@@ -1453,13 +1452,6 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
14531452
if (request->vpath[0] != '/') {
14541453
*p++ = DEFAULT_SLASH;
14551454
}
1456-
q = request->vpath + request->vpath_len;
1457-
while (q > request->vpath) {
1458-
if (*q-- == '.') {
1459-
is_static_file = 1;
1460-
break;
1461-
}
1462-
}
14631455
memmove(p, request->vpath, request->vpath_len);
14641456
#ifdef PHP_WIN32
14651457
q = p + request->vpath_len;
@@ -1489,7 +1481,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
14891481
}
14901482
file++;
14911483
}
1492-
if (!*file || is_static_file) {
1484+
if (!*file) {
14931485
if (prev_path) {
14941486
pefree(prev_path, 1);
14951487
}
@@ -1499,6 +1491,10 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
14991491
}
15001492
break; /* regular file */
15011493
}
1494+
/* If the router has returned false we don't want to look for an index file recursively. */
1495+
if (server->router) {
1496+
break;
1497+
}
15021498
if (prev_path) {
15031499
pefree(prev_path, 1);
15041500
*q = DEFAULT_SLASH;
@@ -1801,7 +1797,7 @@ static int php_cli_server_client_read_request_on_message_complete(php_http_parse
18011797
{
18021798
php_cli_server_client *client = parser->data;
18031799
client->request.protocol_version = parser->http_major * 100 + parser->http_minor;
1804-
php_cli_server_request_translate_vpath(&client->request, client->server->document_root, client->server->document_root_len);
1800+
php_cli_server_request_translate_vpath(client->server, &client->request, client->server->document_root, client->server->document_root_len);
18051801
if (client->request.vpath) {
18061802
const char *vpath = client->request.vpath;
18071803
const char *end = vpath + client->request.vpath_len;

sapi/cli/tests/php_cli_server_009.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ X-Powered-By: PHP/%s
7979
Content-type: text/html; charset=UTF-8
8080

8181
string(9) "/foo/bar/"
82-
HTTP/1.0 404 Not Found
82+
HTTP/1.0 200 OK

sapi/cli/tests/php_cli_server_014.phpt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ X-Powered-By: %s
6161
Content-type: %s
6262

6363
done
64-
HTTP/1.1 404 Not Found
64+
HTTP/1.1 200 OK
6565
Host: %s
6666
Date: %s
6767
Connection: close
68-
Content-Type: %s
69-
Content-Length: %d
68+
X-Powered-By: PHP/%s
69+
Content-type: %s
7070

71-
<!doctype html><html><head><title>404 Not Found</title><style>AAA</style>
72-
</head><body><h1>Not Found</h1><p>The requested resource <code class="url">/main/no-exists.php</code> was not found on this server.</p></body></html>
71+
done

0 commit comments

Comments
 (0)