LinkHeader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. *
  26. * @author James Walker <james@status.net>
  27. * @copyright 2010 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  29. *
  30. * @see http://status.net/
  31. */
  32. namespace Component\FreeNetwork\Util;
  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. *
  42. * @author James Walker <james@status.net>
  43. * @copyright 2010 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. *
  46. * @see http://status.net/
  47. * @see Discovery
  48. */
  49. class LinkHeader
  50. {
  51. public $href;
  52. public $rel;
  53. public $type;
  54. /**
  55. * Initialize from a string
  56. *
  57. * @param string $str Link: header value
  58. */
  59. public function __construct(string $str = '')
  60. {
  61. preg_match('/^<[^>]+>/', $str, $uri_reference);
  62. //if (empty($uri_reference)) return;
  63. $this->href = trim($uri_reference[0], '<>');
  64. $this->rel = [];
  65. $this->type = null;
  66. // remove uri-reference from header
  67. $str = substr($str, strlen($uri_reference[0]));
  68. // parse link-params
  69. $params = explode(';', $str);
  70. foreach ($params as $param) {
  71. if (empty($param)) {
  72. continue;
  73. }
  74. list($param_name, $param_value) = explode('=', $param, 2);
  75. $param_name = trim($param_name);
  76. $param_value = preg_replace('(^"|"$)', '', trim($param_value));
  77. // for now we only care about 'rel' and 'type' link params
  78. // TODO do something with the other links-params
  79. switch ($param_name) {
  80. case 'rel':
  81. $this->rel = trim($param_value);
  82. break;
  83. case 'type':
  84. $this->type = trim($param_value);
  85. }
  86. }
  87. }
  88. /**
  89. * Given an HTTP response, return the requested Link: header
  90. *
  91. * @param HTTP_Request2_Response $response response to check
  92. * @param string $rel relationship to look for
  93. * @param string $type media type to look for
  94. *
  95. * @return LinkHeader discovered header, or null on failure
  96. */
  97. public static function getLink($response, $rel = null, $type = null)
  98. {
  99. $headers = $response->getHeader('Link');
  100. if ($headers) {
  101. // Can get an array or string, so try to simplify the path
  102. if (!is_array($headers)) {
  103. $headers = [$headers];
  104. }
  105. foreach ($headers as $header) {
  106. $lh = new self($header);
  107. if ((is_null($rel) || $lh->rel == $rel) && (is_null($type) || $lh->type == $type)) {
  108. return $lh->href;
  109. }
  110. }
  111. }
  112. return null;
  113. }
  114. }