Skip to content

Commit

Permalink
Merge 9a20805 into 69013f4
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Nov 11, 2019
2 parents 69013f4 + 9a20805 commit 0b40401
Show file tree
Hide file tree
Showing 22 changed files with 455 additions and 46 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ vendor
composer.lock
.php_cs.cache
.phpunit.result.cache
*.har
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -27,7 +27,7 @@
],
"require": {
"php": ">=7.2",
"amphp/amp": "^2.2",
"amphp/amp": "^2.4",
"amphp/byte-stream": "^1.6",
"amphp/hpack": "^2",
"amphp/http": "^1.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/1-get-request.php
Expand Up @@ -29,7 +29,7 @@
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
2 changes: 1 addition & 1 deletion examples/basic/2-custom-header.php
Expand Up @@ -32,7 +32,7 @@
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
2 changes: 1 addition & 1 deletion examples/basic/3-post-body.php
Expand Up @@ -34,7 +34,7 @@
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
2 changes: 1 addition & 1 deletion examples/basic/4-forms.php
Expand Up @@ -40,7 +40,7 @@
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
2 changes: 1 addition & 1 deletion examples/basic/5-unix-sockets.php
Expand Up @@ -39,7 +39,7 @@
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
16 changes: 10 additions & 6 deletions examples/basic/6-customization.php
Expand Up @@ -2,6 +2,7 @@

use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Interceptor\LogIntoHttpArchive;
use Amp\Http\Client\Interceptor\MatchOrigin;
use Amp\Http\Client\Interceptor\SetRequestHeader;
use Amp\Http\Client\Request;
Expand All @@ -13,21 +14,24 @@
Loop::run(static function () use ($argv) {
try {
$client = (new HttpClientBuilder)
->intercept(new LogIntoHttpArchive(__DIR__ . '/log.har'))
->intercept(new MatchOrigin(['https://amphp.org' => new SetRequestHeader('x-amphp', 'true')]))
->followRedirects(0)
->retry(3)
->build();

/** @var Response $response */
$response = yield $client->request(new Request($argv[1] ?? 'https://httpbin.org/user-agent'));
for ($i = 0; $i < 5; $i++) {
/** @var Response $response */
$response = yield $client->request(new Request($argv[1] ?? 'https://httpbin.org/user-agent'));

dumpRequestTrace($response->getRequest());
dumpResponseTrace($response);
dumpRequestTrace($response->getRequest());
dumpResponseTrace($response);

dumpResponseBodyPreview(yield $response->getBody()->buffer());
dumpResponseBodyPreview(yield $response->getBody()->buffer());
}
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
2 changes: 1 addition & 1 deletion examples/basic/7-gzip.php
Expand Up @@ -22,7 +22,7 @@
dumpResponseBodyPreview(yield $response->getBody()->buffer());
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
2 changes: 1 addition & 1 deletion examples/concurrency/1-concurrent-fetch.php
Expand Up @@ -39,7 +39,7 @@
}
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
6 changes: 3 additions & 3 deletions examples/concurrency/3-benchmark.php
Expand Up @@ -13,15 +13,15 @@
use Amp\Socket\ClientTlsContext;
use Amp\Socket\ConnectContext;
use function Amp\coroutine;
use function Amp\Internal\getCurrentTime;
use function Amp\getCurrentTime;

require __DIR__ . '/../.helper/functions.php';

$count = (int) ($argv[1] ?? "1000");

Loop::run(static function () use ($count, $argv) {
// Disable peer verification (not recommended, but we use a random test certificate here)
$tlsContext = (new ClientTlsContext)
$tlsContext = (new ClientTlsContext(''))
->withoutPeerVerification();

$connectContext = (new ConnectContext)
Expand All @@ -33,7 +33,7 @@

$handler = coroutine(static function (int $count) use ($client, $argv) {
for ($i = 0; $i < $count; $i++) {
$request = new Request($argv[2] ?? 'http://localhost:1337/');
$request = new Request($argv[2] ?? 'https://localhost:1338/');
$request->setTcpConnectTimeout(1000);
$request->setTlsHandshakeTimeout(1000);
$request->setTransferTimeout(1000);
Expand Down
2 changes: 1 addition & 1 deletion examples/streaming/1-large-response.php
Expand Up @@ -88,7 +88,7 @@ function formatBytes(int $size, int $precision = 2)
print \sprintf("%s has a size of %.2fMB\r\n", $path, (float) $size / 1024 / 1024);
} catch (HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
// The HttpClient::request() method itself will never throw directly, but returns a promise.
echo $error;
}
});
13 changes: 13 additions & 0 deletions src/Connection/Http1Connection.php
Expand Up @@ -12,6 +12,7 @@
use Amp\Emitter;
use Amp\Http;
use Amp\Http\Client\Connection\Internal\Http1Parser;
use Amp\Http\Client\HarAttributes;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Internal\ForbidCloning;
use Amp\Http\Client\Internal\ForbidSerialization;
Expand All @@ -35,6 +36,7 @@
use Amp\TimeoutCancellationToken;
use function Amp\asyncCall;
use function Amp\call;
use function Amp\getCurrentTime;

/**
* Socket client implementation.
Expand Down Expand Up @@ -188,7 +190,9 @@ private function request(Request $request, CancellationToken $cancellation): Pro
$id = $combinedCancellation->subscribe([$this, 'close']);

try {
$request->setAttribute(HarAttributes::TIME_SEND, getCurrentTime());
yield from $this->writeRequest($request, $protocolVersion, $combinedCancellation);
$request->setAttribute(HarAttributes::TIME_WAIT, getCurrentTime());
return yield from $this->readResponse($request, $cancellation, $combinedCancellation);
} finally {
$combinedCancellation->unsubscribe($id);
Expand Down Expand Up @@ -252,8 +256,15 @@ private function readResponse(

$parser = new Http1Parser($request, $bodyCallback, $trailersCallback);

$firstRead = true;

try {
while (null !== $chunk = yield $this->socket->read()) {
if ($firstRead) {
$request->setAttribute(HarAttributes::TIME_RECEIVE, getCurrentTime());
$firstRead = false;
}

$response = $parser->parse($chunk);
if ($response === null) {
continue;
Expand Down Expand Up @@ -326,6 +337,8 @@ private function readResponse(

$this->busy = false;

$request->setAttribute(HarAttributes::TIME_COMPLETE, getCurrentTime());

$bodyEmitter->complete();
$trailersDeferred->resolve($trailers);
} catch (\Throwable $e) {
Expand Down

0 comments on commit 0b40401

Please sign in to comment.