line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SHARYANTO::HTTP::DetectUA::Simple; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
15477
|
use 5.010; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
323
|
|
4
|
|
|
|
|
|
|
require Exporter; |
5
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
6
|
|
|
|
|
|
|
our @EXPORT_OK = qw(detect_http_ua_simple); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.76'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our %SPEC; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$SPEC{":package"} = { |
13
|
|
|
|
|
|
|
v => 1.1, |
14
|
|
|
|
|
|
|
summary => 'A very simple and generic browser detection library', |
15
|
|
|
|
|
|
|
description => <<'_', |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
I needed a simple and fast routine which can detect whether HTTP client is a GUI |
18
|
|
|
|
|
|
|
browser (like Chrome or Firefox), a text browser (like Lynx or Links), or |
19
|
|
|
|
|
|
|
neither (like curl, or L). Hence, this module. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
_ |
22
|
|
|
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$SPEC{detect_http_ua_simple} = { |
25
|
|
|
|
|
|
|
v => 1.1, |
26
|
|
|
|
|
|
|
summary => 'Detect whether HTTP client is a GUI/TUI browser', |
27
|
|
|
|
|
|
|
description => <<'_', |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
This function is a simple and fast routine to detect whether HTTP client is a |
30
|
|
|
|
|
|
|
GUI browser (like Chrome or Firefox), a text-based browser (like Lynx or Links), |
31
|
|
|
|
|
|
|
or neither (like curl or LWP). Extra information can be provided in the future. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Currently these heuristic rules are used: |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
* check popular browser markers in User-Agent header (e.g. 'Chrome', 'Opera'); |
36
|
|
|
|
|
|
|
* check Accept header for 'image/'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
It is several times faster than the other equivalent Perl modules, this is |
39
|
|
|
|
|
|
|
because it does significantly less. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
_ |
42
|
|
|
|
|
|
|
args => { |
43
|
|
|
|
|
|
|
env => { |
44
|
|
|
|
|
|
|
pos => 0, |
45
|
|
|
|
|
|
|
summary => 'CGI-compatible environment, e.g. \%ENV or PSGI\'s $env', |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
result => { |
49
|
|
|
|
|
|
|
description => <<'_', |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
* 'is_gui_browser' key will be set to true if HTTP client is a GUI browser. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
* 'is_text_browser' key will be set to true if HTTP client is a text/TUI |
54
|
|
|
|
|
|
|
browser. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
* 'is_browser' key will be set to true if either 'is_gui_browser' or |
57
|
|
|
|
|
|
|
'is_text_browser' is set to true. |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
_ |
60
|
|
|
|
|
|
|
schema => 'hash*', |
61
|
|
|
|
|
|
|
}, |
62
|
|
|
|
|
|
|
links => [ |
63
|
|
|
|
|
|
|
{url => "pm://HTML::ParseBrowser", tags => ['see']}, |
64
|
|
|
|
|
|
|
{url => "pm://HTTP::BrowserDetect", tags => ['see']}, |
65
|
|
|
|
|
|
|
{url => "pm://HTTP::DetectUserAgent", tags => ['see']}, |
66
|
|
|
|
|
|
|
{url => "pm://Parse::HTTP::UserAgent", tags => ['see']}, |
67
|
|
|
|
|
|
|
{url => "pm://HTTP::headers::UserAgent", tags => ['see']}, |
68
|
|
|
|
|
|
|
], |
69
|
|
|
|
|
|
|
args_as => "array", |
70
|
|
|
|
|
|
|
result_naked => 0, |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub detect_http_ua_simple { |
74
|
19
|
|
|
19
|
1
|
30441
|
my ($env) = @_; |
75
|
19
|
|
|
|
|
33
|
my $res = {}; |
76
|
19
|
|
|
|
|
18
|
my $det; |
77
|
|
|
|
|
|
|
|
78
|
19
|
|
|
|
|
27
|
my $ua = $env->{HTTP_USER_AGENT}; |
79
|
19
|
100
|
|
|
|
40
|
if ($ua) { |
80
|
|
|
|
|
|
|
# check for popular browser GUI UA |
81
|
17
|
100
|
|
|
|
87
|
if ($ua =~ m!\b(?:Mozilla/|MSIE |Chrome/|Opera/| |
82
|
|
|
|
|
|
|
Profile/MIDP- |
83
|
|
|
|
|
|
|
)!x) { |
84
|
11
|
|
|
|
|
20
|
$res->{is_gui_browser} = 1; |
85
|
11
|
|
|
|
|
12
|
$det++; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
# check for popular webbot UA |
88
|
17
|
100
|
|
|
|
141
|
if ($ua =~ m!\b(?:Links |ELinks/|Lynx/|w3m/)!) { |
89
|
4
|
|
|
|
|
8
|
$res->{is_text_browser} = 1; |
90
|
4
|
|
|
|
|
5
|
$det++; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
19
|
100
|
|
|
|
35
|
if (!$det) { |
95
|
|
|
|
|
|
|
# check for accept mime type |
96
|
4
|
|
|
|
|
6
|
my $ac = $env->{HTTP_ACCEPT}; |
97
|
4
|
100
|
|
|
|
10
|
if ($ac) { |
98
|
2
|
100
|
|
|
|
12
|
if ($ac =~ m!\b(?:image/)!) { |
99
|
1
|
|
|
|
|
3
|
$res->{is_gui_browser} = 1; |
100
|
1
|
|
|
|
|
3
|
$det++; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
19
|
100
|
100
|
|
|
65
|
$res->{is_browser} = 1 if $res->{is_gui_browser} || $res->{is_text_browser}; |
106
|
19
|
|
|
|
|
41
|
$res; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
# ABSTRACT: A very simple and generic browser detection library |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |