Skip to content

Streaming HTTP requests & awaiting promises #453

@bartvanhoutte

Description

@bartvanhoutte

Rather unsure on where to post this because it involves streams, async/await, the event loop and http libraries, so don't hesitate to tell me to move it somewhere else.

Currently, I'm trying to pass on a readable stream from a server request to a HTTP client. However, whenever you call await before passing on the stream*, the stream receives data in the event loop and is closed before the HTTP client is able to make a request, eventually raising an exception as the HTTP client's timeout has been reached.

*I need to do some - eventually cached - async I/O in a middleware.

$server = new HttpServer(
  new StreamingRequestMiddleware,
  async(function (ServerRequestInterface $request) {
      try {
          $body = $request->getBody();
          assert($body instanceof ReadableStreamInterface);

          await(\React\Promise\Timer\sleep(1));

          $browser = new Browser;

          // $body->closed === true
          await($browser->put(
            'http://httpstat.us/200',
            ['Content-Length' => $body->getSize()],
            $body
          ));

          fwrite(STDOUT, 'done');
          $response = new Response(200);
      } catch (Exception $e) {
          fwrite(STDERR, $e->getMessage());
          $response = new Response(500);
      }

      return $response;
  }));

Versions used: react/async:4.x-dev and react/http:1.6.0.

Is this a bug or just not possible in the current design of the libraries? Is there a workaround for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions