Skip to content

Commit

Permalink
Copy event listeners to pushed requests and invoke requestEnd
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Aug 30, 2023
1 parent 474869d commit 95aaa2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Connection/Internal/Http2ConnectionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,10 @@ public function handlePushPromise(int $streamId, int $pushId, array $pseudo, arr
$request->setInactivityTimeout($parentStream->request->getInactivityTimeout());
$request->setTransferTimeout($parentStream->request->getTransferTimeout());

foreach ($parentStream->request->getEventListeners() as $eventListener) {
$request->addEventListener($eventListener);
}

$deferredCancellation = new DeferredCancellation();

$cancellation = new CompositeCancellation(
Expand Down Expand Up @@ -648,6 +652,17 @@ static function () {
events()->requestBodyStart($request, $stream->stream);
events()->requestBodyEnd($request, $stream->stream);

$stream->pendingResponse->getFuture()

Check failure on line 655 in src/Connection/Internal/Http2ConnectionProcessor.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

PossiblyNullReference

src/Connection/Internal/Http2ConnectionProcessor.php:655:35: PossiblyNullReference: Cannot call method getFuture on possibly null value (see https://psalm.dev/083)

Check failure on line 655 in src/Connection/Internal/Http2ConnectionProcessor.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

PossiblyNullReference

src/Connection/Internal/Http2ConnectionProcessor.php:655:35: PossiblyNullReference: Cannot call method getFuture on possibly null value (see https://psalm.dev/083)
->map(function (Response $response) use ($request) {
$response->getTrailers()->map(fn () => events()->requestEnd($request, $response))->ignore();
$response->getTrailers()->catch(fn (\Throwable $e) => events()->requestFailed(
$request,
$e instanceof HttpException ? $e : new HttpException('Unexpected exception: ' . $e->getMessage(), previous: $e)
))->ignore();
})
->catch(fn (\Throwable $e) => events()->requestFailed($request, $e instanceof HttpException ? $e : new HttpException('Unexpected exception: ' . $e->getMessage(), previous: $e)))
->ignore();

$stream->dependency = $streamId;

$this->streams[$pushId] = $stream;
Expand Down
17 changes: 14 additions & 3 deletions test/ClientHttpBinIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,32 @@ public function testHeaderCase(): void

public function testHttp2Push(): void
{
$eventListener = $this->createMock(EventListener::class);
$eventListener->expects($this->exactly(2))->method('requestStart');
$eventListener->expects($this->exactly(2))->method('requestEnd');
$eventListener->expects($this->exactly(1))->method('push');

$this->givenEventListener($eventListener);

$request = new Request('https://http2-server-push-demo.keksi.io/');
$request->setPushHandler(static function (Request $request, Future $response) use (
&$path,
&$contentType,
&$future,
&$pushedRequest
): void {
$future = $response;
$pushedRequest = $request;

$path = $request->getUri()->getPath();
$contentType = $response->await()->getHeader('content-type');
});

$this->executeRequest($request);

$future->await();
/** @var Response $pushedResponse */
$pushedResponse = $future->await();
$pushedResponse->getTrailers()->await();

self::assertSame('/image.jpg', $path);
self::assertSame('image/jpeg', $contentType);
Expand Down Expand Up @@ -818,9 +829,9 @@ private function createRequest(): Request
return new Request('http://' . $this->socket->getAddress());
}

private function givenApplicationInterceptor(ApplicationInterceptor $interceptor): void
private function givenEventListener(EventListener $eventListener): void
{
$this->builder = $this->builder->intercept($interceptor);
$this->builder = $this->builder->listen($eventListener);
$this->client = $this->builder->build();
}

Expand Down

0 comments on commit 95aaa2e

Please sign in to comment.