3 # Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
4 # Created on: 10/13/2006 03:54:33 PM PDT
8 package Catalyst::Request::REST;
13 use base qw/Catalyst::Request Class::Accessor::Fast/;
16 use HTTP::Headers::Util qw(split_header_words);
18 sub _insert_self_into {
19 my ($class, $app_class ) = @_;
20 my $app = Catalyst::Utils::class2appclass( $app_class ) || $app_class;
22 my $req_class = $app->request_class;
23 return if $req_class->isa($class);
24 if ($req_class eq 'Catalyst::Request') {
25 $app->request_class($class);
27 die "$app has a custom request class $req_class, "
28 . "which is not a $class; see Catalyst::Request::REST";
34 Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
38 if ( $c->request->accepts('application/json') ) {
42 my $types = $c->request->accepted_content_types();
46 This is a subclass of C<Catalyst::Request> that adds a few methods to
47 the request object to faciliate writing REST-y code. Currently, these
48 methods are all related to the content types accepted by the client.
50 Note that if you have a custom request class in your application, and it does
51 not inherit from C<Catalyst::Request::REST>, your application will fail with an
52 error indicating a conflict the first time it tries to use
53 C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
54 custom request class inherits from C<Catalyst::Request::REST>.
58 If the request went through the Deserializer action, this method will
59 returned the deserialized data structure.
63 __PACKAGE__->mk_accessors(qw(data accept_only));
67 =item accepted_content_types
69 Returns an array reference of content types accepted by the
72 The list of types is created by looking at the following sources:
76 =item * Content-type header
78 If this exists, this will always be the first type in the list.
80 =item * content-type parameter
82 If the request is a GET request and there is a "content-type"
83 parameter in the query string, this will come before any types in the
88 This will be parsed and the types found will be ordered by the
89 relative quality specified for each type.
93 If a type appears in more than one of these places, it is ordered based on
94 where it is first found.
98 sub accepted_content_types {
101 return $self->{content_types} if $self->{content_types};
105 # First, we use the content type in the HTTP Request. It wins all.
106 $types{ $self->content_type } = 3
107 if $self->content_type;
109 if ($self->method eq "GET" && $self->param('content-type')) {
110 $types{ $self->param('content-type') } = 2;
113 # Third, we parse the Accept header, and see if the client
114 # takes a format we understand.
116 # This is taken from chansen's Apache2::UploadProgress.
117 if ( $self->header('Accept') ) {
118 $self->accept_only(1) unless keys %types;
120 my $accept_header = $self->header('Accept');
123 foreach my $pair ( split_header_words($accept_header) ) {
124 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
125 next if $types{$type};
127 unless ( defined $qvalue ) {
128 $qvalue = 1 - ( ++$counter / 1000 );
131 $types{$type} = sprintf( '%.3f', $qvalue );
135 return $self->{content_types} =
136 [ sort { $types{$b} <=> $types{$a} } keys %types ];
139 =item preferred_content_type
141 This returns the first content type found. It is shorthand for:
143 $request->accepted_content_types->[0]
147 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
151 Given a content type, this returns true if the type is accepted.
153 Note that this does not do any wildcard expansion of types.
161 return grep { $_ eq $type } @{ $self->accepted_content_types };
168 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
172 J. Shirley <jshirley@cpan.org>
176 You may distribute this code under the same terms as Perl itself.