Open Swoole 4.6.2 released: new Coroutine\Socket API, Request::getMethod() and more


Swoole v4.6.2 is released with multiple new features and enhancement recently.


Swoole\Http\Request->getMethod() is added to get the HTTP method of a HTTP request.

Coroutine\Socket->recvLine() and Coroutine\Socket->recvWithBuffer() are added to improve the coroutine Socket API set. These two new socket API are self explained.

Improved Response::create()

You can use Response::create() without a server context.



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

Swoole\Coroutine\run(function () {
    $server = new Server('', 9501, false);

    go(function () use ($server) {
        $server->handle(function (Connection $conn) use ($server) {
            $req = Request::create();
            while(true) {
                $data = $conn->recv();
                if (strlen($data) != $req->parse($data) or $req->isCompleted()) {
            $resp = Response::create([$conn->exportSocket(), $req]);
            $resp->header('X-Server', 'swoole');
            $resp->end('Hello, Swoole');


Use CURL to test the server:

$ curl -I\?hello\=swoole
HTTP/1.1 200 OK
X-Server: swoole
Server: swoole-http-server
Connection: keep-alive
Content-Type: text/html
Date: Mon, 25 Jan 2021 10:58:31 GMT
Content-Length: 13

$ curl\?hello\=swoole
Hello, Swoole

List of changes in v4.6.2

New APIs
+ Added Http::Request::getMethod() (#3987) (@luolaifa000)
+ Added Coroutine::Socket:recvLine() (#4014) (@matyhtf)
+ Added Socket::readWithBuffer() (#4017) (@matyhtf)

+ Improved Response::create() (#3998) (@matyhtf)
+ Supported Coroutine\Redis::hExists return bool with compatibility_mode (swoole/swoole-src@b8cce7c) (@matyhtf)
+ Supported PHP_NORMAL_READ for socket_read (swoole/swoole-src@b1a0dcc) (@matyhtf)

* Fixed Coroutine::defer coredump in PHP8 (#3997) (@huanghantao)
* Fixed Coroutine::Socket::errCode is not set correctly when using thread context (swoole/swoole-src@004d08a) (@matyhtf)
* Fixed build Swoole error on latest macos (#4007) (@matyhtf)
* Fixed php stream context is nullptr when use md5_file with url (#4016) (@ZhiyangLeeCN)
* Fixed rshutdown deprecated warning when throw exception (#4026) (@huanghantao)

* Used AIO thread hook stdio instead of coroutine socket (#4002) (@matyhtf)
* Refactor HttpContext (#3998) (@matyhtf)
* Refactor Process::wait() (#4019) (@matyhtf)