roadrunner-server / roadrunner-plugins

📦 Home for the roadrunner plugins
MIT License
25 stars 9 forks source link

[BUG] Base64 string instead of decoded payload #86

Closed Yurunsoft closed 3 years ago

Yurunsoft commented 3 years ago

I tried this code:

.rr.yaml:

server:
  command: "php rr.php"

http:
  address: 0.0.0.0:13000
  pool:
    num_workers: 2

rpc:
  listen: tcp://127.0.0.1:6001

rr.php:

<?php

declare(strict_types=1);

use Nyholm\Psr7;
use Spiral\RoadRunner;

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

$worker = RoadRunner\Worker::create();
$psrFactory = new Psr7\Factory\Psr17Factory();

$worker = new RoadRunner\Http\PSR7Worker($worker, $psrFactory, $psrFactory, $psrFactory);

while ($req = $worker->waitRequest())
{
    try
    {
        $rsp = new Psr7\Response();
        $rsp->getBody()->write((string) $req->getBody());

        $worker->respond($rsp);
    }
    catch (\Throwable $e)
    {
        $worker->getWorker()->error((string) $e);
    }
}

client.php:

$curl = curl_init();

// First post
curl_setopt_array($curl, [
    \CURLOPT_URL            => 'http://127.0.0.1:13000/',
    \CURLOPT_RETURNTRANSFER => true,
    \CURLOPT_ENCODING       => '',
    \CURLOPT_MAXREDIRS      => 10,
    \CURLOPT_TIMEOUT        => 0,
    \CURLOPT_FOLLOWLOCATION => true,
    \CURLOPT_HTTP_VERSION   => \CURL_HTTP_VERSION_1_1,
    \CURLOPT_CUSTOMREQUEST  => 'POST',
    \CURLOPT_POSTFIELDS     => ['a' => 1],
    \CURLOPT_HTTPHEADER     => [
    ],
]);
$response = curl_exec($curl);
var_dump($response);

// Then json post
do
{
    curl_setopt_array($curl, [
        \CURLOPT_URL            => 'http://127.0.0.1:13000/',
        \CURLOPT_RETURNTRANSFER => true,
        \CURLOPT_ENCODING       => '',
        \CURLOPT_MAXREDIRS      => 10,
        \CURLOPT_TIMEOUT        => 0,
        \CURLOPT_FOLLOWLOCATION => true,
        \CURLOPT_HTTP_VERSION   => \CURL_HTTP_VERSION_1_1,
        \CURLOPT_CUSTOMREQUEST  => 'POST',
        \CURLOPT_POSTFIELDS     => '{"a":2}',
        \CURLOPT_HTTPHEADER     => [
            'Content-Type: application/json',
        ],
    ]);
    $response = curl_exec($curl);
    var_dump($response);
}
while ('"eyJhIjoyfQ=="' !== $response);

curl_close($curl);

Possible outputs:

string(9) "{"a":"1"}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(7) "{"a":2}"
string(14) ""eyJhIjoyfQ==""

When a non JSON request is first posted and then a JSON request is posted, the probability of "eyJhIjoyfQ==" will appear

This bug only occurs in v2.5.0

rustatian commented 3 years ago

Hey @Yurunsoft . eyJhIjoyfQ== is actually base64 for the "{"a":2}". I'm not actually sure, where it comes from, but I'll check.

rustatian commented 3 years ago

Yes @Yurunsoft I can confirm, this is the bug. It Will be resolved ASAP and fixed in 2.5.1

rustatian commented 3 years ago

I'll transfer this issue to the rr-plugins repo

rustatian commented 3 years ago

@Yurunsoft Found the issue, the fix will be in a few hours.

rustatian commented 3 years ago

@Yurunsoft feel free to test https://github.com/spiral/roadrunner-binary/releases/tag/v2.5.1

Yurunsoft commented 3 years ago

Thanks~