I made a simple http proxy, it's work fine, but not fast, because in the 
function handle_request, I use "my $tx = $ua->start( 
Mojo::Transaction::HTTP->new(req=>$request) );" to do the request , it's 
blocking.

I try to use a callback like $ua->start( 
Mojo::Transaction::HTTP->new(req=>$request) )=>sub{ } to make it's 
non-blocking, and then, got a mistake: 'error' => { 'message' => 'Premature 
connection close'}, I guess that's because function handle_request return 
immediately, it does not wait the callback to be finished. if i use CV to 
wait the callback, that's mean it's blocking again.

so, what's the solution? thank you very much.

#!/usr/bin/perl

use strict;
use warnings;
use threads;
use utf8;
use Mojo::IOLoop::Server;
use Mojo::UserAgent;
use Mojo::Message::Response;
use Mojo::Message::Request;
use Mojo::Transaction::HTTP;
use Data::Dumper;

binmode STDOUT, ":encoding(UTF-8)";

my %buffer;

Mojo::IOLoop->server( {port => 3128} => sub {
    my ($loop, $stream, $client) = @_;
 
    $stream->on(
        read => sub {
            my ($stream, $chunk) = @_;
            
            my $buffer = $buffer{$client}{read_buffer} .= $chunk;
            
            if ($buffer =~ /^GET\s+|POST\s+|HEAD\s+(.*)\r\n\r\n$/i) {
                $buffer{$client}{read_buffer} = '';
            &handle_request($client,$stream,$buffer);
            }
            
            elsif ($buffer =~ /^CONNECT\s+(.*)\r\n\r\n$/i) {
                $buffer{$client}{read_buffer} = '';
                &handle_connect($stream,$buffer);
            }
            
            elsif($buffer{$client}{connection})
            {
                $buffer{$client}{read_buffer} = '';
                
Mojo::IOLoop->stream($buffer{$client}{connection})->write($chunk);
            }
    
            if(length($buffer)>= 20 *1024 * 1024) {
                delete $buffer{$client};
                Mojo::IOLoop->remove($client);
                return;
            }
        });
});

sub handle_request{
    
    my($client,$stream,$chunk) = @_;
    
    my $request = Mojo::Message::Request->new;
    $request = $request->parse($chunk);
    
    my $ua = Mojo::UserAgent->new;
    my $tx = $ua->start( Mojo::Transaction::HTTP->new(req=>$request) );
        
    $stream->write( $tx->res->to_string );
}

sub handle_connect{
    my ($stream, $chunk) = @_;
    my $request = Mojo::Message::Request->new;
    my $ua = Mojo::UserAgent->new;
    
    $request = $request->parse($chunk);
        
    print Dumper($request);
}

Mojo::IOLoop->start;


-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to