HTTP-Proxy-GreaseMonkey

 view release on metacpan or  search on metacpan

lib/App/GreaseMonkeyProxy.pm  view on Meta::CPAN

            *{ __PACKAGE__ . '::' . $name } = sub {
                my $self = shift;
                $self->{$name} = $self->$validator( @_ )
                  if ( @_ );
                my $value = $self->{$name};
                return ( wantarray && 'ARRAY' eq ref $value )
                  ? @$value
                  : $value;
            };
        }
    }

    sub new {
        my ( $class, %args ) = @_;

        my $self = bless {}, $class;

        while ( my ( $name, $spec ) = each %ARG_SPEC ) {
            my $value
              = exists $args{$name} ? delete $args{$name} : $spec->[0];
            $self->$name( $value )
              if defined $value;
        }

        croak "Unknown options: ", join( ', ', sort keys %args )
          if keys %args;

        return $self;
    }
}

=head2 C<< args >>

=head2 C<< servers >>

Accessor for the number of servers to start. Defaults to 5.

=head2 C<< port >>

Accessor for the port to listen on. Defaults to 8030.

=head2 C<< verbose >>

Accessor for verbosity. Defaults to 0.

=head2 C<< show_help >>

=head2 C<< show_man >>

=head2 C<< parse_args >>

Parse an argument array - typically C<@ARGV>.

    $app->parse_args( @ARGV );

=cut

sub parse_args {
    my ( $self, @args ) = @_;

    local @ARGV = @args;

    my %options;

    GetOptions(
        'help|?'    => \$options{show_help},
        man         => \$options{show_man},
        'port=i'    => \$options{port},
        'servers=i' => \$options{servers},
        'v|verbose' => \$options{verbose},
    ) or pod2usage();

    while ( my ( $name, $value ) = each %options ) {
        $self->$name( $value ) if defined $value;
    }

    $self->args( @ARGV );
}

=head2 C<< run >>

=cut

sub run {
    my $self = shift;

    if ( $self->show_help ) {
        $self->do_help;
    }
    elsif ( $self->show_man ) {
        pod2usage( -verbose => 2, -exitstatus => 0 );
    }
    else {
        my @args = $self->args;
        pod2usage() unless @args;

        my $proxy = HTTP::Proxy->new(
            port          => $self->port,
            start_servers => $self->servers
        );
        my $gm = HTTP::Proxy::GreaseMonkey::ScriptHome->new;
        $gm->verbose( $self->verbose );
        my @dirs = map glob, @args;
        $gm->add_dir( @dirs );
        $proxy->push_filter(
            mime     => 'text/html',
            response => $gm
        );
        # Make the redirector
        my $redir = HTTP::Proxy::GreaseMonkey::Redirector->new;
        $redir->passthru( $gm->get_passthru_key );
        $redir->state_file(
            File::Spec->catfile( $dirs[0], 'state.yml' ) )
          if @dirs;
        $proxy->push_filter( request => $redir, );
        $proxy->start;
    }
}

=head2 C<do_help>



( run in 1.406 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )