eosnewmedia / JSON-API-Common

Basic php implementation (shared structures for client and server) of the json api specification (jsonapi.org)
https://eosnewmedia.github.io/JSON-API-Common/
MIT License
14 stars 13 forks source link

URL prefix is misused and not well documented #19

Open pmarien opened 5 years ago

pmarien commented 5 years ago

Let's look at where url/api prefix is eventually used \Enm\JsonApi\Model\Request\Request::parseUriPath:

        preg_match(
            '/^(([a-zA-Z0-9\_\-\.\/]+.php)(\/)|)(' . $this->apiPrefix . ')([\/a-zA-Z0-9\_\-\.]+)$/',
            trim($path, '/'),
            $matches
        );

So the prefix is used in a regex using / as open/close characters. This basically means the prefix cannot contain that character (unless escaped).

Why does the default/test implementation work? Because the prefix is trimmed and the example doesn't try subpaths.

What's the problem with this?

What are the solutions?

See original issue: https://github.com/eosnewmedia/JSON-API-Server/issues/9

javi-p-nt commented 5 years ago

Ended using preg_quote(trim($prefix,'/'),'/') to make it work for subpaths.

ruuds commented 3 years ago

I fixed it by escaping the / within the $prefix. preg_quote didn't work for me:

preg_match(
    '/^(([a-zA-Z0-9\_\-\.\/]+.php)(\/)|)(' . str_replace('/','\\/',$this->apiPrefix) . ')([\/a-zA-Z0-9\_\-\.]+)$/',
    trim($path, '/'),
    $matches
);