Fix bugs w/prepend base.

This commit is contained in:
Dave Longley 2013-02-08 15:48:23 -05:00
parent 61e1140b3b
commit 612f56aa6c

View file

@ -282,10 +282,18 @@ function jsonld_parse_url($url) {
* @return string the absolute IRI.
*/
function jsonld_prepend_base($base, $iri) {
// already an absolute IRI
if(strpos($iri, ':') !== false) {
return $iri;
}
if(is_string($base)) {
$base = jsonld_parse_url($base);
}
$authority = $base['host'];
$authority = $base['host'];
if(isset($base['port'])) {
$authority .= ":{$base['port']}";
}
$rel = jsonld_parse_url($iri);
// per RFC3986 normalize slashes and dots in path
@ -352,13 +360,19 @@ function jsonld_prepend_base($base, $iri) {
// add query and hash
if(isset($rel['query'])) {
$path .= '?' . $rel['query'];
$path .= "?{$rel['query']}";
}
if(isset($rel['fragment'])) {
$path .= '#' . $rel['fragment'];
}
$path .= "#{$rel['fragment']}";
}
$absolute_iri = "{$base['scheme']}://";
if(isset($base['user']) || isset($base['pass'])) {
$absolute_iri .= "{$base['user']}:{$base['pass']}@";
}
$absolute_iri .= "$authority$path";
return $base['scheme'] . "://$authority$path";
return $absolute_iri;
}
/**
@ -4340,7 +4354,7 @@ class JsonLdProcessor {
* the @contexts from the urls map, false not to.
* @param string $base the base URL to resolve relative URLs with.
*/
protected function _findContextUrls($input, $urls, $replace, $base) {
protected function _findContextUrls($input, $urls, $replace, $base) {
if(is_array($input)) {
foreach($input as $e) {
$this->_findContextUrls($e, $urls, $replace, $base);