Skip to content

EmptyBodyStream as request body not working #497

Closed

Description

@martin-heralecky

There seems to be a problem when attempting to pass EmptyBodyStream as body to HTTP requests:

(new Browser())
    ->requestStreaming("GET", "https://stackoverflow.com", [], new \React\Http\Io\EmptyBodyStream())
    ->then(var_dump(...)); // never get's triggered

Whereas, empty resource works just fine:

(new Browser())
    ->requestStreaming("GET", "https://stackoverflow.com", [], new ReadableResourceStream(tmpfile()))
    ->then(var_dump(...));

I have tried other servers (URLs) as well, and for some of them I get a response immediatelly, for some there is a 10s delay. Maybe the socket isn't being closed when using EmptyBodyStream?


The full example (reason I care) is that I though this would work as a simple HTTP proxy:

$browser = new Browser();
$browser = $browser->withRejectErrorResponse(false);

$server = new HttpServer(
    new StreamingRequestMiddleware(),
    fn(ServerRequestInterface $req) => $browser
        ->requestStreaming($req->getMethod(), TARGET_URL, [], $req->getBody())
        ->then(fn(ResponseInterface $res) => new Response($res->getStatusCode(), [], $res->getBody())),
);

But it only works for requests with non-empty bodies. Otherwise $req->getBody() is EmptyBodyStream and the requests get stuck.

Activity

  1. SimonFrings commented on Feb 22, 2024

    @SimonFrings
    Member

    Hey @martin-heralecky, thanks for bringing this up 👍

    I talked with @clue about this, and it indeed seems to be a bug that has found its way into the project over the last few releases. We've already started working on a fix for this, so I will keep you updated as we make progress.

    As you also may know, we're currently working on ReactPHP v3 and releasing the roadmap tickets for each component (see reactphp/event-loop#271 and others). The HTTP roadmap ticket will also be released soon, and to give you a sneak peek, we plan to release at least v1.10.0 before branching out with a 3.x branch. So, I think it makes sense to resolve this bug as part of the upcoming v1.1.0.0 and thus also for the upcoming v3.

  2. added this to the v1.10.0 milestone on Feb 22, 2024
  3. clue commented on Feb 22, 2024

    @clue
    Member

    @martin-heralecky Again thank you for reporting, that was a nasty one! I've just filed #516 which fixes the problem and introduces additional tests to avoid any future regressions.

    I've double-checked your gist, and I'm happy to confirm that it now works flawlessly with the applied changes. The fix may actually look surprisingly simple, but analyzing the problem and coming up with this fix took a couple of hours. If you'd like to support this development, consider sponsoring ReactPHP! ❤️ Thank you!

  4. martin-heralecky commented on Feb 28, 2024

    @martin-heralecky
    Author

    Thanks a lot! I'll definitely consider sponsoring the project. You guys are doing an excelent job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions