HTTP::Browscap - Parse and search browscap.ini files
use HTTP::Browscap; my $capable = browscap(); if( $capable->{wap} ) { output_WAP(); } if( $capable->{css} > 1 ) { # Browser can handle CSS2 } # OO interface my $BC = HTTP::Browscap->new( 'browscap.ini' ); $capable = $BC->match( $ENV{HTTP_USER_AGENT} );
Browscap.ini is a file, introduced with Microsoft's IIS, that lists the User-Agent strings that different browsers send, and various capabilities of those browsers. This module parses browscap.ini and allows you to find the capability definitions for a given browser.
Starting with Microsoft's IIS, a browscap.ini file was used to list the capabilities of various browsers. Using the User-Agent header sent by a browser during each HTTP request, the capabilities of a browser are retrieved. If an exact match of the User-Agent string isn't found, wild-card expantion is done. If all fails, a default browser definition is used.
The information in Browscap allows you to adapt your response to the browser's capabalities. There are however limits it's usefulness. It only detects if a browser has a certain capability, but not if this capability has been deactivated nor if it's badly implemented. In particular, most CSS and JavaScript implementations will make you scream. You might want to use HTTP::BrowserDetect or HTTP::BrowserSupport to detect a specific feature or bug.
The browser capability definition returned by "browscap" or "match" is a hash reference. The keys are defined below. Boolean capabilites can have values '' (false) or 1 (true).
Microsoft defines the following capabilities :
Browser supports ActiveX controls? Boolean.
Browser supports background sounds? Boolean.
Is this a beta version of the browser? Boolean.
String containing a useful name for the browser.
Does the browser support Channel Definition Format for Webcasting? Boolean.
Does the browser support cookies? Note that this does not mean that the browser has cookie support currently turned on. Boolean.
Does the browser support HTML <FRAMESET> and <FRAME> tags? Boolean.
Does the browser support Java applets? Note that even if this is true, the user could have Java turned off. Boolean.
Does the browser support javascript? Note that even if this is true, the user could have javascript turned off. Boolean.
Which platform (ie, OS) is the browser running on. Example : WinNT, WinXP, Win2003, Palm, Linux, Debian.
If you want a list of all possible values, run the following on your browscap.ini file :
grep platform= browscap.ini | sort | uniq
Does the browser support HTML <TABLE> tags? Boolean.
Does the browser support VBscript? Note that even if this is true, the user should have VBscript turned off. Boolean.
Full version number of the browser. Example: 1.10
Gary Keith adds the following:
Browser is an alpha version and still under development? Boolean.
Is this an AOL-branded browser? Boolean.
A number indicating what version, if any, of the America Online browser is being used.
Is this browser in fact a web-crawler or spider, often sent by a search engine? Boolean.
CSS version supported by this browser. Possible values : 0 (no CSS support), 1 or 2.
Same as above.
Does this user-agent support CSS? Boolean.
Does the browser support MS's <IFRAME> tags? Boolean.
Is the user-agent string banned by Gary Keith? Boolean.
Is this a browser on a mobile device (iPhone, Blackberry, etc)? Boolean.
Is this user-agent an RSS or ATOM reader? Boolean.
Is this a .NET CLR user-agent? Boolean. (Seems to longer exist.)
Major version number. Example: given a version of 1.10, majorver is 1.
Minor version number. Example: given a version of 1.10, minorver is 10.
Identifies the browser as a crawler that does not obey robots.txt. This includes e-mail harvesters, download managers and so on. Boolean.
Is browser a WAP-enabled mobile phone? Boolean.
Is this the 16-bit windows version of the browser? Detecting this might be useful if you want the user to save a file with 8.3 length. Boolean.
Is this the 32-bit windows version of the browser? Boolean.
HTTP::Browscap adds the following:
HTTP::Browscap
Full text of the User-Agent string used to match this definition.
Line in browscap.ini where the browser's capabilites are defined. Useful for debuging.
If fallback was needed to match a UA string, this contains what was modified to make the match. Can be one of browser, OS or browser+OS.
browser
OS
browser+OS
The browscap.ini standard also defines parent, which is a link to another capability list that complements the current definition. HTTP::Browscap handles these internaly, so that you only have to do one lookup.
parent
Note that, contrary to other implementations, all capability names are in lower case, except for UA and LINE. This means you should look for 'win16' instead of 'Win16'.
UA
LINE
'win16'
'Win16'
Because parsing browscap.ini is slow, a cached version of the parsed data is automatically created and used where possible. Normaly this cached version is created when you ran browscap-update during installation.
browscap-update
The cache file is a MLDBM file created with DB_File and Storable. You may change this by overloading "__save_cache" and "__open_cache".
You will want to periodically fetch a new browscap.ini. This can be done with the following :
wget -O browscap.ini \ "http://browsers.garykeith.com/stream.asp?BrowsCapINI" browscap-update browscap.ini rm browscap.ini
However, you must read http://browsers.garykeith.com/terms.asp before automating this.
$def = browscap(); $def = browscap( $ua );
Find the capabilities of a browser identified by a given User-Agent string. If the string is missing, browscap will attempt to find one. See __guess_ua below.
browscap
__guess_ua
Returns a hashref. See "Capabilities" above.
There is also an object oriented interface to this module.
my $BC = HTTP::Browscap->new; my $BC = HTTP::Browscap->new( $ini_file, $fallback );
Creates a new browscap object. If you do not specify $ini_file, the system's browscap.ini will be used.
$ini_file
If $fallback is true, an attempt is made to make unknown versions of Windows, Firefox, IE and Opera match the most recent known versions. That is IE 24.8 (if/when it is released) should match IE 9.0 (currenty most recent as of this writing).
$fallback
IE 24.8
IE 9.0
Default is true.
$def = $BC->match; $def = $BC->match( $ua );
Find the capabilities of a browser identified by the User-Agent string given in $ua. If the string is missing, match will attempt to find one. See "__guess_ua" below.
$ua
match
$BC->open
Parse and load the browscap.ini file. If there is a cache-file present and it is more recent then browscap.ini, the cache-file is used instead. Creates the cache file if possible
This method is called automatically when needed. You should only call this yourself when you want to create a cache file but not bother with matching.
The following methods are documented in case you wish to create a sub-class.
$BC->__guess_ua;
Used to guess at a User-Agent string. First "__guess_ua" looks in $ENV{HTTP_USER_AGENT} (CGI environement variable). If this fails and $ENV{MOD_PERL} is set, __guess_ua will use the mod_perl's "headers_in()" in Apache to find it. If both these fails, the default User-Agent is returned, which is probably not what you want.
$ENV{HTTP_USER_AGENT}
$ENV{MOD_PERL}
Returns the User-Agent string.
$BC->__set_file( $file );
Called to set a new browscap.ini file. This method set's data members, generates the new cache-file name based on $file and clears any parsed data from memory.
$file
Saves the parsed browscap.ini file (which is in {data}) to the cache file named {cache}.
{data}
{cache}
Returns true on success.
Returns false on failure, with $! set accordingly.
$!
Called to open the cache {cache} and tie it to {data}.
Returns true on success. Dies on failure.
Load and parse the browscap.ini file. You will have to read the source code if you want to modify it.
Converts a UA string from browscap.ini to a Perl patern. The UA strings in browscap.ini may contain * or ., which act like file-globs.
*
.
http://browsers.garykeith.com/, http://www.microsoft.com/windows2000/en/server/iis/default.asp?url=/windows2000/en/server/iis/htm/asp/comp1g11.htm HTTP::BrowserDetect.
Philip Gwyn, <gwyn-AT-cpan.org>
Copyright 2005-2011 by Philip Gwyn
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install HTTP::Browscap, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTTP::Browscap
CPAN shell
perl -MCPAN -e shell install HTTP::Browscap
For more information on module installation, please visit the detailed CPAN module installation guide.