Skip to content

Instantly share code, notes, and snippets.

@hisaichi5518
Last active December 11, 2015 18:29
Show Gist options
  • Select an option

  • Save hisaichi5518/4642192 to your computer and use it in GitHub Desktop.

Select an option

Save hisaichi5518/4642192 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use utf8;
use Plack::Util;
use Encode;
use AnyEvent;
use AnyEvent::HTTP ();
$AnyEvent::HTTP::MAX_PER_HOST = 20;
my $url = 'http://localhost:5000/';
my $app = sub {
my $env = shift;
my $cv = AE::cv;
AnyEvent::HTTP::http_get $url, sub {
my $content = decode_utf8(shift);
$cv->send(
[ 200, [ "Content-Type" => 'text/plain', ], ["あいうえお: $content"] ] );
};
my $res = sub {
my $start_response = shift;
$cv->cb(
sub {
$start_response->( shift->recv );
}
);
};
Plack::Util::response_cb($res, sub {
sub {
my $chunk = shift;
return unless defined $chunk;
return Encode::encode_utf8($chunk) if Encode::is_utf8($chunk);
return $chunk;
}
});
};
# http://blog.nomadscafe.jp/2009/10/psgiplack-web-server.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment