Skip to content

Commit 56ccddf

Browse files
committed
ensure http -> https when destination is https
1 parent 65b61f0 commit 56ccddf

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/HTMLProcessor.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,21 @@ public function detectUnchangedPlaceholderURLs( string $processed_html ) : strin
667667
$destination_host = (string) parse_url( $this->base_url, PHP_URL_HOST );
668668

669669
if ( strpos( $processed_html, $placeholder_host ) !== false ) {
670-
return str_replace( $placeholder_host, $destination_host, $processed_html );
670+
// bulk replace hosts
671+
$processed_html = str_replace(
672+
$placeholder_host,
673+
$destination_host,
674+
$processed_html
675+
);
676+
}
677+
678+
// force http -> https if destination is https
679+
if ( $this->destination_protocol === 'https://' ) {
680+
$processed_html = str_replace(
681+
'http://' . $destination_host,
682+
'https://' . $destination_host,
683+
$processed_html
684+
);
671685
}
672686

673687
return $processed_html;

tests/HTMLProcessorTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public function internalLinkProvider() {
119119
* @covers StaticHTMLOutput\HTMLProcessor::__construct
120120
* @covers StaticHTMLOutput\HTMLProcessor::isInternalLink
121121
* @covers StaticHTMLOutput\HTMLProcessor::normalizeURL
122-
* @covers StaticHTMLOutput\HTMLProcessor::normalizeURL
123122
* @dataProvider anchorTagProvider
124123
*/
125124
public function testNormalizePartialURLInAnchor(
@@ -584,6 +583,7 @@ public function unicodeProvider() {
584583
* @covers StaticHTMLOutput\HTMLProcessor::detectEscapedSiteURLs
585584
* @covers StaticHTMLOutput\HTMLProcessor::detectIfURLsShouldBeHarvested
586585
* @covers StaticHTMLOutput\HTMLProcessor::detectUnchangedPlaceholderURLs
586+
* @covers StaticHTMLOutput\HTMLProcessor::getBaseURLRewritePatterns
587587
* @covers StaticHTMLOutput\HTMLProcessor::getHTML
588588
* @covers StaticHTMLOutput\HTMLProcessor::getProtocolRelativeURL
589589
* @covers StaticHTMLOutput\HTMLProcessor::getTargetSiteProtocol
@@ -772,6 +772,15 @@ public function processlinkProvider() {
772772
'<link rel="https://api.w.org/" href="http://example.com/wp-json/">' .
773773
'</head><title>title</title><body>body</body></html>' . PHP_EOL,
774774
],
775+
'protocol relative link always rewritten to destination protocol' => [
776+
false,
777+
'<!DOCTYPE html><html lang="en-US"><head>' .
778+
'<link rel="prefetch" href="//mydomain.com">' .
779+
'</head><title>title</title><body>body</body></html>',
780+
'<!DOCTYPE html>' . PHP_EOL . '<html lang="en-US"><head>' .
781+
'<link rel="prefetch" href="https://mynewdomain.com">' .
782+
'</head><title>title</title><body>body</body></html>' . PHP_EOL,
783+
],
775784
];
776785
}
777786

0 commit comments

Comments
 (0)