1 package Catalyst::Request::REST;
5 use HTTP::Headers::Util qw(split_header_words);
7 use namespace::autoclean;
9 extends 'Catalyst::Request';
11 has [qw/ data accept_only /] => ( is => 'rw' );
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;
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);
25 die "$app has a custom request class $req_class, "
26 . "which is not a $class; see Catalyst::Request::REST";
30 sub accepted_content_types {
33 return $self->{content_types} if $self->{content_types};
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;
41 if ($self->method eq "GET" && $self->param('content-type')) {
42 $types{ $self->param('content-type') } = 2;
45 # Third, we parse the Accept header, and see if the client
46 # takes a format we understand.
48 # This is taken from chansen's Apache2::UploadProgress.
49 if ( $self->header('Accept') ) {
50 $self->accept_only(1) unless keys %types;
52 my $accept_header = $self->header('Accept');
55 foreach my $pair ( split_header_words($accept_header) ) {
56 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
57 next if $types{$type};
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' ) {
66 unless ( defined $qvalue ) {
67 $qvalue = 1 - ( ++$counter / 1000 );
70 $types{$type} = sprintf( '%.3f', $qvalue );
74 return $self->{content_types} =
75 [ sort { $types{$b} <=> $types{$a} } keys %types ];
78 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
84 return grep { $_ eq $type } @{ $self->accepted_content_types };
87 __PACKAGE__->meta->make_immutable;
92 Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
96 if ( $c->request->accepts('application/json') ) {
100 my $types = $c->request->accepted_content_types();
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.
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>.
120 If the request went through the Deserializer action, this method will
121 return the deserialized data structure.
123 =item accepted_content_types
125 Returns an array reference of content types accepted by the
128 The list of types is created by looking at the following sources:
132 =item * Content-type header
134 If this exists, this will always be the first type in the list.
136 =item * content-type parameter
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
142 =item * Accept header
144 This will be parsed and the types found will be ordered by the
145 relative quality specified for each type.
149 If a type appears in more than one of these places, it is ordered based on
150 where it is first found.
152 =item preferred_content_type
154 This returns the first content type found. It is shorthand for:
156 $request->accepted_content_types->[0]
160 Given a content type, this returns true if the type is accepted.
162 Note that this does not do any wildcard expansion of types.
168 See L<Catalyst::Action::REST> for authors.
172 You may distribute this code under the same terms as Perl itself.