line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SHARYANTO::HTTP::DetectUA::Simple; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19239
|
use 5.010; |
|
1
|
|
|
|
|
4
|
|
4
|
|
|
|
|
|
|
require Exporter; |
5
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
6
|
|
|
|
|
|
|
our @EXPORT_OK = qw(detect_http_ua_simple); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.77'; # 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<LWP>). 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"}, |
64
|
|
|
|
|
|
|
{url => "pm:HTTP::BrowserDetect"}, |
65
|
|
|
|
|
|
|
{url => "pm:HTTP::DetectUserAgent"}, |
66
|
|
|
|
|
|
|
{url => "pm:Parse::HTTP::UserAgent"}, |
67
|
|
|
|
|
|
|
{url => "pm:HTTP::headers::UserAgent"}, |
68
|
|
|
|
|
|
|
], |
69
|
|
|
|
|
|
|
args_as => "array", |
70
|
|
|
|
|
|
|
result_naked => 0, |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub detect_http_ua_simple { |
74
|
19
|
|
|
19
|
1
|
44147
|
my ($env) = @_; |
75
|
19
|
|
|
|
|
32
|
my $res = {}; |
76
|
19
|
|
|
|
|
23
|
my $det; |
77
|
|
|
|
|
|
|
|
78
|
19
|
|
|
|
|
29
|
my $ua = $env->{HTTP_USER_AGENT}; |
79
|
19
|
100
|
|
|
|
44
|
if ($ua) { |
80
|
|
|
|
|
|
|
# check for popular browser GUI UA |
81
|
17
|
100
|
|
|
|
91
|
if ($ua =~ m!\b(?:Mozilla/|MSIE |Chrome/|Opera/| |
82
|
|
|
|
|
|
|
Profile/MIDP- |
83
|
|
|
|
|
|
|
)!x) { |
84
|
11
|
|
|
|
|
21
|
$res->{is_gui_browser} = 1; |
85
|
11
|
|
|
|
|
15
|
$det++; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
# check for popular webbot UA |
88
|
17
|
100
|
|
|
|
95
|
if ($ua =~ m!\b(?:Links |ELinks/|Lynx/|w3m/)!) { |
89
|
4
|
|
|
|
|
9
|
$res->{is_text_browser} = 1; |
90
|
4
|
|
|
|
|
6
|
$det++; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
19
|
100
|
|
|
|
40
|
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
|
|
|
|
9
|
if ($ac =~ m!\b(?:image/)!) { |
99
|
1
|
|
|
|
|
2
|
$res->{is_gui_browser} = 1; |
100
|
1
|
|
|
|
|
2
|
$det++; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
19
|
100
|
66
|
|
|
60
|
$res->{is_browser} = 1 if $res->{is_gui_browser} || $res->{is_text_browser}; |
106
|
19
|
|
|
|
|
44
|
$res; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
# ABSTRACT: A very simple and generic browser detection library |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
__END__ |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=pod |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=encoding UTF-8 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 NAME |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
SHARYANTO::HTTP::DetectUA::Simple - A very simple and generic browser detection library |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 VERSION |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This document describes version 0.77 of SHARYANTO::HTTP::DetectUA::Simple (from Perl distribution SHARYANTO-Utils), released on 2015-09-04. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SEE ALSO |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
L<SHARYANTO> |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 HOMEPAGE |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/SHARYANTO-Utils>. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 SOURCE |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-SHARYANTO-Utils>. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 BUGS |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=SHARYANTO-Utils> |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
143
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
144
|
|
|
|
|
|
|
feature. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 AUTHOR |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
This software is copyright (c) 2015 by perlancar@cpan.org. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
155
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 DESCRIPTION |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
I needed a simple and fast routine which can detect whether HTTP client is a GUI |
161
|
|
|
|
|
|
|
browser (like Chrome or Firefox), a text browser (like Lynx or Links), or |
162
|
|
|
|
|
|
|
neither (like curl, or L<LWP>). Hence, this module. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 FUNCTIONS |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 detect_http_ua_simple($env) -> [status, msg, result, meta] |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Detect whether HTTP client is a GUI/TUI browser. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
This function is a simple and fast routine to detect whether HTTP client is a |
172
|
|
|
|
|
|
|
GUI browser (like Chrome or Firefox), a text-based browser (like Lynx or Links), |
173
|
|
|
|
|
|
|
or neither (like curl or LWP). Extra information can be provided in the future. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Currently these heuristic rules are used: |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=over |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item * check popular browser markers in User-Agent header (e.g. 'Chrome', 'Opera'); |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=item * check Accept header for 'image/'; |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=back |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
It is several times faster than the other equivalent Perl modules, this is |
186
|
|
|
|
|
|
|
because it does significantly less. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=over 4 |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item * B<env> => I<any> |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
CGI-compatible environment, e.g. \%ENV or PSGI's $env. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=back |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
Returns an enveloped result (an array). |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
First element (status) is an integer containing HTTP status code |
201
|
|
|
|
|
|
|
(200 means OK, 4xx caller error, 5xx function error). Second element |
202
|
|
|
|
|
|
|
(msg) is a string containing error message, or 'OK' if status is |
203
|
|
|
|
|
|
|
200. Third element (result) is optional, the actual result. Fourth |
204
|
|
|
|
|
|
|
element (meta) is called result metadata and is optional, a hash |
205
|
|
|
|
|
|
|
that contains extra information. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Return value: (hash) |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=over |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=item * 'is_gui_browser' key will be set to true if HTTP client is a GUI browser. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=item * 'is_text_browser' key will be set to true if HTTP client is a text/TUI |
215
|
|
|
|
|
|
|
browser. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=item * 'is_browser' key will be set to true if either 'is_gui_browser' or |
218
|
|
|
|
|
|
|
'is_text_browser' is set to true. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=back |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
See also: |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=over |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
* L<HTML::ParseBrowser> |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
* L<HTTP::BrowserDetect> |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
* L<HTTP::DetectUserAgent> |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
* L<Parse::HTTP::UserAgent> |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
* L<HTTP::headers::UserAgent> |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=back |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |