Skip to content

Commit 0f0d293

Browse files
authored
Merge pull request #284 from clue-labs/server-streaming
Documentation to recommend Server by default instead of advanced StreamingServer
2 parents 579d6c3 + 2119dd5 commit 0f0d293

17 files changed

Lines changed: 320 additions & 138 deletions

README.md

Lines changed: 202 additions & 90 deletions
Large diffs are not rendered by default.

examples/01-hello-world.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77

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

1010
$loop = Factory::create();
1111

12-
$server = new StreamingServer(function (ServerRequestInterface $request) {
12+
$server = new Server(function (ServerRequestInterface $request) {
1313
return new Response(
1414
200,
1515
array(

examples/02-count-visitors.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77

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

1010
$loop = Factory::create();
1111

1212
$counter = 0;
13-
$server = new StreamingServer(function (ServerRequestInterface $request) use (&$counter) {
13+
$server = new Server(function (ServerRequestInterface $request) use (&$counter) {
1414
return new Response(
1515
200,
16-
array('Content-Type' => 'text/plain'),
16+
array(
17+
'Content-Type' => 'text/plain'
18+
),
1719
"Welcome number " . ++$counter . "!\n"
1820
);
1921
});

examples/03-client-ip.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77

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

1010
$loop = Factory::create();
1111

12-
$server = new StreamingServer(function (ServerRequestInterface $request) {
12+
$server = new Server(function (ServerRequestInterface $request) {
1313
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
1414

1515
return new Response(
1616
200,
17-
array('Content-Type' => 'text/plain'),
17+
array(
18+
'Content-Type' => 'text/plain'
19+
),
1820
$body
1921
);
2022
});

examples/04-query-parameter.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77

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

1010
$loop = Factory::create();
1111

12-
$server = new StreamingServer(function (ServerRequestInterface $request) {
12+
$server = new Server(function (ServerRequestInterface $request) {
1313
$queryParams = $request->getQueryParams();
1414

1515
$body = 'The query parameter "foo" is not set. Click the following link ';
@@ -21,7 +21,9 @@
2121

2222
return new Response(
2323
200,
24-
array('Content-Type' => 'text/html'),
24+
array(
25+
'Content-Type' => 'text/html'
26+
),
2527
$body
2628
);
2729
});

examples/05-cookie-handling.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77

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

1010
$loop = Factory::create();
1111

12-
$server = new StreamingServer(function (ServerRequestInterface $request) {
12+
$server = new Server(function (ServerRequestInterface $request) {
1313
$key = 'react\php';
1414

1515
if (isset($request->getCookieParams()[$key])) {
1616
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
1717

1818
return new Response(
1919
200,
20-
array('Content-Type' => 'text/plain'),
20+
array(
21+
'Content-Type' => 'text/plain'
22+
),
2123
$body
2224
);
2325
}

examples/06-sleep.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77
use React\Promise\Promise;
88

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

1111
$loop = Factory::create();
1212

13-
$server = new StreamingServer(function (ServerRequestInterface $request) use ($loop) {
13+
$server = new Server(function (ServerRequestInterface $request) use ($loop) {
1414
return new Promise(function ($resolve, $reject) use ($request, $loop) {
1515
$loop->addTimer(1.5, function() use ($loop, $resolve) {
1616
$response = new Response(
1717
200,
18-
array('Content-Type' => 'text/plain'),
18+
array(
19+
'Content-Type' => 'text/plain'
20+
),
1921
"Hello world"
2022
);
2123
$resolve($response);

examples/07-error-handling.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77
use React\Promise\Promise;
88

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

1111
$loop = Factory::create();
1212

1313
$count = 0;
14-
$server = new StreamingServer(function (ServerRequestInterface $request) use (&$count) {
14+
$server = new Server(function (ServerRequestInterface $request) use (&$count) {
1515
return new Promise(function ($resolve, $reject) use (&$count) {
1616
$count++;
1717

@@ -21,7 +21,9 @@
2121

2222
$response = new Response(
2323
200,
24-
array('Content-Type' => 'text/plain'),
24+
array(
25+
'Content-Type' => 'text/plain'
26+
),
2527
"Hello World!\n"
2628
);
2729

examples/08-stream-response.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
55
use React\Http\Response;
6-
use React\Http\StreamingServer;
6+
use React\Http\Server;
77
use React\Stream\ThroughStream;
88

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

1111
$loop = Factory::create();
1212

13-
$server = new StreamingServer($loop,function (ServerRequestInterface $request) use ($loop) {
13+
// Note how this example still uses `Server` instead of `StreamingServer`.
14+
// The `StreamingServer` is only required for streaming *incoming* requests.
15+
$server = new Server($loop,function (ServerRequestInterface $request) use ($loop) {
1416
if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/') {
1517
return new Response(404);
1618
}
@@ -28,7 +30,9 @@
2830

2931
return new Response(
3032
200,
31-
array('Content-Type' => 'text/plain'),
33+
array(
34+
'Content-Type' => 'text/plain'
35+
),
3236
$stream
3337
);
3438
});

examples/09-stream-request.php

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

1111
$loop = Factory::create();
1212

13+
// Note how this example uses the advanced `StreamingServer` to allow streaming
14+
// the incoming HTTP request. This very simple example merely counts the size
15+
// of the streaming body, it does not otherwise buffer its contents in memory.
1316
$server = new StreamingServer(function (ServerRequestInterface $request) {
1417
return new Promise(function ($resolve, $reject) use ($request) {
1518
$contentLength = 0;
@@ -20,7 +23,9 @@
2023
$request->getBody()->on('end', function () use ($resolve, &$contentLength){
2124
$response = new Response(
2225
200,
23-
array('Content-Type' => 'text/plain'),
26+
array(
27+
'Content-Type' => 'text/plain'
28+
),
2429
"The length of the submitted request body is: " . $contentLength
2530
);
2631
$resolve($response);
@@ -30,7 +35,9 @@
3035
$request->getBody()->on('error', function (\Exception $exception) use ($resolve, &$contentLength) {
3136
$response = new Response(
3237
400,
33-
array('Content-Type' => 'text/plain'),
38+
array(
39+
'Content-Type' => 'text/plain'
40+
),
3441
"An error occured while reading at length: " . $contentLength
3542
);
3643
$resolve($response);

0 commit comments

Comments
 (0)