1 package Catalyst::TraitFor::Request::REST;
3 use HTTP::Headers::Util qw(split_header_words);
4 use namespace::autoclean;
8 has [qw/ data accept_only /] => ( is => 'rw' );
10 has accepted_content_types => (
14 builder => '_build_accepted_content_types',
18 has preferred_content_type => (
22 builder => '_build_preferred_content_type',
26 sub _build_accepted_content_types {
31 # First, we use the content type in the HTTP Request. It wins all.
32 $types{ $self->content_type } = 3
33 if $self->content_type;
35 if ($self->method eq "GET" && $self->param('content-type')) {
36 $types{ $self->param('content-type') } = 2;
39 # Third, we parse the Accept header, and see if the client
40 # takes a format we understand.
42 # This is taken from chansen's Apache2::UploadProgress.
43 if ( $self->header('Accept') ) {
44 $self->accept_only(1) unless keys %types;
46 my $accept_header = $self->header('Accept');
49 foreach my $pair ( split_header_words($accept_header) ) {
50 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
51 next if $types{$type};
53 # cope with invalid (missing required q parameter) header like:
54 # application/json; charset="utf-8"
55 # http://tools.ietf.org/html/rfc2616#section-14.1
56 unless ( defined $pair->[2] && lc $pair->[2] eq 'q' ) {
60 unless ( defined $qvalue ) {
61 $qvalue = 1 - ( ++$counter / 1000 );
64 $types{$type} = sprintf( '%.3f', $qvalue );
68 [ sort { $types{$b} <=> $types{$a} } keys %types ];
71 sub _build_preferred_content_type { $_[0]->accepted_content_types->[0] }
77 return grep { $_ eq $type } @{ $self->accepted_content_types };
85 Catalyst::TraitFor::Request::REST - A role to apply to Catalyst::Request giving it REST methods and attributes.
89 if ( $c->request->accepts('application/json') ) {
93 my $types = $c->request->accepted_content_types();
97 This is a L<Moose::Role> applied to L<Catalyst::Request> that adds a few
98 methods to the request object to facilitate writing REST-y code.
99 Currently, these methods are all related to the content types accepted by
108 If the request went through the Deserializer action, this method will
109 return the deserialized data structure.
111 =item accepted_content_types
113 Returns an array reference of content types accepted by the
116 The list of types is created by looking at the following sources:
120 =item * Content-type header
122 If this exists, this will always be the first type in the list.
124 =item * content-type parameter
126 If the request is a GET request and there is a "content-type"
127 parameter in the query string, this will come before any types in the
130 =item * Accept header
132 This will be parsed and the types found will be ordered by the
133 relative quality specified for each type.
137 If a type appears in more than one of these places, it is ordered based on
138 where it is first found.
140 =item preferred_content_type
142 This returns the first content type found. It is shorthand for:
144 $request->accepted_content_types->[0]
148 Given a content type, this returns true if the type is accepted.
150 Note that this does not do any wildcard expansion of types.
156 See L<Catalyst::Action::REST> for authors.
160 You may distribute this code under the same terms as Perl itself.