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/;
14 use HTTP::Headers::Util qw(split_header_words);
16 sub _insert_self_into {
17 my ($class, $app) = @_;
18 my $req_class = $app->request_class;
19 return if $req_class->isa($class);
20 if ($req_class eq 'Catalyst::Request') {
21 $app->request_class($class);
23 die "$app has a custom request class $req_class, "
24 . "which is not a $class; see Catalyst::Request::REST";
30 Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
34 if ( $c->request->accepts('application/json') ) {
38 my $types = $c->request->accepted_content_types();
42 This is a subclass of C<Catalyst::Request> that adds a few methods to
43 the request object to faciliate writing REST-y code. Currently, these
44 methods are all related to the content types accepted by the client.
46 Note that if you have a custom request class in your application, and it does
47 not inherit from C<Catalyst::Request::REST>, your application will fail with an
48 error indicating a conflict the first time it tries to use
49 C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
50 custom request class inherits from C<Catalyst::Request::REST>.
54 If the request went through the Deserializer action, this method will
55 returned the deserialized data structure.
59 __PACKAGE__->mk_accessors(qw(data accept_only));
63 =item accepted_content_types
65 Returns an array reference of content types accepted by the
68 The list of types is created by looking at the following sources:
72 =item * Content-type header
74 If this exists, this will always be the first type in the list.
76 =item * content-type parameter
78 If the request is a GET request and there is a "content-type"
79 parameter in the query string, this will come before any types in the
84 This will be parsed and the types found will be ordered by the
85 relative quality specified for each type.
89 If a type appears in more than one of these places, it is ordered based on
90 where it is first found.
94 sub accepted_content_types {
97 return $self->{content_types} if $self->{content_types};
101 # First, we use the content type in the HTTP Request. It wins all.
102 $types{ $self->content_type } = 3
103 if $self->content_type;
105 if ($self->method eq "GET" && $self->param('content-type')) {
106 $types{ $self->param('content-type') } = 2;
109 # Third, we parse the Accept header, and see if the client
110 # takes a format we understand.
112 # This is taken from chansen's Apache2::UploadProgress.
113 if ( $self->header('Accept') ) {
114 $self->accept_only(1) unless keys %types;
116 my $accept_header = $self->header('Accept');
119 foreach my $pair ( split_header_words($accept_header) ) {
120 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
121 next if $types{$type};
123 unless ( defined $qvalue ) {
124 $qvalue = 1 - ( ++$counter / 1000 );
127 $types{$type} = sprintf( '%.3f', $qvalue );
131 return $self->{content_types} =
132 [ sort { $types{$b} <=> $types{$a} } keys %types ];
135 =item preferred_content_type
137 This returns the first content type found. It is shorthand for:
139 $request->accepted_content_types->[0]
143 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
147 Given a content type, this returns true if the type is accepted.
149 Note that this does not do any wildcard expansion of types.
157 return grep { $_ eq $type } @{ $self->accepted_content_types };
164 Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
168 J. Shirley <jshirley@cpan.org>
172 You may distribute this code under the same terms as Perl itself.