Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/php/lib/Protocol/TJSONProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class TJSONProtocol extends TProtocol
public const NAME_SET = "set";
public const NAME_UUID = "uid";

/** Quoted tokens used to round-trip non-finite doubles through the JSON protocol. */
public const TOKEN_NAN = "NaN";
public const TOKEN_POS_INFINITY = "Infinity";
public const TOKEN_NEG_INFINITY = "-Infinity";

private function getTypeNameForTypeID($typeID)
{
switch ($typeID) {
Expand Down Expand Up @@ -232,15 +237,25 @@ private function writeJSONInteger(int $num)
}
}

private function writeJSONDouble($num)
private function writeJSONDouble(float $num): void
{
$this->context->write();

if (is_nan($num)) {
$this->trans->write(self::QUOTE . self::TOKEN_NAN . self::QUOTE);
return;
}

if (is_infinite($num)) {
$token = $num > 0 ? self::TOKEN_POS_INFINITY : self::TOKEN_NEG_INFINITY;
$this->trans->write(self::QUOTE . $token . self::QUOTE);
return;
}

if ($this->context->escapeNum()) {
$this->trans->write(self::QUOTE);
}

#TODO add compatibility with NAN and INF
$this->trans->write(json_encode($num));

if ($this->context->escapeNum()) {
Expand Down Expand Up @@ -398,10 +413,12 @@ private function readJSONDouble()
if (substr($this->reader->peek(), 0, 1) == self::QUOTE) {
$arr = $this->readJSONString(true);

if ($arr == "NaN") {
if ($arr === self::TOKEN_NAN) {
return NAN;
} elseif ($arr == "Infinity") {
} elseif ($arr === self::TOKEN_POS_INFINITY) {
return INF;
} elseif ($arr === self::TOKEN_NEG_INFINITY) {
return -INF;
} elseif (!$this->context->escapeNum()) {
throw new TProtocolException(
"Numeric data unexpectedly quoted " . $arr,
Expand Down
17 changes: 13 additions & 4 deletions lib/php/lib/Protocol/TSimpleJSONProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,27 @@ private function writeJSONInteger(int $num)
}
}

private function writeJSONDouble($num)
private function writeJSONDouble(float $num): void
{
$isMapKey = $this->writeContext->isMapKey();

$this->writeContext->write();

if (is_nan($num)) {
$this->trans->write(self::QUOTE . TJSONProtocol::TOKEN_NAN . self::QUOTE);
return;
}

if (is_infinite($num)) {
$token = $num > 0 ? TJSONProtocol::TOKEN_POS_INFINITY : TJSONProtocol::TOKEN_NEG_INFINITY;
$this->trans->write(self::QUOTE . $token . self::QUOTE);
return;
}

if ($isMapKey) {
$this->trans->write(self::QUOTE);
}

#TODO add compatibility with NAN and INF
$this->trans->write(json_encode((float)$num));
$this->trans->write(json_encode($num));

if ($isMapKey) {
$this->trans->write(self::QUOTE);
Expand Down
51 changes: 12 additions & 39 deletions lib/php/lib/Transport/TBufferedTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,22 @@ public function __construct(
) {
}

public function isOpen()
public function isOpen(): bool
{
return $this->transport->isOpen();
}

/**
* @inheritdoc
*
* @throws TTransportException
*/
public function open()
public function open(): void
{
$this->transport->open();
}

public function close()
public function close(): void
{
$this->transport->close();
}

public function putBack($data)
public function putBack(string $data): void
{
if (strlen($this->rBuf) === 0) {
$this->rBuf = $data;
Expand All @@ -93,10 +88,8 @@ public function putBack($data)
*
* Therefore, use the readAll method of the wrapped transport inside
* the buffered readAll.
*
* @throws TTransportException
*/
public function readAll($len)
public function readAll(int $len): string
{
$have = strlen($this->rBuf);
if ($have == 0) {
Expand All @@ -116,14 +109,7 @@ public function readAll($len)
return $data;
}

/**
* @inheritdoc
*
* @param int $len
* @return string
* @throws TTransportException
*/
public function read($len)
public function read(int $len): string
{
if (strlen($this->rBuf) === 0) {
$this->rBuf = $this->transport->read($this->rBufSize);
Expand All @@ -142,39 +128,26 @@ public function read($len)
return $ret;
}

/**
* @inheritdoc
*
* @param string $buf
* @throws TTransportException
*/
public function write($buf)
public function write(string $buf): void
{
$this->wBuf .= $buf;
if (strlen($this->wBuf) >= $this->wBufSize) {
$out = $this->wBuf;

// Note that we clear the internal wBuf_ prior to the underlying write
// to ensure we're in a sane state (i.e. internal buffer cleaned)
// if the underlying write throws up an exception
// Clear the buffer before writing so we stay in a sane state
// even if the underlying transport throws.
$this->wBuf = '';
$this->transport->write($out);
}
}

/**
* @inheritdoc
*
* @throws TTransportException
*/
public function flush()
public function flush(): void
{
if (strlen($this->wBuf) > 0) {
$out = $this->wBuf;

// Note that we clear the internal wBuf_ prior to the underlying write
// to ensure we're in a sane state (i.e. internal buffer cleaned)
// if the underlying write throws up an exception
// Clear the buffer before writing so we stay in a sane state
// even if the underlying transport throws.
$this->wBuf = '';
$this->transport->write($out);
}
Expand Down
83 changes: 26 additions & 57 deletions lib/php/lib/Transport/TCurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,14 @@ class TCurlClient extends TTransport
protected string|false|null $response = null;

/**
* Read timeout
*
* @var float|int|null
* Read timeout in seconds.
*/
protected $timeout = null;
protected ?float $timeout = null;

/**
* Connection timeout
*
* @var float|int|null
* Connection timeout in seconds.
*/
protected $connectionTimeout = null;
protected ?float $connectionTimeout = null;

/**
* http headers
Expand All @@ -85,81 +81,54 @@ public function __construct(
$this->uri = ($uri === '' || str_starts_with($uri, '/')) ? $uri : '/' . $uri;
}

/**
* Set read timeout
*
* @param float $timeout
*/
public function setTimeoutSecs($timeout)
public function setTimeoutSecs(?float $timeout): void
{
$this->timeout = $timeout;
}

/**
* Set connection timeout
*
* @param float $connectionTimeout
*/
public function setConnectionTimeoutSecs($connectionTimeout)
public function setConnectionTimeoutSecs(?float $connectionTimeout): void
{
$this->connectionTimeout = $connectionTimeout;
}

/**
* Whether this transport is open.
*
* @return boolean true if open
*/
public function isOpen()
public function isOpen(): bool
{
return true;
}

/**
* Open the transport for reading/writing
*
* @throws TTransportException if cannot open
*/
public function open()
public function open(): void
{
}

/**
* Close the transport.
*/
public function close()
public function close(): void
{
$this->request = '';
$this->response = null;
}

/**
* Read some data into the array.
*
* @param int $len How much to read
* @return string The data that has been read
* @throws TTransportException if cannot read any more data
*/
public function read($len)
public function read(int $len): string
{
if ($len >= strlen($this->response)) {
return $this->response;
} else {
$ret = substr($this->response, 0, $len);
$this->response = substr($this->response, $len);

return $ret;
$response = (string) $this->response;
if ($len >= strlen($response)) {
return $response;
}

$ret = substr($response, 0, $len);
$this->response = substr($response, $len);

return $ret;
}

/**
* Guarantees that the full amount of data is read. Since TCurlClient gets entire payload at
* once, parent readAll cannot be used.
*
* @return string The data, of exact length
* @throws TTransportException if cannot read data
*/
public function readAll($len)
public function readAll(int $len): string
{
$data = $this->read($len);

Expand All @@ -171,12 +140,9 @@ public function readAll($len)
}

/**
* Writes some data into the pending buffer
*
* @param string $buf The data to write
* @throws TTransportException if writing fails
*/
public function write($buf)
public function write(string $buf): void
{
$this->request .= $buf;
}
Expand All @@ -186,7 +152,7 @@ public function write($buf)
*
* @throws TTransportException if a writing error occurs
*/
public function flush()
public function flush(): void
{
if (!self::$curlHandle) {
register_shutdown_function(['Thrift\\Transport\\TCurlClient', 'closeCurlHandle']);
Expand Down Expand Up @@ -254,7 +220,7 @@ public function flush()
}
}

public static function closeCurlHandle()
public static function closeCurlHandle(): void
{
try {
if (self::$curlHandle) {
Expand All @@ -269,7 +235,10 @@ public static function closeCurlHandle()
}
}

public function addHeaders($headers)
/**
* @param array<string, string|int> $headers
*/
public function addHeaders(array $headers): void
{
$this->headers = array_merge($this->headers, $headers);
}
Expand Down
Loading
Loading