mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-30 12:14:36 +00:00
Erase base URL parts if their preceding parts have overwritten by relative URL
This commit is contained in:
+52
-45
@@ -92,54 +92,61 @@ class Url
|
||||
private function absolutizeUrl()
|
||||
{
|
||||
$b = $this->parseUrl($this->baseUrl);
|
||||
|
||||
if (!($this->relativeUrl === null)) {
|
||||
$r = $this->parseUrl($this->relativeUrl);
|
||||
|
||||
// Copy relative parts to base url.
|
||||
if (isset($r['scheme'])) {
|
||||
$b['scheme'] = $r['scheme'];
|
||||
}
|
||||
if (isset($r['host'])) {
|
||||
$b['host'] = $r['host'];
|
||||
}
|
||||
if (isset($r['port'])) {
|
||||
$b['port'] = $r['port'];
|
||||
}
|
||||
if (isset($r['user'])) {
|
||||
$b['user'] = $r['user'];
|
||||
}
|
||||
if (isset($r['pass'])) {
|
||||
$b['pass'] = $r['pass'];
|
||||
}
|
||||
|
||||
if (!isset($r['path']) || $r['path'] === '') {
|
||||
$r['path'] = '/';
|
||||
}
|
||||
// Merge relative url with base when relative url's path doesn't start with a slash.
|
||||
if (!(StrUtil::startsWith($r['path'], '/'))) {
|
||||
$base = mb_strrchr($b['path'], '/', true);
|
||||
if ($base === false) {
|
||||
$base = '';
|
||||
}
|
||||
$r['path'] = $base . '/' . $r['path'];
|
||||
}
|
||||
$b['path'] = $r['path'];
|
||||
$b['path'] = $this->removeDotSegments($b['path']);
|
||||
|
||||
if (isset($r['query'])) {
|
||||
$b['query'] = $r['query'];
|
||||
}
|
||||
if (isset($r['fragment'])) {
|
||||
$b['fragment'] = $r['fragment'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset($b['path'])) {
|
||||
$b['path'] = '/';
|
||||
}
|
||||
|
||||
$absolutized_url = $this->unparseUrl($b);
|
||||
if (is_null($this->relativeUrl)) {
|
||||
return $this->unparseUrl($b);
|
||||
}
|
||||
$r = $this->parseUrl($this->relativeUrl);
|
||||
$r['authorized'] = isset($r['scheme']) || isset($r['host']) || isset($r['port'])
|
||||
|| isset($r['user']) || isset($r['pass']);
|
||||
$target = array();
|
||||
if (isset($r['scheme'])) {
|
||||
$target['scheme'] = $r['scheme'];
|
||||
$target['host'] = isset($r['host']) ? $r['host'] : null;
|
||||
$target['port'] = isset($r['port']) ? $r['port'] : null;
|
||||
$target['user'] = isset($r['user']) ? $r['user'] : null;
|
||||
$target['pass'] = isset($r['pass']) ? $r['pass'] : null;
|
||||
$target['path'] = isset($r['path']) ? self::removeDotSegments($r['path']) : null;
|
||||
$target['query'] = isset($r['query']) ? $r['query'] : null;
|
||||
} else {
|
||||
$target['scheme'] = isset($b['scheme']) ? $b['scheme'] : null;
|
||||
if ($r['authorized']) {
|
||||
$target['host'] = isset($r['host']) ? $r['host'] : null;
|
||||
$target['port'] = isset($r['port']) ? $r['port'] : null;
|
||||
$target['user'] = isset($r['user']) ? $r['user'] : null;
|
||||
$target['pass'] = isset($r['pass']) ? $r['pass'] : null;
|
||||
$target['path'] = isset($r['path']) ? self::removeDotSegments($r['path']) : null;
|
||||
$target['query'] = isset($r['query']) ? $r['query'] : null;
|
||||
} else {
|
||||
$target['host'] = isset($b['host']) ? $b['host'] : null;
|
||||
$target['port'] = isset($b['port']) ? $b['port'] : null;
|
||||
$target['user'] = isset($b['user']) ? $b['user'] : null;
|
||||
$target['pass'] = isset($b['pass']) ? $b['pass'] : null;
|
||||
if (!isset($r['path']) || $r['path'] === '') {
|
||||
$target['path'] = $b['path'];
|
||||
$target['query'] = isset($r['query']) ? $r['query'] : (isset($b['query']) ? $b['query'] : null);
|
||||
} else {
|
||||
if (StrUtil::startsWith($r['path'], '/')) {
|
||||
$target['path'] = self::removeDotSegments($r['path']);
|
||||
} else {
|
||||
$base = mb_strrchr($b['path'], '/', true);
|
||||
if ($base === false) {
|
||||
$base = '';
|
||||
}
|
||||
$target['path'] = self::removeDotSegments($base . '/' . $r['path']);
|
||||
}
|
||||
$target['query'] = isset($r['query']) ? $r['query'] : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($this->relativeUrl === '') {
|
||||
$target['fragment'] = isset($b['fragment']) ? $b['fragment'] : null;
|
||||
} else {
|
||||
$target['fragment'] = isset($r['fragment']) ? $r['fragment'] : null;
|
||||
}
|
||||
$absolutized_url = $this->unparseUrl($target);
|
||||
return $absolutized_url;
|
||||
}
|
||||
|
||||
|
||||
@@ -3580,31 +3580,56 @@ class CurlTest extends \PHPUnit\Framework\TestCase
|
||||
foreach ($tests as $test) {
|
||||
$curl = new Curl($test['args']['0']);
|
||||
$curl->setUrl($test['args']['1']);
|
||||
$this->assertEquals($test['expected'], $curl->getOpt(CURLOPT_URL));
|
||||
$this->assertEquals(
|
||||
$test['expected'],
|
||||
$curl->getOpt(CURLOPT_URL),
|
||||
"Joint URLs: '{$test['args']['0']}', '{$test['args']['1']}'"
|
||||
);
|
||||
|
||||
$curl = new Curl($test['args']['0']);
|
||||
$curl->setUrl($test['args']['1'], array('a' => '1', 'b' => '2'));
|
||||
$this->assertEquals($test['expected'] . '?a=1&b=2', $curl->getOpt(CURLOPT_URL));
|
||||
$this->assertEquals(
|
||||
$test['expected'] . '?a=1&b=2',
|
||||
$curl->getOpt(CURLOPT_URL),
|
||||
"Joint URL '{$test['args']['0']}' with parameters a=1, b=2"
|
||||
);
|
||||
|
||||
$curl = new Curl();
|
||||
$curl->setUrl($test['args']['0']);
|
||||
$curl->setUrl($test['args']['1']);
|
||||
$this->assertEquals($test['expected'], $curl->getOpt(CURLOPT_URL));
|
||||
$this->assertEquals(
|
||||
$test['expected'],
|
||||
$curl->getOpt(CURLOPT_URL),
|
||||
"Joint URLs: '{$test['args']['0']}', '{$test['args']['1']}'"
|
||||
);
|
||||
|
||||
$curl = new Curl();
|
||||
$curl->setUrl($test['args']['0'], array('a' => '1', 'b' => '2'));
|
||||
$curl->setUrl($test['args']['1']);
|
||||
$this->assertEquals($test['expected'] . '?a=1&b=2', $curl->getOpt(CURLOPT_URL));
|
||||
$this->assertEquals(
|
||||
$test['expected'],
|
||||
$curl->getOpt(CURLOPT_URL),
|
||||
"Joint URL '{$test['args']['0']}' with parameters a=1, b=2 and URL '{$test['args']['1']}'"
|
||||
);
|
||||
|
||||
$curl = new Curl();
|
||||
$curl->setUrl($test['args']['0']);
|
||||
$curl->setUrl($test['args']['1'], array('a' => '1', 'b' => '2'));
|
||||
$this->assertEquals($test['expected'] . '?a=1&b=2', $curl->getOpt(CURLOPT_URL));
|
||||
$this->assertEquals(
|
||||
$test['expected'] . '?a=1&b=2',
|
||||
$curl->getOpt(CURLOPT_URL),
|
||||
"Joint URL '{$test['args']['0']}' and URL '{$test['args']['1']}' with parameters a=1, b=2"
|
||||
);
|
||||
|
||||
$curl = new Curl();
|
||||
$curl->setUrl($test['args']['0'], array('a' => '1', 'b' => '2'));
|
||||
$curl->setUrl($test['args']['1'], array('c' => '3', 'd' => '4'));
|
||||
$this->assertEquals($test['expected'] . '?c=3&d=4', $curl->getOpt(CURLOPT_URL));
|
||||
$this->assertEquals(
|
||||
$test['expected'] . '?c=3&d=4',
|
||||
$curl->getOpt(CURLOPT_URL),
|
||||
"Joint URL '{$test['args']['0']}' with parameters a=1, b=2 " .
|
||||
"and URL '{$test['args']['1']}' with parameters c=3, d=4"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,56 +8,15 @@ class UrlTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function testUrlPaths()
|
||||
{
|
||||
$tests = array(
|
||||
array(
|
||||
'args' => array(
|
||||
'http://www.example.com/',
|
||||
'/foo',
|
||||
),
|
||||
'expected' => 'http://www.example.com/foo',
|
||||
),
|
||||
array(
|
||||
'args' => array(
|
||||
'http://www.example.com/',
|
||||
'/foo/',
|
||||
),
|
||||
'expected' => 'http://www.example.com/foo/',
|
||||
),
|
||||
array(
|
||||
'args' => array(
|
||||
'http://www.example.com/',
|
||||
'/dir/page.html',
|
||||
),
|
||||
'expected' => 'http://www.example.com/dir/page.html',
|
||||
),
|
||||
array(
|
||||
'args' => array(
|
||||
'http://www.example.com/dir1/page2.html',
|
||||
'/dir/page.html',
|
||||
),
|
||||
'expected' => 'http://www.example.com/dir/page.html',
|
||||
),
|
||||
array(
|
||||
'args' => array(
|
||||
'http://www.example.com/dir1/page2.html',
|
||||
'dir/page.html',
|
||||
),
|
||||
'expected' => 'http://www.example.com/dir1/dir/page.html',
|
||||
),
|
||||
array(
|
||||
'args' => array(
|
||||
'http://www.example.com/dir1/dir3/page.html',
|
||||
'../dir/page.html',
|
||||
),
|
||||
'expected' => 'http://www.example.com/dir1/dir/page.html',
|
||||
),
|
||||
);
|
||||
foreach ($tests as $test) {
|
||||
$reflector = new \ReflectionClass('\Curl\Url');
|
||||
$url = $reflector->newInstanceArgs($test['args']);
|
||||
$urls_file = fopen(dirname(__FILE__) . '/urls.csv', 'r');
|
||||
fgetcsv($urls_file); // header
|
||||
while (($test = fgetcsv($urls_file)) !== false) {
|
||||
$url = new Url($test[0], $test[1]);
|
||||
$actual_url = (string)$url;
|
||||
$this->assertEquals($test['expected'], $actual_url);
|
||||
$expected_url = $test[2];
|
||||
$this->assertEquals($expected_url, $actual_url, "Joint URLs: '{$test[0]}', '{$test[1]}'");
|
||||
}
|
||||
fclose($urls_file);
|
||||
}
|
||||
|
||||
public function testUrlInstances()
|
||||
@@ -84,10 +43,10 @@ class UrlTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals('https://developer.mozilla.org/en-US/docs', $h);
|
||||
|
||||
$k = new Url('https://developers.mozilla.com', 'http://www.example.com');
|
||||
$this->assertEquals('http://www.example.com/', $k);
|
||||
$this->assertEquals('http://www.example.com', $k);
|
||||
|
||||
$l = new Url($b, 'http://www.example.com');
|
||||
$this->assertEquals('http://www.example.com/', $l);
|
||||
$this->assertEquals('http://www.example.com', $l);
|
||||
}
|
||||
|
||||
public function testRemoveDotSegments()
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user