} elseif ( substr( $url, 0, 1 ) == '/' ) {
// If $serverUrl is protocol-relative, prepend $defaultProtoWithoutSlashes,
// otherwise leave it alone.
- $url = ( $serverHasProto ? '' : $defaultProtoWithoutSlashes ) . $serverUrl . $url;
+ if ( $serverHasProto ) {
+ $url = $serverUrl . $url;
+ } else {
+ // If an HTTPS URL is synthesized from a protocol-relative $wgServer, allow the
+ // user to override the port number (T67184)
+ if ( $defaultProto === PROTO_HTTPS && $wgHttpsPort != 443 ) {
+ if ( isset( $bits['port'] ) ) {
+ throw new Exception( 'A protocol-relative $wgServer may not contain a port number' );
+ }
+ $url = $defaultProtoWithoutSlashes . $serverUrl . ':' . $wgHttpsPort . $url;
+ } else {
+ $url = $defaultProtoWithoutSlashes . $serverUrl . $url;
+ }
+ }
}
$bits = wfParseUrl( $url );
- // ensure proper port for HTTPS arrives in URL
- // https://phabricator.wikimedia.org/T67184
- if ( $defaultProto === PROTO_HTTPS && $wgHttpsPort != 443 ) {
- $bits['port'] = $wgHttpsPort;
- }
-
if ( $bits && isset( $bits['path'] ) ) {
$bits['path'] = wfRemoveDotSegments( $bits['path'] );
return wfAssembleUrl( $bits );
* @dataProvider provideExpandableUrls
*/
public function testWfExpandUrl( $fullUrl, $shortUrl, $defaultProto,
- $server, $canServer, $httpsMode, $message
+ $server, $canServer, $httpsMode, $httpsPort, $message
) {
// Fake $wgServer, $wgCanonicalServer and $wgRequest->getProtocol()
$this->setMwGlobals( [
'wgServer' => $server,
'wgCanonicalServer' => $canServer,
- 'wgRequest' => new FauxRequest( [], false, null, $httpsMode ? 'https' : 'http' )
+ 'wgRequest' => new FauxRequest( [], false, null, $httpsMode ? 'https' : 'http' ),
+ 'wgHttpsPort' => $httpsPort
] );
$this->assertEquals( $fullUrl, wfExpandUrl( $shortUrl, $defaultProto ), $message );
foreach ( $defaultProtos as $protoDesc => $defaultProto ) {
$retval[] = [
'http://example.com', 'http://example.com',
- $defaultProto, $server, $canServer, $httpsMode,
+ $defaultProto, $server, $canServer, $httpsMode, 443,
"Testing fully qualified http URLs (no need to expand) "
. "(defaultProto: $protoDesc , wgServer: $server, "
. "wgCanonicalServer: $canServer, current request protocol: $mode )"
];
$retval[] = [
'https://example.com', 'https://example.com',
- $defaultProto, $server, $canServer, $httpsMode,
+ $defaultProto, $server, $canServer, $httpsMode, 443,
"Testing fully qualified https URLs (no need to expand) "
. "(defaultProto: $protoDesc , wgServer: $server, "
. "wgCanonicalServer: $canServer, current request protocol: $mode )"
# Would be nice to support this, see fixme on wfExpandUrl()
$retval[] = [
"wiki/FooBar", 'wiki/FooBar',
- $defaultProto, $server, $canServer, $httpsMode,
+ $defaultProto, $server, $canServer, $httpsMode, 443,
"Test non-expandable relative URLs (defaultProto: $protoDesc, "
. "wgServer: $server, wgCanonicalServer: $canServer, "
. "current request protocol: $mode )"
$retval[] = [
"$p//wikipedia.org", '//wikipedia.org',
- $defaultProto, $server, $canServer, $httpsMode,
+ $defaultProto, $server, $canServer, $httpsMode, 443,
"Test protocol-relative URL (defaultProto: $protoDesc, "
. "wgServer: $server, wgCanonicalServer: $canServer, "
. "current request protocol: $mode )"
$server,
$canServer,
$httpsMode,
+ 443,
"Testing expanding URL beginning with / (defaultProto: $protoDesc, "
. "wgServer: $server, wgCanonicalServer: $canServer, "
. "current request protocol: $mode )"
}
}
+ // Don't add HTTPS port to foreign URLs
+ $retval[] = [
+ 'https://foreign.example.com/foo',
+ 'https://foreign.example.com/foo',
+ PROTO_HTTPS,
+ '//wiki.example.com',
+ 'http://wiki.example.com',
+ 'https',
+ 111,
+ "Don't add HTTPS port to foreign URLs"
+ ];
+ $retval[] = [
+ 'https://foreign.example.com:222/foo',
+ 'https://foreign.example.com:222/foo',
+ PROTO_HTTPS,
+ '//wiki.example.com',
+ 'http://wiki.example.com',
+ 'https',
+ 111,
+ "Don't overwrite HTTPS port of foreign URLs"
+ ];
+ // Do add HTTPS port to local URLs
+ $retval[] = [
+ 'https://wiki.example.com:111/foo',
+ '/foo',
+ PROTO_HTTPS,
+ '//wiki.example.com',
+ 'http://wiki.example.com',
+ 'https',
+ 111,
+ "Do add HTTPS port to protocol-relative URLs"
+ ];
+
return $retval;
}
}