1 package Catalyst::RequestRole::REST;
2 # ABSTRACT: A REST-y role for Catalyst::Request
6 use HTTP::Headers::Util qw(split_header_words);
7 use namespace::clean -except => 'meta';
12 # writer => '_set_accept_only', FIXME fails for me if I use this
16 has accepted_content_types => (
23 has _accepted_content_types_hash => (
30 sub _build_accepted_content_types {
34 # First, we use the content type in the HTTP Request. It wins all.
35 $types{ $self->content_type } = 3 if $self->content_type;
37 if ($self->method eq "GET" &&
38 (my $ct = $self->params->{'content-type'})) {
42 # Third, we parse the Accept header, and see if the client
43 # takes a format we understand.
45 # This is taken from chansen's Apache2::UploadProgress.
46 if ( $self->header('Accept') ) {
47 $self->accept_only(1) unless keys %types; # FIXME fails if _set_accept_only
49 my $accept_header = $self->header('Accept');
52 foreach my $pair ( split_header_words($accept_header) ) {
53 my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
54 next if $types{$type};
56 unless ( defined $qvalue ) {
57 $qvalue = 1 - ( ++$counter / 1000 );
60 $types{$type} = sprintf( '%.3f', $qvalue );
64 return [ sort { $types{$b} <=> $types{$a} } keys %types ];
67 sub preferred_content_type { $_[0]->accepted_content_types->[0] }
69 sub _build__accepted_content_types_hash {
70 return { map {; $_ => 1 } @{ $_[0]->accepted_content_types } };
73 sub accepts { $_[0]->_accepted_content_types_hash->{$_[1]} }
81 if ( $c->request->accepts('application/json') ) {
85 my $types = $c->request->accepted_content_types();
89 This is a subclass of C<Catalyst::Request> that adds a few methods to
90 the request object to faciliate writing REST-y code. Currently, these
91 methods are all related to the content types accepted by the client.
93 Note that if you have a custom request class in your application, and it does
94 not inherit from C<Catalyst::Request::REST>, your application will fail with an
95 error indicating a conflict the first time it tries to use
96 C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
97 custom request class inherits from C<Catalyst::Request::REST>.
99 =method accepted_content_types
101 Returns an array reference of content types accepted by the
104 The list of types is created by looking at the following sources:
108 =item * Content-type header
110 If this exists, this will always be the first type in the list.
112 =item * content-type parameter
114 If the request is a GET request and there is a "content-type"
115 parameter in the query string, this will come before any types in the
118 =item * Accept header
120 This will be parsed and the types found will be ordered by the
121 relative quality specified for each type.
125 If a type appears in more than one of these places, it is ordered based on
126 where it is first found.
128 =method preferred_content_type
130 This returns the first content type found. It is shorthand for:
132 $request->accepted_content_types->[0]
136 Given a content type, this returns true if the type is accepted.
138 Note that this does not do any wildcard expansion of types.