1 package Catalyst::TraitFor::Request::REST;
4 use HTTP::Headers::Util qw(split_header_words);
5 use namespace::autoclean;
7 has [qw/ data accept_only /] => ( is => 'rw' );
9 has accepted_content_types => (
13 builder => '_build_accepted_content_types',
17 has preferred_content_type => (
21 builder => '_build_preferred_content_type',
25 sub _build_accepted_content_types {
30 # First, we use the content type in the HTTP Request. It wins all.
31 $types{ $self->content_type } = 3
32 if $self->content_type;
34 if ($self->method eq "GET" && $self->param('content-type')) {
35 $types{ $self->param('content-type') } = 2;
38 # Third, we parse the Accept header, and see if the client
39 # takes a format we understand.
41 # This is taken from chansen's Apache2::UploadProgress.
42 if ( $self->header('Accept') ) {
43 $self->accept_only(1) unless keys %types;
45 my $accept_header = $self->header('Accept');
48 foreach my $pair ( split_header_words($accept_header) ) {
49 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
50 next if $types{$type};
52 # cope with invalid (missing required q parameter) header like:
53 # application/json; charset="utf-8"
54 # http://tools.ietf.org/html/rfc2616#section-14.1
55 unless ( defined $pair->[2] && lc $pair->[2] eq 'q' ) {
59 unless ( defined $qvalue ) {
60 $qvalue = 1 - ( ++$counter / 1000 );
63 $types{$type} = sprintf( '%.3f', $qvalue );
67 [ sort { $types{$b} <=> $types{$a} } keys %types ];
70 sub _build_preferred_content_type { $_[0]->accepted_content_types->[0] }
76 return grep { $_ eq $type } @{ $self->accepted_content_types };
84 Catalyst::TraitFor::Request::REST - A role to apply to Catalyst::Request giving it REST methods and attributes.
88 if ( $c->request->accepts('application/json') ) {
92 my $types = $c->request->accepted_content_types();
96 This is a L<Moose::Role> applied to L<Catalyst::Request> that adds a few
97 methods to the request object to facilitate writing REST-y code.
98 Currently, these methods are all related to the content types accepted by
107 If the request went through the Deserializer action, this method will
108 return the deserialized data structure.
110 =item accepted_content_types
112 Returns an array reference of content types accepted by the
115 The list of types is created by looking at the following sources:
119 =item * Content-type header
121 If this exists, this will always be the first type in the list.
123 =item * content-type parameter
125 If the request is a GET request and there is a "content-type"
126 parameter in the query string, this will come before any types in the
129 =item * Accept header
131 This will be parsed and the types found will be ordered by the
132 relative quality specified for each type.
136 If a type appears in more than one of these places, it is ordered based on
137 where it is first found.
139 =item preferred_content_type
141 This returns the first content type found. It is shorthand for:
143 $request->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.
155 See L<Catalyst::Action::REST> for authors.
159 You may distribute this code under the same terms as Perl itself.