This file is indexed.

/usr/share/perl5/HTTP/Link/Parser.pm is in libhttp-link-parser-perl 0.200-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package HTTP::Link::Parser;

use 5.010;
use strict;
no warnings;

BEGIN
{
	$HTTP::Link::Parser::AUTHORITY = 'cpan:TOBYINK';
	$HTTP::Link::Parser::VERSION   = '0.200';
	
	require Exporter;
	our @ISA = qw(Exporter);
	our %EXPORT_TAGS = (
		'all'      => [qw/parse_links_into_model parse_links_to_rdfjson parse_links_to_list parse_single_link relationship_uri/],
		'standard' => [qw/parse_links_into_model parse_links_to_rdfjson/],
	);
	our @EXPORT_OK = @{ $EXPORT_TAGS{'all'} };
	our @EXPORT    = @{ $EXPORT_TAGS{'standard'} };
}

use Carp qw(croak carp);
use Encode qw(decode encode_utf8);
use Scalar::Util qw(blessed);
use URI;
use URI::Escape;

use constant (
	LINK_NAMESPACE => 'http://www.iana.org/assignments/relation/',
);

sub parse_links_into_model
{
	my ($response, $model) = @_;
	
	croak "Parameter to parse_links_into_model should be an HTTP::Message"
		unless blessed($response) && $response->isa('HTTP::Message');
	
	require RDF::Trine;
	
	my $model ||= RDF::Trine::Model->temporary_model;
	$model->add_hashref(parse_links_to_rdfjson($response));
	return $model;
}

sub parse_links_to_rdfjson
{
	my ($response) = @_;
	
	croak "Parameter to parse_links_to_rdfjson should be an HTTP::Message."
		unless blessed($response) && $response->isa('HTTP::Message');
		
	my $base  = URI->new($response->base);
	my $links = parse_links_to_list($response);
	my $rv    = {};
	
	foreach my $link (@$links)
	{
		my $subject = $base;
		
		$subject = $link->{'anchor'}
			if defined $link->{'anchor'};
		
		my $object = $link->{'URI'};
		
		foreach my $r (@{ $link->{'rel'} })
		{
			my $r1 = relationship_uri($r);
			push @{ $rv->{ $subject }->{ $r1 } },
				{
					'value'    => "$object",
					'type'     => 'uri',
				};
		}
		
		foreach my $r (@{ $link->{'rev'} })
		{
			my $r1 = relationship_uri($r);
			push @{ $rv->{ $object }->{ $r1 } },
				{
					'value'    => "$subject",
					'type'     => 'uri',
				};
		}
		
		if (defined $link->{'title'})
		{
			if (blessed($link->{'title'}) && $link->{'title'}->isa('HTTP::Link::Parser::PlainLiteral'))
			{
				push @{ $rv->{ $object }->{ 'http://purl.org/dc/terms/title' } },
					{
						'value'    => encode_utf8($link->{'title'}.''),
						'type'     => 'literal',
						'lang'     => $link->{'title'}->lang,
					};
			}
			else
			{
				push @{ $rv->{ $object }->{ 'http://purl.org/dc/terms/title' } },
					{
						'value'    => $link->{'title'},
						'type'     => 'literal',
					};
			}
		}
		
		if (defined $link->{'title*'})
		{
			foreach my $t (@{ $link->{'title*'} })
			{
				push @{ $rv->{ $object }->{ 'http://purl.org/dc/terms/title' } },
					{
						'value'    => encode_utf8("$t"),
						'type'     => 'literal',
						'lang'     => $t->lang,
					};
			}
		}
		
		if (defined $link->{'hreflang'})
		{
			foreach my $lang (@{ $link->{'hreflang'} })
			{
				push @{ $rv->{ $object }->{ 'http://purl.org/dc/terms/language' } },
					{
						'value'    => 'http://www.lingvoj.org/lingvo/' . uri_escape(lc $lang),
						'type'     => 'uri',
					};
			}
		}
		
		if (defined $link->{'type'} && $link->{'type'} =~ m?([A-Z0-9\!\#\$\&\.\+\-\^\_]{1,127})/([A-Z0-9\!\#\$\&\.\+\-\^\_]{1,127})?i)
		{
			my $type    = lc $1;
			my $subtype = lc $2;
			push @{ $rv->{ $object }->{ 'http://purl.org/dc/terms/format' } },
				{
					'value'    => 'http://www.iana.org/assignments/media-types/'.uri_escape($type).'/'.uri_escape($subtype),
					'type'     => 'uri',
				};
		}
	}
	
	return $rv;
}

sub relationship_uri
{
	my ($str) = @_;
	
	if ($str =~ /^([a-z][a-z0-9\+\.\-]{0,126})\:/i)
	{
		# seems to be an absolute URI, so can safely return "as is".
		return $str;
	}
	
	return LINK_NAMESPACE . lc $str;
}

sub parse_links_to_list
{
	my ($response) = @_;
	
	croak "Parameter to parse_links_to_list should be an HTTP::Message."
		unless blessed($response) && $response->isa('HTTP::Message');
	
	my $rv   = [];
	my $base = URI->new($response->base);
	
	my $clang;
	if ($response->header('Content-Language') =~ /^\s*([^,\s]+)/)
	{
		$clang = $1;
	}
	
	foreach my $header ($response->header('Link'))
	{
		push @$rv, parse_single_link($header, $base, $clang);
	}
	
	return $rv;
}

sub parse_single_link
{
	my ($hdrv, $base, $default_lang) = @_;
	my $rv   = {};
	
	my $uri  = undef;
	if ($hdrv =~ /^(\s*<([^>]*)>\s*)/)
	{
		$uri  = $2;
		$hdrv = substr($hdrv, length($1));
	}
	else
	{
		return $rv;
	}
	
	$rv->{'URI'} = URI->new_abs($uri, $base);
	
	while ($hdrv =~ /^(\s*\;\s*(\/|[a-z0-9-]+\*?)\s*\=\s*("[^"]*"|[^\s\"\;\,]+)\s*)/i)
	{
		$hdrv = substr($hdrv, length($1));
		my $key = lc $2;
		my $val = $3;
		
		$val =~ s/(^"|"$)//g if ($val =~ /^".*"$/);
		
		if ($key eq 'rel')
		{
			$val =~ s/(^\s+)|(\s+$)//g;
			$val =~ s/\s+/ /g;
			
			my @rels = split / /, $val;
			foreach my $rel (@rels)
				{ push @{ $rv->{'rel'} }, $rel; }
		}
		elsif ($key eq 'rev')
		{
			$val =~ s/(^\s+)|(\s+$)//g;
			$val =~ s/\s+/ /g;
			
			my @rels = split / /, $val;
			foreach my $rel (@rels)
				{ push @{ $rv->{'rev'} }, $rel; }
		}
		elsif ($key eq 'anchor')
		{
			$rv->{'anchor'} = URI->new_abs($val, $base)
				unless defined $rv->{'anchor'};
		}
		elsif ($key eq 'title')
		{
			if (defined $default_lang)
			{
				my $lit = bless [$val, undef, lc $default_lang], 'HTTP::Link::Parser::PlainLiteral';
				push @{ $rv->{'title'} }, $lit;
			}
			else
			{
				$rv->{'title'} = $val
					unless defined $rv->{'title'};
			}
		}
		elsif ($key eq 'title*')
		{
			my ($charset, $lang, $string) = split /\'/, $val;
			$string = uri_unescape($string);
			$string = decode($charset, $string);
			my $lit = bless [$string, undef, lc $lang], 'HTTP::Link::Parser::PlainLiteral';
			push @{ $rv->{'title*'} }, $lit;
		}
		elsif ($key eq 'type')
		{
			$rv->{'type'} = $val
				unless defined $rv->{'type'};
		}
		else # hreflang, plus any extended types.
		{
			push @{ $rv->{ $key } }, $val;
		}
	}
	
	return $rv;
}

{
	package HTTP::Link::Parser::PlainLiteral;
	
	use overload
		'""' => sub { $_[0]->[0] },
		'eq' => sub { $_[0]->[0] eq $_[1]->[0] and lc $_[0]->[2] eq lc $_[1]->[2] };
	
	sub value { $_[0]->[0]; }
	sub lang { length $_[0]->[2] ? $_[0]->[2] : undef; }
}

1;

__END__

=pod

=encoding utf-8

=for stopwords hreflang prev rel

=head1 NAME

HTTP::Link::Parser - parse HTTP Link headers

=head1 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);
  }

=head1 DESCRIPTION

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

=head2 Functions

To export all functions:

  use HTTP::Link::Parser ':all';

=over 4

=item C<< parse_links_into_model($response, [$existing_model]) >>

Takes an L<HTTP::Response> object (or in fact, any L<HTTP::Message> object)
and returns an L<RDF::Trine::Model> containing link data extracted from the
response. Dublin Core is used to encode 'hreflang', 'title' and 'type' link
parameters.

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

=item C<< 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.

=item C<< 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.

=back

=head2 Internal Functions

These are really just internal implementations, but you can use them if you
like.

=over

=item C<< 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
C<relationship_uri> function.

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

=item C<< 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.

=back

=head1 BUGS

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

=head1 SEE ALSO

L<http://www.ietf.org/rfc/rfc5988.txt>.

L<RDF::Trine>,
L<HTTP::Response>,
L<XRD::Parser>,
L<HTTP::LRDD>.

L<http://n2.talis.com/wiki/RDF_JSON_Specification>.

L<http://www.perlrdf.org/>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 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.

=cut