Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose all status code constants via Response class #432

Merged
merged 2 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 39 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ require __DIR__ . '/vendor/autoload.php';

$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -735,7 +735,7 @@ object and expects a [response](#server-response) object in return:
```php
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -953,7 +953,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
$body .= "The requested path is: " . $request->getUri()->getPath();

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

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -1027,7 +1027,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
}

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

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

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

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(),
"Uploaded $name\n"
);
Expand Down Expand Up @@ -1205,7 +1205,7 @@ $http = new React\Http\HttpServer(

$body->on('end', function () use ($resolve, &$bytes){
$resolve(new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -1216,7 +1216,7 @@ $http = new React\Http\HttpServer(
// 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,
React\Http\Message\Response::STATUS_BAD_REQUEST,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -1272,7 +1272,7 @@ $http = new React\Http\HttpServer(
$body .= 'This example does not accept chunked transfer encoding.';

return new React\Http\Message\Response(
411,
React\Http\Message\Response::STATUS_LENGTH_REQUIRED,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -1281,7 +1281,7 @@ $http = new React\Http\HttpServer(
}

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

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -1352,7 +1352,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
}

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain',
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
Expand Down Expand Up @@ -1410,7 +1410,7 @@ In its most simple form, you can use it like this:
```php
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -1440,7 +1440,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
return new Promise(function ($resolve, $reject) {
Loop::addTimer(1.5, function() use ($resolve) {
$response = new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -1487,7 +1487,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
});

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down Expand Up @@ -1568,7 +1568,7 @@ a `string` response body like this:
```php
$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand All @@ -1593,7 +1593,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf
});

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Length' => '13',
'Content-Type' => 'text/plain',
Expand Down Expand Up @@ -1663,7 +1663,7 @@ a custom `Server` response header like this:
```php
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Server' => 'PHP/3'
)
Expand All @@ -1678,7 +1678,7 @@ string value like this:
```php
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Server' => ''
)
Expand All @@ -1693,7 +1693,7 @@ like this:
```php
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Date' => gmdate('D, d M Y H:i:s \G\M\T')
)
Expand All @@ -1708,7 +1708,7 @@ like this:
```php
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Date' => ''
)
Expand Down Expand Up @@ -1786,7 +1786,7 @@ encourages [Third-Party Middleware](#third-party-middleware) implementations.
In order to use middleware request handlers, simply pass a list of all
callables as defined above to the [`HttpServer`](#httpserver).
The following example adds a middleware request handler that adds the current time to the request as a
header (`Request-Time`) and a final request handler that always returns a 200 code without a body:
header (`Request-Time`) and a final request handler that always returns a `200 OK` status code without a body:

```php
$http = new React\Http\HttpServer(
Expand All @@ -1795,7 +1795,7 @@ $http = new React\Http\HttpServer(
return $next($request);
},
function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(200);
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
}
);
```
Expand All @@ -1821,7 +1821,7 @@ $http = new React\Http\HttpServer(
});
},
function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Message\Response(200);
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
}
);
```
Expand All @@ -1842,7 +1842,7 @@ $http = new React\Http\HttpServer(
});
return $promise->then(null, function (Exception $e) {
return new React\Http\Message\Response(
500,
React\Http\Message\Response::STATUS_INTERNAL_SERVER_ERROR,
array(),
'Internal error: ' . $e->getMessage()
);
Expand All @@ -1852,7 +1852,7 @@ $http = new React\Http\HttpServer(
if (mt_rand(0, 1) === 1) {
throw new RuntimeException('Database error');
}
return new React\Http\Message\Response(200);
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
}
);
```
Expand Down Expand Up @@ -2439,7 +2439,7 @@ represent an outgoing server response message.

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

On top of this, this class implements the
[PSR-7 Message Util `StatusCodeInterface`](https://github.com/php-fig/http-message-util/blob/master/src/StatusCodeInterface.php)
which means that most common HTTP status codes are available as class
constants with the `STATUS_*` prefix. For instance, the `200 OK` and
`404 Not Found` status codes can used as `Response::STATUS_OK` and
`Response::STATUS_NOT_FOUND` respectively.

> Internally, this implementation builds on top of an existing incoming
response message and only adds required streaming support. This base class is
considered an implementation detail that may change in the future.
Expand Down Expand Up @@ -2516,7 +2523,7 @@ $http = new React\Http\HttpServer(
});
$body->on('close', function () use (&$bytes, $resolve) {
$resolve(new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
[],
"Received $bytes bytes\n"
));
Expand Down Expand Up @@ -2653,7 +2660,7 @@ $http = new React\Http\HttpServer(
new React\Http\Middleware\RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
function (Psr\Http\Message\ServerRequestInterface $request) {
// The body from $request->getBody() is now fully available without the need to stream it
return new React\Http\Message\Response(200);
return new React\Http\Message\Response(React\Http\Message\Response::STATUS_OK);
},
);
```
Expand Down Expand Up @@ -2700,7 +2707,7 @@ $handler = function (Psr\Http\Message\ServerRequestInterface $request) {
}

return new React\Http\Message\Response(
200,
React\Http\Message\Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"require": {
"php": ">=5.3.0",
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"fig/http-message-util": "^1.1",
"psr/http-message": "^1.0",
"react/event-loop": "^1.2",
"react/promise": "^2.3 || ^1.2.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/51-server-hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$http = new React\Http\HttpServer(function (ServerRequestInterface $request) {
return new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
2 changes: 1 addition & 1 deletion examples/52-server-count-visitors.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$counter = 0;
$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$counter) {
return new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
2 changes: 1 addition & 1 deletion examples/53-server-whatsmyip.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];

return new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
2 changes: 1 addition & 1 deletion examples/54-server-query-parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

return new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/html'
),
Expand Down
4 changes: 2 additions & 2 deletions examples/55-server-cookie-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$body = "Your cookie value is: " . $request->getCookieParams()[$key];

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

return new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain',
'Set-Cookie' => urlencode($key) . '=' . urlencode('test;more')
Expand Down
2 changes: 1 addition & 1 deletion examples/56-server-sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
return new Promise(function ($resolve, $reject) {
Loop::addTimer(1.5, function() use ($resolve) {
$response = new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
2 changes: 1 addition & 1 deletion examples/57-server-error-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}

$response = new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
4 changes: 2 additions & 2 deletions examples/58-server-stream-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

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

return new Response(
200,
Response::STATUS_OK,
array(
'Content-Type' => 'text/plain'
),
Expand Down
Loading