update distar url
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Engine.pm
index 34e48c3..a77c730 100644 (file)
@@ -10,7 +10,7 @@ use HTML::Entities;
 use HTTP::Headers;
 use Plack::Loader;
 use Catalyst::EngineLoader;
-use Encode 2.21 'decode_utf8';
+use Encode 2.21 'decode_utf8', 'encode', 'decode';
 use Plack::Request::Upload;
 use Hash::MultiValue;
 use namespace::clean -except => 'meta';
@@ -76,7 +76,7 @@ sub finalize_body {
     if($res->_has_response_cb) {
         ## we have not called the response callback yet, so we are safe to send
         ## the whole body to PSGI
-        
+
         my @headers;
         $res->headers->scan(sub { push @headers, @_ });
 
@@ -92,12 +92,12 @@ sub finalize_body {
                     # In the past, Catalyst only looked for ->read not ->getline.  It is very possible
                     # that one might have an object that respected read but did not have getline.
                     # As a result, we need to handle this case for backcompat.
-                
+
                     # We will just do the old loop for now.  In a future version of Catalyst this support
-                    # will be removed and one will have to rewrite their custom object or use 
+                    # will be removed and one will have to rewrite their custom object or use
                     # Plack::Middleware::AdaptFilehandleRead.  In anycase support for this is officially
                     # deprecated and described as such as of 5.90060
-                   
+
                     my $got;
                     do {
                         $got = read $body, my ($buffer), $CHUNKSIZE;
@@ -109,7 +109,7 @@ sub finalize_body {
                 } else {
                     # Looks like for  backcompat reasons we need to be able to deal
                     # with stringyfiable objects.
-                    $body = ["$body"]; 
+                    $body = ["$body"];
                 }
             } elsif(ref $body) {
                 if( (ref($body) eq 'GLOB') or (ref($body) eq 'ARRAY')) {
@@ -130,7 +130,6 @@ sub finalize_body {
             # There's no body...
             $body = [];
         }
-
         $res->_response_cb->([ $res->status, \@headers, $body]);
         $res->_clear_response_cb;
 
@@ -140,7 +139,7 @@ sub finalize_body {
         ## for backcompat we still need to handle a ->body.  I guess I could see
         ## someone calling ->write to presend some stuff, and then doing the rest
         ## via ->body, like in a template.
-        
+
         ## We'll just use the old, existing code for this (or most of it)
 
         if(my $body = $res->body) {
@@ -159,12 +158,12 @@ sub finalize_body {
               close $body;
           }
           else {
-              
-              # Case where body was set afgter calling ->write.  We'd prefer not to
-              # support this, but I can see some use cases with the way most of the
-              # views work.
 
-              $self->write($c, $body );
+              # Case where body was set after calling ->write.  We'd prefer not to
+              # support this, but I can see some use cases with the way most of the
+              # views work. Since body has already been encoded, we need to do
+              # an 'unencoded_write' here.
+              $self->unencoded_write( $c, $body );
           }
         }
 
@@ -203,6 +202,7 @@ sub finalize_cookies {
                 -path    => $val->{path},
                 -secure  => $val->{secure} || 0,
                 -httponly => $val->{httponly} || 0,
+                -samesite => $val->{samesite},
             )
         );
         if (!defined $cookie) {
@@ -250,7 +250,7 @@ sub finalize_error {
 
     $c->res->content_type('text/html; charset=utf-8');
     my $name = ref($c)->config->{name} || join(' ', split('::', ref $c));
-    
+
     # Prevent Catalyst::Plugin::Unicode::Encoding from running.
     # This is a little nasty, but it's the best way to be clean whether or
     # not the user has an encoding plugin.
@@ -574,63 +574,50 @@ process the query string and extract query parameters.
 sub prepare_query_parameters {
     my ($self, $c) = @_;
     my $env = $c->request->env;
+    my $do_not_decode_query = $c->config->{do_not_decode_query};
 
-    if(my $query_obj = $env->{'plack.request.query'}) {
-         $c->request->query_parameters(
-           $c->request->_use_hash_multivalue ?
-              $query_obj->clone :
-              $query_obj->as_hashref_mixed);
-         return;
+    my $old_encoding;
+    if(my $new = $c->config->{default_query_encoding}) {
+      $old_encoding = $c->encoding;
+      $c->encoding($new);
     }
 
+    my $check = $c->config->{do_not_check_query_encoding} ? undef :$c->_encode_check;
+    my $decoder = sub {
+      my $str = shift;
+      return $str if $do_not_decode_query;
+      return $c->_handle_param_unicode_decoding($str, $check);
+    };
+
     my $query_string = exists $env->{QUERY_STRING}
         ? $env->{QUERY_STRING}
         : '';
 
-    # Check for keywords (no = signs)
-    # (yes, index() is faster than a regex :))
-    if ( index( $query_string, '=' ) < 0 ) {
-        my $keywords = $self->unescape_uri($query_string);
-        $keywords = decode_utf8 $keywords;
-        $c->request->query_keywords($keywords);
-        return;
-    }
-
-    my %query;
+    $query_string =~ s/\A[&;]+//;
 
-    # replace semi-colons
-    $query_string =~ s/;/&/g;
+    my @unsplit_pairs = split /[&;]+/, $query_string;
+    my $p = Hash::MultiValue->new();
 
-    my @params = grep { length $_ } split /&/, $query_string;
+    my $is_first_pair = 1;
+    for my $pair (@unsplit_pairs) {
+        my ($name, $value)
+          = map { defined $_ ? $decoder->($self->unescape_uri($_)) : $_ }
+            ( split /=/, $pair, 2 )[0,1]; # slice forces two elements
 
-    for my $item ( @params ) {
+        if ($is_first_pair) {
+            # If the first pair has no equal sign, then it means the isindex
+            # flag is set.
+            $c->request->query_keywords($name) unless defined $value;
 
-        my ($param, $value)
-            = map { decode_utf8($self->unescape_uri($_)) }
-              split( /=/, $item, 2 );
-
-        unless(defined $param) {
-            $param = $self->unescape_uri($item);
-            $param = decode_utf8 $param;
+            $is_first_pair = 0;
         }
 
-        if ( exists $query{$param} ) {
-            if ( ref $query{$param} ) {
-                push @{ $query{$param} }, $value;
-            }
-            else {
-                $query{$param} = [ $query{$param}, $value ];
-            }
-        }
-        else {
-            $query{$param} = $value;
-        }
+        $p->add( $name => $value );
     }
 
-    $c->request->query_parameters( 
-      $c->request->_use_hash_multivalue ?
-        Hash::MultiValue->from_mixed(\%query) :
-        \%query);
+
+    $c->encoding($old_encoding) if $old_encoding;
+    $c->request->query_parameters( $c->request->_use_hash_multivalue ? $p : $p->mixed );
 }
 
 =head2 $self->prepare_read($c)
@@ -674,8 +661,8 @@ sub prepare_uploads {
     my $uploads = $request->_body->upload;
     my $parameters = $request->parameters;
     foreach my $name (keys %$uploads) {
-        $name = $c->_handle_unicode_decoding($name) if $enc;
         my $files = $uploads->{$name};
+        $name = $c->_handle_unicode_decoding($name) if $enc;
         my @uploads;
         for my $upload (ref $files eq 'ARRAY' ? @$files : ($files)) {
             my $headers = HTTP::Headers->new( %{ $upload->{headers} } );
@@ -724,6 +711,20 @@ sub write {
     $c->response->write($buffer);
 }
 
+=head2 $self->unencoded_write($c, $buffer)
+
+Writes the buffer to the client without encoding. Necessary for
+already encoded buffers. Used when a $c->write has been done
+followed by $c->res->body.
+
+=cut
+
+sub unencoded_write {
+    my ( $self, $c, $buffer ) = @_;
+
+    $c->response->unencoded_write($buffer);
+}
+
 =head2 $self->read($c, [$maxlength])
 
 Reads from the input stream by calling C<< $self->read_chunk >>.