Skip to content

Body Stream Closes when running Browser Request #510

@mrsimonbennett

Description

@mrsimonbennett

So I am trying to write some middleware that will authenticate the request, todo the middleware needs to make a call to another HTTP server to find out, (the final version will use cache)

I have boiled down to the smallest working example of things breaking. Observe the body changing to unreadable.

If I use a StreamingRequestMiddleware everything will work fine, however as soon as I do a browser request the request body closes and becomes unreadable.

Maybe I am missing something and this is how it's meant to work, if so a workaround or solution direction would be most welcomed.

I don't really want to do a blocking http call, (I have tested this and this works other than it blocking the whole server)

<?php
require __DIR__ . '/../vendor/autoload.php';

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\HttpServer;
use React\Http\Middleware\StreamingRequestMiddleware;


$http = new HttpServer(

    new StreamingRequestMiddleware(),
    function (ServerRequestInterface $request, callable $next) {
        dump($request->getBody()->isReadable()); //true


        return $next($request);
    },
    function (ServerRequestInterface $request, callable $next) {
        dump($request->getBody()->isReadable()); //true

        $browser = new React\Http\Browser();

        return $browser->get('https://www.google.com')->then(
            function (ResponseInterface $response) use ($next, $request) {
                dump($request->getBody()->isReadable()); //false

                return $next($request);
            }
        );
    },
    function (ServerRequestInterface $request) {

        dump($request->getBody()->isReadable()); //false

        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            'Hello World!'
        );

    }
);
$socket = new React\Socket\SocketServer('127.0.0.1:8080');
$http->listen($socket);
echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL;

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