Skip to content

Commit

Permalink
Optimize Mozilla certdata version check
Browse files Browse the repository at this point in the history
Now only reads the certdata from the server until the CVS_ID line.
  • Loading branch information
EvanDotPro committed Sep 16, 2012
1 parent c72f532 commit 0abb2de
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Sslurp/AbstractCaRootData.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractCaRootData
public function getVersion()
{
if ($this->version === null) {
if (preg_match('/^#?\s?(CVS_ID\s+\".*\")/m', $this->getContent(), $match)) {
if (preg_match('/^#?\s?(CVS_ID\s+\".*\")/m', $this->getContent('CVS_ID'), $match)) {
$parts = explode(' ', $match[1]);
$this->version = $parts[6];
$this->dateTime = new DateTime($parts[9] . ' ' . $parts[10], new DateTimeZone('UTC'));
Expand All @@ -63,5 +63,5 @@ public function getDateTime()
return $this->dateTime;
}

abstract protected function getContent();
abstract public function getContent($until = false);
}
8 changes: 6 additions & 2 deletions src/Sslurp/CaRootPemBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,22 @@ public function __construct($pemContent = null, MozillaCertData $mozCertData = n
/**
* Return the content of the PEM bundle
*/
public function getContent()
public function getContent($until = false)
{
if ($this->pemContent === null) {
$this->pemContent = $this->getUpdatedCaRootBundle();
}

if ($until) {
return substr($this->pemContent, 0, strpos($this->pemContent, "\n", strpos($this->pemContent, $until)));
}

return $this->pemContent;
}

public function isLatest()
{
return $this->getVersion() == $this->mozCertData->getVersion();
return $this->getVersion() === $this->mozCertData->getVersion();
}

public function getUpdatedCaRootBundle()
Expand Down
18 changes: 15 additions & 3 deletions src/Sslurp/MozillaCertData.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@ public function __construct($certData = null)
*
* @return string
*/
public function getContent()
public function getContent($until = false)
{
if ($until) {
// don't cache the partial fetch for version check
if ($this->certData) {
return substr($this->certData, 0, strpos($this->certData, "\n", strpos($this->certData, $until)));
}

return $this->fetchLatestCertData($until);
}

if ($this->certData === null) {
$this->certData = $this->fetchLatestCertData();
}
Expand All @@ -67,7 +76,7 @@ public function getContent()
*/
public function getStreamContext()
{
if (! $this->context) {
if (!$this->context) {
$this->context = stream_context_create(array('ssl' => array(
'capture_peer_cert' => true,
'verify_peer' => true,
Expand All @@ -80,7 +89,7 @@ public function getStreamContext()
return $this->context;
}

protected function fetchLatestCertData()
protected function fetchLatestCertData($until = false)
{
$ctx = $this->getStreamContext();

Expand All @@ -105,6 +114,9 @@ protected function fetchLatestCertData()
$response = '';
while (!feof($fp)) {
$response .= fgets($fp);
if ($until && strpos($response, $until) !== false) {
break;
}
}
fclose($fp);

Expand Down
2 changes: 1 addition & 1 deletion test/SslurpTest/CaRootPemBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function testIsLatestMethodWorks()
$this->assertFalse($this->bundle->isLatest());
}

public function testCanFetchContentFromLiveServer()
public function testWillFetchMozillaCertData()
{
require_once __DIR__ . '/TestAsset/MockMozillaCertData.php';
$bundle = new CaRootPemBundle(null, new TestAsset\MockMozillaCertData);
Expand Down
5 changes: 4 additions & 1 deletion test/SslurpTest/MozillaCertDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ public function testCanParseVersionAndDateDataProperly()
$this->assertSame('1.85', $this->mozCertData->getVersion());
}

public function testCertDataPassedToConstructorIsReturnedFromGetCertData()
public function testExpectedCertDataReturnedFromGetContent()
{
$this->mozCertData = new MozillaCertData('foo');
$this->assertEquals('foo', $this->mozCertData->getContent());
require_once __DIR__ . '/TestAsset/MockMozillaCertDataFetchLatest.php';
$this->mozCertData = new TestAsset\MockMozillaCertDataFetchLatest();
$this->assertEquals('latest', $this->mozCertData->getContent());
}

public function testExceptionThrownIfCertDataIsInvalid()
Expand Down
2 changes: 1 addition & 1 deletion test/SslurpTest/TestAsset/MockMozillaCertData.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class MockMozillaCertData extends MozillaCertData
{
public function getContent()
public function getContent($until = false)
{
return 'foo';
}
Expand Down
13 changes: 13 additions & 0 deletions test/SslurpTest/TestAsset/MockMozillaCertDataFetchLatest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SslurpTest\TestAsset;

use Sslurp\MozillaCertData;

class MockMozillaCertDataFetchLatest extends MozillaCertData
{
protected function fetchLatestCertData($until = false)
{
return 'latest';
}
}

0 comments on commit 0abb2de

Please sign in to comment.