Skip to content

Commit

Permalink
changed types in Mojolicious::Types to consistently use arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 12, 2013
1 parent 0235d99 commit 33adef3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -9,6 +9,7 @@
- Added close_gracefully method to Mojo::IOLoop::Stream.
- Changed Mojolicious default secret to the application moniker to make it
slightly more secure.
- Changed types in Mojolicious::Types to consistently use arrays.
- Removed callback support from body method in Mojo::Message.
- Removed Mojolicious::Plugin::PoweredBy and
Mojolicious::Plugin::RequestTimer.
Expand Down
65 changes: 32 additions & 33 deletions lib/Mojolicious/Types.pm
Expand Up @@ -3,32 +3,32 @@ use Mojo::Base -base;

has types => sub {
{
appcache => 'text/cache-manifest',
atom => 'application/atom+xml',
bin => 'application/octet-stream',
css => 'text/css',
gif => 'image/gif',
gz => 'application/x-gzip',
htm => 'text/html',
html => 'text/html;charset=UTF-8',
ico => 'image/x-icon',
jpeg => 'image/jpeg',
jpg => 'image/jpeg',
js => 'application/javascript',
json => 'application/json',
mp3 => 'audio/mpeg',
mp4 => 'video/mp4',
ogg => 'audio/ogg',
ogv => 'video/ogg',
pdf => 'application/pdf',
png => 'image/png',
rss => 'application/rss+xml',
svg => 'image/svg+xml',
txt => 'text/plain',
webm => 'video/webm',
woff => 'application/font-woff',
appcache => ['text/cache-manifest'],
atom => ['application/atom+xml'],
bin => ['application/octet-stream'],
css => ['text/css'],
gif => ['image/gif'],
gz => ['application/x-gzip'],
htm => ['text/html'],
html => ['text/html;charset=UTF-8'],
ico => ['image/x-icon'],
jpeg => ['image/jpeg'],
jpg => ['image/jpeg'],
js => ['application/javascript'],
json => ['application/json'],
mp3 => ['audio/mpeg'],
mp4 => ['video/mp4'],
ogg => ['audio/ogg'],
ogv => ['video/ogg'],
pdf => ['application/pdf'],
png => ['image/png'],
rss => ['application/rss+xml'],
svg => ['image/svg+xml'],
txt => ['text/plain'],
webm => ['video/webm'],
woff => ['application/font-woff'],
xml => ['application/xml', 'text/xml'],
zip => 'application/zip'
zip => ['application/zip']
};
};

Expand All @@ -40,24 +40,23 @@ sub detect {
/^\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;
my @detected = sort { $types{$b} <=> $types{$a} } sort keys %types;
return [] if !$prioritize && @detected > 1;

# Detect extensions from MIME types
my %reverse;
my $types = $self->types;
for my $ext (sort keys %$types) {
my @types = ref $types->{$ext} ? @{$types->{$ext}} : ($types->{$ext});
my @types = @{$types->{$ext}};
push @{$reverse{$_}}, $ext for map { s/\;.*$//; lc $_ } @types;
}
return [map { @{$reverse{$_} // []} } @types];
return [map { @{$reverse{$_} // []} } @detected];
}

sub type {
my ($self, $ext, $type) = @_;
my $types = $self->types;
return ref $types->{$ext} ? $types->{$ext}[0] : $types->{$ext} unless $type;
$types->{$ext} = $type;
return $self->types->{$ext}[0] unless $type;
$self->types->{$ext} = ref $type ? $type : [$type];
return $self;
}

Expand Down Expand Up @@ -115,7 +114,7 @@ L<Mojolicious::Types> implements the following attributes.
=head2 types
my $map = $types->types;
$types = $types->types({png => 'image/png'});
$types = $types->types({png => ['image/png']});
List of MIME types.
Expand Down
4 changes: 2 additions & 2 deletions t/mojolicious/types.t
Expand Up @@ -55,8 +55,8 @@ $t->type(json => ['application/json', 'text/x-json']);
is $t->types->{json}[0], 'application/json', 'right type';
is $t->types->{json}[1], 'text/x-json', 'right type';
ok !$t->types->{json}[2], 'no type';
is $t->types->{htm}, 'text/html', 'right type';
is $t->types->{html}, 'text/html;charset=UTF-8', 'right type';
is_deeply $t->types->{htm}, ['text/html'], 'right type';
is_deeply $t->types->{html}, ['text/html;charset=UTF-8'], 'right type';
is_deeply $t->detect('application/json'), ['json'], 'right formats';
is_deeply $t->detect('text/x-json'), ['json'], 'right formats';
is_deeply $t->detect('TEXT/X-JSON;q=0.1'), ['json'], 'right formats';
Expand Down

0 comments on commit 33adef3

Please sign in to comment.