1 package Catalyst::Request::REST;
5 use Scalar::Util qw/blessed/;
7 use base qw/Catalyst::Request Class::Accessor::Fast/;
10 use HTTP::Headers::Util qw(split_header_words);
12 sub _insert_self_into {
13 my ($class, $app_class ) = @_;
14 # the fallback to $app_class is for the (rare and deprecated) case when
15 # people are defining actions in MyApp.pm instead of in a controller.
16 my $app = (blessed($app_class) && $app_class->can('_application'))
17 ? $app_class->_application : Catalyst::Utils::class2appclass( $app_class ) || $app_class;
19 my $req_class = $app->request_class;
20 return if $req_class->isa($class);
21 if ($req_class eq 'Catalyst::Request') {
22 $app->request_class($class);
24 die "$app has a custom request class $req_class, "
25 . "which is not a $class; see Catalyst::Request::REST";
29 __PACKAGE__->mk_accessors(qw(data accept_only));
31 sub accepted_content_types {
34 return $self->{content_types} if $self->{content_types};
38 # First, we use the content type in the HTTP Request. It wins all.
39 $types{ $self->content_type } = 3
40 if $self->content_type;
42 if ($self->method eq "GET" && $self->param('content-type')) {
43 $types{ $self->param('content-type') } = 2;
46 # Third, we parse the Accept header, and see if the client
47 # takes a format we understand.
49 # This is taken from chansen's Apache2::UploadProgress.
50 if ( $self->header('Accept') ) {
51 $self->accept_only(1) unless keys %types;
53 my $accept_header = $self->header('Accept');
56 foreach my $pair ( split_header_words($accept_header) ) {
57 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
58 next if $types{$type};
60 # cope with invalid (missing required q parameter) header like:
61 # application/json; charset="utf-8"
62 # http://tools.ietf.org/html/rfc2616#section-14.1
63 unless ( defined $pair->[2] && lc $pair->[2] eq 'q' ) {
67 unless ( defined $qvalue ) {
68 $qvalue = 1 - ( ++$counter / 1000 );
71 $types{$type} = sprintf( '%.3f', $qvalue );
75 return $self->{content_types} =
76 [ sort { $types{$b} <=> $types{$a} } keys %types ];
79 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
85 return grep { $_ eq $type } @{ $self->accepted_content_types };
90 Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
94 if ( $c->request->accepts('application/json') ) {
98 my $types = $c->request->accepted_content_types();
102 This is a subclass of C<Catalyst::Request> that adds a few methods to
103 the request object to faciliate writing REST-y code. Currently, these
104 methods are all related to the content types accepted by the client.
106 Note that if you have a custom request class in your application, and it does
107 not inherit from C<Catalyst::Request::REST>, your application will fail with an
108 error indicating a conflict the first time it tries to use
109 C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
110 custom request class inherits from C<Catalyst::Request::REST>.
118 If the request went through the Deserializer action, this method will
119 return the deserialized data structure.
121 =item accepted_content_types
123 Returns an array reference of content types accepted by the
126 The list of types is created by looking at the following sources:
130 =item * Content-type header
132 If this exists, this will always be the first type in the list.
134 =item * content-type parameter
136 If the request is a GET request and there is a "content-type"
137 parameter in the query string, this will come before any types in the
140 =item * Accept header
142 This will be parsed and the types found will be ordered by the
143 relative quality specified for each type.
147 If a type appears in more than one of these places, it is ordered based on
148 where it is first found.
150 =item preferred_content_type
152 This returns the first content type found. It is shorthand for:
154 $request->accepted_content_types->[0]
158 Given a content type, this returns true if the type is accepted.
160 Note that this does not do any wildcard expansion of types.
166 See L<Catalyst::Action::REST> for authors.
170 You may distribute this code under the same terms as Perl itself.