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/
57 * Initialize from a string
59 * @param string $str Link: header value
61 * @return LinkHeader self
63 function __construct($str)
65 preg_match('/^<[^>]+>/', $str, $uri_reference);
66 //if (empty($uri_reference)) return;
68 $this->href = trim($uri_reference[0], '<>');
72 // remove uri-reference from header
73 $str = substr($str, strlen($uri_reference[0]));
76 $params = explode(';', $str);
78 foreach ($params as $param) {
82 list($param_name, $param_value) = explode('=', $param, 2);
84 $param_name = trim($param_name);
85 $param_value = preg_replace('(^"|"$)', '', trim($param_value));
87 // for now we only care about 'rel' and 'type' link params
88 // TODO do something with the other links-params
89 switch ($param_name) {
91 $this->rel = trim($param_value);
95 $this->type = trim($param_value);
101 * Given an HTTP response, return the requested Link: header
103 * @param HTTP_Request2_Response $response response to check
104 * @param string $rel relationship to look for
105 * @param string $type media type to look for
107 * @return LinkHeader discovered header, or null on failure
109 static function getLink($response, $rel=null, $type=null)
111 $headers = $response->getHeader('Link');
113 // Can get an array or string, so try to simplify the path
114 if (!is_array($headers)) {
115 $headers = array($headers);
118 foreach ($headers as $header) {
119 $lh = new LinkHeader($header);
121 if ((is_null($rel) || $lh->rel == $rel) &&
122 (is_null($type) || $lh->type == $type)) {