Skip to content

Commit f19a9d1

Browse files
committed
update to 0.13.0
1 parent b3c6b33 commit f19a9d1

File tree

6 files changed

+63
-13
lines changed

6 files changed

+63
-13
lines changed

src/Base/TBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
*/
3434
abstract class TBase
3535
{
36-
static public $tmethod = array(
36+
public static $tmethod = array(
3737
TType::BOOL => 'Bool',
3838
TType::BYTE => 'Byte',
3939
TType::I16 => 'I16',

src/Exception/TApplicationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
class TApplicationException extends TException
2828
{
29-
static public $_TSPEC =
29+
public static $_TSPEC =
3030
array(1 => array('var' => 'message',
3131
'type' => TType::STRING),
3232
2 => array('var' => 'code',

src/Exception/TException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct($p1 = null, $p2 = 0)
5656
}
5757
}
5858

59-
static public $tmethod = array(
59+
public static $tmethod = array(
6060
TType::BOOL => 'Bool',
6161
TType::BYTE => 'Byte',
6262
TType::I16 => 'I16',

src/Protocol/TProtocol.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
namespace Thrift\Protocol;
2424

25+
use Thrift\Exception\TException;
26+
use Thrift\Transport\TTransport;
2527
use Thrift\Type\TType;
2628
use Thrift\Exception\TProtocolException;
2729

@@ -38,7 +40,7 @@ abstract class TProtocol
3840
protected $trans_;
3941

4042
/**
41-
* Constructor
43+
* @param TTransport $trans
4244
*/
4345
protected function __construct($trans)
4446
{

src/Transport/TCurlClient.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,24 @@ public function read($len)
169169
}
170170
}
171171

172+
/**
173+
* Guarantees that the full amount of data is read. Since TCurlClient gets entire payload at
174+
* once, parent readAll cannot be used.
175+
*
176+
* @return string The data, of exact length
177+
* @throws TTransportException if cannot read data
178+
*/
179+
public function readAll($len)
180+
{
181+
$data = $this->read($len);
182+
183+
if (TStringFuncFactory::create()->strlen($data) !== $len) {
184+
throw new TTransportException('TCurlClient could not read '.$len.' bytes');
185+
}
186+
187+
return $data;
188+
}
189+
172190
/**
173191
* Writes some data into the pending buffer
174192
*
@@ -212,20 +230,35 @@ public function flush()
212230
curl_setopt(self::$curlHandle, CURLOPT_HTTPHEADER, $headers);
213231

214232
if ($this->timeout_ > 0) {
215-
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_);
233+
if ($this->timeout_ < 1.0) {
234+
// Timestamps smaller than 1 second are ignored when CURLOPT_TIMEOUT is used
235+
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT_MS, 1000 * $this->timeout_);
236+
} else {
237+
curl_setopt(self::$curlHandle, CURLOPT_TIMEOUT, $this->timeout_);
238+
}
216239
}
217240
curl_setopt(self::$curlHandle, CURLOPT_POSTFIELDS, $this->request_);
218241
$this->request_ = '';
219242

220243
curl_setopt(self::$curlHandle, CURLOPT_URL, $fullUrl);
221244
$this->response_ = curl_exec(self::$curlHandle);
245+
$responseError = curl_error(self::$curlHandle);
222246

223-
// Connect failed?
224-
if (!$this->response_) {
247+
$code = curl_getinfo(self::$curlHandle, CURLINFO_HTTP_CODE);
248+
249+
// Handle non 200 status code / connect failure
250+
if ($this->response_ === false || $code !== 200) {
225251
curl_close(self::$curlHandle);
226252
self::$curlHandle = null;
253+
$this->response_ = null;
227254
$error = 'TCurlClient: Could not connect to ' . $fullUrl;
228-
throw new TTransportException($error, TTransportException::NOT_OPEN);
255+
if ($responseError) {
256+
$error .= ', ' . $responseError;
257+
}
258+
if ($code) {
259+
$error .= ', HTTP status code: ' . $code;
260+
}
261+
throw new TTransportException($error, TTransportException::UNKNOWN);
229262
}
230263
}
231264

src/Transport/THttpClient.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,23 @@ class THttpClient extends TTransport
8888
*/
8989
protected $headers_;
9090

91+
/**
92+
* Context additional options
93+
*
94+
* @var array
95+
*/
96+
protected $context_;
97+
9198
/**
9299
* Make a new HTTP client.
93100
*
94101
* @param string $host
95-
* @param int $port
102+
* @param int $port
96103
* @param string $uri
104+
* @param string $scheme
105+
* @param array $context
97106
*/
98-
public function __construct($host, $port = 80, $uri = '', $scheme = 'http')
107+
public function __construct($host, $port = 80, $uri = '', $scheme = 'http', array $context = array())
99108
{
100109
if ((TStringFuncFactory::create()->strlen($uri) > 0) && ($uri{0} != '/')) {
101110
$uri = '/' . $uri;
@@ -108,6 +117,7 @@ public function __construct($host, $port = 80, $uri = '', $scheme = 'http')
108117
$this->handle_ = null;
109118
$this->timeout_ = null;
110119
$this->headers_ = array();
120+
$this->context_ = $context;
111121
}
112122

113123
/**
@@ -211,16 +221,21 @@ public function flush()
211221
$headers[] = "$key: $value";
212222
}
213223

214-
$options = array('method' => 'POST',
224+
$options = $this->context_;
225+
226+
$baseHttpOptions = isset($options["http"]) ? $options["http"] : array();
227+
228+
$httpOptions = $baseHttpOptions + array('method' => 'POST',
215229
'header' => implode("\r\n", $headers),
216230
'max_redirects' => 1,
217231
'content' => $this->buf_);
218232
if ($this->timeout_ > 0) {
219-
$options['timeout'] = $this->timeout_;
233+
$httpOptions['timeout'] = $this->timeout_;
220234
}
221235
$this->buf_ = '';
222236

223-
$contextid = stream_context_create(array('http' => $options));
237+
$options["http"] = $httpOptions;
238+
$contextid = stream_context_create($options);
224239
$this->handle_ = @fopen(
225240
$this->scheme_ . '://' . $host . $this->uri_,
226241
'r',

0 commit comments

Comments
 (0)