Skip to content

Commit ffa9aae

Browse files
authored
Merge pull request #432 from clue-labs/status-codes
2 parents 5e1d1d4 + 50af563 commit ffa9aae

24 files changed

+100
-82
lines changed

README.md

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ require __DIR__ . '/vendor/autoload.php';
109109

110110
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
111111
return new React\Http\Message\Response(
112-
200,
112+
React\Http\Message\Response::STATUS_OK,
113113
array(
114114
'Content-Type' => 'text/plain'
115115
),
@@ -735,7 +735,7 @@ object and expects a [response](#server-response) object in return:
735735
```php
736736
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
737737
return new React\Http\Message\Response(
738-
200,
738+
React\Http\Message\Response::STATUS_OK,
739739
array(
740740
'Content-Type' => 'text/plain'
741741
),
@@ -953,7 +953,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
953953
$body .= "The requested path is: " . $request->getUri()->getPath();
954954

955955
return new React\Http\Message\Response(
956-
200,
956+
React\Http\Message\Response::STATUS_OK,
957957
array(
958958
'Content-Type' => 'text/plain'
959959
),
@@ -995,7 +995,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
995995
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
996996

997997
return new React\Http\Message\Response(
998-
200,
998+
React\Http\Message\Response::STATUS_OK,
999999
array(
10001000
'Content-Type' => 'text/plain'
10011001
),
@@ -1027,7 +1027,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
10271027
}
10281028

10291029
return new React\Http\Message\Response(
1030-
200,
1030+
React\Http\Message\Response::STATUS_OK,
10311031
array(
10321032
'Content-Type' => 'text/html'
10331033
),
@@ -1074,7 +1074,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
10741074
$name = $request->getParsedBody()['name'] ?? 'anonymous';
10751075

10761076
return new React\Http\Message\Response(
1077-
200,
1077+
React\Http\Message\Response::STATUS_OK,
10781078
array(),
10791079
"Hello $name!\n"
10801080
);
@@ -1099,7 +1099,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
10991099
$name = $data->name ?? 'anonymous';
11001100

11011101
return new React\Http\Message\Response(
1102-
200,
1102+
React\Http\Message\Response::STATUS_OK,
11031103
array('Content-Type' => 'application/json'),
11041104
json_encode(['message' => "Hello $name!"])
11051105
);
@@ -1122,7 +1122,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
11221122
$name = isset($files['avatar']) ? $files['avatar']->getClientFilename() : 'nothing';
11231123

11241124
return new React\Http\Message\Response(
1125-
200,
1125+
React\Http\Message\Response::STATUS_OK,
11261126
array(),
11271127
"Uploaded $name\n"
11281128
);
@@ -1205,7 +1205,7 @@ $http = new React\Http\HttpServer(
12051205

12061206
$body->on('end', function () use ($resolve, &$bytes){
12071207
$resolve(new React\Http\Message\Response(
1208-
200,
1208+
React\Http\Message\Response::STATUS_OK,
12091209
array(
12101210
'Content-Type' => 'text/plain'
12111211
),
@@ -1216,7 +1216,7 @@ $http = new React\Http\HttpServer(
12161216
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
12171217
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
12181218
$resolve(new React\Http\Message\Response(
1219-
400,
1219+
React\Http\Message\Response::STATUS_BAD_REQUEST,
12201220
array(
12211221
'Content-Type' => 'text/plain'
12221222
),
@@ -1272,7 +1272,7 @@ $http = new React\Http\HttpServer(
12721272
$body .= 'This example does not accept chunked transfer encoding.';
12731273

12741274
return new React\Http\Message\Response(
1275-
411,
1275+
React\Http\Message\Response::STATUS_LENGTH_REQUIRED,
12761276
array(
12771277
'Content-Type' => 'text/plain'
12781278
),
@@ -1281,7 +1281,7 @@ $http = new React\Http\HttpServer(
12811281
}
12821282

12831283
return new React\Http\Message\Response(
1284-
200,
1284+
React\Http\Message\Response::STATUS_OK,
12851285
array(
12861286
'Content-Type' => 'text/plain'
12871287
),
@@ -1343,7 +1343,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
13431343
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
13441344

13451345
return new React\Http\Message\Response(
1346-
200,
1346+
React\Http\Message\Response::STATUS_OK,
13471347
array(
13481348
'Content-Type' => 'text/plain'
13491349
),
@@ -1352,7 +1352,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
13521352
}
13531353

13541354
return new React\Http\Message\Response(
1355-
200,
1355+
React\Http\Message\Response::STATUS_OK,
13561356
array(
13571357
'Content-Type' => 'text/plain',
13581358
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
@@ -1410,7 +1410,7 @@ In its most simple form, you can use it like this:
14101410
```php
14111411
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
14121412
return new React\Http\Message\Response(
1413-
200,
1413+
React\Http\Message\Response::STATUS_OK,
14141414
array(
14151415
'Content-Type' => 'text/plain'
14161416
),
@@ -1440,7 +1440,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
14401440
return new Promise(function ($resolve, $reject) {
14411441
Loop::addTimer(1.5, function() use ($resolve) {
14421442
$response = new React\Http\Message\Response(
1443-
200,
1443+
React\Http\Message\Response::STATUS_OK,
14441444
array(
14451445
'Content-Type' => 'text/plain'
14461446
),
@@ -1487,7 +1487,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
14871487
});
14881488

14891489
return new React\Http\Message\Response(
1490-
200,
1490+
React\Http\Message\Response::STATUS_OK,
14911491
array(
14921492
'Content-Type' => 'text/plain'
14931493
),
@@ -1568,7 +1568,7 @@ a `string` response body like this:
15681568
```php
15691569
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
15701570
return new React\Http\Message\Response(
1571-
200,
1571+
React\Http\Message\Response::STATUS_OK,
15721572
array(
15731573
'Content-Type' => 'text/plain'
15741574
),
@@ -1593,7 +1593,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
15931593
});
15941594

15951595
return new React\Http\Message\Response(
1596-
200,
1596+
React\Http\Message\Response::STATUS_OK,
15971597
array(
15981598
'Content-Length' => '13',
15991599
'Content-Type' => 'text/plain',
@@ -1663,7 +1663,7 @@ a custom `Server` response header like this:
16631663
```php
16641664
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
16651665
return new React\Http\Message\Response(
1666-
200,
1666+
React\Http\Message\Response::STATUS_OK,
16671667
array(
16681668
'Server' => 'PHP/3'
16691669
)
@@ -1678,7 +1678,7 @@ string value like this:
16781678
```php
16791679
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
16801680
return new React\Http\Message\Response(
1681-
200,
1681+
React\Http\Message\Response::STATUS_OK,
16821682
array(
16831683
'Server' => ''
16841684
)
@@ -1693,7 +1693,7 @@ like this:
16931693
```php
16941694
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
16951695
return new React\Http\Message\Response(
1696-
200,
1696+
React\Http\Message\Response::STATUS_OK,
16971697
array(
16981698
'Date' => gmdate('D, d M Y H:i:s \G\M\T')
16991699
)
@@ -1708,7 +1708,7 @@ like this:
17081708
```php
17091709
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
17101710
return new React\Http\Message\Response(
1711-
200,
1711+
React\Http\Message\Response::STATUS_OK,
17121712
array(
17131713
'Date' => ''
17141714
)
@@ -1786,7 +1786,7 @@ encourages [Third-Party Middleware](#third-party-middleware) implementations.
17861786
In order to use middleware request handlers, simply pass a list of all
17871787
callables as defined above to the [`HttpServer`](#httpserver).
17881788
The following example adds a middleware request handler that adds the current time to the request as a
1789-
header (`Request-Time`) and a final request handler that always returns a 200 code without a body:
1789+
header (`Request-Time`) and a final request handler that always returns a `200 OK` status code without a body:
17901790

17911791
```php
17921792
$http = new React\Http\HttpServer(
@@ -1795,7 +1795,7 @@ $http = new React\Http\HttpServer(
17951795
return $next($request);
17961796
},
17971797
function (Psr\Http\Message\ServerRequestInterface $request) {
1798-
return new React\Http\Message\Response(200);
1798+
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
17991799
}
18001800
);
18011801
```
@@ -1821,7 +1821,7 @@ $http = new React\Http\HttpServer(
18211821
});
18221822
},
18231823
function (Psr\Http\Message\ServerRequestInterface $request) {
1824-
return new React\Http\Message\Response(200);
1824+
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
18251825
}
18261826
);
18271827
```
@@ -1842,7 +1842,7 @@ $http = new React\Http\HttpServer(
18421842
});
18431843
return $promise->then(null, function (Exception $e) {
18441844
return new React\Http\Message\Response(
1845-
500,
1845+
React\Http\Message\Response::STATUS_INTERNAL_SERVER_ERROR,
18461846
array(),
18471847
'Internal error: ' . $e->getMessage()
18481848
);
@@ -1852,7 +1852,7 @@ $http = new React\Http\HttpServer(
18521852
if (mt_rand(0, 1) === 1) {
18531853
throw new RuntimeException('Database error');
18541854
}
1855-
return new React\Http\Message\Response(200);
1855+
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
18561856
}
18571857
);
18581858
```
@@ -2439,7 +2439,7 @@ represent an outgoing server response message.
24392439

24402440
```php
24412441
$response = new React\Http\Message\Response(
2442-
200,
2442+
React\Http\Message\Response::STATUS_OK,
24432443
array(
24442444
'Content-Type' => 'text/html'
24452445
),
@@ -2452,6 +2452,13 @@ This class implements the
24522452
which in turn extends the
24532453
[PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).
24542454

2455+
On top of this, this class implements the
2456+
[PSR-7 Message Util `StatusCodeInterface`](https://github.com/php-fig/http-message-util/blob/master/src/StatusCodeInterface.php)
2457+
which means that most common HTTP status codes are available as class
2458+
constants with the `STATUS_*` prefix. For instance, the `200 OK` and
2459+
`404 Not Found` status codes can used as `Response::STATUS_OK` and
2460+
`Response::STATUS_NOT_FOUND` respectively.
2461+
24552462
> Internally, this implementation builds on top of an existing incoming
24562463
response message and only adds required streaming support. This base class is
24572464
considered an implementation detail that may change in the future.
@@ -2516,7 +2523,7 @@ $http = new React\Http\HttpServer(
25162523
});
25172524
$body->on('close', function () use (&$bytes, $resolve) {
25182525
$resolve(new React\Http\Message\Response(
2519-
200,
2526+
React\Http\Message\Response::STATUS_OK,
25202527
[],
25212528
"Received $bytes bytes\n"
25222529
));
@@ -2653,7 +2660,7 @@ $http = new React\Http\HttpServer(
26532660
new React\Http\Middleware\RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
26542661
function (Psr\Http\Message\ServerRequestInterface $request) {
26552662
// The body from $request->getBody() is now fully available without the need to stream it
2656-
return new React\Http\Message\Response(200);
2663+
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
26572664
},
26582665
);
26592666
```
@@ -2700,7 +2707,7 @@ $handler = function (Psr\Http\Message\ServerRequestInterface $request) {
27002707
}
27012708

27022709
return new React\Http\Message\Response(
2703-
200,
2710+
React\Http\Message\Response::STATUS_OK,
27042711
array(
27052712
'Content-Type' => 'text/plain'
27062713
),

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"require": {
2929
"php": ">=5.3.0",
3030
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
31+
"fig/http-message-util": "^1.1",
3132
"psr/http-message": "^1.0",
3233
"react/event-loop": "^1.2",
3334
"react/promise": "^2.3 || ^1.2.1",

examples/51-server-hello-world.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
99
return new Response(
10-
200,
10+
Response::STATUS_OK,
1111
array(
1212
'Content-Type' => 'text/plain'
1313
),

examples/52-server-count-visitors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$counter = 0;
99
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$counter) {
1010
return new Response(
11-
200,
11+
Response::STATUS_OK,
1212
array(
1313
'Content-Type' => 'text/plain'
1414
),

examples/53-server-whatsmyip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];
1010

1111
return new Response(
12-
200,
12+
Response::STATUS_OK,
1313
array(
1414
'Content-Type' => 'text/plain'
1515
),

examples/54-server-query-parameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717

1818
return new Response(
19-
200,
19+
Response::STATUS_OK,
2020
array(
2121
'Content-Type' => 'text/html'
2222
),

examples/55-server-cookie-handling.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$body = "Your cookie value is: " . $request->getCookieParams()[$key];
1313

1414
return new Response(
15-
200,
15+
Response::STATUS_OK,
1616
array(
1717
'Content-Type' => 'text/plain'
1818
),
@@ -21,7 +21,7 @@
2121
}
2222

2323
return new Response(
24-
200,
24+
Response::STATUS_OK,
2525
array(
2626
'Content-Type' => 'text/plain',
2727
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')

examples/56-server-sleep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
return new Promise(function ($resolve, $reject) {
1212
Loop::addTimer(1.5, function() use ($resolve) {
1313
$response = new Response(
14-
200,
14+
Response::STATUS_OK,
1515
array(
1616
'Content-Type' => 'text/plain'
1717
),

examples/57-server-error-handling.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717

1818
$response = new Response(
19-
200,
19+
Response::STATUS_OK,
2020
array(
2121
'Content-Type' => 'text/plain'
2222
),

examples/58-server-stream-response.php

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

1010
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
1111
if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/') {
12-
return new Response(404);
12+
return new Response(Response::STATUS_NOT_FOUND);
1313
}
1414

1515
$stream = new ThroughStream();
@@ -30,7 +30,7 @@
3030
});
3131

3232
return new Response(
33-
200,
33+
Response::STATUS_OK,
3434
array(
3535
'Content-Type' => 'text/plain'
3636
),

0 commit comments

Comments
 (0)