Skip to content

Commit

Permalink
fixed small hash order bug in MIME type prioritization
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Dec 14, 2012
1 parent 75b17c6 commit 0c38458
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/Mojolicious/Types.pm
Expand Up @@ -37,11 +37,10 @@ sub detect {

# Extract and prioritize MIME types
my %types;
for my $type (split /,/, $accept // '') {
next unless $type =~ /^\s*([^,; ]+)(?:\s*\;\s*q=([\d.]+))?\s*$/i;
$types{lc $1} = $2 // 1;
}
my @types = sort { $types{$b} <=> $types{$a} } keys %types;
/^\s*([^,; ]+)(?:\s*\;\s*q=(\d+(?:\.\d+)?))?\s*$/i
and $types{lc $1} = $2 // 1
for split /,/, $accept // '';
my @types = sort { $types{$b} <=> $types{$a} } sort keys %types;
return [] if !$prioritize && @types > 1;

# Detect extensions from MIME types
Expand Down
2 changes: 2 additions & 0 deletions t/mojolicious/types.t
Expand Up @@ -68,6 +68,8 @@ is $t->type('html'), 'text/html;charset=UTF-8', 'right type';

# Prioritize
is_deeply $t->detect('text/plain', 1), ['txt'], 'right formats';
is_deeply $t->detect('text/plain,text/html', 1), ['htm', 'html', 'txt'],
'right formats';
is_deeply $t->detect('TEXT/HTML; q=0.8 ', 1), ['htm', 'html'], 'right formats';
is_deeply $t->detect('TEXT/HTML;Q=0.8,text/plain;Q=0.9', 1),
['txt', 'htm', 'html'], 'right formats';
Expand Down

0 comments on commit 0c38458

Please sign in to comment.