| 1 | package HTTP::Proxy::Engine::Threaded; | ||
| 2 | $HTTP::Proxy::Engine::Threaded::VERSION = '0.304'; | ||
| 3 | use strict; | ||
| 4 | use HTTP::Proxy; | ||
| 5 | |||
| 6 | my $can_use_threads; | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | $can_use_threads = eval 'use threads; 1'; | ||
| 10 | } | ||
| 11 | |||
| 12 | # A massive hack of Engine::Fork to use the threads stuff | ||
| 13 | # Basically created to work under win32 so that the filters | ||
| 14 | # can share global caches among themselves | ||
| 15 | # Angelos Karageorgiou angelos@unix.gr | ||
| 16 | |||
| 17 | our @ISA = qw( HTTP::Proxy::Engine ); | ||
| 18 | our %defaults = ( | ||
| 19 | max_clients => 60, | ||
| 20 | ); | ||
| 21 | |||
| 22 | __PACKAGE__->make_accessors( qw( kids select ), keys %defaults ); | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | die "This Perl not built to support threads" | ||
| 26 | if !$can_use_threads; | ||
| 27 | shift->SUPER::new(@_); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub start { | ||
| 31 | my $self = shift; | ||
| 32 | $self->kids( [] ); | ||
| 33 | $self->select( IO::Select->new( $self->proxy->daemon ) ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub run { | ||
| 37 | my $self = shift; | ||
| 38 | my $proxy = $self->proxy; | ||
| 39 | my $kids = $self->kids; | ||
| 40 | |||
| 41 | # check for new connections | ||
| 42 | my @ready = $self->select->can_read(1); | ||
| 43 | for my $fh (@ready) { # there's only one, anyway | ||
| 44 | # single-process proxy (useful for debugging) | ||
| 45 | |||
| 46 | # accept the new connection | ||
| 47 | my $conn = $fh->accept; | ||
| 48 | my $child=threads->new(\&_worker,$proxy,$conn); | ||
| 49 | if ( !defined $child ) { | ||
| 50 | $conn->close; | ||
| 51 | $proxy->log( HTTP::Proxy::ERROR, "PROCESS", "Cannot spawn thread" ); | ||
| 52 | next; | ||
| 53 | } | ||
| 54 | $child->detach(); | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | } | ||
| 59 | |||
| 60 | sub stop { | ||
| 61 | my $self = shift; | ||
| 62 | my $kids = $self->kids; | ||
| 63 | |||
| 64 | # not needed | ||
| 65 | } | ||
| 66 | |||
| 67 | sub _worker { | ||
| 68 | my $proxy=shift; | ||
| 69 | my $conn=shift; | ||
| 70 | $proxy->serve_connections($conn); | ||
| 71 | $conn->close(); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | __END__ |