Skip to content

Commit fc1cf25

Browse files
WyriHaximusclue
authored andcommitted
Introduce using PSR-7 HTTP Status code constants
Introducing using these constants makes it easier to identify a specific HTTP status used in our code. And for our users to use readable status codes in their own code.
1 parent 5e1d1d4 commit fc1cf25

22 files changed

+67
-44
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"react/promise-stream": "^1.1",
3535
"react/socket": "^1.9",
3636
"react/stream": "^1.2",
37-
"ringcentral/psr7": "^1.2"
37+
"ringcentral/psr7": "^1.2",
38+
"fig/http-message-util": "^1.1"
3839
},
3940
"require-dev": {
4041
"clue/block-react": "^1.1",

examples/51-server-hello-world.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56

67
require __DIR__ . '/../vendor/autoload.php';
78

89
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
910
return new Response(
10-
200,
11+
StatusCodeInterface::STATUS_OK,
1112
array(
1213
'Content-Type' => 'text/plain'
1314
),

examples/52-server-count-visitors.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56

@@ -8,7 +9,7 @@
89
$counter = 0;
910
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$counter) {
1011
return new Response(
11-
200,
12+
StatusCodeInterface::STATUS_OK,
1213
array(
1314
'Content-Type' => 'text/plain'
1415
),

examples/53-server-whatsmyip.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56

@@ -9,7 +10,7 @@
910
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
1011

1112
return new Response(
12-
200,
13+
StatusCodeInterface::STATUS_OK,
1314
array(
1415
'Content-Type' => 'text/plain'
1516
),

examples/54-server-query-parameter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56

@@ -16,7 +17,7 @@
1617
}
1718

1819
return new Response(
19-
200,
20+
StatusCodeInterface::STATUS_OK,
2021
array(
2122
'Content-Type' => 'text/html'
2223
),

examples/55-server-cookie-handling.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56

@@ -12,7 +13,7 @@
1213
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
1314

1415
return new Response(
15-
200,
16+
StatusCodeInterface::STATUS_OK,
1617
array(
1718
'Content-Type' => 'text/plain'
1819
),
@@ -21,7 +22,7 @@
2122
}
2223

2324
return new Response(
24-
200,
25+
StatusCodeInterface::STATUS_OK,
2526
array(
2627
'Content-Type' => 'text/plain',
2728
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')

examples/56-server-sleep.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\EventLoop\Loop;
56
use React\Http\Message\Response;
@@ -11,7 +12,7 @@
1112
return new Promise(function ($resolve, $reject) {
1213
Loop::addTimer(1.5, function() use ($resolve) {
1314
$response = new Response(
14-
200,
15+
StatusCodeInterface::STATUS_OK,
1516
array(
1617
'Content-Type' => 'text/plain'
1718
),

examples/57-server-error-handling.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56
use React\Promise\Promise;
@@ -16,7 +17,7 @@
1617
}
1718

1819
$response = new Response(
19-
200,
20+
StatusCodeInterface::STATUS_OK,
2021
array(
2122
'Content-Type' => 'text/plain'
2223
),

examples/58-server-stream-response.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\EventLoop\Loop;
56
use React\Http\Message\Response;
@@ -9,7 +10,7 @@
910

1011
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
1112
if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/') {
12-
return new Response(404);
13+
return new Response(StatusCodeInterface::STATUS_NOT_FOUND);
1314
}
1415

1516
$stream = new ThroughStream();
@@ -30,7 +31,7 @@
3031
});
3132

3233
return new Response(
33-
200,
34+
StatusCodeInterface::STATUS_OK,
3435
array(
3536
'Content-Type' => 'text/plain'
3637
),

examples/59-server-json-api.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// $ php examples/59-server-json-api.php 8080
77
// $ curl -v http://localhost:8080/ -H 'Content-Type: application/json' -d '{"name":"Alice"}'
88

9+
use Fig\Http\Message\StatusCodeInterface;
910
use Psr\Http\Message\ServerRequestInterface;
1011
use React\Http\Message\Response;
1112

@@ -14,7 +15,7 @@
1415
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
1516
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
1617
return new Response(
17-
415, // Unsupported Media Type
18+
StatusCodeInterface::STATUS_UNSUPPORTED_MEDIA_TYPE,
1819
array(
1920
'Content-Type' => 'application/json'
2021
),
@@ -25,7 +26,7 @@
2526
$input = json_decode($request->getBody()->getContents());
2627
if (json_last_error() !== JSON_ERROR_NONE) {
2728
return new Response(
28-
400, // Bad Request
29+
StatusCodeInterface::STATUS_BAD_REQUEST,
2930
array(
3031
'Content-Type' => 'application/json'
3132
),
@@ -35,7 +36,7 @@
3536

3637
if (!isset($input->name) || !is_string($input->name)) {
3738
return new Response(
38-
422, // Unprocessable Entity
39+
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
3940
array(
4041
'Content-Type' => 'application/json'
4142
),
@@ -44,7 +45,7 @@
4445
}
4546

4647
return new Response(
47-
200,
48+
StatusCodeInterface::STATUS_OK,
4849
array(
4950
'Content-Type' => 'application/json'
5051
),

examples/61-server-hello-world-https.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
34
use Psr\Http\Message\ServerRequestInterface;
45
use React\Http\Message\Response;
56

67
require __DIR__ . '/../vendor/autoload.php';
78

89
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
910
return new Response(
10-
200,
11+
StatusCodeInterface::STATUS_OK,
1112
array(
1213
'Content-Type' => 'text/plain'
1314
),

examples/62-server-form-upload.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// $ curl --form name=test --form age=30 http://localhost:8080/
88
// $ curl --form name=hi --form [email protected] http://localhost:8080/
99

10+
use Fig\Http\Message\StatusCodeInterface;
1011
use Psr\Http\Message\ServerRequestInterface;
1112
use Psr\Http\Message\UploadedFileInterface;
1213
use React\Http\Message\Response;
@@ -110,7 +111,7 @@
110111
HTML;
111112

112113
return new Response(
113-
200,
114+
StatusCodeInterface::STATUS_OK,
114115
array(
115116
'Content-Type' => 'text/html; charset=UTF-8'
116117
),

examples/63-server-streaming-request.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Fig\Http\Message\StatusCodeInterface;
4+
35
require __DIR__ . '/../vendor/autoload.php';
46

57
// Note how this example uses the advanced `StreamingRequestMiddleware` to allow streaming
@@ -20,7 +22,7 @@ function (Psr\Http\Message\ServerRequestInterface $request) {
2022

2123
$body->on('end', function () use ($resolve, &$bytes){
2224
$resolve(new React\Http\Message\Response(
23-
200,
25+
StatusCodeInterface::STATUS_OK,
2426
array(
2527
'Content-Type' => 'text/plain'
2628
),
@@ -31,7 +33,7 @@ function (Psr\Http\Message\ServerRequestInterface $request) {
3133
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
3234
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
3335
$resolve(new React\Http\Message\Response(
34-
400,
36+
StatusCodeInterface::STATUS_BAD_REQUEST,
3537
array(
3638
'Content-Type' => 'text/plain'
3739
),

examples/71-server-http-proxy.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// $ php examples/71-server-http-proxy.php 8080
44
// $ curl -v --proxy http://localhost:8080 http://reactphp.org/
55

6+
use Fig\Http\Message\StatusCodeInterface;
67
use Psr\Http\Message\RequestInterface;
78
use React\Http\Message\Response;
89
use RingCentral\Psr7;
@@ -16,7 +17,7 @@
1617
$http = new React\Http\HttpServer(function (RequestInterface $request) {
1718
if (strpos($request->getRequestTarget(), '://') === false) {
1819
return new Response(
19-
400,
20+
StatusCodeInterface::STATUS_BAD_REQUEST,
2021
array(
2122
'Content-Type' => 'text/plain'
2223
),
@@ -36,7 +37,7 @@
3637
// left up as an exercise: use an HTTP client to send the outgoing request
3738
// and forward the incoming response to the original client request
3839
return new Response(
39-
200,
40+
StatusCodeInterface::STATUS_OK,
4041
array(
4142
'Content-Type' => 'text/plain'
4243
),

examples/72-server-http-connect-proxy.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// $ php examples/72-server-http-connect-proxy.php 8080
44
// $ curl -v --proxy http://localhost:8080 https://reactphp.org/
55

6+
use Fig\Http\Message\StatusCodeInterface;
67
use Psr\Http\Message\ServerRequestInterface;
78
use React\Http\Message\Response;
89
use React\Socket\Connector;
@@ -19,7 +20,7 @@
1920
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use ($connector) {
2021
if ($request->getMethod() !== 'CONNECT') {
2122
return new Response(
22-
405,
23+
StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED,
2324
array(
2425
'Content-Type' => 'text/plain',
2526
'Allow' => 'CONNECT'
@@ -33,14 +34,14 @@
3334
function (ConnectionInterface $remote) {
3435
// connection established => forward data
3536
return new Response(
36-
200,
37+
StatusCodeInterface::STATUS_OK,
3738
array(),
3839
$remote
3940
);
4041
},
4142
function (Exception $e) {
4243
return new Response(
43-
502,
44+
StatusCodeInterface::STATUS_BAD_GATEWAY,
4445
array(
4546
'Content-Type' => 'text/plain'
4647
),

examples/81-server-upgrade-echo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
< world
1818
*/
1919

20+
use Fig\Http\Message\StatusCodeInterface;
2021
use Psr\Http\Message\ServerRequestInterface;
2122
use React\EventLoop\Loop;
2223
use React\Http\Message\Response;
@@ -30,7 +31,7 @@
3031
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
3132
if ($request->getHeaderLine('Upgrade') !== 'echo' || $request->getProtocolVersion() === '1.0') {
3233
return new Response(
33-
426,
34+
StatusCodeInterface::STATUS_UPGRADE_REQUIRED,
3435
array(
3536
'Upgrade' => 'echo'
3637
),
@@ -48,7 +49,7 @@
4849
});
4950

5051
return new Response(
51-
101,
52+
StatusCodeInterface::STATUS_SWITCHING_PROTOCOLS,
5253
array(
5354
'Upgrade' => 'echo'
5455
),

examples/82-server-upgrade-chat.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Hint: try this with multiple connections :)
2020
*/
2121

22+
use Fig\Http\Message\StatusCodeInterface;
2223
use Psr\Http\Message\ServerRequestInterface;
2324
use React\EventLoop\Loop;
2425
use React\Http\Message\Response;
@@ -38,7 +39,7 @@
3839
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use ($chat) {
3940
if ($request->getHeaderLine('Upgrade') !== 'chat' || $request->getProtocolVersion() === '1.0') {
4041
return new Response(
41-
426,
42+
StatusCodeInterface::STATUS_UPGRADE_REQUIRED,
4243
array(
4344
'Upgrade' => 'chat'
4445
),
@@ -76,7 +77,7 @@
7677
});
7778

7879
return new Response(
79-
101,
80+
StatusCodeInterface::STATUS_SWITCHING_PROTOCOLS,
8081
array(
8182
'Upgrade' => 'chat'
8283
),

0 commit comments

Comments
 (0)