X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=5c51dcb57f9dce354a2d416a62fe0fb9f929af12;hb=dd4530ecdc4684838d9c0e9dc00adebb6100b022;hp=c5aacadd7cd1eec1f819f05904af1ab57d464fc5;hpb=817ed8ab62b7b59cc37e82d67aa45824211f75f6;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index c5aacad..5c51dcb 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -7,21 +7,38 @@ use CGI::Simple::Cookie; use Data::Dump qw/dump/; use Errno 'EWOULDBLOCK'; use HTML::Entities; -use HTTP::Body; use HTTP::Headers; -use URI::QueryParam; use Plack::Loader; use Catalyst::EngineLoader; -use Encode (); +use Encode 2.21 'decode_utf8', 'encode', 'decode'; use Plack::Request::Upload; use Hash::MultiValue; -use utf8; - +use Ref::Util qw(is_plain_arrayref is_plain_globref is_plain_hashref); use namespace::clean -except => 'meta'; +use utf8; # Amount of data to read from input on each pass our $CHUNKSIZE = 64 * 1024; +# XXX - this is only here for compat, do not use! +has env => ( is => 'rw', writer => '_set_env' , weak_ref=>1); +my $WARN_ABOUT_ENV = 0; +around env => sub { + my ($orig, $self, @args) = @_; + if(@args) { + warn "env as a writer is deprecated, you probably need to upgrade Catalyst::Engine::PSGI" + unless $WARN_ABOUT_ENV++; + return $self->_set_env(@args); + } + return $self->$orig; +}; + +# XXX - Only here for Engine::PSGI compat +sub prepare_connection { + my ($self, $ctx) = @_; + $ctx->request->prepare_connection; +} + =head1 NAME Catalyst::Engine - The Catalyst Engine @@ -96,7 +113,7 @@ sub finalize_body { $body = ["$body"]; } } elsif(ref $body) { - if( (ref($body) eq 'GLOB') or (ref($body) eq 'ARRAY')) { + if( (is_plain_globref($body)) or (is_plain_arrayref($body))) { # Again, PSGI can just accept this, no transform needed. We don't officially # document the body as arrayref at this time (and there's not specific test # cases. we support it because it simplifies some plack compatibility logic @@ -114,7 +131,6 @@ sub finalize_body { # There's no body... $body = []; } - $res->_response_cb->([ $res->status, \@headers, $body]); $res->_clear_response_cb; @@ -129,7 +145,7 @@ sub finalize_body { if(my $body = $res->body) { - if ( blessed($body) && $body->can('read') or ref($body) eq 'GLOB' ) { + if ( blessed($body) && $body->can('read') or is_plain_globref($body) ) { ## In this case we have no choice and will fall back on the old ## manual streaming stuff. Not optimal. This is deprecated as of 5.900560+ @@ -144,11 +160,11 @@ sub finalize_body { } else { - # Case where body was set afgter calling ->write. We'd prefer not to + # 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. - - $self->write($c, $body ); + # views work. Since body has already been encoded, we need to do + # an 'unencoded_write' here. + $self->unencoded_write( $c, $body ); } } @@ -218,7 +234,7 @@ sub _dump_error_page_element { # This is fugly, but the metaclass is _HUGE_ and demands waaay too much # scrolling. Suggestions for more pleasant ways to do this welcome. local $val->{'__MOP__'} = "Stringified: " - . $val->{'__MOP__'} if ref $val eq 'HASH' && exists $val->{'__MOP__'}; + . $val->{'__MOP__'} if is_plain_hashref($val) && exists $val->{'__MOP__'}; my $text = encode_entities( dump( $val )); sprintf <<"EOF", $name, $text; @@ -286,6 +302,7 @@ sub finalize_error { (pt) Por favor volte mais tarde (ru) Попробуйте еще раз позже (ua) Спробуйте ще раз пізніше +(it) Per favore riprova più tardi $name = ''; @@ -427,7 +444,7 @@ sub finalize_uploads { foreach my $key (keys %{ $request->uploads }) { my $upload = $request->uploads->{$key}; unlink grep { -e $_ } map { $_->tempname } - (ref $upload eq 'ARRAY' ? @{$upload} : ($upload)); + (is_plain_arrayref($upload) ? @{$upload} : ($upload)); } } @@ -557,15 +574,21 @@ 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} : ''; @@ -573,42 +596,22 @@ sub prepare_query_parameters { # Check for keywords (no = signs) # (yes, index() is faster than a regex :)) if ( index( $query_string, '=' ) < 0 ) { - $c->request->query_keywords($self->unescape_uri($query_string)); + my $keywords = $self->unescape_uri($query_string); + $keywords = $decoder->($keywords); + $c->request->query_keywords($keywords); return; } - my %query; - - # replace semi-colons - $query_string =~ s/;/&/g; - - my @params = grep { length $_ } split /&/, $query_string; + $query_string =~ s/\A[&;]+//; - for my $item ( @params ) { + my $p = Hash::MultiValue->new( + map { defined $_ ? $decoder->($self->unescape_uri($_)) : $_ } + map { ( split /=/, $_, 2 )[0,1] } # slice forces two elements + split /[&;]+/, $query_string + ); - my ($param, $value) - = map { $self->unescape_uri($_) } - split( /=/, $item, 2 ); - - $param = $self->unescape_uri($item) unless defined $param; - - if ( exists $query{$param} ) { - if ( ref $query{$param} ) { - push @{ $query{$param} }, $value; - } - else { - $query{$param} = [ $query{$param}, $value ]; - } - } - else { - $query{$param} = $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) @@ -634,6 +637,7 @@ sub prepare_request { my ($self, $ctx, %args) = @_; $ctx->log->psgienv($args{env}) if $ctx->log->can('psgienv'); $ctx->request->_set_env($args{env}); + $self->_set_env($args{env}); # Nasty back compat! $ctx->response->_set_response_cb($args{response_cb}); } @@ -647,20 +651,26 @@ sub prepare_uploads { my $request = $c->request; return unless $request->_body; + my $enc = $c->encoding; my $uploads = $request->_body->upload; my $parameters = $request->parameters; foreach my $name (keys %$uploads) { my $files = $uploads->{$name}; + $name = $c->_handle_unicode_decoding($name) if $enc; my @uploads; - for my $upload (ref $files eq 'ARRAY' ? @$files : ($files)) { + for my $upload (is_plain_arrayref($files) ? @$files : ($files)) { my $headers = HTTP::Headers->new( %{ $upload->{headers} } ); + my $filename = $upload->{filename}; + $filename = $c->_handle_unicode_decoding($filename) if $enc; + my $u = Catalyst::Request::Upload->new ( size => $upload->{size}, type => scalar $headers->content_type, + charset => scalar $headers->content_type_charset, headers => $headers, tempname => $upload->{tempname}, - filename => $upload->{filename}, + filename => $filename, ); push @uploads, $u; } @@ -670,7 +680,7 @@ sub prepare_uploads { my @filenames = map { $_->{filename} } @uploads; # append, if there's already params with this name if (exists $parameters->{$name}) { - if (ref $parameters->{$name} eq 'ARRAY') { + if (is_plain_arrayref($parameters->{$name})) { push @{ $parameters->{$name} }, @filenames; } else { @@ -695,6 +705,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 >>. @@ -741,7 +765,7 @@ sub run { # FIXME - we should stash the options in an attribute so that custom args # like Gitalist's --git_dir are possible to get from the app without stupid tricks. my $server = pop @args if (scalar @args && blessed $args[-1]); - my $options = pop @args if (scalar @args && ref($args[-1]) eq 'HASH'); + my $options = pop @args if (scalar @args && is_plain_hashref($args[-1])); # Back compat hack for applications with old (non Catalyst::Script) scripts to work in FCGI. if (scalar @args && !ref($args[0])) { if (my $listen = shift @args) {