File: LinkHeader.pm

package info (click to toggle)
libhttp-headers-actionpack-perl 0.09-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 364 kB
  • sloc: perl: 2,614; makefile: 2
file content (190 lines) | stat: -rw-r--r-- 4,214 bytes parent folder | download | duplicates (5)
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
package HTTP::Headers::ActionPack::LinkHeader;
BEGIN {
  $HTTP::Headers::ActionPack::LinkHeader::AUTHORITY = 'cpan:STEVAN';
}
{
  $HTTP::Headers::ActionPack::LinkHeader::VERSION = '0.09';
}
# ABSTRACT: A Link

use strict;
use warnings;

use URI::Escape                     qw[ uri_escape uri_unescape ];
use HTTP::Headers::ActionPack::Util qw[ join_header_words ];

use parent 'HTTP::Headers::ActionPack::Core::BaseHeaderType';

sub BUILDARGS {
    my $class = shift;
    my ($href, @params) = @_;

    $href =~ s/^<//;
    $href =~ s/>$//;

    $class->SUPER::BUILDARGS( $href, @params );
}

sub BUILD {
    my $self = shift;
    foreach my $param ( grep { /\*$/ } @{ $self->_param_order } ) {
        my ($encoding, $language, $content) = ( $self->params->{ $param } =~ /^(.*)\'(.*)\'(.*)$/);
        $self->params->{ $param } = {
            encoding => $encoding,
            language => $language,
            content  => uri_unescape( $content )
        };
    }
}

sub href { (shift)->subject         }
sub rel  { (shift)->params->{'rel'} }

sub relation_matches {
    my ($self, $relation) = @_;

    if ( my $rel = $self->params->{'rel'} ) {
        # if it is an extension rel type
        # then it is a URI and it should
        # not be compared in a case-insensitive
        # manner ...
        if ( $rel =~ m!^\w+\://! ) {
            $self->params->{'rel'} eq $relation ? 1 : 0;
        }
        # if it is not a URI, then compare
        # it case-insensitively
        else {
            (lc $self->params->{'rel'} ) eq (lc $relation) ? 1 : 0;
        }
    }
}

sub as_string {
    my $self = shift;

    my @params;
    foreach my $param ( @{ $self->_param_order } ) {
        if ( $param =~ /\*$/ ) {
            my $complex = $self->params->{ $param };
            push @params => ( $param,
                join "'" => (
                    $complex->{'encoding'},
                    $complex->{'language'},
                    uri_escape( $complex->{'content'} ),
                )
            );
        }
        else {
            push @params => ( $param, $self->params->{ $param } );
        }
        my ($encoding, $language, $content) = ( $self->params->{ $param } =~ /^(.*)\'(.*)\'(.*)$/);
    }

    join_header_words( '<' . $self->href . '>', @params );
}

1;

__END__

=pod

=head1 NAME

HTTP::Headers::ActionPack::LinkHeader - A Link

=head1 VERSION

version 0.09

=head1 SYNOPSIS

  use HTTP::Headers::ActionPack::LinkHeader;

  # build from string
  my $link = HTTP::Headers::ActionPack::LinkHeader->new_from_string(
      '<http://example.com/TheBook/chapter2>;rel="previous";title="previous chapter"'
  );

  # normal constructor
  my $link = HTTP::Headers::ActionPack::LinkHeader->new(
      '<http://example.com/TheBook/chapter2>' => (
          rel   => "previous",
          title => "previous chapter"
      )
  );

  # normal constructor, and <> around link are optional
  my $link = HTTP::Headers::ActionPack::LinkHeader->new(
      'http://example.com/TheBook/chapter2' => (
          rel   => "previous",
          title => "previous chapter"
      )
  );

=head1 DESCRIPTION

This is an object which represents an HTTP Link header. It
is most often used as a member of a L<HTTP::Headers::ActionPack::LinkList>
object.

=head1 METHODS

=over 4

=item C<href>

=item C<new_from_string ( $link_header_string )>

This will take an HTTP header Link string
and parse it into and object.

=item C<as_string>

This stringifies the link respecting the
parameter order.

NOTE: This will canonicalize the header such
that it will add a space between each semicolon
and quotes and unquotes all headers appropriately.

=back

=head1 AUTHOR

Stevan Little <stevan.little@iinteractive.com>

=head1 CONTRIBUTORS

=over 4

=item *

Andrew Nelson <anelson@cpan.org>

=item *

Dave Rolsky <autarch@urth.org>

=item *

Florian Ragwitz <rafl@debian.org>

=item *

Jesse Luehrs <doy@tozt.net>

=item *

Karen Etheridge <ether@cpan.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Infinity Interactive, Inc..

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut