line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::GreaseMonkeyProxy; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
28687
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
183
|
|
6
|
1
|
|
|
1
|
|
1942
|
use Getopt::Long; |
|
1
|
|
|
|
|
28218
|
|
|
1
|
|
|
|
|
8
|
|
7
|
1
|
|
|
1
|
|
1484
|
use HTTP::Proxy; |
|
1
|
|
|
|
|
162353
|
|
|
1
|
|
|
|
|
63
|
|
8
|
1
|
|
|
1
|
|
749
|
use HTTP::Proxy::GreaseMonkey; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
9
|
1
|
|
|
1
|
|
658
|
use HTTP::Proxy::GreaseMonkey::ScriptHome; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
25
|
|
10
|
1
|
|
|
1
|
|
576
|
use HTTP::Proxy::GreaseMonkey::Redirector; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
33
|
|
11
|
1
|
|
|
1
|
|
6
|
use File::Spec; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
12
|
1
|
|
|
1
|
|
1137
|
use Pod::Usage; |
|
1
|
|
|
|
|
77608
|
|
|
1
|
|
|
|
|
300
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
App::GreaseMonkeyProxy - Command line GreaseMonkey proxy |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This document describes App::GreaseMonkeyProxy version 0.05 |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use App::GreaseMonkeyProxy; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $app = App::GreaseMonkeyProxy->new; |
31
|
|
|
|
|
|
|
$app->parse_args(@ARGV); |
32
|
|
|
|
|
|
|
$app->run; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 INTERFACE |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head2 C<< new >> |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
{ |
43
|
|
|
|
|
|
|
my %ARG_SPEC; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
BEGIN { |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _array_spec { |
48
|
|
|
|
|
|
|
return [ |
49
|
|
|
|
|
|
|
[], |
50
|
|
|
|
|
|
|
sub { |
51
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
52
|
0
|
0
|
|
|
|
0
|
return [ map { 'ARRAY' eq ref $_ ? @$_ : $_ } @_ ]; |
|
0
|
|
|
|
|
0
|
|
53
|
|
|
|
|
|
|
}, |
54
|
1
|
|
|
1
|
|
14
|
]; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
1
|
|
|
1
|
|
4
|
%ARG_SPEC = ( |
58
|
|
|
|
|
|
|
show_man => [0], |
59
|
|
|
|
|
|
|
show_help => [0], |
60
|
|
|
|
|
|
|
args => _array_spec(), |
61
|
|
|
|
|
|
|
servers => [5], |
62
|
|
|
|
|
|
|
port => [8030], |
63
|
|
|
|
|
|
|
verbose => [0], |
64
|
|
|
|
|
|
|
); |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
9
|
while ( my ( $name, $spec ) = each %ARG_SPEC ) { |
67
|
1
|
|
|
1
|
|
14
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
145
|
|
68
|
6
|
|
100
|
|
|
34
|
my $validator = $spec->[1] || sub { shift; shift }; |
69
|
6
|
|
|
|
|
637
|
*{ __PACKAGE__ . '::' . $name } = sub { |
70
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
71
|
0
|
0
|
|
|
|
0
|
$self->{$name} = $self->$validator( @_ ) |
72
|
|
|
|
|
|
|
if ( @_ ); |
73
|
0
|
|
|
|
|
0
|
my $value = $self->{$name}; |
74
|
0
|
0
|
0
|
|
|
0
|
return ( wantarray && 'ARRAY' eq ref $value ) |
75
|
|
|
|
|
|
|
? @$value |
76
|
|
|
|
|
|
|
: $value; |
77
|
6
|
|
|
|
|
28
|
}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new { |
82
|
0
|
|
|
0
|
1
|
|
my ( $class, %args ) = @_; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
while ( my ( $name, $spec ) = each %ARG_SPEC ) { |
87
|
0
|
0
|
|
|
|
|
my $value |
88
|
|
|
|
|
|
|
= exists $args{$name} ? delete $args{$name} : $spec->[0]; |
89
|
0
|
0
|
|
|
|
|
$self->$name( $value ) |
90
|
|
|
|
|
|
|
if defined $value; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
croak "Unknown options: ", join( ', ', sort keys %args ) |
94
|
|
|
|
|
|
|
if keys %args; |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $self; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 C<< args >> |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head2 C<< servers >> |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Accessor for the number of servers to start. Defaults to 5. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 C<< port >> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Accessor for the port to listen on. Defaults to 8030. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 C<< verbose >> |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Accessor for verbosity. Defaults to 0. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head2 C<< show_help >> |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 C<< show_man >> |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 C<< parse_args >> |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Parse an argument array - typically C<@ARGV>. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$app->parse_args( @ARGV ); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub parse_args { |
127
|
0
|
|
|
0
|
1
|
|
my ( $self, @args ) = @_; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
local @ARGV = @args; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
my %options; |
132
|
|
|
|
|
|
|
|
133
|
0
|
0
|
|
|
|
|
GetOptions( |
134
|
|
|
|
|
|
|
'help|?' => \$options{show_help}, |
135
|
|
|
|
|
|
|
man => \$options{show_man}, |
136
|
|
|
|
|
|
|
'port=i' => \$options{port}, |
137
|
|
|
|
|
|
|
'servers=i' => \$options{servers}, |
138
|
|
|
|
|
|
|
'v|verbose' => \$options{verbose}, |
139
|
|
|
|
|
|
|
) or pod2usage(); |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
while ( my ( $name, $value ) = each %options ) { |
142
|
0
|
0
|
|
|
|
|
$self->$name( $value ) if defined $value; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
$self->args( @ARGV ); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 C<< run >> |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub run { |
153
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
if ( $self->show_help ) { |
|
|
0
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
$self->do_help; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
elsif ( $self->show_man ) { |
159
|
0
|
|
|
|
|
|
pod2usage( -verbose => 2, -exitstatus => 0 ); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
else { |
162
|
0
|
|
|
|
|
|
my @args = $self->args; |
163
|
0
|
0
|
|
|
|
|
pod2usage() unless @args; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
my $proxy = HTTP::Proxy->new( |
166
|
|
|
|
|
|
|
port => $self->port, |
167
|
|
|
|
|
|
|
start_servers => $self->servers |
168
|
|
|
|
|
|
|
); |
169
|
0
|
|
|
|
|
|
my $gm = HTTP::Proxy::GreaseMonkey::ScriptHome->new; |
170
|
0
|
|
|
|
|
|
$gm->verbose( $self->verbose ); |
171
|
0
|
|
|
|
|
|
my @dirs = map glob, @args; |
172
|
0
|
|
|
|
|
|
$gm->add_dir( @dirs ); |
173
|
0
|
|
|
|
|
|
$proxy->push_filter( |
174
|
|
|
|
|
|
|
mime => 'text/html', |
175
|
|
|
|
|
|
|
response => $gm |
176
|
|
|
|
|
|
|
); |
177
|
|
|
|
|
|
|
# Make the redirector |
178
|
0
|
|
|
|
|
|
my $redir = HTTP::Proxy::GreaseMonkey::Redirector->new; |
179
|
0
|
|
|
|
|
|
$redir->passthru( $gm->get_passthru_key ); |
180
|
0
|
0
|
|
|
|
|
$redir->state_file( |
181
|
|
|
|
|
|
|
File::Spec->catfile( $dirs[0], 'state.yml' ) ) |
182
|
|
|
|
|
|
|
if @dirs; |
183
|
0
|
|
|
|
|
|
$proxy->push_filter( request => $redir, ); |
184
|
0
|
|
|
|
|
|
$proxy->start; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head2 C |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Output help page |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
sub do_help { |
195
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
196
|
0
|
|
|
|
|
|
pod2usage( -verbose => 1 ); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
1; |
200
|
|
|
|
|
|
|
__END__ |