In version 4.6, there are some enhancements to Swoole\Http\Response:

  • addHttp\Response::isWritable (db56827) (@matyhtf)
  • To enhanceResponse\create()Method that can be used independently of the Server (#3998) (@matyhtf)

Look again at the sample code from the previous article:

use Swoole\Server;
use Swoole\Http\Request;

$server = new Server('127.0.0.1'.9501);

$server->on('Receive'.function (Server $server.$fd.$reactor_id.$data) {
    / * *@var Request $request */
    $request = Request::create();
    $request->parse($data);

    $body = 'Hello, Swoole';
    $body_len = strlen($body);
    $send_data = "HTTP/1.1 200 OK\r\nServer: swoole-server\r\ ncontent-type: text/ HTML; charset=utf-8\r\nContent-Length:{$body_len}\r\nConnection: keep-alive\r\n\r\n{$body}";
    $server->send($fd.$send_data);
});

$server->start();
Copy the code

The $send_data data needs to be handled by the user, but with this enhancement, it can be used like this:

use Swoole\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$server = new Server('127.0.0.1'.9501);

$server->on('Receive'.function (Server $server.$fd.$reactor_id.$data) {
    / * *@var Request $request */
    $request = Request::create();
    $request->parse($data);

    / * *@var Response $response */
    $response = Response::create($server.$fd);
    $response->status(200);
    $response->end('Hello, Swoole');
});

$server->start();
Copy the code

You can also do this:

$server->on('Receive'.function (Server $server.$fd.$reactor_id.$data) {
    / * *@var Request $request */
    $request = Request::create();
    $request->parse($data);

    / * *@var Response $response */
    $response = Response::create([$server.$request].$fd);
    $response->status(200);
    $response->end('Hello, Swoole');
});
Copy the code

This can be used not only in asynchronous servers, but also in coroutine servers:

use Swoole\Coroutine\Server;
use Swoole\Coroutine\Server\Connection;
use Swoole\Http\Request;
use Swoole\Http\Response;
use function Swoole\Coroutine\run;

run(function () {
    $server = new Server('0.0.0.0'.9501.false);
    $server->handle(function (Connection $conn) use ($server) {
        $request = Request::create();
        while(true) {
            $data = $conn->recv();
            if (strlen($data) != $request->parse($data) | |$request->isCompleted()) {
                break; }}$response = Response::create([$conn->exportSocket(), $request]);
        $response->end('Hello, Swoole');
    });
    $server->start();
});
Copy the code

This allows you to directly use the Http\Response API for Response processing without having to assemble the Http protocol Response data yourself.

isWritable

IsWritable () is used to determine whether the Http\Response object is finished (end) or detached (detach), for example:

use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

$http = new Server('0.0.0.0'.9501);

$http->on('request'.function (Request $req, Response $resp) {
    assert($resp->isWritable(), true);
    $resp->end('hello');
    assert($resp->isWritable(), false);
    $resp->setStatusCode(403);
});

$http->start();
Copy the code

If you call the relevant API again after calling end(), an error occurs

PHP Warning:  Swoole\Http\Response::setStatusCode(): http response is unavailable (maybe it has been ended or detached)
Copy the code