Skip to content

Instantly share code, notes, and snippets.

@mxey
Created February 27, 2010 22:45
Show Gist options
  • Select an option

  • Save mxey/317026 to your computer and use it in GitHub Desktop.

Select an option

Save mxey/317026 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use common::sense;
use lib 'lib';
use App::Senex::HTTPServer;
use AnyEvent::HTTP;
use Coro;
use Coro::Channel;
my $server = App::Senex::HTTPServer->new(
port => 8080,
);
$server->reg_cb(
connect => sub {
my($server, $con) = @_;
$con->reg_cb(request => sub {
my($con, $method, $url, $hdr, $body) = @_;
$url = 'http://' . $hdr->{host} . $url;
my $q = Coro::Channel->new();
http_request($method, $url, headers => $hdr, body => $body,
on_header => sub {
my($hdr) = @_;
$con->response($hdr->{Status}, $hdr->{Reason}, $hdr, sub {
my($send) = @_;
async {
while (my $chunk = $q->get) {
$send->($chunk);
}
$send->(undef);
};
});
return 1;
},
on_body => sub {
my($partial) = @_;
$q->put($partial);
return 1;
},
sub {
$q->put(undef);
},
);
});
},
);
AnyEvent->condvar->recv();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment