Skip to content

Instantly share code, notes, and snippets.

@WyriHaximus
Created May 11, 2016 19:12
Show Gist options
  • Select an option

  • Save WyriHaximus/37d632483793c6819b4c4c5c5581f1a6 to your computer and use it in GitHub Desktop.

Select an option

Save WyriHaximus/37d632483793c6819b4c4c5c5581f1a6 to your computer and use it in GitHub Desktop.
<html>
<form action="http://localhost:1337/" method="post" enctype="multipart/form-data">
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="text" name="name[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="file" name="file[]" /><br>
<input type="submit" /><br>
</form>
</html>
<?php
use React\Http\DeferredStream;
use React\Http\FormParserFactory;
use React\Http\Parser\NoBody;
use React\Http\Request;
use React\Http\Response;
use React\Promise\Deferred;
require 'vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$http = new React\Http\Server($socket);
$http->on('request', function (Request $request, Response $response) {
if ($request->getMethod() == 'GET') {
$response->writeHead(200, array('Content-Type' => 'text/html'));
$response->end(file_get_contents('example.html'));
return;
}
$parser = FormParserFactory::create($request);
if ($parser instanceof NoBody) {
$deferred = new Deferred();
$promise = $deferred->promise();
$buffer = '';
$request->on('data', function ($data) use (&$buffer, $deferred, $request) {
$buffer .= $data;
$json = json_decode($buffer);
if (json_last_error() == JSON_ERROR_NONE) {
$request->removeAllListeners('data');
$deferred->resolve($json);
}
});
} else {
$promise = DeferredStream::create($parser);
}
$promise->then(function ($result) use ($response) {
@var_export($result);
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end("Hello World!\n");
});
});
$socket->listen(1337);
$loop->run();
@andig
Copy link

andig commented May 12, 2016

I'm confused by the example's use of NoBody- why would we read and json-parse a request that does not contain a body? Do I have the wrong understanding of what body means in this context?

@WyriHaximus
Copy link
Author

Honestly these is just my debug files I use to test browser compatibility. NoBody is when we can't tell with certainty what the body consists off or how big the body is so we assume it doesn't have a body. Take a look at these comments, that is the path we're going to take: reactphp/http#41 (comment)

My apologize for the confusion. Might want to reconsider the naming of the class

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