Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
Removed dev dependency on league/uri-schemes.
  • Loading branch information
trowski committed Nov 5, 2019
1 parent 21d6286 commit 4344460
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
8 changes: 1 addition & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
"require-dev": {
"phpunit/phpunit": "^6",
"amphp/phpunit-util": "^1",
"amphp/php-cs-fixer-config": "dev-master",
"league/uri-schemes": "^1.2.1"
"amphp/php-cs-fixer-config": "dev-master"
},
"autoload": {
"psr-4": {
Expand All @@ -58,11 +57,6 @@
"Amp\\Socket\\Test\\": "test"
}
},
"config": {
"platform": {
"php": "7.1.0"
}
},
"scripts": {
"check": [
"@cs",
Expand Down
17 changes: 8 additions & 9 deletions examples/simple-http-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
require __DIR__ . '/../vendor/autoload.php';

// This is a very simple HTTP client that just prints the response without parsing.
// league/uri-schemes required for this example.

use Amp\ByteStream\ResourceOutputStream;
use Amp\Loop;
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
Expand All @@ -15,29 +13,30 @@
use function Amp\Socket\connect;

Loop::run(static function () use ($argv) {
$stdout = new ResourceOutputStream(STDOUT);
$stdout = Amp\ByteStream\getStdout();

if (\count($argv) !== 2) {
yield $stdout->write('Usage: examples/simple-http-client.php <url>' . PHP_EOL);
exit(1);
}

$uri = Uri\Http::createFromString($argv[1]);
$host = $uri->getHost();
$port = $uri->getPort() ?? ($uri->getScheme() === 'https' ? 443 : 80);
$path = $uri->getPath() ?: '/';
$parts = Uri\parse($argv[1]);

$host = $parts['host'];
$port = $parts['port'] ?? ($parts['scheme'] === 'https' ? 443 : 80);
$path = $parts['path'] ?: '/';

$connectContext = (new ConnectContext)
->withTlsContext(new ClientTlsContext($host));

/** @var EncryptableSocket $socket */
$socket = yield connect($host . ':' . $port, $connectContext);

if ($uri->getScheme() === 'https') {
if ($parts['scheme'] === 'https') {
yield $socket->setupTls();
}

yield $socket->write("GET {$path} HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");
yield $socket->write("GET {$path} HTTP/1.1\r\nHost: {$host}\r\nConnection: close\r\n\r\n");

while (null !== $chunk = yield $socket->read()) {
yield $stdout->write($chunk);
Expand Down

0 comments on commit 4344460

Please sign in to comment.