@@ -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
0 commit comments