3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Parse HTTP response for interesting Link: headers
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author James Walker <james@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
36 * Class to represent Link: headers in an HTTP response
38 * Since these are a fairly important part of Hammer-stack discovery, they're
39 * reified and implemented here.
43 * @author James Walker <james@status.net>
44 * @copyright 2010 StatusNet, Inc.
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
46 * @link http://status.net/
58 * Initialize from a string
60 * @param string $str Link: header value
62 * @return LinkHeader self
65 function __construct($str)
67 preg_match('/^<[^>]+>/', $str, $uri_reference);
68 //if (empty($uri_reference)) return;
70 $this->href = trim($uri_reference[0], '<>');
74 // remove uri-reference from header
75 $str = substr($str, strlen($uri_reference[0]));
78 $params = explode(';', $str);
80 foreach ($params as $param) {
84 list($param_name, $param_value) = explode('=', $param, 2);
86 $param_name = trim($param_name);
87 $param_value = preg_replace('(^"|"$)', '', trim($param_value));
89 // for now we only care about 'rel' and 'type' link params
90 // TODO do something with the other links-params
91 switch ($param_name) {
93 $this->rel = trim($param_value);
97 $this->type = trim($param_value);
103 * Given an HTTP response, return the requested Link: header
105 * @param HTTP_Request2_Response $response response to check
106 * @param string $rel relationship to look for
107 * @param string $type media type to look for
109 * @return LinkHeader discovered header, or null on failure
112 static function getLink($response, $rel=null, $type=null)
114 $headers = $response->getHeader('Link');
116 // Can get an array or string, so try to simplify the path
117 if (!is_array($headers)) {
118 $headers = array($headers);
121 foreach ($headers as $header) {
122 $lh = new LinkHeader($header);
124 if ((is_null($rel) || $lh->rel == $rel) &&
125 (is_null($type) || $lh->type == $type)) {