Skip to content

Commit

Permalink
feat(tcp): support TLS/SSL
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Mar 27, 2024
1 parent b66e921 commit e16144c
Show file tree
Hide file tree
Showing 37 changed files with 1,318 additions and 145 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"ext-mbstring": "*",
"ext-sodium": "*",
"ext-intl": "*",
"ext-openssl": "*",
"revolt/event-loop": "^1.0.1"
},
"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- [Psl\Str\Byte](./component/str-byte.md)
- [Psl\Str\Grapheme](./component/str-grapheme.md)
- [Psl\TCP](./component/tcp.md)
- [Psl\TCP\TLS](./component/tcp-tls.md)
- [Psl\Trait](./component/trait.md)
- [Psl\Type](./component/type.md)
- [Psl\Unix](./component/unix.md)
Expand Down
21 changes: 21 additions & 0 deletions docs/component/tcp-tls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
This markdown file was generated using `docs/documenter.php`.
Any edits to it will likely be lost.
-->

[*index](./../README.md)

---

### `Psl\TCP\TLS` Component

#### `Classes`

- [Certificate](./../../src/Psl/TCP/TLS/Certificate.php#L16)
- [ConnectOptions](./../../src/Psl/TCP/TLS/ConnectOptions.php#L34)
- [HashingAlgorithm](./../../src/Psl/TCP/TLS/HashingAlgorithm.php#L15)
- [SecurityLevel](./../../src/Psl/TCP/TLS/SecurityLevel.php#L18)
- [Version](./../../src/Psl/TCP/TLS/Version.php#L10)


2 changes: 1 addition & 1 deletion docs/component/tcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#### `Functions`

- [connect](./../../src/Psl/TCP/connect.php#L18)
- [connect](./../../src/Psl/TCP/connect.php#L37)

#### `Classes`

Expand Down
1 change: 1 addition & 0 deletions docs/documenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ function get_all_components(): array
'Psl\\Str\\Byte',
'Psl\\Str\\Grapheme',
'Psl\\TCP',
'Psl\\TCP\\TLS',
'Psl\\Trait',
'Psl\\Type',
'Psl\\Unix',
Expand Down
5 changes: 2 additions & 3 deletions examples/channel/bounded.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

for ($i = 0; $i < 10; $i++) {
$file = File\open_read_only(__FILE__);
$reader = new IO\Reader($file);
while (!$reader->isEndOfFile()) {
$byte = $reader->readByte();
while (!$file->reachedEndOfDataSource()) {
$byte = $file->readFixedSize(1);

$sender->send($byte);
}
Expand Down
45 changes: 31 additions & 14 deletions examples/tcp/basic-http-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,45 @@

declare(strict_types=1);

namespace Psl\Example\TCP;

use Psl\Async;
use Psl\IO;
use Psl\Str;
use Psl\TCP;

require __DIR__ . '/../../vendor/autoload.php';

function fetch(string $host, string $path): string
Async\main(static function(): void {
[$headers, $content] = fetch('https://psalm.dev/');

$output = IO\error_handle() ?? IO\output_handle();

$output->writeAll($headers);
$output->writeAll("\n");
$output->writeAll($content);
});

function fetch(string $url): array
{
$client = TCP\connect($host, 80);
$client->writeAll("GET {$path} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");
$response = $client->readAll();
$client->close();
$parsed_url = parse_url($url);
$host = $parsed_url['host'];
$port = $parsed_url['scheme'] === 'https' ? 443 : 80;
$path = $parsed_url['path'] ?? '/';

return $response;
}
$options = TCP\ConnectOptions::create();
if ($port === 443) {
$options = $options->withTLSConnectOptions(
TCP\TLS\ConnectOptions::default()->withPeerName($host),
);
}

Async\main(static function (): int {
$response = fetch('example.com', '/');
$client = TCP\connect($host, $port, $options);
$client->writeAll("GET $path HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");

IO\write_error_line($response);
$response = $client->readAll();

return 0;
});
$position = Str\search($response, "\r\n\r\n");
$headers = Str\slice($response, 0, $position);
$content = Str\slice($response, $position + 4);

return [$headers, $content];
}
8 changes: 8 additions & 0 deletions src/Psl/File/ReadHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public function __construct(string $file)
parent::__construct($this->readHandle);
}

/**
* {@inheritDoc}
*/
public function reachedEndOfDataSource(): bool
{
return $this->readHandle->reachedEndOfDataSource();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Psl/File/ReadWriteHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ public function __construct(string $file, WriteMode $write_mode = WriteMode::Ope
parent::__construct($this->readWriteHandle);
}

/**
* {@inheritDoc}
*/
public function reachedEndOfDataSource(): bool
{
return $this->readWriteHandle->reachedEndOfDataSource();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Psl/IO/CloseReadStreamHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function __construct(mixed $stream)
$this->handle = new Internal\ResourceHandle($stream, read: true, write: false, seek: false, close: true);
}

/**
* {@inheritDoc}
*/
public function reachedEndOfDataSource(): bool
{
return $this->handle->reachedEndOfDataSource();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Psl/IO/CloseReadWriteStreamHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function __construct(mixed $stream)
$this->handle = new Internal\ResourceHandle($stream, read: true, write: true, seek: false, close: true);
}

/**
* {@inheritDoc}
*/
public function reachedEndOfDataSource(): bool
{
return $this->handle->reachedEndOfDataSource();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Psl/IO/CloseSeekReadStreamHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function __construct(mixed $stream)
$this->handle = new Internal\ResourceHandle($stream, read: true, write: false, seek: true, close: true);
}

/**
* {@inheritDoc}
*/
public function reachedEndOfDataSource(): bool
{
return $this->handle->reachedEndOfDataSource();
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Psl/IO/CloseSeekReadWriteStreamHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function __construct(mixed $stream)
$this->handle = new Internal\ResourceHandle($stream, read: true, write: true, seek: true, close: true);
}

/**
* {@inheritDoc}
*/
public function reachedEndOfDataSource(): bool
{
return $this->handle->reachedEndOfDataSource();
}

/**
* {@inheritDoc}
*/
Expand Down
Loading

0 comments on commit e16144c

Please sign in to comment.