[dancer-users] Failing to send a file in iso-8859-1

Lutz Gehlen lrg_ml at gmx.net
Fri Oct 7 21:08:34 BST 2016


Hi Russell,

On Friday 07.10.2016 12:27:26 Russell Jenkins wrote:
> On 6/10/2016 11:56 PM, Lutz Gehlen wrote:
> >>> send_file(
> >>> 
> >>>      \$str,
> >>>      content_type => 'text/csv',
> >>>      filename     => 'foo.csv',
> >>> 
> >>> );
> >>> 
> >>> The problem is that external requirements force me to send the file
> >>> in iso-8859-1 and it seems that I cannot manage to achieve that.
> >> 
> >> As far as I understand, the charset is set in config, so you could
> >> try to override the setting in your route like:
> >> 
> >> set 'charset'      => 'iso-8859-1;
> >> 
> >> Never tried, but it should work.
> > 
> > Yes. this works. However, it changes charset permanently (for the
> > current process) and I don't get the chance to change it back because
> > send_file does not return. Is there no way to set it temporarily just
> > for this send_file?
> 
> There should be no need to change your apps charset.
> 
> If you look at any of the HTTP specs, a Content-Type header looks like:
> 
>    Content-Type: text/html; charset=utf-8
> 
> Without digging into the Dancer1 code,
> 
>    send_file(\$str, content_type => 'text/html; charset=utf-8', filename
> => 'foo.html' );
> 
> should DWIM (it will in Dancer2). Substitute your own mime types and
> encoding as needed.

Thank you for your suggestion. However, this does not work in Dancer1. I 
have already tried this approach (see my first email) and the default 
character set is still attached to the Content-Type header resulting in
Content-Type: text/csv; charset=iso-8859-1; charset=utf-8
Moreover, the data get still encoded in utf-8.

I believe that the relevant code is in Dancer::Handler::render_response:

...
        my $charset = setting('charset');
        my $ctype   = $response->header('Content-Type');
 
        if ( $charset && $ctype && _is_text($ctype) ) {
            $content = Encode::encode( $charset, $content ) unless 
$response->_already_encoded;
            $response->header( 'Content-Type' => "$ctype; charset=$charset" 
)
              if $ctype !~ /$charset/;
        }
...

Best wishes,
Lutz


More information about the dancer-users mailing list