Skip to content

Commit

Permalink
Encode strings as UTF-8 when it has wide characters so as the server …
Browse files Browse the repository at this point in the history
…won't crash
  • Loading branch information
miyagawa committed Sep 8, 2013
1 parent 53ff7dc commit 86602ce
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 86602ce

Please sign in to comment.