Skip to content

Commit 7ec68c5

Browse files
committed
Replace Request class with PSR-7 Request
1 parent aaca3fb commit 7ec68c5

File tree

9 files changed

+481
-433
lines changed

9 files changed

+481
-433
lines changed

examples/01-hello-world.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
use React\EventLoop\Factory;
44
use React\Socket\Server;
5-
use React\Http\Request;
65
use React\Http\Response;
6+
use Psr\Http\Message\RequestInterface;
77

88
require __DIR__ . '/../vendor/autoload.php';
99

1010
$loop = Factory::create();
1111
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
1212

13-
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
13+
$server = new \React\Http\Server($socket, function (RequestInterface $request, Response $response) {
1414
$response->writeHead(200, array('Content-Type' => 'text/plain'));
1515
$response->end("Hello world!\n");
1616
});

examples/02-hello-world-https.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
use React\EventLoop\Factory;
44
use React\Socket\Server;
5-
use React\Http\Request;
65
use React\Http\Response;
76
use React\Socket\SecureServer;
7+
use Psr\Http\Message\RequestInterface;
88

99
require __DIR__ . '/../vendor/autoload.php';
1010

@@ -14,7 +14,7 @@
1414
'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem'
1515
));
1616

17-
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
17+
$server = new \React\Http\Server($socket, function (RequestInterface $request, Response $response) {
1818
$response->writeHead(200, array('Content-Type' => 'text/plain'));
1919
$response->end("Hello world!\n");
2020
});

examples/03-handling-body-data.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@
22

33
use React\EventLoop\Factory;
44
use React\Socket\Server;
5-
use React\Http\Request;
65
use React\Http\Response;
6+
use Psr\Http\Message\RequestInterface;
77

88
require __DIR__ . '/../vendor/autoload.php';
99

1010
$loop = Factory::create();
1111
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
1212

13-
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
13+
$server = new \React\Http\Server($socket, function (RequestInterface $request, Response $response) {
1414
$contentLength = 0;
15-
$request->on('data', function ($data) use (&$contentLength) {
15+
$request->getBody()->on('data', function ($data) use (&$contentLength) {
1616
$contentLength += strlen($data);
1717
});
1818

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

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

src/HttpBodyStream.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
namespace React\Http;
3+
4+
use Psr\Http\Message\StreamInterface;
5+
use React\Stream\ReadableStreamInterface;
6+
use React\Stream\WritableStreamInterface;
7+
use Evenement\EventEmitter;
8+
use React\Stream\Util;
9+
10+
/**
11+
* @internal
12+
* Uses a StreamInterface from PSR-7 and a ReadableStreamInterface from ReactPHP
13+
* to use PSR-7 objects and use ReactPHP streams
14+
* This is class is used in `HttpServer` to stream the body of a response
15+
* to the client. Because of this you can stream big amount of data without
16+
* necessity of buffering this data. The data will be send directly to the client.
17+
*/
18+
class HttpBodyStream extends EventEmitter implements StreamInterface, ReadableStreamInterface
19+
{
20+
private $input;
21+
private $closed = false;
22+
private $size;
23+
24+
/**
25+
* @param ReadableStreamInterface $input - Stream data from $stream as a body of a PSR-7 object4
26+
* @param int|null $size - size of the data body
27+
*/
28+
public function __construct(ReadableStreamInterface $input, $size)
29+
{
30+
$this->input = $input;
31+
$this->size = $size;
32+
33+
$this->input->on('data', array($this, 'handleData'));
34+
$this->input->on('end', array($this, 'handleEnd'));
35+
$this->input->on('error', array($this, 'handleError'));
36+
$this->input->on('close', array($this, 'close'));
37+
}
38+
39+
public function isReadable()
40+
{
41+
return !$this->closed && $this->input->isReadable();
42+
}
43+
44+
public function pause()
45+
{
46+
$this->input->pause();
47+
}
48+
49+
public function resume()
50+
{
51+
$this->input->resume();
52+
}
53+
54+
public function pipe(WritableStreamInterface $dest, array $options = array())
55+
{
56+
Util::pipe($this, $dest, $options);
57+
58+
return $dest;
59+
}
60+
61+
public function close()
62+
{
63+
if ($this->closed) {
64+
return;
65+
}
66+
67+
$this->closed = true;
68+
69+
$this->input->close();
70+
71+
$this->emit('close');
72+
$this->removeAllListeners();
73+
}
74+
75+
public function getSize()
76+
{
77+
return $this->size;
78+
}
79+
80+
/** @ignore */
81+
public function __toString()
82+
{
83+
return '';
84+
}
85+
86+
/** @ignore */
87+
public function detach()
88+
{
89+
return null;
90+
}
91+
92+
/** @ignore */
93+
public function tell()
94+
{
95+
throw new \BadMethodCallException();
96+
}
97+
98+
/** @ignore */
99+
public function eof()
100+
{
101+
throw new \BadMethodCallException();
102+
}
103+
104+
/** @ignore */
105+
public function isSeekable()
106+
{
107+
return false;
108+
}
109+
110+
/** @ignore */
111+
public function seek($offset, $whence = SEEK_SET)
112+
{
113+
throw new \BadMethodCallException();
114+
}
115+
116+
/** @ignore */
117+
public function rewind()
118+
{
119+
throw new \BadMethodCallException();
120+
}
121+
122+
/** @ignore */
123+
public function isWritable()
124+
{
125+
return false;
126+
}
127+
128+
/** @ignore */
129+
public function write($string)
130+
{
131+
throw new \BadMethodCallException();
132+
}
133+
134+
/** @ignore */
135+
public function read($length)
136+
{
137+
throw new \BadMethodCallException();
138+
}
139+
140+
/** @ignore */
141+
public function getContents()
142+
{
143+
return '';
144+
}
145+
146+
/** @ignore */
147+
public function getMetadata($key = null)
148+
{
149+
return null;
150+
}
151+
152+
/** @internal */
153+
public function handleData($data)
154+
{
155+
$this->emit('data', array($data));
156+
}
157+
158+
/** @internal */
159+
public function handleError(\Exception $e)
160+
{
161+
$this->emit('error', array($e));
162+
$this->close();
163+
}
164+
165+
/** @internal */
166+
public function handleEnd()
167+
{
168+
if (!$this->closed) {
169+
$this->emit('end');
170+
$this->close();
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)