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

Published:

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

New API

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.

Example:

<?php

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

Swoole\Coroutine\run(function () {
    $server = new Server('0.0.0.0', 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()) {
                    break;
                }
            }
            var_dump($req->get);
            $resp = Response::create([$conn->exportSocket(), $req]);
            $resp->header('X-Server', 'swoole');
            $resp->end('Hello, Swoole');

            $server->shutdown();
        });
        $server->start();
    });
});

Use CURL to test the server:


$ curl -I http://127.0.0.1:9501/\?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 http://127.0.0.1:9501/\?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)

Enhancement
---
+ 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
---
* 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)

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