make a dev release because I'm cautious
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Request / REST.pm
CommitLineData
256c894f 1#
2# REST.pm
be3c588a 3# Created by: Adam Jacob, Marchex, <adam@hjksolutions.com>
256c894f 4# Created on: 10/13/2006 03:54:33 PM PDT
5#
6# $Id: $
7
8package Catalyst::Request::REST;
9
10use strict;
11use warnings;
12
fec6d454 13use base qw/Catalyst::Request Class::Accessor::Fast/;
9a76221e 14use HTTP::Headers::Util qw(split_header_words);
256c894f 15
5132f5e4 16sub _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);
22 } else {
23 die "$app has a custom request class $req_class, "
24 . "which is not a $class; see Catalyst::Request::REST";
25 }
26}
256c894f 27
9a76221e 28=head1 NAME
29
30Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
31
32=head1 SYNOPSIS
33
d6fb033c 34 if ( $c->request->accepts('application/json') ) {
9a76221e 35 ...
36 }
37
38 my $types = $c->request->accepted_content_types();
39
40=head1 DESCRIPTION
41
42This is a subclass of C<Catalyst::Request> that adds a few methods to
43the request object to faciliate writing REST-y code. Currently, these
44methods are all related to the content types accepted by the client.
45
5132f5e4 46Note that if you have a custom request class in your application, and it does
47not inherit from C<Catalyst::Request::REST>, your application will fail with an
48error indicating a conflict the first time it tries to use
49C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
50custom request class inherits from C<Catalyst::Request::REST>.
9a76221e 51
52=head1 METHODS
53
9a76221e 54If the request went through the Deserializer action, this method will
55returned the deserialized data structure.
56
57=cut
58
59__PACKAGE__->mk_accessors(qw(data accept_only));
60
fec6d454 61=over 4
62
9a76221e 63=item accepted_content_types
64
65Returns an array reference of content types accepted by the
66client.
67
68The list of types is created by looking at the following sources:
69
70=over 8
71
72=item * Content-type header
73
74If this exists, this will always be the first type in the list.
75
76=item * content-type parameter
77
78If the request is a GET request and there is a "content-type"
79parameter in the query string, this will come before any types in the
80Accept header.
81
82=item * Accept header
83
84This will be parsed and the types found will be ordered by the
85relative quality specified for each type.
86
87=back
88
89If a type appears in more than one of these places, it is ordered based on
90where it is first found.
91
92=cut
93
94sub accepted_content_types {
95 my $self = shift;
96
97 return $self->{content_types} if $self->{content_types};
256c894f 98
9a76221e 99 my %types;
100
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;
104
105 if ($self->method eq "GET" && $self->param('content-type')) {
9a76221e 106 $types{ $self->param('content-type') } = 2;
107 }
108
109 # Third, we parse the Accept header, and see if the client
110 # takes a format we understand.
111 #
112 # This is taken from chansen's Apache2::UploadProgress.
113 if ( $self->header('Accept') ) {
114 $self->accept_only(1) unless keys %types;
115
116 my $accept_header = $self->header('Accept');
117 my $counter = 0;
118
119 foreach my $pair ( split_header_words($accept_header) ) {
120 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
121 next if $types{$type};
122
123 unless ( defined $qvalue ) {
124 $qvalue = 1 - ( ++$counter / 1000 );
125 }
126
127 $types{$type} = sprintf( '%.3f', $qvalue );
128 }
129 }
130
131 return $self->{content_types} =
132 [ sort { $types{$b} <=> $types{$a} } keys %types ];
133}
134
135=item preferred_content_type
136
137This returns the first content type found. It is shorthand for:
138
139 $request->accepted_content_types->[0]
140
141=cut
142
143sub preferred_content_type { $_[0]->accepted_content_types->[0] }
144
145=item accepts($type)
146
147Given a content type, this returns true if the type is accepted.
148
149Note that this does not do any wildcard expansion of types.
150
151=cut
152
153sub accepts {
154 my $self = shift;
155 my $type = shift;
156
157 return grep { $_ eq $type } @{ $self->accepted_content_types };
158}
159
fec6d454 160=back
161
9a76221e 162=head1 AUTHOR
163
164Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and jrockway
165
fec6d454 166=head1 MAINTAINER
167
168J. Shirley <jshirley@cpan.org>
169
9a76221e 170=head1 LICENSE
171
172You may distribute this code under the same terms as Perl itself.
173
174=cut
175
1761;