Skip to content

Commit

Permalink
Introduce using PSR-7 HTTP Status code constants
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
WyriHaximus authored and clue committed Nov 7, 2021
1 parent 5e1d1d4 commit fc1cf25
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 44 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"react/promise-stream": "^1.1",
"react/socket": "^1.9",
"react/stream": "^1.2",
"ringcentral/psr7": "^1.2"
"ringcentral/psr7": "^1.2",
"fig/http-message-util": "^1.1"
},
"require-dev": {
"clue/block-react": "^1.1",
Expand Down
3 changes: 2 additions & 1 deletion examples/51-server-hello-world.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

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

$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
3 changes: 2 additions & 1 deletion examples/52-server-count-visitors.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

Expand All @@ -8,7 +9,7 @@
$counter = 0;
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$counter) {
return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
3 changes: 2 additions & 1 deletion examples/53-server-whatsmyip.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

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

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
3 changes: 2 additions & 1 deletion examples/54-server-query-parameter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

Expand All @@ -16,7 +17,7 @@
}

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/html'
),
Expand Down
5 changes: 3 additions & 2 deletions examples/55-server-cookie-handling.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

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

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -21,7 +22,7 @@
}

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain',
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
Expand Down
3 changes: 2 additions & 1 deletion examples/56-server-sleep.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\Message\Response;
Expand All @@ -11,7 +12,7 @@
return new Promise(function ($resolve, $reject) {
Loop::addTimer(1.5, function() use ($resolve) {
$response = new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
3 changes: 2 additions & 1 deletion examples/57-server-error-handling.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;
use React\Promise\Promise;
Expand All @@ -16,7 +17,7 @@
}

$response = new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
5 changes: 3 additions & 2 deletions examples/58-server-stream-response.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\Message\Response;
Expand All @@ -9,7 +10,7 @@

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

$stream = new ThroughStream();
Expand All @@ -30,7 +31,7 @@
});

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
9 changes: 5 additions & 4 deletions examples/59-server-json-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// $ php examples/59-server-json-api.php 8080
// $ curl -v http://localhost:8080/ -H 'Content-Type: application/json' -d '{"name":"Alice"}'

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

Expand All @@ -14,7 +15,7 @@
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
return new Response(
415, // Unsupported Media Type
StatusCodeInterface::STATUS_UNSUPPORTED_MEDIA_TYPE,
array(
'Content-Type' => 'application/json'
),
Expand All @@ -25,7 +26,7 @@
$input = json_decode($request->getBody()->getContents());
if (json_last_error() !== JSON_ERROR_NONE) {
return new Response(
400, // Bad Request
StatusCodeInterface::STATUS_BAD_REQUEST,
array(
'Content-Type' => 'application/json'
),
Expand All @@ -35,7 +36,7 @@

if (!isset($input->name) || !is_string($input->name)) {
return new Response(
422, // Unprocessable Entity
StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
array(
'Content-Type' => 'application/json'
),
Expand All @@ -44,7 +45,7 @@
}

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'application/json'
),
Expand Down
3 changes: 2 additions & 1 deletion examples/61-server-hello-world-https.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;

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

$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
3 changes: 2 additions & 1 deletion examples/62-server-form-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// $ curl --form name=test --form age=30 http://localhost:8080/
// $ curl --form name=hi --form [email protected] http://localhost:8080/

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use React\Http\Message\Response;
Expand Down Expand Up @@ -110,7 +111,7 @@
HTML;

return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/html; charset=UTF-8'
),
Expand Down
6 changes: 4 additions & 2 deletions examples/63-server-streaming-request.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Fig\Http\Message\StatusCodeInterface;

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

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

$body->on('end', function () use ($resolve, &$bytes){
$resolve(new React\Http\Message\Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -31,7 +33,7 @@ function (Psr\Http\Message\ServerRequestInterface $request) {
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
$body->on('error', function (Exception $e) use ($resolve, &$bytes) {
$resolve(new React\Http\Message\Response(
400,
StatusCodeInterface::STATUS_BAD_REQUEST,
array(
'Content-Type' => 'text/plain'
),
Expand Down
5 changes: 3 additions & 2 deletions examples/71-server-http-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// $ php examples/71-server-http-proxy.php 8080
// $ curl -v --proxy http://localhost:8080 http://reactphp.org/

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\RequestInterface;
use React\Http\Message\Response;
use RingCentral\Psr7;
Expand All @@ -16,7 +17,7 @@
$http = new React\Http\HttpServer(function (RequestInterface $request) {
if (strpos($request->getRequestTarget(), '://') === false) {
return new Response(
400,
StatusCodeInterface::STATUS_BAD_REQUEST,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -36,7 +37,7 @@
// left up as an exercise: use an HTTP client to send the outgoing request
// and forward the incoming response to the original client request
return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
7 changes: 4 additions & 3 deletions examples/72-server-http-connect-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// $ php examples/72-server-http-connect-proxy.php 8080
// $ curl -v --proxy http://localhost:8080 https://reactphp.org/

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\Response;
use React\Socket\Connector;
Expand All @@ -19,7 +20,7 @@
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use ($connector) {
if ($request->getMethod() !== 'CONNECT') {
return new Response(
405,
StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED,
array(
'Content-Type' => 'text/plain',
'Allow' => 'CONNECT'
Expand All @@ -33,14 +34,14 @@
function (ConnectionInterface $remote) {
// connection established => forward data
return new Response(
200,
StatusCodeInterface::STATUS_OK,
array(),
$remote
);
},
function (Exception $e) {
return new Response(
502,
StatusCodeInterface::STATUS_BAD_GATEWAY,
array(
'Content-Type' => 'text/plain'
),
Expand Down
5 changes: 3 additions & 2 deletions examples/81-server-upgrade-echo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
< world
*/

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\Message\Response;
Expand All @@ -30,7 +31,7 @@
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
if ($request->getHeaderLine('Upgrade') !== 'echo' || $request->getProtocolVersion() === '1.0') {
return new Response(
426,
StatusCodeInterface::STATUS_UPGRADE_REQUIRED,
array(
'Upgrade' => 'echo'
),
Expand All @@ -48,7 +49,7 @@
});

return new Response(
101,
StatusCodeInterface::STATUS_SWITCHING_PROTOCOLS,
array(
'Upgrade' => 'echo'
),
Expand Down
5 changes: 3 additions & 2 deletions examples/82-server-upgrade-chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Hint: try this with multiple connections :)
*/

use Fig\Http\Message\StatusCodeInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\Message\Response;
Expand All @@ -38,7 +39,7 @@
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use ($chat) {
if ($request->getHeaderLine('Upgrade') !== 'chat' || $request->getProtocolVersion() === '1.0') {
return new Response(
426,
StatusCodeInterface::STATUS_UPGRADE_REQUIRED,
array(
'Upgrade' => 'chat'
),
Expand Down Expand Up @@ -76,7 +77,7 @@
});

return new Response(
101,
StatusCodeInterface::STATUS_SWITCHING_PROTOCOLS,
array(
'Upgrade' => 'chat'
),
Expand Down
Loading

0 comments on commit fc1cf25

Please sign in to comment.