Skip to content

Commit 791f3c1

Browse files
Only use the server-command router when there is an index.php to route to (#371)
The router of wp-cli/server-command sends every request for a missing file to index.php. When the project under test has wp-cli/server-command in its vendor directory, as in the automated tests of the bundle, the "a PHP built-in web server" step also used the router for a plain directory of files without an index.php. A request for a missing file then failed with a fatal error (a 500, or an error page with a 200 when errors are displayed) instead of a 404. That is why "Install from a remote zip URL that returns 404" in wp-cli/package-command fails in the automated tests. Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent c87933c commit 791f3c1

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,78 @@ Feature: Test that WP-CLI Behat steps work as expected
492492
404 Not Found
493493
"""
494494

495+
Scenario: Test "a PHP built-in web server" step without an index.php does not use the server-command router
496+
Given an empty directory
497+
And a vendor/wp-cli/server-command/router.php file:
498+
"""
499+
<?php
500+
// Like the router of wp-cli/server-command, send requests for missing files to index.php.
501+
$root = (string) ( $_SERVER['DOCUMENT_ROOT'] ?? '' );
502+
$path = (string) parse_url( (string) ( $_SERVER['REQUEST_URI'] ?? '/' ), PHP_URL_PATH );
503+
if ( is_file( $root . $path ) ) {
504+
return false;
505+
}
506+
echo 'Routed by router.php: ';
507+
require $root . '/index.php';
508+
"""
509+
And a fetch.php file:
510+
"""
511+
<?php
512+
$url = (string) ( $_SERVER['argv'][1] ?? '' );
513+
$headers = @get_headers( $url );
514+
echo is_array( $headers ) ? $headers[0] : 'no response', PHP_EOL, (string) @file_get_contents( $url );
515+
"""
516+
And a PHP built-in web server
517+
518+
# The router would require a missing index.php, so the directory is served as-is.
519+
When I run `php fetch.php http://localhost:8080/missing.html`
520+
Then STDOUT should contain:
521+
"""
522+
404 Not Found
523+
"""
524+
And STDOUT should not contain:
525+
"""
526+
Routed by router.php
527+
"""
528+
529+
Scenario: Test "a PHP built-in web server" step with an index.php uses the server-command router
530+
Given an empty directory
531+
And a vendor/wp-cli/server-command/router.php file:
532+
"""
533+
<?php
534+
// Like the router of wp-cli/server-command, send requests for missing files to index.php.
535+
$root = (string) ( $_SERVER['DOCUMENT_ROOT'] ?? '' );
536+
$path = (string) parse_url( (string) ( $_SERVER['REQUEST_URI'] ?? '/' ), PHP_URL_PATH );
537+
if ( is_file( $root . $path ) ) {
538+
return false;
539+
}
540+
echo 'Routed by router.php: ';
541+
require $root . '/index.php';
542+
"""
543+
And an index.php file:
544+
"""
545+
<?php
546+
echo 'Hello from index.php';
547+
"""
548+
And a fetch.php file:
549+
"""
550+
<?php
551+
$url = (string) ( $_SERVER['argv'][1] ?? '' );
552+
$headers = @get_headers( $url );
553+
echo is_array( $headers ) ? $headers[0] : 'no response', PHP_EOL, (string) @file_get_contents( $url );
554+
"""
555+
And a PHP built-in web server
556+
557+
When I run `php fetch.php http://localhost:8080/hello-world/`
558+
Then STDOUT should contain:
559+
"""
560+
200 OK
561+
"""
562+
And STDOUT should contain:
563+
"""
564+
Routed by router.php: Hello from index.php
565+
"""
566+
495567
@require-wp
496568
Scenario: Test background process launch
497569
Given a WP installation

‎src/Context/FeatureContext.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2559,10 +2559,12 @@ public function start_php_server( $subdir = '' ): void {
25592559

25602560
// Route requests through the router script of wp-cli/server-command when
25612561
// it is available, so that a WordPress installation gets pretty permalinks.
2562-
// Without a router, PHP's built-in web server serves the directory as-is,
2563-
// which is what a plain directory of static files needs. Passing a router
2564-
// path that does not exist would make every request fail with a fatal error.
2565-
$router = $this->get_php_server_router();
2562+
// The router sends every request for a missing file to index.php, so it is
2563+
// only used when the directory has one. Without a router, PHP's built-in
2564+
// web server serves the directory as-is, which is what a plain directory of
2565+
// static files needs, and a missing file is a 404. Passing a router path
2566+
// that does not exist would make every request fail with a fatal error.
2567+
$router = is_file( $dir . 'index.php' ) ? $this->get_php_server_router() : null;
25662568
if ( null !== $router ) {
25672569
$cmd .= ' ' . escapeshellarg( $router );
25682570
}

0 commit comments

Comments
 (0)