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
|
use strictures 2;
package HTTP::Link;
# ABSTRACT: RFC5988 Web Linking
use Exporter qw(import);
use MIME::EcoEncode::Param qw(mime_eco_param mime_deco_param);
our $VERSION = '0.001'; # VERSION
our @EXPORT_OK = qw(
httplink_new
httplink_rel
httplink_multi
httplink_parse
httplink_parse_hash
);
our %EXPORT_TAGS = ( all => \@EXPORT_OK, );
sub _encode {
my $str = shift;
my $estr = '=' . $str;
my $xstr = mime_eco_param( $estr, 'UTF-8?B', '', 2**31 );
if ( $estr eq $xstr ) {
return '="' . $str . '"';
}
else {
return '*' . $xstr;
}
}
sub httplink_new {
unshift @_ => __PACKAGE__;
goto &new;
}
sub httplink_rel {
unshift @_ => __PACKAGE__;
goto &rel;
}
sub httplink_multi {
unshift @_ => __PACKAGE__;
goto &multi;
}
sub httplink_parse {
unshift @_ => __PACKAGE__;
goto &parse;
}
sub httplink_parse_hash {
unshift @_ => __PACKAGE__;
goto &parse_hash;
}
sub new {
my $self = shift;
my ( $iri, %options ) = @_;
my $str = "<$iri>";
if ( my $relation = delete $options{relation} ) {
if ( ref $relation eq 'ARRAY' ) {
$str .= '; rel="' . join( ' ', @$relation ) . '"';
}
else {
if ( my $extension = delete $options{extension} ) {
$relation .= " $extension";
}
$str .= '; rel="' . $relation . '"';
}
}
if ( my $anchor = delete $options{anchor} ) {
$str .= '; anchor="' . $anchor . '"';
}
if ( my $hreflang = delete $options{hreflang} ) {
$str .= '; hreflang="' . $hreflang . '"';
}
if ( my $media = delete $options{media} ) {
$str .= '; media="' . $media . '"';
}
if ( my $title = delete $options{title} ) {
$str .= '; title' . _encode($title);
}
if ( my $type = delete $options{type} ) {
$str .= '; type="' . $type . '"';
}
return $str;
}
sub rel {
my ( $self, $iri, $relation, $extension, %options ) = @_;
return $self->new(
$iri,
%options,
relation => $relation,
extension => $extension
);
}
sub multi {
my $self = shift;
if ( @_ == 1 and ref $_[0] eq 'HASH' ) {
return $self->multi( %{ $_[0] } );
}
my %map = @_;
my @str;
foreach my $iri ( keys %map ) {
my $part = delete $map{$iri};
push @str => $self->new( $iri, %$part );
}
return join ', ', sort @str;
}
my $ptoken = qr{[
\!\#\$\%\&\'\(\)\*\+\-\.\/\d\:\<\=\>\?\@\[\]\^\_\`\{\|\}\~
a-z
]+}xsi;
my $lparam = qr{ = (?: " [^"]*? " | $ptoken ) }sx;
my $split = qr{ \s* = \s* (?<quot>"?) \s* (?<val> .* ) \s* \k<quot> \s* }sx;
my $re = qr{
< (?<iri> [^>]+ ) >
(?:
\s*
;[\s*;]*
\s*
(?:
rel (?<relation> $lparam )
|
anchor (?<anchor> $lparam )
|
rev (?<rev> $lparam )
|
hreflang (?<hreflang> $lparam )
|
media (?<media> $lparam )
|
title (?<title> $lparam )
|
title\* (?<xtitle> $lparam )
|
type (?<type> $lparam )
)
\s*
)*
[\s*;]*
}sx;
sub parse {
my ( $self, $str ) = @_;
$str .= ',';
pos($str) = 0;
my @results;
while ( $str =~ m{ \G \s* $re \s* ,+ }sxg ) {
my %r = %+;
my $r = {};
foreach my $k (qw(iri relation rev anchor hreflang media title type)) {
next unless defined $r{$k};
my $v = $r{$k};
$v =~ s{^$split$}{$+{val}}sxe;
$r->{$k} = $v;
}
if (
defined $r->{relation}
and $r->{relation} =~ m{^
( [a-z] [a-z\d\.\-]* ) \s+ (.+)
$}six
)
{
$r->{relation} = $1;
$r->{extension} = $2;
}
if ( exists $r{xtitle} ) {
my ( $decoded, $param, $charset, $lang, $value ) =
mime_deco_param( $r{xtitle} );
$r->{title} = $value;
}
push @results => $r;
}
return @results;
}
sub parse_hash {
my ( $self, $str ) = @_;
my @list = $self->parse($str);
my %hash;
foreach my $elem (@list) {
my $iri = delete $elem->{iri};
$hash{$iri} = $elem;
}
return %hash;
}
1;
__END__
=pod
=head1 NAME
HTTP::Link - RFC5988 Web Linking
=head1 VERSION
version 0.001
=head1 METHODS
=head2 new
Return a new Web Link as string
my $link = HTTP::Link->new('/TheBook/Chapter/2',
relation => 'next',
title => 'next chapter',
);
# Result:
# </TheBook/Chapter/2>; rel="next"; title="next chapter"
B<Arguments>:
=over 4
=item * C<$iri> An internationalized resource identifier. This is a target uri
reference.
=item * C<%options> Additional parameters (see below)
=back
B<Parameters> for C<%options>:
=over 4
=item * C<relation>
Possible relation types are:
I<alternate>, I<appendix>, I<bookmark>, I<chapter>, I<contents>, I<copyright>,
I<current>, I<describedby>, I<edit>, I<edit-media>, I<enclosure>, I<first>,
I<glossary>, I<help>, I<hub>, I<index>, I<last>, I<latest-version>, I<license>,
I<next>, I<next-archive>, I<payment>, I<prev>, I<predecessor-version>,
I<previous>, I<prev-archive>, I<related>, I<replies>, I<section>, I<self>,
I<service>, I<start>, I<stylesheet>, I<subsection>, I<successor-version>,
I<up>, I<version-history>, I<via>, I<working-copy>, I<working-copy-of> and
others (see IANA registry)
=item * C<extension>
An extension for a relation type.
=item * C<anchor>
An anchor for the context IRI.
=item * C<hreflang>
A hint indicating what the language of the destination's content is
=item * C<media>
Intended destination medium, like in a CSS media query.
=item * C<title>
A human-readable label. This will be encoded with L<MIME::EcoEncode>.
=item * C<type>
A hint indication the most possible MIME type of the destination's content.
=back
=head2 rel
Shortcut method for L</new> with a different signature:
HTTP::Link->rel('/TheBook/Chapter/2',
start => '/TheBook/Chapter/0'
);
# same as
HTTP::Link->new('/TheBook/Chapter/2',
relation => 'start',
extension => '/TheBook/Chapter/0',
);
B<Arguments>:
=over 4
=item * C<$iri>
See L</new>
=item * C<$relation>
Will appended to C<%options> by C< relation => $relation >>
=item * C<$extension>
Will appended to C<%options> by C< extension => $extension >>
=item * C<%options>
See L</new>
=back
=head2 multi
When more than one link should be provides, this method is useful. It accepts
a hash or a HashRef with a mapping of IRIs to options.
HTTP::Link->multi(
'/TheBook/Chapter/2' => {
relation => 'previous'
},
'/TheBook/Chapter/4' => {
relation => 'next'
},
);
# Result:
# </TheBook/Chapter/2>; rel="previous", </TheBook/Chapter/4>; rel="next"
=head2 parse
Parses a Link-header field a returns all founded links as a list of HashRefs.
my @links = HTTP::Link->parse(<<EOT);
</TheBook/Chapter/2>; rel="previous", </TheBook/Chapter/4>; rel="next"
EOT
# Result:
@links = ({
iri => '/TheBook/Chapter/2',
relation => 'previous'
},{
iri => '/TheBook/Chapter/4',
relation => 'next'
});
=head2 parse_hash
Parses a Link-header field a returns all founded links as a HashRef with IRIs
as key:
my %links = HTTP::Link->parse_hash(<<EOT);
</TheBook/Chapter/2>; rel="previous", </TheBook/Chapter/4>; rel="next"
EOT
# Result:
%links = (
'/TheBook/Chapter/2' => {
relation => 'previous'
},
'/TheBook/Chapter/4' => {
relation => 'next'
}
);
=head1 FUNCTIONS
=head2 httplink_new
Wrapper for C<< HTTP::Link-> >>L</new>
=head2 httplink_rel
Wrapper for C<< HTTP::Link-> >>L</rel>
=head2 httplink_multi
Wrapper for C<< HTTP::Link-> >>L</multi>
=head2 httplink_parse
Wrapper for C<< HTTP::Link-> >>L</parse>
=head2 httplink_parse_hash
Wrapper for C<< HTTP::Link-> >>L</parse_hash>
=head1 BUGS
Please report any bugs or feature requests on the bugtracker website
https://github.com/zurborg/libhttp-link-perl/issues
When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.
=head1 AUTHOR
David Zurborg <zurborg@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2015 by David Zurborg.
This is free software, licensed under:
The ISC License
=cut
|