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