Fix link header parsing bugs.

This commit is contained in:
Dave Longley 2013-09-14 14:45:41 -04:00
parent d27ad5f210
commit 5566889660

View file

@ -174,7 +174,8 @@ function jsonld_to_rdf($input, $options=array()) {
* } * }
* *
* If there is more than one "rel" with the same IRI, then entries in the * If there is more than one "rel" with the same IRI, then entries in the
* resulting map for that "rel" will be arrays. * resulting map for that "rel" will be arrays of objects, otherwise they will
* be single objects.
* *
* @param string $header the link header to parse. * @param string $header the link header to parse.
* *
@ -183,27 +184,27 @@ function jsonld_to_rdf($input, $options=array()) {
function jsonld_parse_link_header($header) { function jsonld_parse_link_header($header) {
$rval = array(); $rval = array();
// split on unbracketed/unquoted commas // split on unbracketed/unquoted commas
if(!preg_match_all('/(?:<[^>]*?>|"[^"]*?"|[^,])+/', $header, $entries)) { if(!preg_match_all(
'/(?:<[^>]*?>|"[^"]*?"|[^,])+/', $header, $entries, PREG_SET_ORDER)) {
return $rval; return $rval;
} }
$r_link_header = '/\s*<([^>]*?)>\s*(?:;\s*(.*))?/'; $r_link_header = '/\s*<([^>]*?)>\s*(?:;\s*(.*))?/';
foreach($entries as $entry) { foreach($entries as $entry) {
$match = preg_match($r_link_header, $entry[0]); if(!preg_match($r_link_header, $entry[0], $match)) {
if(!$match) {
continue; continue;
} }
$result = array('target' => $match[1]); $result = (object)array('target' => $match[1]);
$params = $match[2]; $params = $match[2];
$r_params = '/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/'; $r_params = '/(.*?)=(?:(?:"([^"]*?)")|([^"]*?))\s*(?:(?:;\s*)|$)/';
$matches = preg_match_all($r_params, $params); preg_match_all($r_params, $params, $matches, PREG_SET_ORDER);
foreach($matches as $match) { foreach($matches as $match) {
$result[$match[1]] = $match[2] ?: $match[3]; $result->{$match[1]} = $match[2] ?: $match[3];
} }
$rel = $result['rel'] ?: ''; $rel = property_exists($result, 'rel') ? $result->rel : '';
if(!isset($rval, $rel)) { if(!isset($rval[$rel])) {
$rval[$rel] = $result; $rval[$rel] = $result;
} }
else if(is_array($rval, $rel)) { else if(is_array($rval[$rel])) {
$rval[$rel][] = $result; $rval[$rel][] = $result;
} }
else { else {