Skip to content

Add empty host and leading slash to Windows file paths #82

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

Merged
merged 4 commits into from
Sep 26, 2022
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ChangeLog
=========

3.0.0 (2022-09-20)
------------------

* #82: Add empty host and leading slash to windows file paths (@peterpostmann @phil-davis)

2.3.2 (2022-09-19)
------------------

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The library provides the following functions:

1. `resolve` to resolve relative urls.
2. `normalize` to aid in comparing urls.
3. `parse`, which works like PHP's [parse_url][6].
3. `parse`, which works like PHP's [parse_url][6] with special cases for some Windows-style paths [9].
4. `build` to do the exact opposite of `parse`.
5. `split` to easily get the 'dirname' and 'basename' of a URL without all the
problems those two functions have.
Expand Down Expand Up @@ -54,3 +54,4 @@ This library is being developed by [fruux](https://fruux.com/). Drop us a line f
[6]: http://php.net/manual/en/function.parse-url.php
[7]: http://sabre.io/uri/install/
[8]: http://sabre.io/uri/usage/
[9]: https://github.com/sabre-io/uri/pull/71
2 changes: 1 addition & 1 deletion lib/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ class Version
/**
* Full version number.
*/
public const VERSION = '2.3.2';
public const VERSION = '3.0.0';
}
13 changes: 13 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ function ($matches) {
$result = parse_url($uri);
if (!$result) {
$result = _parse_fallback($uri);
} else {
// Add empty host and leading slash to Windows file paths
// file:///C:/path or file:///C:\path
// Note: the regex fragment [a-zA-Z]:[\/\\\\].* end up being
// [a-zA-Z]:[\/\\].*
// The 4 backslash in a row are the way to get 2 backslash into the actual string
// that is used as the regex. The 2 backslash are then the way to get 1 backslash
// character into the character set "a forward slash or a backslash"
if (isset($result['scheme']) && 'file' === $result['scheme'] && isset($result['path']) &&
preg_match('/^(?<windows_path> [a-zA-Z]:([\/\\\\].*)?)$/x', $result['path'])) {
$result['path'] = '/'.$result['path'];
$result['host'] = '';
}
}

/*
Expand Down
58 changes: 3 additions & 55 deletions tests/Uri/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ParseTest extends TestCase
{
/**
* @dataProvider parseData
* @dataProvider windowsFormatTestCasesForParse
* @dataProvider windowsFormatTestCases
*
* @param array<int, array<int, array<string, int|string|null>|string>> $out
*/
Expand All @@ -24,7 +24,7 @@ public function testParse(string $in, array $out): void

/**
* @dataProvider parseData
* @dataProvider windowsFormatTestCasesForParseFallback
* @dataProvider windowsFormatTestCases
*
* @param array<int, array<int, array<string, int|string|null>|string>> $out
*/
Expand Down Expand Up @@ -233,7 +233,7 @@ public function parseData(): array
/**
* @return array<int, array<int, array<string, int|string|null>|string>>
*/
public function windowsFormatTestCasesForParseFallback(): array
public function windowsFormatTestCases(): array
{
return [
[
Expand Down Expand Up @@ -274,56 +274,4 @@ public function windowsFormatTestCasesForParseFallback(): array
],
];
}

/**
* These test cases demonstrate that the parse() function does not
* return a "/" at the start of the path for URIs of the form
* file:///C:/something...
*
* See issue comment https://github.com/sabre-io/uri/pull/71#issuecomment-1250724859
* and gthe linked issues and PRs.
*
* @return array<int, array<int, array<string, int|string|null>|string>>
*/
public function windowsFormatTestCasesForParse(): array
{
return [
[
'file:///C:/path/file.ext',
[
'scheme' => 'file',
'host' => '',
'path' => 'C:/path/file.ext',
'port' => null,
'user' => null,
'query' => null,
'fragment' => null,
],
],
[
'file:///C:\path\file.ext',
[
'scheme' => 'file',
'host' => '',
'path' => 'C:\path\file.ext',
'port' => null,
'user' => null,
'query' => null,
'fragment' => null,
],
],
[
'file:///C:',
[
'scheme' => 'file',
'host' => '',
'path' => 'C:',
'port' => null,
'user' => null,
'query' => null,
'fragment' => null,
],
],
];
}
}
5 changes: 5 additions & 0 deletions tests/Uri/ResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public function resolveData(): array
'file_b.ext',
'file:///C:/path/file_b.ext',
],
[
'file:///C:/path/of/dirs/',
'file.txt',
'file:///C:/path/of/dirs/file.txt',
],
];
}
}