Crawl4AI/Detect.pm

filter: Statementstmt 82.6% 43 / 52 Branchbran 79.4% 27 / 34 Conditioncond 69.5% 32 / 46 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 87.5% 7 / 8 Podpod 100% 5 / 5 totaltotal 73.3% 124 / 169 CC 36 SCAR 23.1
CC
36
Cov
77%
signals
12.2
_probe_ua
12.0
is_good
11.2
CRAP 10.1
« Crawl4AI/Client.pm ↑ Summary Crawl4AI/DeepCrawlIterator.pm »
1 package WWW::Crawl4AI::Detect;
2 # ABSTRACT: service detection and content-quality classification for Crawl4AI
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.001';
7  
8  
9 # Default: a result needs at least this many markdown characters to count.
10 3 our $MIN_MARKDOWN = 500;
11  
12 # HTTP status codes that mean "the target pushed back", not "transport broke".
13 3 my %SOFT_FAIL = map { $_ => 1 } ( 401, 403, 429 );
14  
15 # A WAF / bot-management gate (Cloudflare, DataDome, PerimeterX, Akamai) often
16 # does not embed a widget into the requested page -- it REDIRECTS to a dedicated
17 # challenge URL. reCAPTCHA / hCaptcha redirects land on the provider's own
18 # verification endpoint. We key purely on the final (post-redirect) URL's
19 # host+path matching a known challenge endpoint: a real content page's
20 # final_url never contains /cdn-cgi/challenge etc., so URL equality with the
21 # requested URL is irrelevant (and checking it would false-positive on cosmetic
22 # http->https / www<->apex / trailing-slash redirects).
23 3 my $RE_CHALLENGE_CAPTCHA = qr{
24 (?:www\.)?google\.com/recaptcha # reCAPTCHA verification endpoint
25 | /recaptcha/api # reCAPTCHA api2/anchor frame
26 | \bhcaptcha\.com\b # hCaptcha challenge host
27 }ix;
28 3 my $RE_CHALLENGE_WALL = qr{
29 /cdn-cgi/challenge # Cloudflare managed challenge
30 | __cf_chl # Cloudflare challenge query/path token
31 | /challenge-platform/ # Cloudflare challenge-platform asset
32 | datadome # DataDome (host or path)
33 | geo\.captcha-delivery\.com # DataDome captcha delivery host
34 | /px/captcha # PerimeterX captcha path
35 | perimeterx # PerimeterX (host or path)
36 }ix;
37  
38 #----------------------------------------------------------------------
39 # Content classification
40 #----------------------------------------------------------------------
41  
42 sub signals {
43 132 ▶ my ( $page, %opt ) = @_;
Subroutine signals: called CC=12 SCAR=25.0
Pod documented
44 132 ▶ my $min = defined $opt{min_markdown} ? $opt{min_markdown} : $MIN_MARKDOWN;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'min_markdown'} ? : 3 129 132
45 132 ▶ $page ||= {};
Condition50%1/2
$page ||= {}
l!l
1320
MC/DC0%0/1
$page ||= {}
$page
Truth table50%1/2
A
A= $page
Aresult
11
00
46 132 ▶ my $md = $page->{markdown} // '';
Condition50%1/2
$page->{"markdown"} // ""
l!l
1320
MC/DC0%0/1
$page->{"markdown"} // ""
$page->{"markdown"}
Truth table50%1/2
A
A= $page->{"markdown"}
Aresult
11
00
47 132 ▶ my $code = $page->{status_code} // 0;
Condition50%1/2
$page->{"status_code"} // 0
l!l
1320
MC/DC0%0/1
$page->{"status_code"} // 0
$page->{"status_code"}
Truth table50%1/2
A
A= $page->{"status_code"}
Aresult
11
00
48 # The post-redirect URL, falling back to the requested URL when the normalized
49 # page omits it. Either may be absent (signals() is also called on bare test
50 # hashes) -- when so, the challenge-URL checks below simply find no match and
51 # no signal is raised. Never warns/dies on missing keys.
52 132 ▶ my $final = $page->{final_url} // $page->{url} // '';
Condition80%4/5
($page->{"final_url"} // $page->{"url"}) // ""
l!l
6468
$page->{"final_url"} // $page->{"url"}
l!l&&r!l&&!r
64068
MC/DC50%1/2
($page->{"final_url"} // $page->{"url"}) // ""
$page->{"final_url"} $page->{"url"}
Truth table66%2/3
A // B
A= $page->{"final_url"}B= $page->{"url"}
ABresult
1X1
011
000
53  
54 # Content volume is the master signal. A bot-wall / JS-shell / captcha gate
55 # REPLACES the page content -- it is, by definition, thin. So a content-rich
56 # page (>= $min markdown chars) that came back 200 IS the scrape: nothing in
57 # its body text or <title> may discard it. The body/title phrase heuristics
58 # ($RE_BLOCK, $RE_JS, $RE_CAPTCHA body arms, $RE_WALL HTML-token, $RE_TITLE)
59 # were removed in 0.005: on a thin page they were redundant (thin_html already
60 # fails the page), and on a full page they were pure false-positives -- a
61 # 386 KB article carrying Cloudflare's passive __cf_ beacon, or a legit
62 # "Access Denied" <title>, was wrongly thrown away. The ONLY size-independent
63 # block signals kept are the fingerprints a real content page can never carry:
64 # an HTTP push-back status, or a redirect whose final_url is a known WAF /
65 # captcha challenge endpoint (the page physically left the origin).
66 132 my $thin = length($md) < $min;
67  
68 # 'blocked' / 'captcha' fire only when the post-redirect final_url is a known
69 # WAF / captcha challenge endpoint. Not HTTP status -- that lives on the
70 # http_error axis. A site that soft-blocks us by serving one identical
71 # interstitial for every URL (200, no redirect) is caught one level up, by the
72 # caller comparing markdown across the fetched pages -- not here, per-page.
73 132 ▶ my $blocked = ( $final =~ $RE_CHALLENGE_WALL ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$final =~ /$RE_CHALLENGE_WALL/ ? : 15 117 132
74 132 ▶ my $captcha = ( $final =~ $RE_CHALLENGE_CAPTCHA ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$final =~ /$RE_CHALLENGE_CAPTCHA/ ? : 3 129 132
75  
76 return {
77 blocked => $blocked,
78 captcha => $captcha,
79 thin_html => $thin ? 1 : 0,
80 132 ▶ http_error => ( $code >= 500 || $SOFT_FAIL{$code} ) ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
$thin ? : 43 89 132
$code >= 500 || $SOFT_FAIL{$code} ? : 2 130 132
Condition100%3/3
$code >= 500 or $SOFT_FAIL{$code}
l!l&&r!l&&!r
11130
MC/DC100%2/2
$code >= 500 or $SOFT_FAIL{$code}
$code >= 500 $SOFT_FAIL{$code}
Truth table100%3/3
A or B
A= $code >= 500B= $SOFT_FAIL{$code}
ABresult
1X1
011
000
81 };
82 }
83  
84  
85 sub is_good {
86 57 ▶ my ( $page, %opt ) = @_;
Subroutine is_good: called CC=11 SCAR=24.2
Pod documented
87 57 ▶ return 0 unless ref $page eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page eq "HASH" 0 57 57
88 57 ▶ return 0 if defined $page->{success} && !$page->{success};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"success"} and not $page->{"success"} 1 56 57
Condition100%3/3
defined $page->{"success"} and not $page->{"success"}
!ll&&!rl&&r
43131
MC/DC100%2/2
defined $page->{"success"} and not $page->{"success"}
defined $page->{"success"} not $page->{"success"}
Truth table100%3/3
A and not(B)
A= defined $page->{"success"}B= not $page->{"success"}
ABresult
0X0
100
111
89 56 ▶ my $code = $page->{status_code} // 0;
Condition50%1/2
$page->{"status_code"} // 0
l!l
560
MC/DC0%0/1
$page->{"status_code"} // 0
$page->{"status_code"}
Truth table50%1/2
A
A= $page->{"status_code"}
Aresult
11
00
90 56 ▶ return 0 if $code && ( $code >= 500 || $SOFT_FAIL{$code} );
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code >= 500 || $SOFT_FAIL{$code} 1 55 56
Condition66%4/6
$code and $code >= 500 || $SOFT_FAIL{$code}
!ll&&!rl&&r
0551
$code >= 500 or $SOFT_FAIL{$code}
l!l&&r!l&&!r
0155
MC/DC33%1/3
$code and $code >= 500 || $SOFT_FAIL{$code}
$code $code >= 500 $SOFT_FAIL{$code}
Truth table50%2/4
A and B or C
A= $codeB= $code >= 500C= $SOFT_FAIL{$code}
ABCresult
0XX0
1000
11X1
1011
91 55 my $sig = signals( $page, %opt );
92 55 ▶ return 0 if $sig->{blocked} || $sig->{captcha} || $sig->{thin_html};
Branch: 1 branch
BranchTrueFalseTotal
if $sig->{"blocked"} or $sig->{"captcha"} or $sig->{"thin_html"} 16 34 50
Condition100%6/6
$sig->{"blocked"} or $sig->{"captcha"} or $sig->{"thin_html"}
l!l&&r!l&&!r
61534
$sig->{"blocked"} or $sig->{"captcha"}
l!l&&r!l&&!r
5149
MC/DC100%3/3
$sig->{"blocked"} or $sig->{"captcha"} or $sig->{"thin_html"}
$sig->{"blocked"} $sig->{"captcha"} $sig->{"thin_html"}
Truth table100%4/4
A or B or C
A= $sig->{"blocked"}B= $sig->{"captcha"}C= $sig->{"thin_html"}
ABCresult
1XX1
01X1
0011
0000
93 34 return 1;
94 }
95  
96  
97 # Most specific reason first.
98 sub why_failed {
99 26 ▶ my ( $page, %opt ) = @_;
Subroutine why_failed: called CC=9 SCAR=22.1
Pod documented
100 26 ▶ return 'empty' unless ref $page eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page eq "HASH" 0 26 26
101 26 my $sig = signals( $page, %opt );
102 26 ▶ return 'captcha' if $sig->{captcha};
Branch: 1 branch
BranchTrueFalseTotal
if $sig->{"captcha"} 1 25 26
103 25 ▶ return 'bot_wall_detected' if $sig->{blocked};
Branch: 1 branch
BranchTrueFalseTotal
if $sig->{"blocked"} 5 20 25
104 20 ▶ my $code = $page->{status_code} // 0;
Condition50%1/2
$page->{"status_code"} // 0
l!l
200
MC/DC0%0/1
$page->{"status_code"} // 0
$page->{"status_code"}
Truth table50%1/2
A
A= $page->{"status_code"}
Aresult
11
00
105 20 ▶ return "http_$code" if $code && ( $code >= 500 || $SOFT_FAIL{$code} );
Branch: 1 branch
BranchTrueFalseTotal
if $code and $code >= 500 || $SOFT_FAIL{$code} 2 18 20
Condition83%5/6
$code and $code >= 500 || $SOFT_FAIL{$code}
!ll&&!rl&&r
0191
$code >= 500 or $SOFT_FAIL{$code}
l!l&&r!l&&!r
1118
MC/DC33%1/3
$code and $code >= 500 || $SOFT_FAIL{$code}
$code $code >= 500 $SOFT_FAIL{$code}
Truth table50%2/4
A and B or C
A= $codeB= $code >= 500C= $SOFT_FAIL{$code}
ABCresult
0XX0
1000
11X1
1011
106 18 ▶ return 'thin_content' if $sig->{thin_html};
Branch: 1 branch
BranchTrueFalseTotal
if $sig->{"thin_html"} 14 4 18
107 4 return undef;
108 }
109  
110  
111 #----------------------------------------------------------------------
112 # Service detection
113 #----------------------------------------------------------------------
114  
115 sub _probe_ua {
116 0 ▶ my ( $ua, $timeout ) = @_;
Subroutine _probe_ua: not called CC=3 SCAR=24.8
117 0 ▶ return $ua if $ua;
Branch: 1 branch
BranchTrueFalseTotal
if $ua 0 0 0
118 0 require LWP::UserAgent;
119 0 ▶ return LWP::UserAgent->new( agent => "WWW-Crawl4AI/$VERSION", timeout => ( $timeout // 5 ) );
Condition0%0/2
$timeout // 5
l!l
00
MC/DC0%0/1
$timeout // 5
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
120 }
121  
122 sub probe_cloakbrowser {
123 1 ▶ my ( $cdp_url, %opt ) = @_;
Subroutine probe_cloakbrowser: called CC=4 SCAR=22.9
Pod documented
124 1 ▶ return 0 unless defined $cdp_url && length $cdp_url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cdp_url and length $cdp_url 1 0 1
Condition33%1/3
defined $cdp_url and length $cdp_url
!ll&&!rl&&r
100
MC/DC0%0/2
defined $cdp_url and length $cdp_url
defined $cdp_url length $cdp_url
Truth table33%1/3
A and B
A= defined $cdp_urlB= length $cdp_url
ABresult
0X0
100
111
125 0 ( my $base = $cdp_url ) =~ s{/+$}{};
126 0 $base =~ s{\?.*$}{}; # strip CloakBrowser query params (fingerprint=...)
127 0 my $ua = _probe_ua( $opt{ua}, $opt{timeout} );
128 0 my $res = $ua->get( $base . '/json/version' );
129 0 ▶ return $res->is_success ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$res->is_success ? : 0 0 0
130 }
131  
132  
133 sub detect_proxy_env {
134 1 ▶ return $ENV{CRAWL4AI_PROXY_URL} || undef;
Subroutine detect_proxy_env: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$ENV{'CRAWL4AI_PROXY_URL'} || undef
l!l
10
MC/DC0%0/1
$ENV{'CRAWL4AI_PROXY_URL'} || undef
$ENV{'CRAWL4AI_PROXY_URL'}
Truth table50%1/2
A
A= $ENV{'CRAWL4AI_PROXY_URL'}
Aresult
11
00
135 }
136  
137  
138 3 1;
139  
140 __END__
« Crawl4AI/Client.pm ↑ Summary Crawl4AI/DeepCrawlIterator.pm »