Avoid manging request headers by cloning them. Neater solutions welcome.
Tomas Doran [Tue, 3 Feb 2009 22:09:14 +0000 (22:09 +0000)]
Changes
lib/Catalyst/View/ContentNegotiation/XHTML.pm

diff --git a/Changes b/Changes
index 1ad28f9..a63c95c 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 1.100
   - Refactor into a Moose Role for use with alternate views. (rafl)
   - Additional documentation (t0m)
+  - Clone request headers rather than changing them (t0m)
 1.004
   - Nick the OSX fragment out of the Catalyst::Runtime Makefile.PL to
     beat my Mac into generating a correct dist. (t0m)
index e968ccc..a548cd7 100644 (file)
@@ -28,10 +28,10 @@ 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|;
         }
     }
@@ -39,13 +39,14 @@ after process => sub {
 
 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;