Skip to content

Instantly share code, notes, and snippets.

@atrodo
Created July 25, 2011 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atrodo/1103397 to your computer and use it in GitHub Desktop.
Save atrodo/1103397 to your computer and use it in GitHub Desktop.
Reflex Proxy?
#!/usr/bin/env perl
#sub POE::Kernel::TRACE_EVENTS () { 1 };
use strict;
use warnings;
use v5.10;
{
package WebProxy;
use Moose;
extends 'Reflex::Base';
use Reflex::Callbacks "make_null_handler";
with 'Reflex::Role::Collectible';
has client => ( is => "rw", isa => "FileHandle", required => 1 );
has server => ( is => "rw", isa => "FileHandle", required => 1 );
has buffer => ( is => "ro", isa => "Str");
has active => ( is => "ro", isa => "Bool", default => 1 );
make_null_handler("on_client_error");
make_null_handler("on_server_error");
with "Reflex::Role::Streaming" => {
att_active => "active",
att_handle => "client",
};
with "Reflex::Role::Streaming" => {
att_active => "active",
att_handle => "server",
};
sub on_client_data
{
my ( $self, $arg ) = @_;
$self->put_server($arg->{data});
}
sub on_server_data
{
my ( $self, $arg ) = @_;
$self->put_client($arg->{data});
}
sub stop {
my $self = shift;
$self->stop_client;
$self->stop_server;
};
sub on_client_closed
{
my $self = shift;
$self->stop();
}
sub on_server_closed
{
my $self = shift;
$self->stop();
}
}
{
package TcpWebProxy;
use Moose;
extends 'Reflex::Acceptor';
use Reflex::Collection;
use Try::Tiny;
has_many clients => ( handles => { remember_client => "remember" } );
sub on_accept
{
my ( $self, $args ) = @_;
try
{
my $sock = $args->{socket};
my $conn = IO::Socket::INET->new('search.cpan.org:80');
my $ws = WebProxy->new(
client => $sock,
server => $conn,
);
$self->remember_client($ws);
}
catch
{
warn $_;
};
}
}
$SIG{__WARN__} = undef;
say "Starting...";
my $server = TcpWebProxy->new(
listener => IO::Socket::INET->new(
#LocalAddr => '127.0.0.1',
LocalPort => 8443,
Listen => 5,
Reuse => 1,
)
);
if (fork == 0)
{
require LWP::UserAgent;;
my $ua = LWP::UserAgent->new;
my $response = $ua->get('http://localhost:8443/');
if ($response->is_success) {
print $response->decoded_content; # or whatever
}
else {
die $response->status_line;
}
readline;
exit;
}
$server->run_all;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment