v1.16
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / TraitFor / Request / REST.pm
1 package Catalyst::TraitFor::Request::REST;
2 use Moose::Role;
3 use HTTP::Headers::Util qw(split_header_words);
4 use namespace::autoclean;
5
6 # VERSION
7
8 has [qw/ data accept_only /] => ( is => 'rw' );
9
10 has accepted_content_types => (
11     is       => 'ro',
12     isa      => 'ArrayRef',
13     lazy     => 1,
14     builder  => '_build_accepted_content_types',
15     init_arg => undef,
16 );
17
18 has preferred_content_type => (
19     is       => 'ro',
20     isa      => 'Str',
21     lazy     => 1,
22     builder  => '_build_preferred_content_type',
23     init_arg => undef,
24 );
25
26 sub _build_accepted_content_types {
27     my $self = shift;
28
29     my %types;
30
31     # First, we use the content type in the HTTP Request.  It wins all.
32     $types{ $self->content_type } = 3
33         if $self->content_type;
34
35     if ($self->method eq "GET" && $self->param('content-type')) {
36         $types{ $self->param('content-type') } = 2;
37     }
38
39     # Third, we parse the Accept header, and see if the client
40     # takes a format we understand.
41     #
42     # This is taken from chansen's Apache2::UploadProgress.
43     if ( $self->header('Accept') ) {
44         $self->accept_only(1) unless keys %types;
45
46         my $accept_header = $self->header('Accept');
47         my $counter       = 0;
48
49         foreach my $pair ( split_header_words($accept_header) ) {
50             my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
51             next if $types{$type};
52
53             # cope with invalid (missing required q parameter) header like:
54             # application/json; charset="utf-8"
55             # http://tools.ietf.org/html/rfc2616#section-14.1
56             unless ( defined $pair->[2] && lc $pair->[2] eq 'q' ) {
57                 $qvalue = undef;
58             }
59
60             unless ( defined $qvalue ) {
61                 $qvalue = 1 - ( ++$counter / 1000 );
62             }
63
64             $types{$type} = sprintf( '%.3f', $qvalue );
65         }
66     }
67
68     [ sort { $types{$b} <=> $types{$a} } keys %types ];
69 }
70
71 sub _build_preferred_content_type { $_[0]->accepted_content_types->[0] }
72
73 sub accepts {
74     my $self = shift;
75     my $type = shift;
76
77     return grep { $_ eq $type } @{ $self->accepted_content_types };
78 }
79
80 1;
81 __END__
82
83 =head1 NAME
84
85 Catalyst::TraitFor::Request::REST - A role to apply to Catalyst::Request giving it REST methods and attributes.
86
87 =head1 SYNOPSIS
88
89      if ( $c->request->accepts('application/json') ) {
90          ...
91      }
92
93      my $types = $c->request->accepted_content_types();
94
95 =head1 DESCRIPTION
96
97 This is a L<Moose::Role> applied to L<Catalyst::Request> that adds a few
98 methods to the request object to facilitate writing REST-y code.
99 Currently, these methods are all related to the content types accepted by
100 the client.
101
102 =head1 METHODS
103
104 =over
105
106 =item data
107
108 If the request went through the Deserializer action, this method will
109 return the deserialized data structure.
110
111 =item accepted_content_types
112
113 Returns an array reference of content types accepted by the
114 client.
115
116 The list of types is created by looking at the following sources:
117
118 =over 8
119
120 =item * Content-type header
121
122 If this exists, this will always be the first type in the list.
123
124 =item * content-type parameter
125
126 If the request is a GET request and there is a "content-type"
127 parameter in the query string, this will come before any types in the
128 Accept header.
129
130 =item * Accept header
131
132 This will be parsed and the types found will be ordered by the
133 relative quality specified for each type.
134
135 =back
136
137 If a type appears in more than one of these places, it is ordered based on
138 where it is first found.
139
140 =item preferred_content_type
141
142 This returns the first content type found. It is shorthand for:
143
144   $request->accepted_content_types->[0]
145
146 =item accepts($type)
147
148 Given a content type, this returns true if the type is accepted.
149
150 Note that this does not do any wildcard expansion of types.
151
152 =back
153
154 =head1 AUTHORS
155
156 See L<Catalyst::Action::REST> for authors.
157
158 =head1 LICENSE
159
160 You may distribute this code under the same terms as Perl itself.
161
162 =cut
163