Validate URLs; check for file/json read errors.

This commit is contained in:
Dave Longley 2013-09-14 21:27:55 -04:00
parent d85c25bdb3
commit 4ddb4e8c2c
2 changed files with 21 additions and 4 deletions

View file

@ -5063,6 +5063,9 @@ class JsonLdProcessor {
// for tracking the URLs to retrieve
$urls = new stdClass();
// regex for validating URLs
$regex = '/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/';
// find all URLs in the given input
$this->_findContextUrls($input, $urls, false, $base);
@ -5070,6 +5073,12 @@ class JsonLdProcessor {
$queue = array();
foreach($urls as $url => $ctx) {
if($ctx === false) {
// validate URL
if(!preg_match($regex, $url)) {
throw new JsonLdException(
'Malformed or unsupported URL.', 'jsonld.InvalidUrl',
'loading remote context failed', array('url' => $url));
}
$queue[] = $url;
}
}
@ -5098,7 +5107,7 @@ class JsonLdProcessor {
catch(Exception $e) {
throw new JsonLdException(
'Could not parse JSON from URL.',
'jsonld.ParseError', 'invalid remote context',
'jsonld.ParseError', 'loading remote context failed',
array('url' => $url), $e);
}
}

View file

@ -570,15 +570,23 @@ class EarlReport implements PHPUnit_Framework_TestListener {
class Util {
public static function readFile($filename) {
return file_get_contents($filename);
$rval = @file_get_contents($filename);
if($rval === false) {
throw new Exception('File read error: ' . $filename);
}
return $rval;
}
public static function readJson($filename) {
return json_decode(file_get_contents($filename));
$rval = json_decode(self::readFile($filename));
if($rval === null) {
throw new Exception('JSON parse error');
}
return $rval;
}
public static function readNQuads($filename) {
return readFile($filename);
return self::readFile($filename);
}
public static function jsonldEncode($input) {