--- lib/Catalyst/Request/REST.pm~ 2007-03-09 21:44:54.000000000 +0100
+++ lib/Catalyst/Request/REST.pm 2007-10-22 15:59:24.000000000 +0200
@@ -80,15 +80,22 @@
return $self->{content_types} if $self->{content_types};
- my %types;
+ my %requested_types;
+ my %provided_types = ( # FIXME: Make configurable/reflect what's installed
+ 'text/html' => 1,
+ 'application/xhtml+xml' => 0.9,
+ 'text/xml' => 0.8,
+ 'application/xml' => 0.79,
+ 'image/png' => 0.1,
+ );
# First, we use the content type in the HTTP Request. It wins all.
- $types{ $self->content_type } = 3
+ $requested_types{ $self->content_type } = 3
if $self->content_type;
if ($self->method eq "GET" && $self->param('content-type')) {
- $types{ $self->param('content-type') } = 2;
+ $requested_types{ $self->param('content-type') } = 2;
}
# Third, we parse the Accept header, and see if the client
@@ -96,25 +103,25 @@
#
# This is taken from chansen's Apache2::UploadProgress.
if ( $self->header('Accept') ) {
- $self->accept_only(1) unless keys %types;
+ $self->accept_only(1) unless keys %requested_types;
my $accept_header = $self->header('Accept');
- my $counter = 0;
foreach my $pair ( split_header_words($accept_header) ) {
my ( $type, $qvalue ) = @{$pair}[ 0, 3 ];
- next if $types{$type};
+ next if $requested_types{$type};
- unless ( defined $qvalue ) {
- $qvalue = 1 - ( ++$counter / 1000 );
- }
+ $qvalue = 1 unless defined $qvalue;
- $types{$type} = sprintf( '%.3f', $qvalue );
+ $requested_types{$type} = sprintf( '%.1f', $qvalue );
}
}
return $self->{content_types} =
- [ sort { $types{$b} <=> $types{$a} } keys %types ];
+ [ sort {
+ $requested_types{$b} <=> $requested_types{$a} ||
+ $provided_types{$b} <=> $provided_types{$a}
+ } keys %requested_types ];
}
=item preferred_content_type
--- t/catalyst-request-rest.t 2007-03-09 21:28:30.000000000 +0100
+++ t/catalyst-request-rest.t 2007-10-22 16:06:50.000000000 +0200
@@ -66,15 +66,18 @@
);
is_deeply( $request->accepted_content_types,
- [ qw( text/xml application/xml application/xhtml+xml
+ [ qw(
+ application/xhtml+xml
+ text/xml
+ application/xml
image/png
text/html
text/plain
*/*
) ],
'accept header is parsed properly' );
- is( $request->preferred_content_type, 'text/xml',
- 'preferred content type is text/xml' );
+ is( $request->preferred_content_type, 'application/xhtml+xml',
+ 'preferred content type is application/xhtml+xml' );
ok( $request->accept_only, 'accept_only is true' );
ok( $request->accepts('text/html'), 'accepts text/html' );
ok( $request->accepts('image/png'), 'accepts image/png' );
@@ -95,8 +98,11 @@
);
is_deeply( $request->accepted_content_types,
- [ qw( text/x-json
- text/xml application/xml application/xhtml+xml
+ [ qw(
+ text/x-json
+ application/xhtml+xml
+ text/xml
+ application/xml
image/png
text/html
text/plain