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