Skip to content

Commit e81dc13

Browse files
Copilotswissspidy
andauthored
Add --adapt-scheme flag to rewrite HTTPS URLs to HTTP in server responses (#94)
* Initial plan * Add --adapt-scheme flag to replace HTTPS URLs with HTTP in server responses Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> * Apply suggestion from @swissspidy --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascalb@google.com>
1 parent b188fc7 commit e81dc13

3 files changed

Lines changed: 52 additions & 1 deletion

File tree

features/server.feature

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,23 @@ Feature: Serve WordPress locally
5151
"""
5252
Hello world!
5353
"""
54+
55+
Scenario: Adapt HTTPS scheme to HTTP
56+
Given a WP install
57+
And a wp-content/mu-plugins/test-https-url.php file:
58+
"""
59+
<?php add_filter( 'wp_head', function() { echo '<meta name="test-https" content="https://localhost:8184/wp-content/uploads/test.jpg">'; } );
60+
"""
61+
And I run `wp option update home https://localhost:8184`
62+
And I run `wp option update siteurl https://localhost:8184`
63+
And I launch in the background `wp server --host=localhost --port=8184 --adapt-scheme`
64+
65+
When I run `curl -sS localhost:8184`
66+
Then STDOUT should contain:
67+
"""
68+
http://localhost:8184/wp-content/uploads/test.jpg
69+
"""
70+
And STDOUT should not contain:
71+
"""
72+
https://localhost:8184
73+
"""

router.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ function ( $url ) {
116116

117117
add_filter( 'got_url_rewrite', '__return_true' );
118118

119+
if ( getenv( 'WPCLI_SERVER_ADAPT_SCHEME' ) ) {
120+
ob_start(
121+
static function ( $buffer ) {
122+
if ( ! isset( $GLOBALS['wpcli_server_original_url'] ) ) {
123+
return $buffer;
124+
}
125+
$original_host = _get_full_host( $GLOBALS['wpcli_server_original_url'] );
126+
return str_replace(
127+
'https://' . $original_host,
128+
'http://' . $_SERVER['HTTP_HOST'],
129+
$buffer
130+
);
131+
}
132+
);
133+
}
134+
119135
$_SERVER['SERVER_ADDR'] = gethostbyname( $_SERVER['SERVER_NAME'] );
120136
$wpcli_server_root = $_SERVER['DOCUMENT_ROOT'];
121137
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url

src/Server_Command.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class Server_Command extends WP_CLI_Command {
3434
* [--config=<file>]
3535
* : Configure the server with a specific .ini file.
3636
*
37+
* [--adapt-scheme]
38+
* : Replace HTTPS URLs matching the original site URL with HTTP in server responses.
39+
* Useful when the site is configured with HTTPS but the development server runs on HTTP.
40+
*
3741
* [<passthrough>...]
3842
* : Optional arguments to pass to the PHP binary. Any arguments after `--`
3943
* will be passed through to the `php` command.
@@ -68,10 +72,17 @@ class Server_Command extends WP_CLI_Command {
6872
* Document root is /var/www/public
6973
* Press Ctrl-C to quit.
7074
*
75+
* # Adapt HTTPS links when the site is configured with HTTPS
76+
* $ wp server --adapt-scheme
77+
* PHP 8.0.0 Development Server started at Wed Nov 10 18:00:00 2025
78+
* Listening on http://localhost:8080
79+
* Document root is /var/www/html
80+
* Press Ctrl-C to quit.
81+
*
7182
* @when before_wp_load
7283
*
7384
* @param array<string> $args Positional arguments passed through to the PHP binary.
74-
* @param array{host: string, port: string, docroot?: string, config?: string} $assoc_args Associative arguments passed to the command.
85+
* @param array{host: string, port: string, docroot?: string, config?: string, 'adapt-scheme'?: bool} $assoc_args Associative arguments passed to the command.
7586
* @return void
7687
*/
7788
public function __invoke( $args, $assoc_args ) {
@@ -123,6 +134,10 @@ public function __invoke( $args, $assoc_args ) {
123134

124135
$descriptors = array( STDIN, STDOUT, STDERR );
125136

137+
if ( Utils\get_flag_value( $assoc_args, 'adapt-scheme', false ) ) { // @phpstan-ignore argument.type
138+
putenv( 'WPCLI_SERVER_ADAPT_SCHEME=1' );
139+
}
140+
126141
// https://bugs.php.net/bug.php?id=60181
127142
$options = array();
128143
if ( Utils\is_windows() ) {

0 commit comments

Comments
 (0)