Commits

Burak Gürsoy committed a209151

Fix version handling to safeguard against strings parsed as versions.
Also added some iDevice strings to the test suite.

Comments (0)

Files changed (5)

 Revision history for Perl extension Parse::HTTP::UserAgent
 
-0.34 Tue Nov 15 06:41:45 2011
+0.34 Sun Apr  8 00:08:41 2012
    => "normalize" option.
+   => Fix RT#76351 reported by DAMOG.
 
 0.33 Tue Nov 15 05:22:15 2011
    => Fix version warning regex.

lib/Parse/HTTP/UserAgent.pm

     $self->[UA_EXTRAS] = [ @buf ];
 
     if ( $self->[UA_TOOLKIT] ) {
-        push @{ $self->[UA_TOOLKIT] },
-             $self->_numify( $self->[UA_TOOLKIT][TK_ORIGINAL_VERSION] );
+        my $v = $self->[UA_TOOLKIT][TK_ORIGINAL_VERSION];
+        push @{ $self->[UA_TOOLKIT] }, defined $v ? $self->_numify( $v ) : 0;
     }
 
     if( $self->[UA_MOZILLA] ) {
     # if version::vpp is used it'll identify 420 as a v-string
     # add a floating point to fool it
     $v .= q{.0} if index($v, q{.}) == NO_IMATCH;
+    (my $check = $v) =~ tr/0-9//cd;
+    return 0 if ! $check; # A string parsed as version (i.e.: AppleWebKit/en_SG)
     my $rv;
     eval {
         $rv = version->new("$v")->numify;

lib/Parse/HTTP/UserAgent/Base/Parsers.pm

     }
 
     if ( $thing->[0] && lc $thing->[0] eq 'iphone' ) {
-        $self->[UA_DEVICE]  = shift @{$thing};
+        $self->[UA_MOBILE] = 1;
+        $self->[UA_DEVICE] = shift @{$thing};
+        my $check_os       = $thing->[LAST_ELEMENT];
+
+        if ( $check_os && index( $check_os, 'Mac OS X' ) != NO_IMATCH ) {
+            if ( $self->[UA_OS] ) {
+                push @{$self->[UA_EXTRAS]}, $self->[UA_OS];
+            }
+            $self->[UA_OS] = pop @{ $thing };
+            # Another oddity: tk as "AppleWebKit/en_SG"
+            if ( ! $self->[UA_LANG] && $self->[UA_TOOLKIT] ) {
+                my $v = $self->[UA_TOOLKIT][TK_ORIGINAL_VERSION];
+                if ( $v && $v =~ m< [a-zA-Z]{2}_[a-zA-Z]{2} >xms ) {
+                    $self->[UA_LANG] = $v;
+                    $self->[UA_TOOLKIT][TK_ORIGINAL_VERSION] = undef;
+                }
+            }
+        }
     }
-    $self->[UA_EXTRAS]      = [ @{$thing}, @others ];
+
+    $self->[UA_EXTRAS] = [ @{$thing}, @others ];
 
     if ( $self->[UA_OS] && length($self->[UA_OS]) == 1 ) {
         push @{$self->[UA_EXTRAS]}, $self->[UA_OS];

t/data/mobile/phone/iphone/1

 
 Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
 
-'parser'           => 'safari',
-'strength'         => 'U',
-'os'               => 'CPU like Mac OS X',
-'extras'           => ['KHTML, like Gecko','Mobile/1A543a','Safari/419.3'],
-'version_raw'      => '3.0',
-'lang'             => 'en',
-'version'          => '3.000',
-'name'             => 'Safari',
-'device'           => 'iPhone',
-'toolkit'          => ['AppleWebKit','420+','420.000']
+parser             => 'safari',
+strength           => 'U',
+os                 => 'CPU like Mac OS X',
+extras             => ['KHTML, like Gecko','Mobile/1A543a','Safari/419.3'],
+version_raw        => '3.0',
+lang               => 'en',
+version            => '3.000',
+name               => 'Safari',
+device             => 'iPhone',
+toolkit            => ['AppleWebKit','420+','420.000'],
+mobile             => 1,
+
+[AGENT]
+
+Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
+
+device          => 'iPhone',
+extras          => [ 'KHTML, like Gecko', 'Mobile/9A334', 'Safari/7534.48.3' ],
+mobile          => 1,
+name            => 'Safari',
+os              => 'CPU iPhone OS 5_0 like Mac OS X',
+parser          => 'safari',
+toolkit         => [ 'AppleWebKit', '534.46', '534.460' ],
+version         => '5.100',
+version_raw     => '5.1',
+
+[AGENT]
+
+Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1 like Mac OS X; 6534.46) AppleWebKit/en_SG (KHTML, like Gecko) Mobile/8C148 Safari/6533.18.5
+
+device          => 'iPhone',
+extras          => [ 'KHTML, like Gecko', 'Safari/6533.18.5' ],
+lang            => 'en_SG',
+mobile          => 1,
+name            => 'Safari',
+os              => 'CPU iPhone OS 5_1 like Mac OS X',
+parser          => 'safari',
+strength        => 'U',
+toolkit         => [ 'AppleWebKit', undef, 0 ],
+version         => '8.148',
+version_raw     => '8C148',

t/data/mobile/tablet/ipad/1

 version_raw => '5.0.2',
 mobile      => 1,
 tablet      => 1,
+
+[AGENT]
+
+Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3
+
+extras          => [ 'KHTML, like Gecko', 'Mobile/9A334', 'Safari/7534.48.3' ],
+mobile          => 1,
+name            => 'iPad',
+os              => 'CPU OS 5_0 like Mac OS X',
+parser          => 'safari',
+tablet          => 1,
+toolkit         => [ 'AppleWebKit', '534.46', '534.460' ],
+version         => '5.100',
+version_raw     => '5.1',