Skip to content

Commit

Permalink
[WIP] Added SSL support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul committed May 30, 2017
1 parent d8cd138 commit f321481
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Expand Up @@ -3,6 +3,11 @@ notifications:

sudo: false

addons:
apt:
packages:
- stunnel4

language: php

php:
Expand All @@ -14,7 +19,7 @@ php:
env:
matrix:
-
- DEPENDENCIES=--prefer-lowest
#- DEPENDENCIES=--prefer-lowest

matrix:
fast_finish: true
Expand Down
4 changes: 4 additions & 0 deletions src/Net/Http/HttpConnector.php
Expand Up @@ -59,6 +59,10 @@ public function fetchFreshData($source, EncapsulatedOptions $options = null)
$this->options->extractHttpContextOptions(),
$options ? $options->extractHttpContextOptions() : []
),
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
])
)) {
$error = error_get_last();
Expand Down
65 changes: 56 additions & 9 deletions test/Functional/Porter/Net/Http/HttpConnectorTest.php
Expand Up @@ -12,6 +12,7 @@
final class HttpConnectorTest extends \PHPUnit_Framework_TestCase
{
const HOST = '[::1]:12345';
const SSL_HOST = '[::1]:6666';
const URI = '/test?baz=qux';

private static $dir;
Expand All @@ -33,12 +34,26 @@ public function testConnectionToLocalWebserver()
{
$server = $this->startServer('feedback');
$response = $this->fetch(new HttpConnector((new HttpOptions)->addHeader($header = 'Foo: Bar')));
$this->stopServer($server);
$server->stop();

self::assertRegExp('[\AGET \Q' . self::HOST . self::URI . '\E HTTP/\d+\.\d+$]m', $response);
self::assertRegExp("[^$header$]m", $response);
}

/**
* @requires OS Linux
*/
public function testSslConnectionToLocalWebserver()
{
$this->startSsl();
$server = $this->startServer('feedback');
$response = $this->fetchViaSsl();
$server->stop();
$this->stopSsl();

self::assertRegExp('[\AGET \Q' . self::SSL_HOST . '\E/ HTTP/\d+\.\d+$]m', $response);
}

public function testConnectionTimeout()
{
$this->setExpectedException(HttpConnectionException::class);
Expand All @@ -59,7 +74,7 @@ public function testErrorResponse()

throw $exception;
} finally {
$this->stopServer($server);
$server->stop();
}
}

Expand All @@ -70,23 +85,24 @@ public function testErrorResponse()
*/
private function startServer($script)
{
$server = (
new Process(sprintf(
$server = new Process(
sprintf(
'%sphp -S %s %s.php',
// Prevent forking on some Unix systems.
file_exists('/bin/sh') ? 'exec ' : '',
self::HOST,
$script
))
)->setWorkingDirectory(self::$dir);
),
self::$dir
);
$server->start();

// Wait for server to spawn.
\ScriptFUSION\Retry\retry(5, function () {
$this->fetch();
}, function (\Exception $exception) {
static $handler;
$handler = $handler ?: new ExponentialBackoffExceptionHandler();
$handler = $handler ?: new ExponentialBackoffExceptionHandler;

if (!$exception instanceof HttpConnectionException) {
return false;
Expand All @@ -98,9 +114,35 @@ private function startServer($script)
return $server;
}

private function stopServer(Process $server)
private function startSsl()
{
$server->stop();
$accept = strtr(self::SSL_HOST, $filter = ['[' => '', ']' => '']);
$connect = strtr(self::HOST, $filter);
$temp = tempnam(sys_get_temp_dir(), 'Porter');

// Generate SSL certificate.
`yes '' | openssl req -new -x509 -nodes -keyout '$temp' -out '$temp'`;

$ssl = new Process(
"{ stunnel4 -fd 0 || stunnel -fd 0; } <<.
# Disable PID to run as non-root user.
pid=
# Must run as foreground process on Travis, for some reason.
foreground=yes
[]
cert=$temp
accept=$accept
connect=$connect
.");
$ssl->start();

return $ssl;
}

private function stopSsl()
{
`pkill stunnel`;
}

private function fetch(Connector $connector = null)
Expand All @@ -109,4 +151,9 @@ private function fetch(Connector $connector = null)

return $connector->fetch('http://' . self::HOST . self::URI);
}

private function fetchViaSsl()
{
return $this->connector->fetch('https://' . self::SSL_HOST);
}
}

0 comments on commit f321481

Please sign in to comment.