Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
removed support for multiple MIME types again
  • Loading branch information
kraih committed Apr 4, 2012
1 parent 91d684a commit df0101a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 77 deletions.
2 changes: 0 additions & 2 deletions Changes
@@ -1,8 +1,6 @@
This file documents the revision history for Perl extension Mojolicious.

2.75 2012-04-05
- Added multiple MIME type and quality support for Ajax content
negotiation with respond_to.
- Improved documentation.

2.74 2012-04-04
Expand Down
12 changes: 5 additions & 7 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -386,10 +386,9 @@ sub respond_to {
my $args = ref $_[0] ? $_[0] : {@_};

# Detect formats
my $app = $self->app;
my @formats =
@{$app->types->detect($self->req->headers->accept, $self->req->is_xhr)};
my $stash = $self->stash;
my $app = $self->app;
my @formats = @{$app->types->detect($self->req->headers->accept)};
my $stash = $self->stash;
unless (@formats) {
my $format = $stash->{format} || $self->req->param('format');
push @formats, $format ? $format : $app->renderer->default_format;
Expand Down Expand Up @@ -880,9 +879,8 @@ L<Mojo::Message::Response> object.
Automatically select best possible representation for resource from C<Accept>
request header, C<format> stash value or C<format> GET/POST parameter,
defaults to rendering an empty C<204> response. Multiple MIME types are only
allowed for Ajax requests, which are determined by the presence of a
C<X-Requested-With> request header with the value C<XMLHttpRequest>.
defaults to rendering an empty C<204> response. Unspecific C<Accept> request
headers that contain more than one MIME type are ignored.
$c->respond_to(
json => sub { $c->render_json({just => 'works'}) },
Expand Down
45 changes: 12 additions & 33 deletions lib/Mojolicious/Types.pm
Expand Up @@ -33,23 +33,20 @@ has types => sub {

# "Magic. Got it."
sub detect {
my ($self, $accept, $all) = @_;
$accept ||= '';

# First MIME type only
return [] unless $all || $accept =~ /^[^,]+?(?:\;[^,]*)*$/;
my ($self, $accept) = @_;

# Detect extensions from MIME type
my %results;
my $reverse = $self->_reverse;
for my $type (split /,/, $accept) {
next unless $type =~ /^\s*([^;]+?)\s*(?:\;.*)*?$/;
next unless my $exts = $reverse->{lc $1};
my $quality = $type =~ /\;\s*q=(\d+(?:\.\d+)?)/ ? $1 : 1;
$results{$_} = $quality for @$exts;
return [] unless (($accept || '') =~ /^([^,]+?)(?:\;[^,]*)*$/);
my $type = lc $1;
my @exts;
my $types = $self->types;
for my $ext (sort keys %$types) {
my $try = lc $types->{$ext};
$try =~ s/\;.*$//;
push @exts, $ext if $type eq $try;
}

return [sort { $results{$b} <=> $results{$a} } sort keys %results];
return \@exts;
}

sub type {
Expand All @@ -59,22 +56,6 @@ sub type {
return $self;
}

sub _reverse {
my $self = shift;

# Index types
unless ($self->{reverse}) {
my $types = $self->types;
for my $ext (keys %$types) {
my $type = lc $types->{$ext};
$type =~ s/\;.*$//;
push @{$self->{reverse}->{$type}}, $ext;
}
}

return $self->{reverse};
}

1;
__END__
Expand Down Expand Up @@ -110,11 +91,9 @@ the following ones.
=head2 C<detect>
my $first = $types->detect('application/json;q=9');
my $all = $types->detect('application/json;q=9,text/plain', 1);
my $ext = $types->detect('application/json;q=9');
Detect file extensions from C<Accept> header value, detection of more than
one MIME type is disabled by default.
Detect file extensions from C<Accept> header value.
=head2 C<type>
Expand Down
19 changes: 1 addition & 18 deletions t/mojolicious/restful_lite_app.t
Expand Up @@ -6,7 +6,7 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 345;
use Test::More tests => 333;

# "Woohoo, time to go clubbin'! Baby seals here I come!"
use Mojolicious::Lite;
Expand Down Expand Up @@ -426,20 +426,3 @@ $t->get_ok('/rest.html' => {Accept => $chrome})->status_is(200)
$t->get_ok('/rest?format=html' => {Accept => $chrome})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->text_is('html > body', 'works');

# GET /rest (not Ajax)
my $ajax = 'text/html,text/xml;q=1.1';
$t->get_ok('/rest' => {Accept => $ajax})->status_is(200)
->content_type_is('text/html;charset=UTF-8')
->content_is('<html><body>works');

# GET /rest (Ajax)
$t->get_ok(
'/rest' => {Accept => $ajax, 'X-Requested-With' => 'XMLHttpRequest'})
->status_is(200)->content_type_is('text/xml')
->content_is('<just>works</just>');

# GET /rest (Ajax with html format and query)
$t->get_ok('/rest.html?format=html' =>
{Accept => $ajax, 'X-Requested-With' => 'XMLHttpRequest'})->status_is(200)
->content_type_is('text/xml')->content_is('<just>works</just>');
20 changes: 3 additions & 17 deletions t/mojolicious/types.t
@@ -1,6 +1,6 @@
use Mojo::Base -strict;

use Test::More tests => 34;
use Test::More tests => 30;

# "Your mistletoe is no match for my *tow* missile."
use Mojolicious::Types;
Expand Down Expand Up @@ -35,24 +35,10 @@ is_deeply $t->detect('text/xml'), ['xml', 'xsl'], 'right formats';
is_deeply $t->detect('application/zip'), ['zip'], 'right format';

# Detect special cases
is_deeply $t->detect('Text/Xml'), ['xml', 'xsl'], 'right formats';
is_deeply $t->detect('TEXT/XML'), ['xml', 'xsl'], 'right formats';
is_deeply $t->detect('text/html;q=0.9'), ['htm', 'html'], 'right formats';
is_deeply $t->detect('text/html,*/*'), [], 'no formats';
is_deeply $t->detect('text/html;q=0.9,*/*'), [], 'no formats';
is_deeply $t->detect('text/html,*/*;q=0.9'), [], 'no formats';
is_deeply $t->detect('text/html;q=0.8,*/*;q=0.9'), [], 'no formats';

# Multiple MIME types
is_deeply $t->detect('text/html;q=0.9,text/plain', 1), ['txt', 'htm', 'html'],
'right formats';
is_deeply $t->detect('Text/Html;q=0.9,Text/Plain', 1), ['txt', 'htm', 'html'],
'right formats';
is_deeply $t->detect('TEXT/HTML;v=9;q=0.9,TEXT/PLAIN', 1),
['txt', 'htm', 'html'],
'right formats';
is_deeply $t->detect('application/json, text/javascript, */*; q=0.01', 1),
['json'], 'right formats';
is_deeply $t->detect('application/json , image/svg+xml ; q=10, image/png', 1),
['svg', 'json', 'png'], 'right formats';
is_deeply $t->detect('text/html;quality=0.9,text/plain', 1),
['htm', 'html', 'txt'],
'right formats';

0 comments on commit df0101a

Please sign in to comment.