Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode strings as UTF-8 when it has wide characters #429

Merged
merged 1 commit into from Sep 23, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/HTTP/Server/PSGI.pm
Expand Up @@ -280,6 +280,7 @@ sub write_timeout {
sub write_all { sub write_all {
my ($self, $sock, $buf, $timeout) = @_; my ($self, $sock, $buf, $timeout) = @_;
return 0 unless defined $buf; return 0 unless defined $buf;
_encode($buf);
my $off = 0; my $off = 0;
while (my $len = length($buf) - $off) { while (my $len = length($buf) - $off) {
my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout) my $ret = $self->write_timeout($sock, $buf, $len, $off, $timeout)
Expand All @@ -289,6 +290,14 @@ sub write_all {
return length $buf; 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; 1;


__END__ __END__
Expand Down