Crawl4AI/Strategy/CloakBrowser.pm

filter: Statementstmt 90.3% 28 / 31 Branchbran 100% 4 / 4 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 77.7% 7 / 9 Podpod 75.0% 3 / 4 totaltotal 84.3% 43 / 51 CC 5 SCAR 6.5
CC
5
Cov
89%
_cdp_url
3.0
cost_class
2.0
applicable
2.0
CRAP 1.9
« Crawl4AI/Request.pm ↑ Summary Crawl4AI/Strategy.pm »
1 package WWW::Crawl4AI::Strategy::CloakBrowser;
2 # ABSTRACT: Crawl4AI strategy attaching to CloakBrowser over CDP
3 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use WWW::Crawl4AI::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 with 'WWW::Crawl4AI::Strategy';
7  
8 6 our $VERSION = '0.001';
9  
10  
11 11 ▶sub name { 'crawl4ai_cloakbrowser' }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
12  
13  
14 0 ▶sub cost_class { 'stealth' }
Subroutine cost_class: not called CC=1 SCAR=6.9
Pod documented
15  
16  
17 9 ▶sub applicable {
Subroutine applicable: called CC=2 SCAR=6.9
Pod documented
18 9 my ( $self, $crawler ) = @_;
19 9 ▶ return $crawler->cloakbrowser_url ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$crawler->cloakbrowser_url ? : 1 8 9
20 }
21  
22  
23 0 ▶sub build_request {
Subroutine build_request: not called CC=1 SCAR=6.9
Pod undocumented
24 0 my ( $self, $crawler, $url ) = @_;
25 0 return $self->_request(
26 $url,
27 browser => {
28 browser_mode => 'custom',
29 cdp_url => $self->_cdp_url( $crawler, $url ),
30 cache_cdp_connection => WWW::Crawl4AI::Request::JSON_true(),
31 create_isolated_context => WWW::Crawl4AI::Request::JSON_true(),
32 },
33 crawler => { wait_until => 'networkidle' },
34 );
35 }
36  
37 # Per-domain stable fingerprint: if the configured CDP URL has no query string,
38 # append ?fingerprint=<seed> so each domain gets a consistent CloakBrowser
39 # identity. CloakBrowser requires the seed to be a NON-NEGATIVE INTEGER — a
40 # non-numeric value (e.g. a raw host string) is rejected with HTTP 400. So the
41 # host is folded into a deterministic 32-bit hash: same domain → same seed,
42 # different domains → different seeds. A URL that already carries query params
43 # is used verbatim.
44 6 ▶sub _cdp_url {
Subroutine _cdp_url: called CC=3 SCAR=11.0
45 6 my ( $self, $crawler, $url ) = @_;
46 6 my $cdp = $crawler->cloakbrowser_url;
47 6 ▶ return $cdp if $cdp =~ /\?/;
Branch: 1 branch
BranchTrueFalseTotal
if $cdp =~ /\?/ 1 5 6
48 5 ▶ my $host = eval { URI->new($url)->host } || 'default';
Condition50%1/2
eval { do { 'URI'->new($url)->host } } || 'default'
l!l
50
MC/DC0%0/1
eval { do { 'URI'->new($url)->host } } || 'default'
eval { do { 'URI'->new($url)->host } }
Truth table50%1/2
A
A= eval { do { 'URI'->new($url)->host } }
Aresult
11
00
49 5 ( my $base = $cdp ) =~ s{/+$}{};
50 5 return "$base?fingerprint=" . $self->_fingerprint_seed($host);
51 }
52  
53 # Deterministic 32-bit FNV-1a hash of the host. Zero-dependency, stable across
54 # runs and processes, yields a non-negative integer CloakBrowser accepts.
55 8 ▶sub _fingerprint_seed {
Subroutine _fingerprint_seed: called CC=2 SCAR=6.9
56 8 my ( $self, $host ) = @_;
57 8 my $hash = 2166136261; # FNV-1a 32-bit offset basis
58 8 for my $byte ( unpack 'C*', $host ) {
59 112 $hash ^= $byte;
60 112 $hash = ( $hash * 16777619 ) & 0xFFFFFFFF; # FNV prime, wrap to 32 bits
61 }
62 8 return $hash;
63 }
64  
65 6 1;
66  
67 __END__
« Crawl4AI/Request.pm ↑ Summary Crawl4AI/Strategy.pm »