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