| 1 |
<?php |
| 2 |
/** |
| 3 |
* Transform URIs. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Preload; |
| 11 |
|
| 12 |
use SolidWP\Performance\Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 13 |
use SolidWP\Performance\Symfony\Component\HttpClient\HttpClientTrait; |
| 14 |
|
| 15 |
/** |
| 16 |
* Transform URIs. |
| 17 |
* |
| 18 |
* @package SolidWP\Performance |
| 19 |
*/ |
| 20 |
final class Uri { |
| 21 |
|
| 22 |
use HttpClientTrait; |
| 23 |
|
| 24 |
/** |
| 25 |
* Ensure we pass a relative URL to our Scoped HTTP client, which is configured |
| 26 |
* to use the WP Home URL as the base_uri in order to prevent SSRF attacks. |
| 27 |
* |
| 28 |
* @param string $uri The URI that will be forced to a relative path. |
| 29 |
* |
| 30 |
* @throws InvalidArgumentException If the URL is malformed. |
| 31 |
* |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
public function make_relative( string $uri ): string { |
| 35 |
$uri = self::parseUrl( $uri ); |
| 36 |
$uri['scheme'] = ''; |
| 37 |
$uri['authority'] = ''; |
| 38 |
|
| 39 |
$uri = implode( '', $uri ); |
| 40 |
|
| 41 |
return '/' . ltrim( $uri, '/\\' ); |
| 42 |
} |
| 43 |
} |
| 44 |
|