Skip to content

Commit

Permalink
Merge pull request #429 from plack/server-encode-utf8
Browse files Browse the repository at this point in the history
Encode strings as UTF-8 when it has wide characters
  • Loading branch information
miyagawa committed Sep 23, 2013
2 parents 4b6481a + 86602ce commit 6053f8c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/HTTP/Server/PSGI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ sub write_timeout {
sub write_all {
my ($self, $sock, $buf, $timeout) = @_;
return 0 unless defined $buf;
_encode($buf);
my $off = 0;
while (my $len = length($buf) - $off) {
my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout)
Expand All @@ -289,6 +290,14 @@ sub write_all {
return length $buf;
}

# syswrite() will crash when given wide characters
sub _encode {
if ($_[0] =~ /[^\x00-\xff]/) {
Carp::carp("Wide character outside byte range in response. Encoding data as UTF-8");
utf8::encode($_[0]);
}
}

1;

__END__
Expand Down

0 comments on commit 6053f8c

Please sign in to comment.