3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/13/2006 03:54:33 PM PDT
7 package Catalyst::Request::REST;
9 extends qw/Catalyst::Request/;
10 with qw/Catalyst::RequestRole::REST Catalyst::RequestRole::Deserialize/;
14 sub _insert_self_into {
15 my ($class, $app_class ) = @_;
16 # the fallback to $app_class is for the (rare and deprecated) case when
17 # people are defining actions in MyApp.pm instead of in a controller.
18 my $app = (blessed($app_class) && $app_class->can('_application'))
19 ? $app_class->_application : Catalyst::Utils::class2appclass( $app_class ) || $app_class;
21 my $req_class = $app->request_class;
22 return if $req_class->isa($class);
23 if ($req_class eq 'Catalyst::Request') {
24 $app->request_class($class);
26 die "$app has a custom request class $req_class, "
27 . "which is not a $class; see Catalyst::Request::REST";
33 Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
37 if ( $c->request->accepts('application/json') ) {
41 my $types = $c->request->accepted_content_types();
45 This is a subclass of C<Catalyst::Request> that adds a few methods to
46 the request object to faciliate writing REST-y code. Currently, these
47 methods are all related to the content types accepted by the client.
49 Note that if you have a custom request class in your application, and it does
50 not inherit from C<Catalyst::Request::REST>, your application will fail with an
51 error indicating a conflict the first time it tries to use
52 C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
53 custom request class inherits from C<Catalyst::Request::REST>.
57 If the request went through the Deserializer action, this method will
58 returned the deserialized data structure.
62 __PACKAGE__->mk_accessors(qw(data accept_only));
66 =item accepted_content_types
68 Returns an array reference of content types accepted by the
71 The list of types is created by looking at the following sources:
75 =item * Content-type header
77 If this exists, this will always be the first type in the list.
79 =item * content-type parameter
81 If the request is a GET request and there is a "content-type"
82 parameter in the query string, this will come before any types in the
87 This will be parsed and the types found will be ordered by the
88 relative quality specified for each type.
92 If a type appears in more than one of these places, it is ordered based on
93 where it is first found.
97 sub accepted_content_types {
100 return $self->{content_types} if $self->{content_types};
104 # First, we use the content type in the HTTP Request. It wins all.
105 $types{ $self->content_type } = 3
106 if $self->content_type;
108 if ($self->method eq "GET" && $self->param('content-type')) {
109 $types{ $self->param('content-type') } = 2;
112 # Third, we parse the Accept header, and see if the client
113 # takes a format we understand.
115 # This is taken from chansen's Apache2::UploadProgress.
116 if ( $self->header('Accept') ) {
117 $self->accept_only(1) unless keys %types;
119 my $accept_header = $self->header('Accept');
122 foreach my $pair ( split_header_words($accept_header) ) {
123 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
124 next if $types{$type};
126 unless ( defined $qvalue ) {
127 $qvalue = 1 - ( ++$counter / 1000 );
130 $types{$type} = sprintf( '%.3f', $qvalue );
134 return $self->{content_types} =
135 [ sort { $types{$b} <=> $types{$a} } keys %types ];
138 =item preferred_content_type
140 This returns the first content type found. It is shorthand for:
142 $request->accepted_content_types->[0]
146 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
150 Given a content type, this returns true if the type is accepted.
152 Note that this does not do any wildcard expansion of types.
160 return grep { $_ eq $type } @{ $self->accepted_content_types };
167 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
171 J. Shirley <jshirley@cpan.org>
175 You may distribute this code under the same terms as Perl itself.