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

Add PSR-7 ServerRequestInterface to the PSR-7 Request #170

Merged
merged 2 commits into from
Apr 20, 2017
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
36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This is an HTTP server which responds with `Hello World` to every request.
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server(8080, $loop);

$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
return new Response(
200,
array('Content-Type' => 'text/plain'),
Expand Down Expand Up @@ -54,7 +54,7 @@ constructor with the respective [request](#request) and
```php
$socket = new React\Socket\Server(8080, $loop);

$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
return new Response(
200,
array('Content-Type' => 'text/plain'),
Expand All @@ -75,7 +75,7 @@ $socket = new React\Socket\SecureServer($socket, $loop, array(
'local_cert' => __DIR__ . '/localhost.pem'
));

$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
return new Response(
200,
array('Content-Type' => 'text/plain'),
Expand Down Expand Up @@ -137,11 +137,13 @@ connections and then processing each incoming HTTP request.
The request object will be processed once the request headers have
been received by the client.
This request object implements the
[PSR-7 ServerRequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface)
which in turn extends the
[PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface)
and will be passed to the callback function like this.

```php
$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
$body = "The method of the request is: " . $request->getMethod();
$body .= "The requested path is: " . $request->getUri()->getPath();

Expand All @@ -154,8 +156,14 @@ $http = new Server($socket, function (RequestInterface $request) {
```

For more details about the request object, check out the documentation of
[PSR-7 ServerRequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#321-psrhttpmessageserverrequestinterface)
and
[PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface).

> Currently the the server params, cookies and uploaded files are not added by the
`Server`, but you can add these parameters by yourself using the given methods.
The next versions of this project will cover these features.

Note that the request object will be processed once the request headers have
been received.
This means that this happens irrespective of (i.e. *before*) receiving the
Expand Down Expand Up @@ -184,7 +192,7 @@ Instead, you should use the `ReactPHP ReadableStreamInterface` which
gives you access to the incoming request body as the individual chunks arrive:

```php
$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
return new Promise(function ($resolve, $reject) use ($request) {
$contentLength = 0;
$request->getBody()->on('data', function ($data) use (&$contentLength) {
Expand Down Expand Up @@ -248,7 +256,7 @@ Note that this value may be `null` if the request body size is unknown in
advance because the request message uses chunked transfer encoding.

```php
$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
$size = $request->getBody()->getSize();
if ($size === null) {
$body = 'The request does not contain an explicit length.';
Expand Down Expand Up @@ -308,7 +316,7 @@ but feel free to use any implemantation of the
`PSR-7 ResponseInterface` you prefer.

```php
$http = new Server($socket, function (RequestInterface $request) {
$http = new Server($socket, function (ServerRequestInterface $request) {
return new Response(
200,
array('Content-Type' => 'text/plain'),
Expand All @@ -327,7 +335,7 @@ To prevent this you SHOULD use a
This example shows how such a long-term action could look like:

```php
$server = new \React\Http\Server($socket, function (RequestInterface $request) use ($loop) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use ($loop) {
return new Promise(function ($resolve, $reject) use ($request, $loop) {
$loop->addTimer(1.5, function() use ($loop, $resolve) {
$response = new Response(
Expand Down Expand Up @@ -355,7 +363,7 @@ Note that other implementations of the `PSR-7 ResponseInterface` likely
only support string.

```php
$server = new Server($socket, function (RequestInterface $request) use ($loop) {
$server = new Server($socket, function (ServerRequestInterface $request) use ($loop) {
$stream = new ReadableStream();

$timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
Expand Down Expand Up @@ -389,7 +397,7 @@ If you know the length of your stream body, you MAY specify it like this instead

```php
$stream = new ReadableStream()
$server = new Server($socket, function (RequestInterface $request) use ($loop, $stream) {
$server = new Server($socket, function (ServerRequestInterface $request) use ($loop, $stream) {
return new Response(
200,
array(
Expand Down Expand Up @@ -437,7 +445,7 @@ A `Date` header will be automatically added with the system date and time if non
You can add a custom `Date` header yourself like this:

```php
$server = new Server($socket, function (RequestInterface $request) {
$server = new Server($socket, function (ServerRequestInterface $request) {
return new Response(200, array('Date' => date('D, d M Y H:i:s T')));
});
```
Expand All @@ -446,7 +454,7 @@ If you don't have a appropriate clock to rely on, you should
unset this header with an empty string:

```php
$server = new Server($socket, function (RequestInterface $request) {
$server = new Server($socket, function (ServerRequestInterface $request) {
return new Response(200, array('Date' => ''));
});
```
Expand All @@ -455,7 +463,7 @@ Note that it will automatically assume a `X-Powered-By: react/alpha` header
unless your specify a custom `X-Powered-By` header yourself:

```php
$server = new Server($socket, function (RequestInterface $request) {
$server = new Server($socket, function (ServerRequestInterface $request) {
return new Response(200, array('X-Powered-By' => 'PHP 3'));
});
```
Expand All @@ -464,7 +472,7 @@ If you do not want to send this header at all, you can use an empty string as
value like this:

```php
$server = new Server($socket, function (RequestInterface $request) {
$server = new Server($socket, function (ServerRequestInterface $request) {
return new Response(200, array('X-Powered-By' => ''));
});
```
Expand Down
5 changes: 2 additions & 3 deletions examples/01-hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use React\Promise\Promise;
use Psr\Http\Message\ServerRequestInterface;

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

$loop = Factory::create();
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$server = new \React\Http\Server($socket, function (RequestInterface $request) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand Down
4 changes: 2 additions & 2 deletions examples/02-count-visitors.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;

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

$loop = Factory::create();
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$counter = 0;
$server = new \React\Http\Server($socket, function (RequestInterface $request) use (&$counter) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use (&$counter) {
return new Response(
200,
array('Content-Type' => 'text/plain'),
Expand Down
4 changes: 2 additions & 2 deletions examples/03-stream-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Stream\ReadableStream;

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

$loop = Factory::create();
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$server = new \React\Http\Server($socket, function (RequestInterface $request) use ($loop) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use ($loop) {
$stream = new ReadableStream();

$timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
Expand Down
4 changes: 2 additions & 2 deletions examples/04-stream-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Promise\Promise;

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

$loop = Factory::create();
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$server = new \React\Http\Server($socket, function (RequestInterface $request) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) {
return new Promise(function ($resolve, $reject) use ($request) {
$contentLength = 0;
$request->getBody()->on('data', function ($data) use (&$contentLength) {
Expand Down
4 changes: 2 additions & 2 deletions examples/05-error-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Promise\Promise;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -12,7 +12,7 @@
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$count = 0;
$server = new \React\Http\Server($socket, function (RequestInterface $request) use (&$count) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use (&$count) {
return new Promise(function ($resolve, $reject) use (&$count) {
$count++;

Expand Down
4 changes: 2 additions & 2 deletions examples/11-hello-world-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use React\Socket\Server;
use React\Http\Response;
use React\Socket\SecureServer;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;

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

Expand All @@ -14,7 +14,7 @@
'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem'
));

$server = new \React\Http\Server($socket, function (RequestInterface $request) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) {
return new Response(
200,
array('Content-Type' => 'text/plain'),
Expand Down
4 changes: 2 additions & 2 deletions examples/21-connect-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;

Expand All @@ -13,7 +13,7 @@
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
$connector = new Connector($loop);

$server = new \React\Http\Server($socket, function (RequestInterface $request) use ($connector) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use ($connector) {
if ($request->getMethod() !== 'CONNECT') {
return new Response(
405,
Expand Down
4 changes: 2 additions & 2 deletions examples/99-benchmark-download.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use React\EventLoop\Factory;
use React\Socket\Server;
use React\Http\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use React\Stream\ReadableStream;

require __DIR__ . '/../vendor/autoload.php';
Expand Down Expand Up @@ -62,7 +62,7 @@ public function getSize()
}
}

$server = new \React\Http\Server($socket, function (RequestInterface $request) use ($loop) {
$server = new \React\Http\Server($socket, function (ServerRequestInterface $request) use ($loop) {
switch ($request->getUri()->getPath()) {
case '/':
return new Response(
Expand Down
7 changes: 7 additions & 0 deletions src/RequestHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ private function parseRequest($data)
}

$request = g7\parse_request($headers);
$request = new ServerRequest(
$request->getMethod(),
$request->getUri(),
$request->getHeaders(),
$request->getBody(),
$request->getProtocolVersion()
);

// Do not assume this is HTTPS when this happens to be port 443
// detecting HTTPS is left up to the socket layer (TLS detection)
Expand Down
9 changes: 5 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Psr\Http\Message\ResponseInterface;
use React\Promise\Promise;
use RingCentral\Psr7 as Psr7Implementation;
use Psr\Http\Message\ServerRequestInterface;

/**
* The `Server` class is responsible for handling incoming connections and then
Expand Down Expand Up @@ -170,7 +171,7 @@ public function handleConnection(ConnectionInterface $conn)
}

/** @internal */
public function handleRequest(ConnectionInterface $conn, RequestInterface $request)
public function handleRequest(ConnectionInterface $conn, ServerRequestInterface $request)
{
// only support HTTP/1.1 and HTTP/1.0 requests
if ($request->getProtocolVersion() !== '1.1' && $request->getProtocolVersion() !== '1.0') {
Expand Down Expand Up @@ -306,7 +307,7 @@ function ($error) use ($that, $conn, $request) {
}

/** @internal */
public function writeError(ConnectionInterface $conn, $code, RequestInterface $request = null)
public function writeError(ConnectionInterface $conn, $code, ServerRequestInterface $request = null)
{
$message = 'Error ' . $code;
if (isset(ResponseCodes::$statusTexts[$code])) {
Expand All @@ -322,15 +323,15 @@ public function writeError(ConnectionInterface $conn, $code, RequestInterface $r
);

if ($request === null) {
$request = new Psr7Implementation\Request('GET', '/', array(), null, '1.1');
$request = new ServerRequest('GET', '/', array(), null, '1.1');
}

$this->handleResponse($conn, $request, $response);
}


/** @internal */
public function handleResponse(ConnectionInterface $connection, RequestInterface $request, ResponseInterface $response)
public function handleResponse(ConnectionInterface $connection, ServerRequestInterface $request, ResponseInterface $response)
{
$response = $response->withProtocolVersion($request->getProtocolVersion());

Expand Down
Loading