File Coverage

blib/lib/SHARYANTO/HTTP/DetectUA/Simple.pm
Criterion Covered Total %
statement 22 22 100.0
branch 14 14 100.0
condition 3 3 100.0
subroutine 2 2 100.0
pod 1 1 100.0
total 42 42 100.0


line stmt bran cond sub pod time code
1             package SHARYANTO::HTTP::DetectUA::Simple;
2              
3 1     1   38591 use 5.010;
  1         6  
  1         645  
4             require Exporter;
5             our @ISA = qw(Exporter);
6             our @EXPORT_OK = qw(detect_http_ua_simple);
7              
8             our $VERSION = '0.75'; # 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 56923 my ($env) = @_;
75 19         30 my $res = {};
76 19         26 my $det;
77              
78 19         32 my $ua = $env->{HTTP_USER_AGENT};
79 19 100       41 if ($ua) {
80             # check for popular browser GUI UA
81 17 100       105 if ($ua =~ m!\b(?:Mozilla/|MSIE |Chrome/|Opera/|
82             Profile/MIDP-
83             )!x) {
84 11         26 $res->{is_gui_browser} = 1;
85 11         17 $det++;
86             }
87             # check for popular webbot UA
88 17 100       122 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       45 if (!$det) {
95             # check for accept mime type
96 4         9 my $ac = $env->{HTTP_ACCEPT};
97 4 100       11 if ($ac) {
98 2 100       11 if ($ac =~ m!\b(?:image/)!) {
99 1         4 $res->{is_gui_browser} = 1;
100 1         2 $det++;
101             }
102             }
103             }
104              
105 19 100 100     79 $res->{is_browser} = 1 if $res->{is_gui_browser} || $res->{is_text_browser};
106 19         52 $res;
107             }
108              
109             1;
110             # ABSTRACT: A very simple and generic browser detection library
111              
112             __END__