finished the scheme matching and uri_for updates
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index eaf9581..570abc8 100644 (file)
@@ -86,8 +86,10 @@ has response => (
     lazy => 1,
 );
 sub _build_response_constructor_args {
-    my $self = shift;
-    { _log => $self->log };
+    return +{
+      _log => $_[0]->log,
+      encoding => $_[0]->encoding,
+    };
 }
 
 has namespace => (is => 'rw');
@@ -127,7 +129,7 @@ __PACKAGE__->stats_class('Catalyst::Stats');
 __PACKAGE__->_encode_check(Encode::FB_CROAK | Encode::LEAVE_SRC);
 
 # Remember to update this in Catalyst::Runtime as well!
-our $VERSION = '5.90080_001';
+our $VERSION = '5.90079_001';
 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
 
 sub import {
@@ -1372,6 +1374,10 @@ path, use C<< $c->uri_for_action >> instead.
   # Path to a static resource
   $c->uri_for('/static/images/logo.png');
 
+In general the scheme of the generated URI object will follow the incoming request
+however if your targeted action or action chain has the Scheme attribute it will
+use that instead.
+
 =cut
 
 sub uri_for {
@@ -1407,6 +1413,7 @@ sub uri_for {
       }
     }
 
+    my $target_action = $path->$_isa('Catalyst::Action') ? $path : undef;
     if ( $path->$_isa('Catalyst::Action') ) { # action object
         s|/|%2F|g for @encoded_args;
         my $captures = [ map { s|/|%2F|g; $_; }
@@ -1418,7 +1425,6 @@ sub uri_for {
         # ->uri_for( $action, \@captures_and_args, \%query_values? )
         if( !@encoded_args && $action->number_of_args ) {
             my $expanded_action = $c->dispatcher->expand_action( $action );
-
             my $num_captures = $expanded_action->number_of_captures;
             unshift @encoded_args, splice @$captures, $num_captures;
         }
@@ -1450,7 +1456,19 @@ sub uri_for {
     my ($base, $class) = ('/', 'URI::_generic');
     if(blessed($c)) {
       $base = $c->req->base;
-      $class = ref($base);
+      if($target_action) {
+        $target_action = $c->dispatcher->expand_action($target_action);
+        if(my $s = $target_action->scheme) {
+          $s = lc($s);
+          $class = "URI::$s";
+          $base->scheme($s);
+        } else {
+          $class = ref($base);
+        }
+      } else {
+        $class = ref($base);
+      }
+
       $base =~ s{(?<!/)$}{/};
     }
 
@@ -2031,13 +2049,30 @@ sub finalize_headers {
 
     $c->response->finalize_headers();
 
+    if(my $enc = $c->encoding) {
+       my ($ct, $ct_enc) = $c->response->content_type;
+
+        # Only touch 'text-like' contents
+        if($c->response->content_type =~ /^text|xml$|javascript$/) {
+          if ($ct_enc && $ct_enc =~ /charset=([^;]*)/) {
+            if (uc($1) ne uc($enc->mime_name)) {
+              $c->log->debug("Catalyst encoding config is set to encode in '" .
+                           $enc->mime_name .
+                           "', content type is '$1', not encoding ");
+            }
+          } else {
+            $c->res->content_type($c->res->content_type . "; charset=" . $enc->mime_name);
+          }
+        }
+    }
+
     # Done
     $response->finalized_headers(1);
 }
 
 =head2 $c->finalize_encoding
 
-Make sure your headers and body are encoded properly IF you set an encoding.  By
+Make sure your body is encoded properly IF you set an encoding.  By
 default the encoding is UTF-8 but you can disable it by explictly setting the
 encoding configuration value to undef.
 
@@ -2056,27 +2091,12 @@ sub finalize_encoding {
 
     return unless $enc;
 
-    my ($ct, $ct_enc) = $c->response->content_type;
-
     # Only touch 'text-like' contents
-    return unless $c->response->content_type =~ /^text|xml$|javascript$/;
-
-    if ($ct_enc && $ct_enc =~ /charset=([^;]*)/) {
-        if (uc($1) ne uc($enc->mime_name)) {
-            $c->log->debug("Unicode::Encoding is set to encode in '" .
-                           $enc->mime_name .
-                           "', content type is '$1', not encoding ");
-            return;
-        }
-    } else {
-        $c->res->content_type($c->res->content_type . "; charset=" . $enc->mime_name);
+    if($c->response->content_type =~ /^text|xml$|javascript$/) {
+      if (ref(\$body) eq 'SCALAR') {
+        $c->response->body( $c->encoding->encode( $body, $c->_encode_check ) );
+      }
     }
-
-    # Oh my, I wonder what filehandle responses and streams do... - jnap.
-    # Encode expects plain scalars (IV, NV or PV) and segfaults on ref's
-    if (ref(\$body) eq 'SCALAR') {
-      $c->response->body( $c->encoding->encode( $body, $c->_encode_check ) );
-    };
 }
 
 =head2 $c->finalize_output