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