import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
392
libs/Zend/Http/Client/Adapter/Curl.php
Normal file
392
libs/Zend/Http/Client/Adapter/Curl.php
Normal file
@ -0,0 +1,392 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Curl.php 14379 2009-03-19 14:57:23Z matthew $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
|
||||
/** Zend_Uri_Http */
|
||||
require_once 'Zend/Uri/Http.php';
|
||||
|
||||
/** Zend_Http_Client_Adapter_Interface */
|
||||
require_once 'Zend/Http/Client/Adapter/Interface.php';
|
||||
|
||||
/**
|
||||
* An adapter class for Zend_Http_Client based on the curl extension.
|
||||
* Curl requires libcurl. See for full requirements the PHP manual: http://php.net/curl
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Http_Client_Adapter_Curl implements Zend_Http_Client_Adapter_Interface
|
||||
{
|
||||
/**
|
||||
* Parameters array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_config = array();
|
||||
|
||||
/**
|
||||
* What host/port are we connected to?
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_connected_to = array(null, null);
|
||||
|
||||
/**
|
||||
* The curl session handle
|
||||
*
|
||||
* @var resource|null
|
||||
*/
|
||||
protected $_curl = null;
|
||||
|
||||
/**
|
||||
* List of cURL options that should never be overwritten
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_invalidOverwritableCurlOptions = array(
|
||||
CURLOPT_HTTPGET,
|
||||
CURLOPT_POST,
|
||||
CURLOPT_PUT,
|
||||
CURLOPT_CUSTOMREQUEST,
|
||||
CURLOPT_HEADER,
|
||||
CURLOPT_RETURNTRANSFER,
|
||||
CURLOPT_HTTPHEADER,
|
||||
CURLOPT_POSTFIELDS,
|
||||
CURLOPT_INFILE,
|
||||
CURLOPT_INFILESIZE,
|
||||
CURLOPT_PORT,
|
||||
CURLOPT_MAXREDIRS,
|
||||
CURLOPT_TIMEOUT,
|
||||
CURL_HTTP_VERSION_1_1,
|
||||
CURL_HTTP_VERSION_1_0,
|
||||
);
|
||||
|
||||
/**
|
||||
* Response gotten from server
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $_response = null;
|
||||
|
||||
/**
|
||||
* Adapter constructor
|
||||
*
|
||||
* Config is set using setConfig()
|
||||
*
|
||||
* @return void
|
||||
* @throws Zend_Http_Client_Adapter_Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('curl')) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('cURL extension has to be loaded to use this Zend_Http_Client adapter.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the configuration array for the adapter
|
||||
*
|
||||
* @throws Zend_Http_Client_Adapter_Exception
|
||||
* @param array $config
|
||||
* @return Zend_Http_Client_Adapter_Curl
|
||||
*/
|
||||
public function setConfig($config = array())
|
||||
{
|
||||
if (!is_array($config)) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Http Adapter configuration expects an array, ' . gettype($config) . ' recieved.');
|
||||
}
|
||||
|
||||
foreach ($config as $k => $v) {
|
||||
$this->_config[strtolower($k)] = $v;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Direct setter for cURL adapter related options.
|
||||
*
|
||||
* @param string|int $option
|
||||
* @param mixed $value
|
||||
* @return Zend_Http_Adapter_Curl
|
||||
*/
|
||||
public function setCurlOption($option, $value)
|
||||
{
|
||||
if (!isset($this->_config['curloptions'])) {
|
||||
$this->_config['curloptions'] = array();
|
||||
}
|
||||
$this->_config['curloptions'][$option] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize curl
|
||||
*
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param boolean $secure
|
||||
* @return void
|
||||
* @throws Zend_Http_Client_Adapter_Exception if unable to connect
|
||||
*/
|
||||
public function connect($host, $port = 80, $secure = false)
|
||||
{
|
||||
// If we're already connected, disconnect first
|
||||
if ($this->_curl) {
|
||||
$this->close();
|
||||
}
|
||||
|
||||
// If we are connected to a different server or port, disconnect first
|
||||
if ($this->_curl
|
||||
&& is_array($this->_connected_to)
|
||||
&& ($this->_connected_to[0] != $host
|
||||
|| $this->_connected_to[1] != $port)
|
||||
) {
|
||||
$this->close();
|
||||
}
|
||||
|
||||
// Do the actual connection
|
||||
$this->_curl = curl_init();
|
||||
if ($port != 80) {
|
||||
curl_setopt($this->_curl, CURLOPT_PORT, intval($port));
|
||||
}
|
||||
|
||||
// Set timeout
|
||||
curl_setopt($this->_curl, CURLOPT_TIMEOUT, $this->_config['timeout']);
|
||||
|
||||
// Set Max redirects
|
||||
curl_setopt($this->_curl, CURLOPT_MAXREDIRS, $this->_config['maxredirects']);
|
||||
|
||||
if (!$this->_curl) {
|
||||
$this->close();
|
||||
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception('Unable to Connect to ' . $host . ':' . $port);
|
||||
}
|
||||
|
||||
if ($secure !== false) {
|
||||
// Behave the same like Zend_Http_Adapter_Socket on SSL options.
|
||||
if (isset($this->_config['sslcert'])) {
|
||||
curl_setopt($this->_curl, CURLOPT_SSLCERT, $this->_config['sslcert']);
|
||||
}
|
||||
if (isset($this->_config['sslpassphrase'])) {
|
||||
curl_setopt($this->_curl, CURLOPT_SSLCERTPASSWD, $this->_config['sslpassphrase']);
|
||||
}
|
||||
}
|
||||
|
||||
// Update connected_to
|
||||
$this->_connected_to = array($host, $port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request to the remote server
|
||||
*
|
||||
* @param string $method
|
||||
* @param Zend_Uri_Http $uri
|
||||
* @param float $http_ver
|
||||
* @param array $headers
|
||||
* @param string $body
|
||||
* @return string $request
|
||||
* @throws Zend_Http_Client_Adapter_Exception If connection fails, connected to wrong host, no PUT file defined, unsupported method, or unsupported cURL option
|
||||
*/
|
||||
public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
|
||||
{
|
||||
// Make sure we're properly connected
|
||||
if (!$this->_curl) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
|
||||
}
|
||||
|
||||
if ($this->_connected_to[0] != $uri->getHost() || $this->_connected_to[1] != $uri->getPort()) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong host");
|
||||
}
|
||||
|
||||
// set URL
|
||||
curl_setopt($this->_curl, CURLOPT_URL, $uri->__toString());
|
||||
|
||||
// ensure correct curl call
|
||||
$curlValue = true;
|
||||
switch ($method) {
|
||||
case Zend_Http_Client::GET:
|
||||
$curlMethod = CURLOPT_HTTPGET;
|
||||
break;
|
||||
|
||||
case Zend_Http_Client::POST:
|
||||
$curlMethod = CURLOPT_POST;
|
||||
break;
|
||||
|
||||
case Zend_Http_Client::PUT:
|
||||
// There are two different types of PUT request, either a Raw Data string has been set
|
||||
// or CURLOPT_INFILE and CURLOPT_INFILESIZE are used.
|
||||
if (isset($this->_config['curloptions'][CURLOPT_INFILE])) {
|
||||
if (!isset($this->_config['curloptions'][CURLOPT_INFILESIZE])) {
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Cannot set a file-handle for cURL option CURLOPT_INFILE without also setting its size in CURLOPT_INFILESIZE.");
|
||||
}
|
||||
|
||||
// Now we will probably already have Content-Length set, so that we have to delete it
|
||||
// from $headers at this point:
|
||||
foreach ($headers AS $k => $header) {
|
||||
if (stristr($header, "Content-Length:") !== false) {
|
||||
unset($headers[$k]);
|
||||
}
|
||||
}
|
||||
|
||||
$curlMethod = CURLOPT_PUT;
|
||||
} else {
|
||||
$curlMethod = CURLOPT_CUSTOMREQUEST;
|
||||
$curlValue = "PUT";
|
||||
}
|
||||
break;
|
||||
|
||||
case Zend_Http_Client::DELETE:
|
||||
$curlMethod = CURLOPT_CUSTOMREQUEST;
|
||||
$curlValue = "DELETE";
|
||||
break;
|
||||
|
||||
case Zend_Http_Client::OPTIONS:
|
||||
$curlMethod = CURLOPT_CUSTOMREQUEST;
|
||||
$curlValue = "OPTIONS";
|
||||
break;
|
||||
|
||||
case Zend_Http_Client::TRACE:
|
||||
$curlMethod = CURLOPT_CUSTOMREQUEST;
|
||||
$curlValue = "TRACE";
|
||||
break;
|
||||
|
||||
default:
|
||||
// For now, through an exception for unsupported request methods
|
||||
require_once 'Zend/Http/Client/Adapter/Exception.php';
|
||||
throw new Zend_Http_Client_Adapter_Exception("Method currently not supported");
|
||||
}
|
||||
|
||||
// get http version to use
|
||||
$curlHttp = ($http_ver = 1.1) ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_1_0;
|
||||
|
||||
// mark as HTTP request and set HTTP method
|
||||
curl_setopt($this->_curl, $curlHttp, true);
|
||||
curl_setopt($this->_curl, $curlMethod, $curlValue);
|
||||
|
||||
// ensure headers are also returned
|
||||
curl_setopt($this->_curl, CURLOPT_HEADER, true);
|
||||
|
||||
// ensure actual response is returned
|
||||
curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, true);
|
||||
|
||||
// set additional headers
|
||||
$headers['Accept'] = '';
|
||||
curl_setopt($this->_curl, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
/**
|
||||
* Make sure POSTFIELDS is set after $curlMethod is set:
|
||||
* @link http://de2.php.net/manual/en/function.curl-setopt.php#81161
|
||||
*/
|
||||
if ($method == Zend_Http_Client::POST) {
|
||||
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
|
||||
} elseif ($curlMethod == CURLOPT_PUT) {
|
||||
// this covers a PUT by file-handle:
|
||||
// Make the setting of this options explicit (rather than setting it through the loop following a bit lower)
|
||||
// to group common functionality together.
|
||||
curl_setopt($this->_curl, CURLOPT_INFILE, $this->_config['curloptions'][CURLOPT_INFILE]);
|
||||
curl_setopt($this->_curl, CURLOPT_INFILESIZE, $this->_config['curloptions'][CURLOPT_INFILESIZE]);
|
||||
unset($this->_config['curloptions'][CURLOPT_INFILE]);
|
||||
unset($this->_config['curloptions'][CURLOPT_INFILESIZE]);
|
||||
} elseif ($method == Zend_Http_Client::PUT) {
|
||||
// This is a PUT by a setRawData string, not by file-handle
|
||||
curl_setopt($this->_curl, CURLOPT_POSTFIELDS, $body);
|
||||
}
|
||||
|
||||
// set additional curl options
|
||||
if (isset($this->_config['curloptions'])) {
|
||||
foreach ((array)$this->_config['curloptions'] as $k => $v) {
|
||||
if (!in_array($k, $this->_invalidOverwritableCurlOptions)) {
|
||||
if (curl_setopt($this->_curl, $k, $v) == false) {
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
throw new Zend_Http_Client_Exception(sprintf("Unknown or erroreous cURL option '%s' set", $k));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// send the request
|
||||
$this->_response = curl_exec($this->_curl);
|
||||
|
||||
$request = curl_getinfo($this->_curl, CURLINFO_HEADER_OUT);
|
||||
$request .= $body;
|
||||
|
||||
if (empty($this->_response)) {
|
||||
require_once 'Zend/Http/Client/Exception.php';
|
||||
throw new Zend_Http_Client_Exception("Error in cURL request: " . curl_error($this->_curl));
|
||||
}
|
||||
|
||||
// cURL automatically decodes chunked-messages, this means we have to disallow the Zend_Http_Response to do it again
|
||||
if (stripos($this->_response, "Transfer-Encoding: chunked\r\n")) {
|
||||
$this->_response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $this->_response);
|
||||
}
|
||||
|
||||
// Eliminate multiple HTTP responses.
|
||||
do {
|
||||
$parts = preg_split('|(?:\r?\n){2}|m', $this->_response, 2);
|
||||
$again = false;
|
||||
|
||||
if (isset($parts[1]) && preg_match("|^HTTP/1\.[01](.*?)\r\n|mi", $parts[1])) {
|
||||
$this->_response = $parts[1];
|
||||
$again = true;
|
||||
}
|
||||
} while ($again);
|
||||
|
||||
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string:
|
||||
if (stripos($this->_response, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
|
||||
$this->_response = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $this->_response);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return read response from server
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
return $this->_response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the connection to the server
|
||||
*
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if(is_resource($this->_curl)) {
|
||||
curl_close($this->_curl);
|
||||
}
|
||||
$this->_curl = null;
|
||||
$this->_connected_to = array(null, null);
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* @category Zend
|
||||
* @package Zend_Http
|
||||
* @subpackage Client_Adapter
|
||||
* @version $Id: Socket.php 12105 2008-10-23 23:38:55Z shahar $
|
||||
* @version $Id: Socket.php 13013 2008-12-04 12:04:24Z yoshida@zend.co.jp $
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
@ -135,7 +135,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
|
||||
$flags = STREAM_CLIENT_CONNECT;
|
||||
if ($this->config['persistent']) $flags |= STREAM_CLIENT_PERSISTENT;
|
||||
|
||||
|
||||
$this->socket = @stream_socket_client($host . ':' . $port,
|
||||
$errno,
|
||||
$errstr,
|
||||
@ -228,7 +228,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
}
|
||||
|
||||
$statusCode = Zend_Http_Response::extractCode($response);
|
||||
|
||||
|
||||
// Handle 100 and 101 responses internally by restarting the read again
|
||||
if ($statusCode == 100 || $statusCode == 101) return $this->read();
|
||||
|
||||
@ -236,7 +236,7 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
* Responses to HEAD requests and 204 or 304 responses are not expected
|
||||
* to have a body - stop reading here
|
||||
*/
|
||||
if ($statusCode == 304 || $statusCode == 204 ||
|
||||
if ($statusCode == 304 || $statusCode == 204 ||
|
||||
$this->method == Zend_Http_Client::HEAD) return $response;
|
||||
|
||||
// Check headers to see what kind of connection / transfer encoding we have
|
||||
@ -260,14 +260,19 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
|
||||
// Convert the hexadecimal value to plain integer
|
||||
$chunksize = hexdec($chunksize);
|
||||
|
||||
|
||||
// Read chunk
|
||||
$left_to_read = $chunksize;
|
||||
while ($left_to_read > 0) {
|
||||
$line = @fread($this->socket, $left_to_read);
|
||||
$chunk .= $line;
|
||||
$left_to_read -= strlen($line);
|
||||
|
||||
if ($line === false || strlen($line) === 0)
|
||||
{
|
||||
break;
|
||||
} else {
|
||||
$chunk .= $line;
|
||||
$left_to_read -= strlen($line);
|
||||
}
|
||||
|
||||
// Break if the connection ended prematurely
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
@ -275,35 +280,45 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
$chunk .= @fgets($this->socket);
|
||||
$response .= $chunk;
|
||||
} while ($chunksize > 0);
|
||||
|
||||
|
||||
} else {
|
||||
throw new Zend_Http_Client_Adapter_Exception('Cannot handle "' .
|
||||
$headers['transfer-encoding'] . '" transfer encoding');
|
||||
}
|
||||
|
||||
|
||||
// Else, if we got the content-length header, read this number of bytes
|
||||
} elseif (isset($headers['content-length'])) {
|
||||
$left_to_read = $headers['content-length'];
|
||||
$chunk = '';
|
||||
while ($left_to_read > 0) {
|
||||
$chunk = @fread($this->socket, $left_to_read);
|
||||
$left_to_read -= strlen($chunk);
|
||||
$response .= $chunk;
|
||||
|
||||
if ($chunk === false || strlen($chunk) === 0)
|
||||
{
|
||||
break;
|
||||
} else {
|
||||
$left_to_read -= strlen($chunk);
|
||||
$response .= $chunk;
|
||||
}
|
||||
|
||||
// Break if the connection ended prematurely
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
|
||||
|
||||
// Fallback: just read the response until EOF
|
||||
} else {
|
||||
while (($buff = @fread($this->socket, 8192)) !== false) {
|
||||
$response .= $buff;
|
||||
if (feof($this->socket)) break;
|
||||
}
|
||||
do {
|
||||
$buff = @fread($this->socket, 8192);
|
||||
if ($buff === false || strlen($buff) === 0)
|
||||
{
|
||||
break;
|
||||
} else {
|
||||
$response .= $buff;
|
||||
}
|
||||
} while (feof($this->socket) === false);
|
||||
|
||||
$this->close();
|
||||
}
|
||||
|
||||
|
||||
// Close the connection if requested to do so by the server
|
||||
if (isset($headers['connection']) && $headers['connection'] == 'close') {
|
||||
$this->close();
|
||||
@ -324,8 +339,8 @@ class Zend_Http_Client_Adapter_Socket implements Zend_Http_Client_Adapter_Interf
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor: make sure the socket is disconnected
|
||||
*
|
||||
* Destructor: make sure the socket is disconnected
|
||||
*
|
||||
* If we are in persistent TCP mode, will not close the connection
|
||||
*
|
||||
*/
|
||||
|
Reference in New Issue
Block a user