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.

Fixes GH-12604
Closes GH-12992
This commit is contained in:
Ilija Tovilo 2023-12-21 15:09:27 +01:00
parent 8876639d11
commit d7d0d19d32
No known key found for this signature in database
GPG key ID: A4F5D403F118200A
6 changed files with 35 additions and 20 deletions

View file

@ -1437,14 +1437,13 @@ static void php_cli_server_request_dtor(php_cli_server_request *req) /* {{{ */
}
} /* }}} */
static void php_cli_server_request_translate_vpath(php_cli_server_request *request, const char *document_root, size_t document_root_len) /* {{{ */
static void php_cli_server_request_translate_vpath(const php_cli_server *server, php_cli_server_request *request, const char *document_root, size_t document_root_len) /* {{{ */
{
zend_stat_t sb = {0};
static const char *index_files[] = { "index.php", "index.html", NULL };
char *buf = safe_pemalloc(1, request->vpath_len, 1 + document_root_len + 1 + sizeof("index.html"), 1);
char *p = buf, *prev_path = NULL, *q, *vpath;
size_t prev_path_len = 0;
int is_static_file = 0;
memmove(p, document_root, document_root_len);
p += document_root_len;
@ -1453,13 +1452,6 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
if (request->vpath[0] != '/') {
*p++ = DEFAULT_SLASH;
}
q = request->vpath + request->vpath_len;
while (q > request->vpath) {
if (*q-- == '.') {
is_static_file = 1;
break;
}
}
memmove(p, request->vpath, request->vpath_len);
#ifdef PHP_WIN32
q = p + request->vpath_len;
@ -1489,7 +1481,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
}
file++;
}
if (!*file || is_static_file) {
if (!*file) {
if (prev_path) {
pefree(prev_path, 1);
}
@ -1801,7 +1793,7 @@ static int php_cli_server_client_read_request_on_message_complete(php_http_parse
{
php_cli_server_client *client = parser->data;
client->request.protocol_version = parser->http_major * 100 + parser->http_minor;
php_cli_server_request_translate_vpath(&client->request, client->server->document_root, client->server->document_root_len);
php_cli_server_request_translate_vpath(client->server, &client->request, client->server->document_root, client->server->document_root_len);
if (client->request.vpath) {
const char *vpath = client->request.vpath;
const char *end = vpath + client->request.vpath_len;