Parser.pm

filter: Statementstmt 30.4% 75 / 246 Branchbran 11.9% 10 / 84 Conditioncond 16.6% 6 / 36 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 55.1% 16 / 29 Podpod 40.0% 6 / 15 totaltotal 26.1% 113 / 433 CC 62 SCAR 46.3
CC
62
Cov
25%
_loadDB
347.5
_parse
132.0
new
31.7
CRAP 102.4
↑ Summary Browser.pm »
1 package HTTP::UserAgentString::Parser;
2  
3 =head1 NAME
4  
5 HTTP::UserAgentStringParser - User-Agent string parser
6  
7 =head1 SYNOPSIS
8  
9 my $p = HTTP::UserAgentString::Parser->new();
10 my $ua = $p->parse("Opera/9.80 (X11; Linux x86_64; U; en) Presto/2.9.168 Version/11.50");
11  
12 if ($ua->isRobot) {
13 print "It's a robot: ", $ua->name, "\n";
14 } else {
15 print "It's a browser: ", $ua->name, " - version: ", $ua->version, "\n";
16 }
17  
18 =head1 DESCRIPTION
19  
20 C<HTTP::UserAgentString::Parser> is a Perl API for user-agent-string.info. It
21 can be used to parse user agent strings and determine whether the agent is a robot,
22 a normal browser, mobile browser, e-mail client. It can also tell browser version,
23 company that makes it, home page URL. In most of the cases it can also tell in which
24 OS the browser is running.
25  
26 HTTP::UserAgentString::Parser will download the .ini file provided by user-agent-string.info
27 which contains all the information to do the parsing. The file will be cached by default
28 for 7 days. After that time, it will check whether a new version was released. The
29 default cache time can be modified, as well as the cache path (default is /tmp). A
30 cache reload can also be forced.
31  
32 In order to parse a string, a parse() method is provided which returns an object
33 of classes HTTP::UserAgentString::Browser or HTTP::UserAgentString::Robot. Both classes
34 have accesors to determine agent capabilities. In case the string does not match any known
35 browser or robot, undef() is returned.
36  
37 =head1 CONSTRUCTOR
38  
39 $p = HTTP::UserAgentString::Parser->new(%opts)
40  
41 Valid options are:
42  
43 cache_max_age: in seconds (default is 7 days)
44 cache_dir: path must be writeable - default is /tmp
45 parse_cache_size: size of parsing cache in number of elements. Default is 100_000
46  
47 =head1 METHODS
48  
49 =over 4
50  
51 =item $agent = $p->parse($string)
52  
53 Parses a User-Agent string and returns a HTTP::UserAgentString::Browser or
54 HTTP::UserAgentString::Robot object, or undef() if no matches where found.
55  
56 =item $p->updateDB($force)
57  
58 Updates the cache file from user-agent-string.info. If force is false or undef(), the
59 check is only executed if the cache file has expired. If force is true, the method
60 checks whether there is a new file and downloads it accordingly.
61  
62 =item $p->getCurrentVersion()
63  
64 Retrieves the current database version from user-agent-string.info. Returns the version
65 number or undef() if an error occurs.
66  
67 =item $p->getCachedVersion()
68  
69 Returns the version of the cached .ini file, or undef() if there is no cached file.
70  
71 =item $p->cache_file()
72  
73 Local path to the cached .ini file.
74  
75 =item $p->version_file()
76  
77 Local path to file that contains the version of the cached .ini file.
78  
79 =back
80  
81 =head1 SEE ALSO
82  
83 See L<HTTP::UserAgentString::Browser> and L<HTTP::UserAgentString::Robot> for description
84 of the objects returned by parse().
85  
86 =head1 COPYRIGHT
87  
88 Copyright (c) 2011 Nicolas Moldavsky (http://www.e-planning.net/)
89 This is free software. You can redistribute it or modify it under the terms of the
90 Perl license
91  
92 =cut
93  
94 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
96 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 4 ▶use HTTP::UserAgentString::Browser;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 4 ▶use HTTP::UserAgentString::Robot;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 4 ▶use HTTP::UserAgentString::OS;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 4 our $VERSION = '0.6.1';
104  
105 4 my @REQUIRED_SECS = qw(robots os browser browser_type browser_reg browser_os os_reg);
106  
107 4 my $REGEX_SECS = { 'browser_reg' => 1, 'os_reg' => 1 };
108  
109 4 my $INI_URL = 'http://user-agent-string.info/rpc/get_data.php?key=free&format=ini';
110 4 my $VER_URL = 'http://user-agent-string.info/rpc/get_data.php?key=free&format=ini&ver=y';
111 4 my $MD5_URL = 'http://user-agent-string.info/rpc/get_data.php?format=ini&md5=y';
112  
113 4 my $DEFAULT_CACHE_DIR = '/tmp';
114 4 my $DEFAULT_CACHE_MAX_AGE = 7 * 86400;
115  
116 4 my $DEFAULT_PARSE_CACHE_SIZE = 100000;
117  
118 4 my $INI_FILE = 'uas.ini';
119 4 my $VER_FILE = 'uas.version';
120  
121 12 ▶sub cache_dir($) { $_[0]->{cache_dir} }
Subroutine cache_dir: called CC=1 SCAR=0.0
Pod undocumented
122 0 ▶sub parse_cache_count($) { $_[0]->{parse_cache_count} }
Subroutine parse_cache_count: not called CC=1 SCAR=6.9
Pod undocumented
123 0 ▶sub cache_max_age($) { $_[0]->{cache_max_age} }
Subroutine cache_max_age: not called CC=1 SCAR=6.9
Pod undocumented
124  
125 8 ▶sub cache_file($) {
Subroutine cache_file: called CC=1 SCAR=0.0
Pod documented
126 8 my $self = shift;
127 8 return File::Spec->catfile($self->cache_dir, $INI_FILE);
128 }
129 4 ▶sub version_file($) {
Subroutine version_file: called CC=1 SCAR=0.0
Pod documented
130 4 my $self = shift;
131 4 return File::Spec->catfile($self->cache_dir, $VER_FILE);
132 }
133  
134 4 ▶sub getCurrentVersion($) {
Subroutine getCurrentVersion: called CC=2 SCAR=7.1
Pod documented
135 4 my $self = shift;
136 4 my $lwp = LWP::UserAgent->new();
137 4 $lwp->env_proxy();
138 4 my $res = $lwp->get($VER_URL);
139 4 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 4 4
140 0 return $res->content;
141 } else {
142 4 Carp::carp( "Can't get current file version from $VER_URL: " . $res->status_line . "\n");
143 4 return undef();
144 }
145 }
146  
147 4 ▶sub getCachedVersion($) {
Subroutine getCachedVersion: called CC=3 SCAR=16.8
Pod documented
148 4 my $self = shift;
149 4 my $path = $self->version_file;
150 4 ▶ if (-f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) { } 0 4 4
151 0 ▶ if (open(my $fh, "<", $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $path) { } 0 0 0
152 0 my $version = <$fh>;
153 0 close($fh);
154 0 return $version;
155 } else {
156 0 Carp::carp("Can't open $path: $!\n");
157 0 return undef();
158 }
159 } else {
160 4 return undef();
161 }
162 }
163  
164  
165 0 ▶sub _writeCacheFile($$$) {
Subroutine _writeCacheFile: not called CC=4 SCAR=30.0
166 0 my ($self, $filename, $content) = @_;
167  
168 0 ▶ if (open(my $fh, ">", $filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $filename) { } 0 0 0
169 0 ▶ if (print $fh $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (print $fh $content) { } 0 0 0
170 0 ▶ if (close($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (close $fh) { } 0 0 0
171 0 return 1;
172 } else {
173 0 Carp::carp("Can't close $filename: $!\n");
174 0 return 0;
175 }
176 } else {
177 0 Carp::carp("Can't write to $filename: $!\n");
178 0 return 0;
179 }
180 } else {
181 0 Carp::carp("Can't open $filename for writing: $!\n");
182 0 return 0;
183 }
184 }
185  
186 0 ▶sub _updateCache($$$) {
Subroutine _updateCache: not called CC=2 SCAR=17.9
187 0 my ($self, $inidata, $version) = @_;
188  
189 0 ▶ return ($self->_writeCacheFile($self->cache_file, $inidata) and $self->_writeCacheFile($self->version_file, $version));
Condition0%0/3
$self->_writeCacheFile($self->cache_file, $inidata) && $self->_writeCacheFile($self->version_file, $version)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_writeCacheFile($self->cache_file, $inidata) && $self->_writeCacheFile($self->version_file, $version)
$self->_writeCacheFile($self->cache_file, $inidata) $self->_writeCacheFile($self->version_file, $version)
Truth table0%0/3
A && B
A= $self->_writeCacheFile($self->cache_file, $inidata)B= $self->_writeCacheFile($self->version_file, $version)
ABresult
0X0
100
111
190 }
191  
192 0 ▶sub _downloadDB($$) {
Subroutine _downloadDB: not called CC=4 SCAR=30.0
193 0 my ($self, $current_version) = @_;
194 0 my $lwp = LWP::UserAgent->new();
195 0 $lwp->env_proxy();
196 0 my $res_ini = $lwp->get($INI_URL);
197 0 ▶ if ($res_ini->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_ini->is_success) { } 0 0 0
198 0 my $inidata = $res_ini->content;
199 0 my $res_md5 = $lwp->get($MD5_URL);
200 0 ▶ if ($res_md5->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_md5->is_success) { } 0 0 0
201 0 my $expected_hash = $res_md5->content;
202 0 my $ctx = Digest::MD5->new();
203 0 $ctx->add($inidata);
204 0 my $hash = $ctx->hexdigest();
205 0 ▶ if ($hash eq $expected_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash eq $expected_hash) { } 0 0 0
206 # Write files to disk
207 0 return $self->_updateCache($inidata, $current_version);
208 } else {
209 0 Carp::carp("MD5 digest does not match - expected=$expected_hash; calculate=$hash\n");
210 0 return 0;
211 }
212 } else {
213 0 Carp::carp("Can't get MD5 from $MD5_URL: " . $res_md5->status_line . "\n");
214 0 return 0;
215 }
216 } else {
217 0 Carp::carp("Can't get .ini from $INI_URL: " . $res_ini->status_line . "\n");
218 0 return 0;
219 }
220 }
221  
222 4 ▶sub updateDB($;$) {
Subroutine updateDB: called CC=8 SCAR=30.3
Pod documented
223 4 my ($self, $force) = @_;
224  
225 # Check if cache file needs to be updated according to max_age
226  
227 4 my $cache_file = $self->cache_file;
228  
229 4 my $do_check;
230 4 ▶ if (! -f $cache_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $cache_file) { } 4 0 4
231 4 $do_check = 1;
232 } else {
233 0 my @stat = stat($cache_file);
234 0 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) { } 0 0 0
235 0 my $mtime = $stat[9];
236 0 my $limit = time() - $self->cache_max_age;
237 0 $do_check = ($mtime < $limit);
238 } else {
239 0 Carp::carp("Can't stat() $cache_file: $!\n");
240 0 return undef();
241 }
242 }
243  
244 4 ▶ if ($do_check or $force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_check or $force) { } 4 0 4
Condition33%1/3
$do_check or $force
l!l&&r!l&&!r
400
MC/DC0%0/2
$do_check or $force
$do_check $force
Truth table33%1/3
A or B
A= $do_checkB= $force
ABresult
1X1
011
000
245 4 my $current_version = $self->getCurrentVersion();
246 4 my $cache_version = $self->getCachedVersion();
247 4 ▶ if (defined($current_version) and ((! defined($cache_version)) or ($current_version gt $cache_version))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_version and !defined($cache_version) || $current_version gt $cache_version) { } 0 4 4
Condition16%1/6
defined $current_version and !defined($cache_version) || $current_version gt $cache_version
!ll&&!rl&&r
400
not defined $cache_version or $current_version gt $cache_version
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $current_version and !defined($cache_version) || $current_version gt $cache_version
defined $current_version not defined $cache_version $current_version gt $cache_version
Truth table25%1/4
A and not(B) or C
A= defined $current_versionB= not defined $cache_versionC= $current_version gt $cache_version
ABCresult
0XX0
1000
11X1
1011
248 0 return $self->_downloadDB($current_version);
249 } else {
250 4 return -1;
251 }
252 } else {
253 0 return -1;
254 }
255 }
256  
257  
258 0 ▶sub _compileRegexes($$) {
Subroutine _compileRegexes: not called CC=3 SCAR=24.8
259 0 my ($self, $regexes) = @_;
260  
261 0 foreach my $ir (@$regexes) {
262 0 my $r = $ir->[0];
263 0 my $regex = eval "qr" . $r;
264 0 ▶ if (defined($regex)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regex) { } 0 0 0
265 0 $ir->[2] = $r;
266 0 $ir->[0] = $regex;
267 } else {
268 0 Carp::carp("Invalid regex: " . $ir->[0] . "($@)\n");
269 0 return 0;
270 }
271 }
272  
273 0 return 1;
274 }
275  
276 4 ▶sub _loadDB($) {
Subroutine _loadDB: called CC=20 SCAR=58.5
277 4 my $self = shift;
278 4 my $file = $self->cache_file;
279 4 ▶ if (open(my $fh, "<", $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<', $file) { } 0 4 4
280 0 my $cursec;
281 0 my $nline = 1;
282 0 my $lastvalues;
283 my $lastid;
284 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
285 0 $nline++;
286 0 ▶ next if (/^;/);
Branch: 1 branch
BranchTrueFalseTotal
if /^;/ 0 0 0
287 0 chop;
288 0 ▶ if (/^\[([\w_]+)\]\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\[([\w_]+)\]\s*$/) { } 0 0 0
elsif (/^(\d+)\[\] = "(.*)"\s*$/) { } 0 0 0
289 0 ▶ if (defined($lastvalues)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastvalues) 0 0 0
290 0 push(@{$self->{$cursec}}, $lastvalues);
291 }
292 0 $cursec = $1;
293 0 $lastid = undef();
294 0 $lastvalues = undef();
295 } elsif (/^(\d+)\[\] = "(.*)"\s*$/) {
296 0 my ($id, $value) = ($1, $2);
297 0 ▶ if ($REGEX_SECS->{$cursec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($REGEX_SECS->{$cursec}) { } 0 0 0
298 0 ▶ if (defined($lastid) and ($id == $lastid)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastid and $id == $lastid) { } 0 0 0
Condition0%0/3
defined $lastid and $id == $lastid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $lastid and $id == $lastid
defined $lastid $id == $lastid
Truth table0%0/3
A and B
A= defined $lastidB= $id == $lastid
ABresult
0X0
100
111
299 0 push(@$lastvalues, $value);
300 } else {
301 0 ▶ push(@{$self->{$cursec}}, $lastvalues) if (defined($lastid));
Branch: 1 branch
BranchTrueFalseTotal
if defined $lastid 0 0 0
302 0 $lastid = $id;
303 0 $lastvalues = [ $value ];
304 }
305 } else {
306 0 push(@{$self->{$cursec}[$id]}, $value);
307 }
308 } else {
309 0 Carp::carp("Invalid format in line $nline: $_\n");
310 0 return 0;
311 }
312 }
313 0 ▶ if (defined($lastvalues)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastvalues) 0 0 0
314 0 push(@{$self->{$cursec}}, $lastvalues);
315 }
316 0 close($fh);
317  
318 # Check that we have all required sections
319 0 foreach my $sec (@REQUIRED_SECS) {
320 0 my $a = $self->{$sec};
321 0 ▶ if (! defined($a) or (! @$a)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a and @$a) 0 0 0
Condition0%0/3
defined $a and @$a
!ll&&!rl&&r
000
MC/DC0%0/2
defined $a and @$a
defined $a @$a
Truth table0%0/3
A and B
A= defined $aB= @$a
ABresult
0X0
100
111
322 0 Carp::carp("Section $a is not present in $file");
323 0 return 0;
324 }
325 }
326  
327 # Compile regexes
328 0 foreach my $key (keys %$REGEX_SECS) {
329 0 ▶ $self->_compileRegexes($self->{$key}) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_compileRegexes($self->{$key}) or return 0 0 0 0
330 }
331  
332  
333 # Index for robots
334 0 $self->{robot_index} = {};
335 0 my @r;
336 0 foreach my $robot (grep { defined($_) } @{$self->{robots}}) {
337 0 my $os_id = $robot->[7];
338 0 my $os;
339 0 ▶ if ($os_id and defined($self->{os}[$os_id])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($os_id and defined $self->{"os"}[$os_id]) 0 0 0
Condition0%0/3
$os_id and defined $self->{"os"}[$os_id]
!ll&&!rl&&r
000
MC/DC0%0/2
$os_id and defined $self->{"os"}[$os_id]
$os_id defined $self->{"os"}[$os_id]
Truth table0%0/3
A and B
A= $os_idB= defined $self->{"os"}[$os_id]
ABresult
0X0
100
111
340 0 $os = HTTP::UserAgentString::OS->new($self->{os}[$os_id]);
341 }
342 0 my $bot = HTTP::UserAgentString::Robot->new($robot, $os);
343 0 push(@r, $bot);
344 0 $self->{robot_index}{$robot->[0]} = $bot;
345 }
346 0 $self->{robots} = \@r;
347  
348 0 $self->{parse_cache} = {};
349 0 $self->{parse_cache_count} = 0;
350  
351 0 return 1;
352 } else {
353 4 Carp::carp("Can't open $file for reading: $!\n");
354 4 return 0;
355 }
356 }
357  
358 4 ▶sub new($;%) {
Subroutine new: called CC=11 SCAR=34.6
Pod undocumented
359 4 my ($pkg, %opts) = @_;
360  
361 4 foreach my $key (qw(cache_max_age parse_cache_size)) {
362 8 my $val = $opts{$key};
363 8 ▶ if (defined($val) and ($val !~ /^\d+$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and not $val =~ /^\d+$/) 0 8 8
Condition33%1/3
defined $val and not $val =~ /^\d+$/
!ll&&!rl&&r
800
MC/DC0%0/2
defined $val and not $val =~ /^\d+$/
defined $val not $val =~ /^\d+$/
Truth table33%1/3
A and not(B)
A= defined $valB= not $val =~ /^\d+$/
ABresult
0X0
100
111
364 0 Carp::carp("$key must be an integer!\n");
365 0 return undef();
366 }
367 }
368
369 4 ▶ if ($opts{cache_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"cache_dir"}) 0 4 4
370 0 ▶ if (! -d $opts{cache_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opts{"cache_dir"}) 0 0 0
371 0 Carp::carp($opts{cache_dir} . " is not a valid directory: $!");
372 0 return undef();
373 }
374 }
375  
376 my $self = bless({
377 cache_dir => $opts{cache_dir} || $DEFAULT_CACHE_DIR,
378 cache_max_age => $opts{cache_max_age} || $DEFAULT_CACHE_MAX_AGE,
379 4 ▶ parse_cache_size => defined($opts{parse_cache_size}) ? $opts{parse_cache_size} : $DEFAULT_PARSE_CACHE_SIZE
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'parse_cache_size'} ? : 0 4 4
Condition33%2/6
$opts{'cache_dir'} || $DEFAULT_CACHE_DIR
l!l&&r!l&&!r
040
$opts{'cache_max_age'} || $DEFAULT_CACHE_MAX_AGE
l!l&&r!l&&!r
040
MC/DC0%0/2
$opts{'cache_dir'} || $DEFAULT_CACHE_DIR
$opts{'cache_dir'} $DEFAULT_CACHE_DIR
MC/DC0%0/2
$opts{'cache_max_age'} || $DEFAULT_CACHE_MAX_AGE
$opts{'cache_max_age'} $DEFAULT_CACHE_MAX_AGE
Truth table33%1/3
A || B
A= $opts{'cache_dir'}B= $DEFAULT_CACHE_DIR
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $opts{'cache_max_age'}B= $DEFAULT_CACHE_MAX_AGE
ABresult
1X1
011
000
380 }, $pkg);
381  
382 4 ▶ if ($self->updateDB and $self->_loadDB()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->updateDB and $self->_loadDB) { } 0 4 4
Condition33%1/3
$self->updateDB and $self->_loadDB
!ll&&!rl&&r
040
MC/DC0%0/2
$self->updateDB and $self->_loadDB
$self->updateDB $self->_loadDB
Truth table33%1/3
A and B
A= $self->updateDBB= $self->_loadDB
ABresult
0X0
100
111
383 0 return $self;
384 } else {
385 4 return undef();
386 }
387 }
388  
389 0 ▶sub robots($) { $_[0]->{robots} }
Subroutine robots: not called CC=1 SCAR=6.9
Pod undocumented
390 0 ▶sub browser_reg($) { $_[0]->{browser_reg} }
Subroutine browser_reg: not called CC=1 SCAR=6.9
Pod undocumented
391 0 ▶sub os_reg($) { $_[0]->{os_reg} }
Subroutine os_reg: not called CC=1 SCAR=6.9
Pod undocumented
392  
393 0 ▶sub getBrowser($$) {
Subroutine getBrowser: not called CC=2 SCAR=17.9
Pod undocumented
394 0 my ($self, $browser_id) = @_;
395 0 my $bos = $self->{browser_os}[$browser_id];
396 0 my $os;
397 0 ▶ $os = $self->getOS($bos->[0]) if (defined($bos));
Branch: 1 branch
BranchTrueFalseTotal
if defined $bos 0 0 0
398 0 return HTTP::UserAgentString::Browser->new($self->{browser}[$browser_id], "", "", $os);
399 }
400  
401 0 ▶sub getOS($$) {
Subroutine getOS: not called CC=1 SCAR=6.9
Pod undocumented
402 0 my ($self, $os_id) = @_;
403 0 return HTTP::UserAgentString::OS->new($self->{os}[$os_id]);
404 }
405  
406 # Real parsing with no cache checking
407 0 ▶sub _parse($$) {
Subroutine _parse: not called CC=11 SCAR=48.8
408 0 my ($self, $string) = @_;
409  
410 # First we check whether it is a robot
411 0 ▶ if (defined(my $robot = $self->{robot_index}{$string})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $robot = $self->{"robot_index"}{$string})) 0 0 0
412 0 return $robot;
413 }
414  
415 # Now we check browser regexes
416 0 my $idx = 0;
417 0 foreach my $br (grep { defined($_) } @{$self->{browser_reg}}) {
418 0 my ($regex, $browser_id) = @$br;
419 0 ▶ if ($string =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regex/) 0 0 0
420 0 my $version = $1;
421 0 my $browser = $self->{browser}[$browser_id];
422 0 my $typeDesc;
423 0 my $type = $browser->[0];
424 0 ▶ if (defined($self->{browser_type}[$type])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"browser_type"}[$type]) 0 0 0
425 0 $typeDesc = $self->{browser_type}[$type][0];
426 }
427 0 my $bos = $self->{browser_os}[$browser_id];
428 0 my $os_id;
429 0 ▶ $os_id = $bos->[0] if (defined($bos));
Branch: 1 branch
BranchTrueFalseTotal
if defined $bos 0 0 0
430 0 my $os;
431  
432 0 ▶ if (! defined($os_id)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $os_id) 0 0 0
433 # Use regexes to search lookup OS
434 0 OS: foreach my $or (grep { defined($_) } @{$self->{os_reg}}) {
435 0 my ($osregex, $id) = @$or;
436 0 ▶ if ($string =~ $osregex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$osregex/) 0 0 0
437 0 $os_id = $id;
438 0 last OS;
439 }
440 }
441 }
442  
443 0 ▶ if (defined($os_id) and defined($self->{os}[$os_id])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $os_id and defined $self->{"os"}[$os_id]) 0 0 0
Condition0%0/3
defined $os_id and defined $self->{"os"}[$os_id]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $os_id and defined $self->{"os"}[$os_id]
defined $os_id defined $self->{"os"}[$os_id]
Truth table0%0/3
A and B
A= defined $os_idB= defined $self->{"os"}[$os_id]
ABresult
0X0
100
111
444 0 $os = HTTP::UserAgentString::OS->new($self->{os}[$os_id]);
445 }
446
447 0 return HTTP::UserAgentString::Browser->new($browser, $typeDesc, $version, $os);
448 }
449 0 $idx++;
450 }
451  
452 0 return undef();
453 }
454  
455 0 ▶sub parse($$) {
Subroutine parse: not called CC=3 SCAR=24.8
Pod documented
456 0 my ($self, $string) = @_;
457  
458 0 my $obj;
459 0 ▶ if (exists $self->{parse_cache}{$string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'parse_cache'}{$string}) { } 0 0 0
460 0 $obj = $self->{parse_cache}{$string};
461 } else {
462 0 $obj = $self->_parse($string);
463 # Cache it if we have enough space
464 0 ▶ if ($self->{parse_cache_count} < $self->{parse_cache_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parse_cache_count"} < $self->{"parse_cache_size"}) 0 0 0
465 0 $self->{parse_cache_count}++;
466 0 $self->{parse_cache}{$string} = $obj;
467 }
468 }
469  
470 0 return $obj;
471 }
472  
473 4 1;
474  
↑ Summary Browser.pm »