sabre-io / uri

:earth_asia: Functions for making sense out of URIs in PHP

Home Page:http://sabre.io/uri/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

windows file:// uri resolving

nobbynobbs opened this issue · comments

commented

Hello!

I'm using league/json-reference library for json validation, and this library uses sabre/uri for resolving external references in json-schema.

Everything works fine on Linux machines, but not on Windows.
For example if function Sabre\Uri\resolve being called with parameters

$basePath = 'file://D:/development/projects/markirovka/storage/app/jsonSchemas/undefined_order.json';
$newPath = './types/turnover_type.json';

result will be

file://D/development/projects/markirovka/storage/app/jsonSchemas/types/turnover_type.json

The comma after disk partition letter will be missed, and it couse an error.

I've being debugging a bit, and I think it can be fixed with minor changes in lib/functions.
Function for Windows detection:

/**
 * This function returns true if aplication run in Windows environment
 * @return bool
 */
function isWindows() {
    return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}

And changes in build function:

function build(array $parts) {

    $uri = '';

    $authority = '';
    if (!empty($parts['host'])) {
        $authority = $parts['host'];
        if (!empty($parts['user'])) {
            $authority = $parts['user'] . '@' . $authority;
        }
        if (!empty($parts['port'])) {
            $authority = $authority . ':' . $parts['port'];
        }
    }
// ============= add this ===============
    /**
     * on Windows systems for local URIs $parts['host'] will be a partition letter,
     * and we must add colon after it
     */
    if (isWindows() && !empty($parts['scheme']) && $parts['scheme'] == 'file') {
        $authority .= ':';
    }
// ==================================

    if (!empty($parts['scheme'])) {
        // If there's a scheme, there's also a host.
        $uri = $parts['scheme'] . ':';

    }
    if ($authority || (!empty($parts['scheme']) && $parts['scheme'] === 'file')) {
        // No scheme, but there is a host.
        $uri .= '//' . $authority;

    }

    if (!empty($parts['path'])) {
        $uri .= $parts['path'];
    }
    if (!empty($parts['query'])) {
        $uri .= '?' . $parts['query'];
    }
    if (!empty($parts['fragment'])) {
        $uri .= '#' . $parts['fragment'];
    }

    return $uri;
}

What do you think about this suggestion?

And excuse my english, it's not my native language.

Does this change result in a green unit testsuite?

Does #25 also fix your issue?

commented

Does this change result in a green unit testsuite?

No it doesn't :-( This make tests even more red. Sorry, I've posted issue before I clone project and run the tests.

Does #25 also fix your issue?

Actually I don't know. I've read that issue, and in first sight it looks like something different - I have problem with missed colon, and the first issue is about slashes. But probably both issues are related, because they are about Windows and file:// scheme.

The reason you are having this issue, is because your uri is not valid. This uri:

file://D:/development/projects/markirovka/storage/app/jsonSchemas/undefined_order.json

means that the path is located on a hostname D. The reason the colon : gets stripped is because it's a separator for a hostname and port. Similar to this:

http://D:80/

What you want, is to locate that file on localhost. To do this, you specify an empty hostname (authority):

file:///D:/development/projects/markirovka/storage/app/jsonSchemas/undefined_order.json

This results in 3 subsequent slashes.

Not sure if you saw the json-reference issue about windows file uris but it may be helpful. @peterpostmann wrote this library to transform windows paths into valid file uris.

commented

What you want, is to locate that file on localhost. To do this, you specify an empty hostname (authority):
file:///D:/development/projects/markirovka/storage/app/jsonSchemas/undefined_order.json

Thank you. I've try it from the beginning, but json-reference threw SchemaLoadingException, so I just removed one slash and shema was succesfully loaded, and validation worked fine. But when I've added external reference in shema it collapsed, and I supposed the problem in reference resolving.

My bad, sorry.

Not sure if you saw the json-reference issue about windows file uris but it may be helpful. @peterpostmann wrote this library to transform windows paths into valid file uris

No, I didn't see it, thank you.