Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle edge case: "invalid url in base tag" #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/UsesUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ public function currentHost(): string
*/
public function currentBaseHost(): string
{
$uri = Uri::createFromString($this->baseHref() ?? $this->currentUrl());
//In case baseHref is a relative URL
$currentBase = $this->baseHref();
if ($currentBase === null || !preg_match('/^https?:\/\//', $currentBase)) {
$currentBase = $this->currentUrl();
}

$uri = Uri::createFromString($currentBase);

return $uri->getScheme() . '://' . $uri->getHost();
}
Expand All @@ -61,7 +67,7 @@ public function makeUrlAbsolute(?string $url = null, string $baseUrl = null): ?s
// Resolve the Url using one of the provided/set base href.
return (string) UriResolver::resolve(
Http::createFromString($url),
Http::createFromString($baseUrl ?? $this->baseHref() ?? $this->currentBaseHost()),
Http::createFromString($baseUrl ?? $this->currentBaseHost()),
);
}
}
14 changes: 14 additions & 0 deletions tests/BaseHrefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ public function testBaseHref()
$web->baseHref
);
}

public function testBaseHrefContainRelativePath()
{
$web = new \Spekulatius\PHPScraper\PHPScraper(['disable_ssl' => true]);

// Navigate to the test page.
// Contains: <base href="/links/invalid-base-href.html"> (relative path)
$web->go('https://test-pages.phpscraper.de/links/invalid-base-href.html');
// Check the baseHref
$this->assertSame(
'/links/invalid-base-href.html',
$web->baseHref
);
}
}
48 changes: 48 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public function testCurrentBaseHostWithBase()
);
}

/**
* @test
*/
public function testCurrentBaseHostWithBaseIsRelativeUri()
{
$web = new \Spekulatius\PHPScraper\PHPScraper;

// Navigate to the test page.
// Contains: <base href="/links/invalid-base-href.html">
$web->go('https://test-pages.phpscraper.de/links/invalid-base-href.html');

// Check the base href being passed through the current base host.
$this->assertSame(
'https://test-pages.phpscraper.de',
$web->currentBaseHost
);
}

/**
* Basic processing of the URLs.
*
Expand Down Expand Up @@ -167,6 +185,36 @@ public function testMakeUrlAbsoluteConsiderBaseHref()
);
}

/**
* Special case where the base href is a relative URL. So we need to use the current base host.
*
* @test
*/
public function testMakeUrlAbsoluteConsiderBaseHrefIsRelativeUrl()
{
$web = new \Spekulatius\PHPScraper\PHPScraper;

/**
* Navigate to test page: This sets the base URL.
*
* It contains:
*
* ```html
* <base href="/links/invalid-base-href.html">
* ```
*
* While it's located on `test-pages.phpscraper.de`.
*
* This page isn't actually used. It's purely to set the context.
*/
$web->go('https://test-pages.phpscraper.de/links/invalid-base-href.html');

$this->assertSame(
'https://test-pages.phpscraper.de/test/index.html',
$web->makeUrlAbsolute('test/index.html'),
);
}

/**
* Test if passed in hosts are considered. It trumps any base-href and current url.
*
Expand Down