Skip to content

Commit

Permalink
Initial implementation of Client Hint detection based on the current …
Browse files Browse the repository at this point in the history
…impementation in Chrome Canary
  • Loading branch information
NielsLeenheer committed Jan 16, 2020
1 parent c7b71a4 commit 46dd731
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Analyser/Header.php
Expand Up @@ -39,6 +39,13 @@ private function &analyseHeaders()
}


/* Analyse the Client Hint header */

if ($header = $this->getHeader('Sec-CH-UA')) {
$this->analyseClientHints();
}


/* Analyse browser specific headers */

if ($header = $this->getHeader('X-OperaMini-Phone')) {
Expand Down Expand Up @@ -85,6 +92,11 @@ private function analyseUserAgent($header)
new Header\Useragent($header, $this->data, $this->options);
}

private function analyseClientHints()
{
new Header\ClientHints($this->headers, $this->data);
}

private function analyseBaiduHeader($header)
{
new Header\Baidu($header, $this->data);
Expand Down
31 changes: 31 additions & 0 deletions src/Analyser/Header/ClientHints.php
@@ -0,0 +1,31 @@
<?php

namespace WhichBrowser\Analyser\Header;

class ClientHints
{
use ClientHints\Browser, ClientHints\Platform;

public function __construct($headers, &$data)
{
$this->headers =& $headers;
$this->data =& $data;

if ($header = $this->getHeader('Sec-CH-UA')) {
$this->detectBrowser($header);
}

if ($header = $this->getHeader('Sec-CH-UA-Platform')) {
$this->detectPlatform($header);
}
}

private function getHeader($h)
{
foreach ($this->headers as $k => $v) {
if (strtolower($h) == strtolower($k)) {
return $v;
}
}
}
}
24 changes: 24 additions & 0 deletions src/Analyser/Header/ClientHints/Browser.php
@@ -0,0 +1,24 @@
<?php

namespace WhichBrowser\Analyser\Header\ClientHints;

use WhichBrowser\Constants;
use WhichBrowser\Data;
use WhichBrowser\Model\Version;

trait Browser
{
private function &detectBrowser($ch)
{
if (preg_match('/Google Chrome ([0-9.]*)/u', $ch, $matches)) {
$this->data->browser->reset([
'name' => 'Chrome',
'type' => Constants\BrowserType::BROWSER,
'stock' => false,
'version' => new Version([ 'value' => $matches[1] ])
]);
}

return $this;
}
}
22 changes: 22 additions & 0 deletions src/Analyser/Header/ClientHints/Platform.php
@@ -0,0 +1,22 @@
<?php

namespace WhichBrowser\Analyser\Header\ClientHints;

use WhichBrowser\Constants;
use WhichBrowser\Data;

trait Platform
{
private function &detectPlatform($ch)
{
if (preg_match('/Mac OS X/u', $ch, $matches)) {
$this->data->os->reset([
'name' => 'OS X',
'alias' => 'macOS',
'type' => Constants\DeviceType::DESKTOP
]);
}

return $this;
}
}
8 changes: 8 additions & 0 deletions tests/data/desktop/browser-chrome.yaml
Expand Up @@ -62,3 +62,11 @@
headers: 'User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.11 Safari/534.16'
result: { browser: { name: Chrome, version: '10', type: browser }, engine: { name: Webkit, version: '534.16' }, os: { name: Linux }, device: { type: desktop } }
readable: 'Chrome 10 on Linux'
-
headers: { User-Agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.1.2222.33 Safari/537.36', Sec-CH-UA: 'Google Chrome 81' }
readable: 'Chrome 81 on Windows 10'
result: { browser: { name: Chrome, version: '81', type: browser }, engine: { name: Blink }, os: { name: Windows, version: { value: '10.0', alias: '10' } }, device: { type: desktop } }
-
headers: { User-Agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.1.2222.33 Safari/537.36', Sec-CH-UA: 'Google Chrome 81', Sec-CH-UA-Platform: 'Mac OS X' }
readable: 'Chrome 81 on macOS'
result: { browser: { name: Chrome, version: '81', type: browser }, engine: { name: Blink }, os: { name: 'OS X', alias: macOS }, device: { type: desktop, manufacturer: Apple, model: Macintosh } }

0 comments on commit 46dd731

Please sign in to comment.