Skip to content

Commit

Permalink
Merge pull request #683 from hydephp/normalize-protocol-relative-url-…
Browse files Browse the repository at this point in the history
…handling

Normalize protocol relative URL handling
  • Loading branch information
caendesilva authored Dec 21, 2024
2 parents 88b62a2 + 0527115 commit ee9b130
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function mediaLink(string $destination, bool $validate = false): string
*/
public function asset(string $name, bool $preferQualifiedUrl = false): string
{
if (str_starts_with($name, 'http')) {
if (static::isRemote($name)) {
return $name;
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public function url(string $path = ''): string
{
$path = $this->formatLink(trim($path, '/'));

if (str_starts_with($path, 'http')) {
if (static::isRemote($path)) {
return $path;
}

Expand All @@ -177,4 +177,12 @@ public function route(string $key): ?Route
{
return $this->kernel->routes()->get($key);
}

/**
* Determine if the given URL is a remote link.
*/
public static function isRemote(string $url): bool
{
return str_starts_with($url, 'http') || str_starts_with($url, '//');
}
}
3 changes: 2 additions & 1 deletion src/Framework/Factories/FeaturedImageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RuntimeException;
use Illuminate\Support\Str;
use Hyde\Markdown\Models\FrontMatter;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;

Expand Down Expand Up @@ -72,7 +73,7 @@ protected function makeSource(): string
throw new RuntimeException(sprintf('No featured image source was found in "%s"', $this->filePath ?? 'unknown file'));
}

if (FeaturedImage::isRemote($value)) {
if (Hyperlinks::isRemote($value)) {
return $value;
}

Expand Down
13 changes: 10 additions & 3 deletions src/Framework/Features/Blogging/Models/FeaturedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Hyde\Facades\Config;
use Illuminate\Support\Str;
use Hyde\Support\BuildWarnings;
use JetBrains\PhpStorm\Deprecated;
use Illuminate\Support\Facades\Http;
use Hyde\Foundation\Kernel\Hyperlinks;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Markdown\Contracts\FrontMatter\SubSchemas\FeaturedImageSchema;

Expand All @@ -19,7 +21,6 @@
use function filesize;
use function sprintf;
use function key;
use function str_starts_with;

/**
* Object representation of a blog post's featured image.
Expand Down Expand Up @@ -63,7 +64,7 @@ public function __construct(
protected readonly ?string $licenseUrl = null,
protected readonly ?string $copyrightText = null
) {
$this->type = self::isRemote($source) ? self::TYPE_REMOTE : self::TYPE_LOCAL;
$this->type = Hyperlinks::isRemote($source) ? self::TYPE_REMOTE : self::TYPE_LOCAL;
$this->source = $this->setSource($source);
}

Expand Down Expand Up @@ -241,8 +242,14 @@ protected function getContentLengthForRemoteImage(): int
return 0;
}

/**
* @codeCoverageIgnore Deprecated method.
*
* @deprecated This method will be removed in v2.0. Please use `Hyperlinks::isRemote` instead.
*/
#[Deprecated(reason: 'Replaced by the \Hyde\Foundation\Kernel\Hyperlinks::isRemote method', replacement: '\Hyde\Foundation\Kernel\Hyperlinks::isRemote(%parametersList%)', since: '1.8.0')]
public static function isRemote(string $source): bool
{
return str_starts_with($source, 'http') || str_starts_with($source, '//');
return Hyperlinks::isRemote($source);
}
}
4 changes: 2 additions & 2 deletions src/Framework/Features/Metadata/PageMetadataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use Hyde\Facades\Meta;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Pages\MarkdownPost;
use Hyde\Foundation\Kernel\Hyperlinks;

use function str_starts_with;
use function substr_count;
use function str_repeat;

Expand Down Expand Up @@ -77,7 +77,7 @@ protected function resolveImageLink(string $image): string
{
// Since this is run before the page is rendered, we don't have the currentPage property.
// So we need to run some of the same calculations here to resolve the image path link.
return str_starts_with($image, 'http') ? $image
return Hyperlinks::isRemote($image) ? $image
: str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')).$image;
}
}
30 changes: 30 additions & 0 deletions tests/Feature/Foundation/HyperlinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,34 @@ public function testRouteHelperWithInvalidRoute()
{
$this->assertNull($this->class->route('foo'));
}

public function testIsRemoteWithHttpUrl()
{
$this->assertTrue(Hyperlinks::isRemote('http://example.com'));
}

public function testIsRemoteWithHttpsUrl()
{
$this->assertTrue(Hyperlinks::isRemote('https://example.com'));
}

public function testIsRemoteWithProtocolRelativeUrl()
{
$this->assertTrue(Hyperlinks::isRemote('//example.com'));
}

public function testIsRemoteWithRelativeUrl()
{
$this->assertFalse(Hyperlinks::isRemote('/path/to/resource'));
}

public function testIsRemoteWithAbsoluteLocalPath()
{
$this->assertFalse(Hyperlinks::isRemote('/var/www/html/index.php'));
}

public function testIsRemoteWithEmptyString()
{
$this->assertFalse(Hyperlinks::isRemote(''));
}
}

0 comments on commit ee9b130

Please sign in to comment.