if ( my $error = $@ ) {
#rethow if this can be handled by middleware
- if(blessed $error && ($error->can('as_psgi') || $error->can('code'))) {
+ if(
+ blessed $error && (
+ $error->can('as_psgi') ||
+ (
+ $error->can('code') &&
+ $error->code =~m/^[1-5][0-9][0-9]$/
+ )
+ )
+ ) {
foreach my $err (@{$c->error}) {
$c->log->error($err);
}
$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.
+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.
+
See L</ENCODING>.
=cut
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
$status = $c->finalize;
} catch {
#rethow if this can be handled by middleware
- if(blessed $_ && ($_->can('as_psgi') || $_->can('code'))) {
+ if(
+ blessed($_) && (
+ $_->can('as_psgi') ||
+ (
+ $_->can('code') &&
+ $_->code =~m/^[1-5][0-9][0-9]$/
+ )
+ )
+ ) {
$_->can('rethrow') ? $_->rethrow : croak $_;
}
chomp(my $error = $_);
sub prepare_body_parameters {
my $c = shift;
$c->request->prepare_body_parameters( $c, @_ );
-
- # If we have an encoding configured (like UTF-8) in general we expect a client
- # to POST with the encoding we fufilled the request in. Otherwise don't do any
- # encoding (good change wide chars could be in HTML entity style llike the old
- # days -JNAP
-
- # so, now that HTTP::Body prepared the body params, we gotta 'walk' the structure
- # and do any needed decoding.
-
- # This only does something if the encoding is set via the encoding param. Remember
- # this is assuming the client is not bad and responds with what you provided. In
- # general you can just use utf8 and get away with it.
-
- if($c->encoding) {
- my $current_parameters = $c->request->body_parameters;
- $c->request->body_parameters($c->_handle_unicode_decoding($current_parameters));
- }
}
=head2 $c->prepare_connection
sub prepare_uploads {
my $c = shift;
$c->engine->prepare_uploads( $c, @_ );
-
- my $enc = $c->encoding;
- return unless $enc;
-
- ## Only trying to decode the filenames.
- for my $value ( values %{ $c->request->uploads } ) {
- # skip if it fails for uploads, as we don't usually want uploads touched
- # in any way
- for my $inner_value ( ref($value) eq 'ARRAY' ? @{$value} : $value ) {
- $inner_value->{filename} = try {
- $enc->decode( $inner_value->{filename}, $c->_encode_check )
- } catch {
- $c->handle_unicode_encoding_exception({
- param_value => $inner_value->{filename},
- error_msg => $_,
- encoding_step => 'uploads',
- });
- };
- }
- }
}
=head2 $c->prepare_write
sub setup_encoding {
my $c = shift;
- my $enc = delete $c->config->{encoding};
- $c->encoding( $enc ) if defined $enc;
+ if( exists($c->config->{encoding}) && !defined($c->config->{encoding}) ) {
+ # Ok, so the user has explicitly said "I don't want encoding..."
+ return;
+ } else {
+ my $enc = defined($c->config->{encoding}) ?
+ delete $c->config->{encoding} : 'UTF-8'; # not sure why we delete it... (JNAP)
+ $c->encoding($enc);
+ }
}
=head2 handle_unicode_encoding_exception
foreach (keys %$value) {
my $encoded_key = $self->_handle_param_unicode_decoding($_);
$value->{$encoded_key} = $self->_handle_unicode_decoding($value->{$_});
- delete $value->{$_};
+
+ # If the key was encoded we now have two (the original and current so
+ # delete the original.
+ delete $value->{$_} if $_ ne $encoded_key;
}
return $value;
}
C<encoding> - See L</ENCODING>
+This now defaults to 'UTF-8'. You my turn it off by setting this configuration
+value to undef.
+
=item *
C<abort_chain_on_error_fix>
On request, decodes all params from encoding into a sequence of
logical characters. On response, encodes body into encoding.
+By default encoding is now 'UTF-8'. You may turn it off by setting
+the encoding configuration to undef.
+
=head2 Methods
=over 4