Skip to content

Commit

Permalink
Added preliminary TLS support in lieu of connectors/ssl.
Browse files Browse the repository at this point in the history
Fixed remaining tests.
  • Loading branch information
Bilge committed Nov 4, 2022
1 parent ad8ece2 commit 8b51a2c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"scriptfusion/porter": "dev-master as 7"
},
"require-dev": {
"amphp/socket": "v2.0.0-beta.6",
"mockery/mockery": "^1.3.3",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^9.5.23",
"symfony/process": "^6.1"
},
"autoload": {
Expand Down
10 changes: 9 additions & 1 deletion src/AsyncHttpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Amp\ByteStream\StreamException;
use Amp\Dns\DnsException;
use Amp\Http\Client\Connection\ConnectionPool;
use Amp\Http\Client\Connection\DefaultConnectionFactory;
use Amp\Http\Client\Connection\UnlimitedConnectionPool;
use Amp\Http\Client\Cookie\CookieInterceptor;
use Amp\Http\Client\Cookie\CookieJar;
Expand All @@ -17,6 +18,8 @@
use Amp\Http\Client\InvalidRequestException;
use Amp\Http\Client\ParseException;
use Amp\Http\Client\Request;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Connector\DataSource;

Expand All @@ -32,7 +35,12 @@ public function __construct(AsyncHttpOptions $options = null, CookieJar $cookieJ
{
$this->options = $options ?: new AsyncHttpOptions;
$this->cookieJar = $cookieJar ?: new LocalCookieJar();
$this->pool = new UnlimitedConnectionPool();
$this->pool = new UnlimitedConnectionPool(new DefaultConnectionFactory(
connectContext: (new ConnectContext())->withTlsContext(
(new ClientTlsContext(''))
->withCaFile($options?->getCertificateAuthorityFilePath())
)
));
}

public function __clone()
Expand Down
14 changes: 14 additions & 0 deletions src/AsyncHttpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ final class AsyncHttpOptions
// Maximum body length in bytes. Default 10MiB.
private int $maxBodyLength = 0x100_000 * 10;

private ?string $certificateAuthorityFilePath = null;

public function getTransferTimeout(): int
{
return $this->transferTimeout;
Expand Down Expand Up @@ -52,4 +54,16 @@ public function setMaxBodyLength($maxBodyLength): self

return $this;
}

public function getCertificateAuthorityFilePath(): ?string
{
return $this->certificateAuthorityFilePath;
}

public function setCertificateAuthorityFilePath(string $path): self
{
$this->certificateAuthorityFilePath = $path;

return $this;
}
}
16 changes: 7 additions & 9 deletions test/Functional/HttpConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,12 @@ public function testDefaultBodyLengthTooLong(): void
$server = $this->startServer();

$this->connector = new AsyncHttpConnector();
$response = $this->fetch(self::buildDataSource('big.php'));

$this->expectException(HttpException::class);

try {
$this->fetch(self::buildDataSource('big.php'));
$response->getBody();
} finally {
$this->stopServer($server);
}
Expand All @@ -205,12 +206,12 @@ public function testCustomBodyLengthTooLong(): void
$server = $this->startServer();

$this->connector = new AsyncHttpConnector((new AsyncHttpOptions)->setMaxBodyLength(1));
$response = $this->fetch(self::buildDataSource());

// N.B. Actual type is Amp\Artax\ParseException.
$this->expectException(HttpException::class);

try {
$this->fetch(self::buildDataSource());
$response->getBody();
} finally {
$this->stopServer($server);
}
Expand Down Expand Up @@ -242,7 +243,7 @@ private function startSsl(): string
// Create SSL tunnel process.
Process::fromShellCommandline(
// Generate self-signed SSL certificate in PEM format.
"openssl req -new -x509 -nodes -subj /CN=::1 -keyout '$certificate' -out '$certificate'
"openssl req -new -x509 -nodes -subj /CN=[::1] -keyout '$certificate' -out '$certificate'
{ stunnel4 -fd 0 || stunnel -fd 0; } <<.
# Disable PID to run as non-root user.
Expand Down Expand Up @@ -281,7 +282,7 @@ private static function buildDataSource(string $url = self::URI): DataSource

private function fetchViaSsl(Connector $connector): HttpResponse
{
return $connector->fetch(new HttpDataSource('https://' . self::SSL_HOST . '/' . self::URI));
return $connector->fetch(new AsyncHttpDataSource('https://' . self::SSL_HOST . '/' . self::URI));
}

/**
Expand Down Expand Up @@ -309,9 +310,6 @@ static function (\Exception $exception) {

private static function createSslConnector(string $certificate): AsyncHttpConnector
{
$connector = new HttpConnector($options = new HttpOptions);
$options->getSslOptions()->setCertificateAuthorityFilePath($certificate);

return $connector;
return new AsyncHttpConnector((new AsyncHttpOptions)->setCertificateAuthorityFilePath($certificate));
}
}

0 comments on commit 8b51a2c

Please sign in to comment.