Use more sensible module name in Synopsis.
[catagits/Catalyst-View-ContentNegotiation-XHTML.git] / lib / Catalyst / View / ContentNegotiation / XHTML.pm
index e968ccc..84ef84f 100644 (file)
@@ -8,7 +8,7 @@ use HTTP::Negotiate qw/choose/;
 use namespace::clean -except => 'meta';
 
 # Remember to bump $VERSION in View::TT::XHTML also.
-our $VERSION = '1.100';
+our $VERSION = '1.102';
 
 requires 'process';
 
@@ -28,24 +28,26 @@ sub _build_variants {
 
 after process => sub {
     my ($self, $c) = @_;
-    if ($c->request->header('Accept') && $c->response->headers->{'content-type'} =~ m|text/html|) {
-        $self->pragmatic_accept($c);
-        my $var = choose($self->variants, $c->request->headers);
-        if ($var eq 'xhtml') {
+    if ( my $accept = $self->pragmatic_accept($c) and $c->response->headers->{'content-type'} =~ m|text/html|) {
+        my $headers = $c->request->headers->clone;
+        $headers->header('Accept' => $accept);
+        if ( choose($self->variants, $headers) eq 'xhtml') {
             $c->response->headers->{'content-type'} =~ s|text/html|application/xhtml+xml|;
         }
     }
+    $c->response->headers->push_header(Vary => 'Accept');
 };
 
 sub pragmatic_accept {
     my ($self, $c) = @_;
-    my $accept = $c->request->header('Accept');
+    my $accept = $c->request->header('Accept') or return;
     if ($accept =~ m|text/html|) {
         $accept =~ s!\*/\*\s*([,]+|$)!*/*;q=0.5$1!;
-    } else {
+    } 
+    else {
         $accept =~ s!\*/\*\s*([,]+|$)!text/html,*/*;q=0.5$1!;
     }
-    $c->request->header('Accept' => $accept);
+    return $accept;
 }
 
 1;
@@ -54,13 +56,12 @@ __END__
 
 =head1 NAME
 
-Catalyst::View::ContentNegotiation::XHTML - A Moose Role to apply to
-Catalyst views adjusts the response Content-Type header to 
-application/xhtml+xml content if the browser accepts it.
+Catalyst::View::ContentNegotiation::XHTML - Adjusts the response Content-Type
+header to application/xhtml+xml if the browser accepts it.
 
 =head1 SYNOPSIS
 
-    package Catalyst::View::TT;
+    package MyApp::View::TT;
 
     use Moose;
     use namespace::clean -except => 'meta';
@@ -72,40 +73,46 @@ application/xhtml+xml content if the browser accepts it.
 
 =head1 DESCRIPTION
 
-This is a very simple Role which uses a method modifier to run after the
-C<process> method, and sets the response C<Content-Type> to be 
-C<application/xhtml+xml> if the users browser sends an C<Accept> header 
+This is a simple Role which sets the response C<Content-Type> to be
+C<application/xhtml+xml> if the users browser sends an C<Accept> header
 indicating that it is willing to process that MIME type.
 
-Changing the C<Content-Type> causes browsers to interpret the page as
-XML, meaning that the markup must be well formed.
+Changing the C<Content-Type> to C<application/xhtml+xml> causes browsers to
+interpret the page as XML, meaning that your markup must be well formed.
 
-This is useful when you're developing your application, as you know that
-all pages you view are parsed as XML, so any errors caused by your markup
-not being well-formed will show up at once.
+=head1 CAVEATS
+
+This is useful when you're developing your application, as you know that all
+pages you view are parsed as XML, so any errors caused by your markup not
+being well-formed will show up at once.
+
+Whilst this module is has been tested against most popular browsers including
+Internet Explorer, it may cause unexpected results on browsers which do not
+properly support the C<application/xhtml+xml> MIME type.
 
 =head1 METHOD MODIFIERS
 
 =head2 after process
 
-Changes the response C<Content-Type> if appropriate (from the requests C<Accept> header).
+Changes the response C<Content-Type> if appropriate (from the requests
+C<Accept> header).
 
 =head1 METHODS
 
 =head2 pragmatic_accept
 
-Some browsers (such as Internet Explorer) have a nasty way of sending
-Accept */* and this claiming to support XHTML just as well as HTML.
-Saving to a file on disk or opening with another application does
-count as accepting, but it really should have a lower q value then
-text/html. This sub takes a pragmatic approach and corrects this mistake
-by modifying the Accept header before passing it to content negotiation.
+Some browsers (such as Internet Explorer) have a nasty way of sending Accept
+*/* and this claiming to support XHTML just as well as HTML. Saving to a file
+on disk or opening with another application does count as accepting, but it
+really should have a lower q value then text/html. This sub takes a pragmatic
+approach and corrects this mistake by modifying the Accept header before
+passing it to content negotiation.
 
 =head1 ATTRIBUTES
 
 =head2 variants
 
-Returns an array ref of 3 part arrays, comprising name, priority, output 
+Returns an array ref of 3 part arrays, comprising name, priority, output
 mime-type, which is used for the content negotiation algorithm.
 
 =head1 PRIVATE METHODS
@@ -120,30 +127,38 @@ Returns the default variant attribute contents.
 
 =item L<Catalyst::View::TT::XHTML> - Trivial Catalyst TT view using this role.
 
-=item L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html> - Content negotiation RFC.
+=item L<http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html> - Content
+negotiation RFC.
 
 =back
 
 =head1 BUGS
 
-Should be split into a base ContentNegotiation role which is consumed by ContentNegotiation::XHTML.
+Should be split into a base ContentNegotiation role which is consumed by
+ContentNegotiation::XHTML.
 
 =head1 AUTHOR
 
-Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
+=over
+
+=item Maintainer and contributor of various features - David Dorward (dorward) C<< <david@dorward.me.uk> >>
+
+=item Original author and maintainer - Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
+
+=back
 
 =head1 CONTRIBUTORS
 
 =over
 
-=item David Dorward - test patches and */* pragmatism. 
-
-=item Florian Ragwitz (rafl) C<< <rafl@debian.org> >> - Conversion into a Moose Role
+=item Florian Ragwitz (rafl) C<< <rafl@debian.org> >> - Conversion into a
+Moose Role, which is what the module should have been originally.
 
 =back
 
 =head1 COPYRIGHT
 
-This module itself is copyright (c) 2008 Tomas Doran and is licensed under the same terms as Perl itself.
+This module itself is copyright (c) 2008 Tomas Doran and is licensed under the
+same terms as Perl itself.
 
 =cut