LinkHeader.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * Parse HTTP response for interesting Link: headers
  8. *
  9. * PHP version 5
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * @category Discovery
  25. * @package StatusNet
  26. *
  27. * @author James Walker <james@status.net>
  28. * @copyright 2010 StatusNet, Inc.
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  30. *
  31. * @see http://status.net/
  32. */
  33. namespace Component\FreeNetwork\Util;
  34. /**
  35. * Class to represent Link: headers in an HTTP response
  36. *
  37. * Since these are a fairly important part of Hammer-stack discovery, they're
  38. * reified and implemented here.
  39. *
  40. * @category Discovery
  41. * @package StatusNet
  42. *
  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. *
  47. * @see http://status.net/
  48. * @see Discovery
  49. */
  50. class LinkHeader
  51. {
  52. public $href;
  53. public $rel;
  54. public $type;
  55. /**
  56. * Initialize from a string
  57. *
  58. * @param string $str Link: header value
  59. */
  60. public function __construct(string $str = '')
  61. {
  62. preg_match('/^<[^>]+>/', $str, $uri_reference);
  63. //if (empty($uri_reference)) return;
  64. $this->href = trim($uri_reference[0], '<>');
  65. $this->rel = [];
  66. $this->type = null;
  67. // remove uri-reference from header
  68. $str = mb_substr($str, mb_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. [$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. public 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 = [$headers];
  105. }
  106. foreach ($headers as $header) {
  107. $lh = new self($header);
  108. if ((\is_null($rel) || $lh->rel == $rel) && (\is_null($type) || $lh->type == $type)) {
  109. return $lh->href;
  110. }
  111. }
  112. }
  113. }
  114. }