linkheader.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Implementation of discovery using HTTP Link header
  4. *
  5. * Discovers XRD file for a user by fetching the URL and reading any
  6. * Link: headers in the HTTP response.
  7. *
  8. * @category Discovery
  9. * @package StatusNet
  10. * @author James Walker <james@status.net>
  11. * @copyright 2010 StatusNet, Inc.
  12. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  13. * @link http://status.net/
  14. */
  15. class LRDDMethod_LinkHeader extends LRDDMethod
  16. {
  17. /**
  18. * For HTTP IDs fetch the URL and look for Link headers.
  19. *
  20. * @todo fail out of WebFinger URIs faster
  21. */
  22. public function discover($uri)
  23. {
  24. $response = self::fetchUrl($uri, HTTPClient::METHOD_HEAD);
  25. $link_header = $response->getHeader('Link');
  26. if (empty($link_header)) {
  27. throw new Exception('No Link header found');
  28. }
  29. common_debug('LRDD LinkHeader found: '.var_export($link_header,true));
  30. return self::parseHeader($link_header);
  31. }
  32. /**
  33. * Given a string or array of headers, returns JRD-like assoc array
  34. *
  35. * @param string|array $header string or array of strings for headers
  36. *
  37. * @return array of associative arrays in JRD-like array format
  38. */
  39. protected static function parseHeader($header)
  40. {
  41. $lh = new LinkHeader($header);
  42. $link = new XML_XRD_Element_Link($lh->rel, $lh->href, $lh->type);
  43. return array($link);
  44. }
  45. }