legionth
changed the title
Replace Response class with PSR-7 Response
Replace Response class with PSR-7 Response class
| $response->writeHead(200, array( | ||
| 'Date' => date('D, d M Y H:i:s T') | ||
| )); | ||
| $server = new Server($socket, function (RequestInterface $request) use ($loop) { |
| $response->writeHead(200, array( | ||
| 'Date' => array() | ||
| )); | ||
| $server = new Server($socket, function (RequestInterface $request) use ($loop) { |
| $response->writeHead(200, array( | ||
| 'X-Powered-By' => 'PHP 3' | ||
| )); | ||
| $server = new Server($socket, function (RequestInterface $request) use ($loop) { |
| $response->writeHead(200, array( | ||
| 'X-Powered-By' => array() | ||
| )); | ||
| $server = new Server($socket, function (RequestInterface $request) use ($loop) { |
| $response = $response->withBody($body); | ||
| } | ||
|
|
||
| if ($response->hasHeader('Transfer-Encoding')) { |
| See also [example #3](examples) for more details. | ||
|
|
||
| The constructor is internal, you SHOULD NOT call this yourself. | ||
| The `Server` is responsible for emitting `Request` and `Response` objects. |
| @@ -156,22 +169,39 @@ gives you access to the incoming request body as the individual chunks arrive: | |||
|
|
|||
| ```php | |||
| $http = new Server($socket, function (RequestInterface $request, Response $response) { | |||
| @@ -118,9 +126,14 @@ and will be passed to the callback function like this. | |||
|
|
|||
| ```php | |||
| $http = new Server($socket, function (RequestInterface $request, Response $response) { | |||
| @@ -71,8 +76,11 @@ $socket = new React\Socket\SecureServer($socket, $loop, array( | |||
| )); | |||
|
|
|||
| $http = new Server($socket, function (RequestInterface $request, Response $response) { | |||
| @@ -53,8 +55,11 @@ constructor with the respective [request](#request) and | |||
| $socket = new React\Socket\Server(8080, $loop); | |||
|
|
|||
| $http = new Server($socket, function (RequestInterface $request, Response $response) { | |||
| use Psr\Http\Message\ResponseInterface; | ||
| use RingCentral; | ||
| use React\Stream\ReadableStream; | ||
| use RingCentral\Psr7\Response; |
| $promise = new Promise(function ($resolve, $reject) use ($promise) { | ||
| $resolve($promise); | ||
| }); | ||
| } |
| } | ||
| $that->handleResponse($conn, $response, $request->getProtocolVersion()); | ||
| } | ||
| ); |
| $http = new Server($socket, function (RequestInterface $request) { | ||
| return new Response( | ||
| 200, | ||
| array('Content-Type' => 'text/plain') |
andig
mentioned this pull request
Closed
|
|
||
| The `Response` class is responsible for streaming the outgoing response body. | ||
| The callback function passed to the constructor of the [Server](#server) | ||
| is responsible to return process a response, which will be delivered to the client. |
| The `Response` class is responsible for streaming the outgoing response body. | ||
| The callback function passed to the constructor of the [Server](#server) | ||
| is responsible to return process a response, which will be delivered to the client. | ||
| This function MUST return either a implementation of the |
| which implements the `PSR-7 RequestInterface` in this project. | ||
| We use instantiation of this class in our projects, | ||
| but feel free to use any implemantation of the | ||
| `PSR-7 RequestInterface` you prefer. |
|
|
||
| The `Response` will automatically use the same HTTP protocol version as the | ||
| corresponding `Request`. | ||
| If your response takes time to be processed you SHOULD use a `ReactPHP Promise`. |
| This needs to be a promise, because the request body may needs time to be completed. | ||
| The `ReactPHP Promise` will resolve in a `Response` object when the request | ||
| body ends. | ||
| Always use the `ReactPHP Promise` when your |
| This is just a example you could use of the streaming, | ||
| you could also send a big amount of data via little chunks | ||
| or use it for body data that needs to calculated. | ||
| Use the oppertunties of the `ReactPHP Streams` |
| 'Date' => date('D, d M Y H:i:s T') | ||
| )); | ||
| $server = new Server($socket, function (RequestInterface $request) { | ||
| return new \RingCentral\Psr7\Response(200, array('Date' => date('D, d M Y H:i:s T'))); |
| 200, | ||
| array( | ||
| 'Content-Type' => 'text/plain', | ||
| 'Content-Length' => strlen($data) |
| Make sure to catch these `Exceptions` to create own response messages. | ||
|
|
||
| After the return in the callback function the response will be processed by the `Server`. | ||
| The `Server` will add the protocol version of the reequest, so you don't have to. |
|
|
||
| An unhandled Exception in the code of the callback function will result | ||
| in an `500 Internal Server Error` message. | ||
| Make sure to catch these `Exceptions` to create own response messages. |
| $that->handleResponse($conn, $response, $request->getProtocolVersion()); | ||
| }, | ||
| function ($ex) use ($that, $conn) { | ||
| $that->emit('error', array($ex)); |
| use Psr\Http\Message\ResponseInterface; | ||
| use RingCentral; | ||
| use React\Stream\ReadableStream; | ||
| use React\Promise\Promise; |
| $that->handleResponse($conn, $response, $request->getProtocolVersion()); | ||
| }, | ||
| function ($ex) use ($that, $conn) { | ||
| if (!$ex instanceof \Exception) { |
| }, | ||
| function ($ex) use ($that, $conn) { | ||
| if (!$ex instanceof \Exception) { | ||
| $ex = new \InvalidArgumentException('Unknown rejection type'); |
| $promise->then( | ||
| function ($response) use ($that, $conn, $request) { | ||
| if (!$response instanceof ResponseInterface) { | ||
| $that->emit('error', array(new \InvalidArgumentException('Invalid response type'))); |
| }, | ||
| function ($ex) use ($that, $conn) { | ||
| if (!$ex instanceof \Exception) { | ||
| $ex = new \InvalidArgumentException('Unknown rejection type'); |
| $that->handleResponse($conn, $response, $request->getProtocolVersion()); | ||
| }, | ||
| function ($ex) use ($that, $conn) { | ||
| if (!$ex instanceof \Exception) { |
| ``` | ||
|
|
||
| If you do not want to send this header at all, you can use an empty array as | ||
| value like this: |
| return new Response( | ||
| 200, | ||
| array( | ||
| 'Content-Length' => strlen("Hello world\n"), |
|
|
||
| if ($response->getHeaderLine('Transfer-Encoding') === 'chunked') { | ||
| $stream = new ChunkedEncoder($body); | ||
| } |
| 'X-Powered-By' => 'PHP 3' | ||
| )); | ||
| $server = new Server($socket, function (RequestInterface $request) { | ||
| return new Response(200, array('X-Powered-By' => 'PHP 3')); |
jsor approved these changes Mar 30, 2017
clue approved these changes Mar 30, 2017
jsor
mentioned this pull request
Merged
| function ($error) use ($that, $conn) { | ||
| $message = 'The response callback is expected to resolve with an object implementing Psr\Http\Message\ResponseInterface, but rejected with "%s" instead.'; | ||
| $message = sprintf($message, is_object($error) ? get_class($error) : gettype($error)); | ||
| $exception = new \RuntimeException($message, null, $error instanceof \Exception ? $error : null); |
This was referenced
Mar 30, 2017Merged
Merged
Merged