Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling data and end events #166

Closed
hotrush opened this issue Apr 13, 2017 · 5 comments
Closed

Handling data and end events #166

hotrush opened this issue Apr 13, 2017 · 5 comments
Labels

Comments

@hotrush
Copy link

hotrush commented Apr 13, 2017

Why next code not working with 0.4 version?

$loop = \React\EventLoop\Factory::create();
$socket = new \React\Socket\Server($loop);
$http = new \React\Http\Server($socket);
$http->on('request', function (\React\Http\Request $request, \React\Http\Response $response) {

    $contentLength = 0;
    $request->on('data', function ($data) use (&$contentLength) {
        $contentLength += strlen($data);
    });

    $request->on('end', function () use ($response, &$contentLength){
        $response->writeHead(200, array('Content-Type' => 'text/plain'));
        $response->end("The length of the submitted request body is: " . $contentLength);
    });

    // an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
    $request->on('error', function (\Exception $exception) use ($response, &$contentLength) {
        $response->writeHead(400, array('Content-Type' => 'text/plain'));
        $response->end("An error occured while reading at length: " . $contentLength);
    });

});
$socket->listen(8080);
$loop->run();

seems that end event not emmiting

@clue clue added the question label Apr 13, 2017
@clue
Copy link
Member

clue commented Apr 13, 2017

The stream handling logic has been implemented as part of the v0.6.0 release, see the changelog for more details: https://github.com/reactphp/http/blob/master/CHANGELOG.md#060-2016-03-09

As such, you're highly recommended to use the latest version of this package.

I hope this helps 👍

@clue clue closed this as completed Apr 13, 2017
@hotrush
Copy link
Author

hotrush commented Apr 14, 2017

I'm happy to update, but it is impossible because of depending on lot of other libraries.

Is there any way to handle post data in 0.4 version?

@clue
Copy link
Member

clue commented Apr 14, 2017

I would suggest making sure everything is compatible with the latest versions and file issues and PRs for all libraries that may not be compatible yet. Updating each library should usually not take more than a few minutes, so I wonder what libraries you're referring to?

Our focus is currently on supporting the latest features in the latest tagged releases, but I suppose we would accept PRs that backport relevant functionality to old release branches as long as BC is preserved 👍

@hotrush
Copy link
Author

hotrush commented Apr 14, 2017

I have found a workaround that works for me:

$loop = \React\EventLoop\Factory::create();
$socket = new \React\Socket\Server(8080, $loop);
$http = new \React\Http\Server($socket);
$http->on('request', function (\React\Http\Request $request, \React\Http\Response $response) {

    if ($request->hasHeader('Content-Length') && $request->getHeader('Content-Length')[0] > 0) {
        $request->on('data', function ($data) use ($response) {
            echo 'Data'.PHP_EOL;
            $response->writeHead(200, array('Content-Type' => 'text/plain'));
            $response->end("The length of the submitted request body is: " . serialize($data));
        });
    } else {
        echo 'End'.PHP_EOL;
        $response->writeHead(200, array('Content-Type' => 'text/plain'));
        $response->end('No data provided');
    }

    // an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
    $request->on('error', function (\Exception $exception) use ($response, &$contentLength) {
        echo 'Error'.PHP_EOL;
        $response->writeHead(400, array('Content-Type' => 'text/plain'));
        $response->end("An error occured while reading at length: " . $contentLength);
    });

});
$loop->run();

At now i have not enough time to pr, but need to make this work very soon) Thanks

@clue
Copy link
Member

clue commented Apr 14, 2017

Happy to hear to this works for you 👍 This may or may not work for some smaller request bodies, but will certainly break for larger bodies or using chunked transfer encoding. See also the referenced release notes and all tickets linked to this release to see why we decided to bump the major version here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants