Proxy.pm

filter: Statementstmt 91.8% 56 / 61 Branchbran 70.0% 14 / 20 Conditioncond 55.5% 10 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 93.3% 14 / 15 Podpod 100% 11 / 11 totaltotal 79.5% 109 / 137 CC 18 SCAR 20.7
CC
18
Cov
81%
new
12.7
get_url
12.4
get_preferred_method
2.0
CRAP 7.9
↑ Summary ProxyList.pm »
1 package Geonode::Free::Proxy;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Geonode::Free::Proxy - Geonode's Proxy Object returned by Geonode::Free::ProxyList
11  
12 Note: this a companion module for Geonode::Free::ProxyList
13  
14 =head1 VERSION
15  
16 Version 0.0.5
17  
18 =cut
19  
20 3 our $VERSION = '0.0.5';
21  
22 3 my $preferred_method = 'socks';
23  
24 =head1 SYNOPSIS
25  
26 Geonode's Proxy Object returned by Geonode::Free::ProxyList
27  
28 my $some_proxy = Geonode::Free::Proxy->new(
29 'some-id',
30 '127.0.0.1',
31 3128,
32 [ 'http', 'socks5' ]
33 );
34  
35 $some_proxy->get_host(); # '127.0.0.1'
36 $some_proxy->get_port(); # 3128
37 $some_proxy->get_methods(); # [ 'http', 'socks5' ]
38  
39 my $other_proxy = Geonode::Free::Proxy->new(
40 'some-id',
41 '127.0.0.1',
42 3128,
43 [ 'http', 'socks5' ]
44 );
45  
46 Geonode::Free::Proxy::prefer_socks();
47 $some_proxy->get_url(); # 'socks://127.0.0.1:3128';
48  
49 Geonode::Free::Proxy::prefer_http();
50 $other_proxy->get_url(); # 'http://127.0.0.1:3128';
51  
52 my $http_proxy = Geonode::Free::Proxy->new(
53 'some-id',
54 '127.0.0.1',
55 3128,
56 [ 'http' ]
57 );
58  
59 $http_proxy->can_use_http(); # 1;
60 $http_proxy->can_use_socks(); # 0;
61  
62 Geonode::Free::Proxy::prefer_socks();
63 $http_proxy->get_url(); # 'http://127.0.0.1:3128';
64  
65 =head1 SUBROUTINES/METHODS
66  
67 =head2 new
68  
69 Instantiate Geonode::Free::Proxy object (id, host, port, methods)
70  
71 =cut
72  
73 17 ▶sub new {
Subroutine new: called CC=12 SCAR=25.4
Pod documented
74 17 my ( $class, $id, $host, $port, $methods ) = @_;
75  
76 17 ▶ if ( !defined $id || $id =~ m{^\s*+$}sxmi ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $id or $id =~ /^\s*+$/imsx) 0 17 17
Condition33%1/3
not defined $id or $id =~ /^\s*+$/imsx
l!l&&r!l&&!r
0017
MC/DC0%0/2
not defined $id or $id =~ /^\s*+$/imsx
not defined $id $id =~ /^\s*+$/imsx
Truth table33%1/3
not(A) or B
A= not defined $idB= $id =~ /^\s*+$/imsx
ABresult
1X1
011
000
77 0 croak 'ERROR: id not informed';
78 }
79  
80 17 ▶ if ( !defined $host || $host =~ m{^\s*+$}sxmi ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $host or $host =~ /^\s*+$/imsx) 1 16 17
Condition66%2/3
not defined $host or $host =~ /^\s*+$/imsx
l!l&&r!l&&!r
0116
MC/DC50%1/2
not defined $host or $host =~ /^\s*+$/imsx
not defined $host $host =~ /^\s*+$/imsx
Truth table66%2/3
not(A) or B
A= not defined $hostB= $host =~ /^\s*+$/imsx
ABresult
1X1
011
000
81 1 croak 'ERROR: host not informed';
82 }
83  
84 16 ▶ if ( !defined $port || $port !~ m{^[1-9][0-9]*+$}sxmi ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port and $port =~ /^[1-9][0-9]*+$/imsx) 1 15 16
Condition66%2/3
defined $port and $port =~ /^[1-9][0-9]*+$/imsx
!ll&&!rl&&r
0115
MC/DC50%1/2
defined $port and $port =~ /^[1-9][0-9]*+$/imsx
defined $port $port =~ /^[1-9][0-9]*+$/imsx
Truth table66%2/3
A and B
A= defined $portB= $port =~ /^[1-9][0-9]*+$/imsx
ABresult
0X0
100
111
85 1 croak 'ERROR: port must be a number';
86 }
87  
88 15 ▶ if ( !defined $methods || ref($methods) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $methods or ref $methods ne "ARRAY") 1 14 15
Condition66%2/3
not defined $methods or ref $methods ne "ARRAY"
l!l&&r!l&&!r
0114
MC/DC50%1/2
not defined $methods or ref $methods ne "ARRAY"
not defined $methods ref $methods ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not defined $methodsB= ref $methods ne "ARRAY"
ABresult
1X1
011
000
89 1 croak 'ERROR: methods must be an array reference';
90 }
91  
92 14 ▶ if ( @{$methods} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$methods;} == 0) 1 13 14
93 1 croak 'ERROR: no informed methods';
94 }
95  
96 13 foreach my $method ( @{$methods} ) {
97 23 ▶ if ( $method !~ m{^(?>socks[45]|https?)$}sxm ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /^(?>socks[45]|https?)$/msx) 1 22 23
98 1 croak q()
99 . "ERROR: '$method' is not a valid method\n"
100 . ' Valid methods are: socks4/socks5/http/https';
101 }
102 }
103  
104 12 my $self = {
105 id => $id,
106 host => $host,
107 port => $port,
108 methods => $methods
109 };
110  
111 12 return bless $self, $class;
112 }
113  
114 =head2 prefer_socks
115  
116 Sets preferred method to socks. This is used when getting the full proxy url.
117  
118 Preferred method is set up *globally*.
119  
120 =cut
121  
122 1 ▶sub prefer_socks {
Subroutine prefer_socks: called CC=1 SCAR=0.0
Pod documented
123 1 $preferred_method = 'socks';
124 1 return;
125 }
126  
127 =head2 prefer_http
128  
129 Sets preferred method to http. This is used when getting the full proxy url.
130  
131 Preferred method is set up *globally*.
132  
133 =cut
134  
135 1 ▶sub prefer_http {
Subroutine prefer_http: called CC=1 SCAR=0.0
Pod documented
136 1 $preferred_method = 'http';
137 1 return;
138 }
139  
140 =head2 get_preferred_method
141  
142 Gets preferred method
143  
144 =cut
145  
146 0 ▶sub get_preferred_method {
Subroutine get_preferred_method: not called CC=1 SCAR=6.9
Pod documented
147 0 return $preferred_method;
148 }
149  
150 =head2 get_id
151  
152 Gets host
153  
154 =cut
155  
156 3 ▶sub get_id {
Subroutine get_id: called CC=1 SCAR=0.0
Pod documented
157 3 my $self = shift;
158 3 return $self->{id};
159 }
160  
161 =head2 get_host
162  
163 Gets host
164  
165 =cut
166  
167 4 ▶sub get_host {
Subroutine get_host: called CC=1 SCAR=0.0
Pod documented
168 4 my $self = shift;
169 4 return $self->{host};
170 }
171  
172 =head2 get_port
173  
174 Gets port
175  
176 =cut
177  
178 4 ▶sub get_port {
Subroutine get_port: called CC=1 SCAR=0.0
Pod documented
179 4 my $self = shift;
180 4 return $self->{port};
181 }
182  
183 =head2 get_methods
184  
185 Gets methods
186  
187 =cut
188  
189 7 ▶sub get_methods {
Subroutine get_methods: called CC=1 SCAR=0.0
Pod documented
190 7 my $self = shift;
191 7 return @{ $self->{methods} };
192 }
193  
194 =head2 can_use_socks
195  
196 Returns truthy if proxy can use socks method
197  
198 =cut
199  
200 3 ▶sub can_use_socks {
Subroutine can_use_socks: called CC=1 SCAR=0.0
Pod documented
201 3 my $self = shift;
202 3 return 0 < grep { m{^socks}sxmi } $self->get_methods();
203 }
204  
205 =head2 can_use_http
206  
207 Returns truthy if proxy can use http method
208  
209 =cut
210  
211 2 ▶sub can_use_http {
Subroutine can_use_http: called CC=1 SCAR=0.0
Pod documented
212 2 my $self = shift;
213 2 return 0 < grep { m{^http}sxmi } $self->get_methods();
214 }
215  
216 =head2 get_url
217  
218 Gets proxy url
219  
220 =cut
221  
222 2 ▶sub get_url {
Subroutine get_url: called CC=7 SCAR=25.1
Pod documented
223 2 my $self = shift;
224  
225 2 my $method;
226 2 ▶ if ( $self->can_use_socks && $preferred_method eq 'socks' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->can_use_socks and $preferred_method eq 'socks') { } 1 1 2
elsif ($self->can_use_http and $preferred_method eq 'http') { } 1 0 1
elsif ($self->can_use_socks) { } 0 0 0
elsif ($self->can_use_http) { } 0 0 0
Condition66%2/3
$self->can_use_socks and $preferred_method eq "socks"
!ll&&!rl&&r
011
MC/DC50%1/2
$self->can_use_socks and $preferred_method eq "socks"
$self->can_use_socks $preferred_method eq "socks"
Truth table66%2/3
A and B
A= $self->can_use_socksB= $preferred_method eq "socks"
ABresult
0X0
100
111
227 1 $method = 'socks';
228 }
229 ▶ elsif ( $self->can_use_http && $preferred_method eq 'http' ) {
Condition33%1/3
$self->can_use_http and $preferred_method eq "http"
!ll&&!rl&&r
001
MC/DC0%0/2
$self->can_use_http and $preferred_method eq "http"
$self->can_use_http $preferred_method eq "http"
Truth table33%1/3
A and B
A= $self->can_use_httpB= $preferred_method eq "http"
ABresult
0X0
100
111
230 1 $method = 'http';
231 }
232 elsif ( $self->can_use_socks ) {
233 0 $method = 'socks';
234 }
235 elsif ( $self->can_use_http ) {
236 0 $method = 'http';
237 }
238 else {
239 0 croak 'ERROR: Cannot find a valid method';
240 }
241  
242 2 return $method . q(://) . $self->get_host() . q(:) . $self->get_port();
243 }
244  
245 =head1 AUTHOR
246  
247 Julio de Castro, C<< <julio.dcs at gmail.com> >>
248  
249 =head1 BUGS
250  
251 Please report any bugs or feature requests to C<bug-geonode-free-proxylist at rt.cpan.org>, or through
252 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geonode-Free-ProxyList>.
253  
254 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
255  
256 =head1 SUPPORT
257  
258 You can find documentation for this module with the perldoc command.
259  
260 perldoc Geonode::Free::Proxy
261  
262  
263 You can also look for information at:
264  
265 =over 4
266  
267 =item * RT: CPAN's request tracker (report bugs here)
268  
269 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Geonode-Free-ProxyList>
270  
271 =item * CPAN Ratings
272  
273 L<https://cpanratings.perl.org/d/Geonode-Free-ProxyList>
274  
275 =item * Search CPAN
276  
277 L<https://metacpan.org/release/Geonode-Free-ProxyList>
278  
279 =back
280  
281  
282 =head1 ACKNOWLEDGEMENTS
283  
284  
285 =head1 LICENSE AND COPYRIGHT
286  
287 This software is Copyright (c) 2021 by Julio de Castro.
288  
289 This is free software, licensed under:
290  
291 The Artistic License 2.0 (GPL Compatible)
292  
293  
294 =cut
295  
296 3 1; # End of Geonode::Free::Proxy
↑ Summary ProxyList.pm »