HTTP::Link::Parser(3) parse HTTP Link headers

SYNOPSIS


use HTTP::Link::Parser ':standard';
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
my $response = $ua->get("http://example.com/foo");

# Parse link headers into an RDF::Trine::Model.
my $model = parse_links_into_model($response);
# Find data about <http://example.com/foo>.
my $iterator = $model->get_statements(
RDF::Trine::Node::Resource->new('http://example.com/foo'),
undef,
undef);
while ($statement = $iterator->next)
{
# Skip data where the value is not a resource (i.e. link)
next unless $statement->object->is_resource;
printf("Link to <%s> with rel=\"%s\".\n",
$statement->object->uri,
$statement->predicate->uri);
}

DESCRIPTION

HTTP::Link::Parser parses HTTP ``Link'' headers found in an HTTP::Response object. Headers should conform to the format described in RFC 5988.

Functions

To export all functions:

  use HTTP::Link::Parser ':all';
"parse_links_into_model($response, [$existing_model])"
Takes an HTTP::Response object (or in fact, any HTTP::Message object) and returns an RDF::Trine::Model containing link data extracted from the response. Dublin Core is used to encode 'hreflang', 'title' and 'type' link parameters.

$existing_model is an RDF::Trine::Model to add data to. If omitted, a new, empty model is created.

"parse_links_to_rdfjson($response)"
Returns a hashref with a structure inspired by the RDF/JSON specification. This can be thought of as a shortcut for:

  parse_links_into_model($response)->as_hashref

But it's faster as no intermediate model is built.

"relationship_uri($short)"
This function is not exported by default.

It may be used to convert short strings identifying relationships, such as ``next'' and ``prev'', into longer URIs identifying the same relationships, such as ``http://www.iana.org/assignments/relation/next'' and ``http://www.iana.org/assignments/relation/prev''.

If passed a string which is a URI already, simply returns it as-is.

Internal Functions

These are really just internal implementations, but you can use them if you like.
"parse_links_to_list($response)"
This function is not exported by default.

Returns an arrayref of hashrefs. Each hashref contains keys corresponding to the link parameters of the link, and a key called 'URI' corresponding to the target of the link.

The 'rel' and 'rev' keys are arrayrefs containing lists of relationships. If the Link used the short form of a registered relationship, then the short form is present on this list. Short forms can be converted to long forms (URIs) using the "relationship_uri" function.

The structure returned by this function should not be considered stable.

"parse_single_link($link, $base, [$default_lang])"
This function is not exported by default.

This parses a single Link header (minus the ``Link:'' bit itself) into a hashref structure. A base URI must be included in case the link contains relative URIs. A default language can be provided for the 'title' parameter.

The structure returned by this function should not be considered stable.

BUGS

Please report any bugs to <http://rt.cpan.org/>.

AUTHOR

Toby Inkster <[email protected]>.

COPYRIGHT AND LICENCE

Copyright (C) 2009-2011, 2014 by Toby Inkster

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ``Software''), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.