linkheader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Parse HTTP response for interesting Link: headers
  7. *
  8. * PHP version 5
  9. *
  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.
  14. *
  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.
  19. *
  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/>.
  22. *
  23. * @category Discovery
  24. * @package StatusNet
  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/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Class to represent Link: headers in an HTTP response
  35. *
  36. * Since these are a fairly important part of Hammer-stack discovery, they're
  37. * reified and implemented here.
  38. *
  39. * @category Discovery
  40. * @package StatusNet
  41. * @author James Walker <james@status.net>
  42. * @copyright 2010 StatusNet, Inc.
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  44. * @link http://status.net/
  45. *
  46. * @see Discovery
  47. */
  48. class LinkHeader
  49. {
  50. var $href;
  51. var $rel;
  52. var $type;
  53. /**
  54. * Initialize from a string
  55. *
  56. * @param string $str Link: header value
  57. *
  58. * @return LinkHeader self
  59. */
  60. function __construct($str)
  61. {
  62. preg_match('/^<[^>]+>/', $str, $uri_reference);
  63. //if (empty($uri_reference)) return;
  64. $this->href = trim($uri_reference[0], '<>');
  65. $this->rel = array();
  66. $this->type = null;
  67. // remove uri-reference from header
  68. $str = substr($str, strlen($uri_reference[0]));
  69. // parse link-params
  70. $params = explode(';', $str);
  71. foreach ($params as $param) {
  72. if (empty($param)) {
  73. continue;
  74. }
  75. list($param_name, $param_value) = explode('=', $param, 2);
  76. $param_name = trim($param_name);
  77. $param_value = preg_replace('(^"|"$)', '', trim($param_value));
  78. // for now we only care about 'rel' and 'type' link params
  79. // TODO do something with the other links-params
  80. switch ($param_name) {
  81. case 'rel':
  82. $this->rel = trim($param_value);
  83. break;
  84. case 'type':
  85. $this->type = trim($param_value);
  86. }
  87. }
  88. }
  89. /**
  90. * Given an HTTP response, return the requested Link: header
  91. *
  92. * @param HTTP_Request2_Response $response response to check
  93. * @param string $rel relationship to look for
  94. * @param string $type media type to look for
  95. *
  96. * @return LinkHeader discovered header, or null on failure
  97. */
  98. static function getLink($response, $rel=null, $type=null)
  99. {
  100. $headers = $response->getHeader('Link');
  101. if ($headers) {
  102. // Can get an array or string, so try to simplify the path
  103. if (!is_array($headers)) {
  104. $headers = array($headers);
  105. }
  106. foreach ($headers as $header) {
  107. $lh = new LinkHeader($header);
  108. if ((is_null($rel) || $lh->rel == $rel) &&
  109. (is_null($type) || $lh->type == $type)) {
  110. return $lh->href;
  111. }
  112. }
  113. }
  114. return null;
  115. }
  116. }