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
),

0 commit comments

Comments
 (0)