+++ /dev/null
-<?php\r
-/**\r
- * Adapter for HTTP_Request2 wrapping around cURL extension\r
- *\r
- * PHP version 5\r
- *\r
- * LICENSE:\r
- *\r
- * Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions\r
- * are met:\r
- *\r
- * * Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- * * Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- * * The names of the authors may not be used to endorse or promote products\r
- * derived from this software without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @license http://opensource.org/licenses/bsd-license.php New BSD License\r
- * @version CVS: $Id: Curl.php 278226 2009-04-03 21:32:48Z avb $\r
- * @link http://pear.php.net/package/HTTP_Request2\r
- */\r
-\r
-/**\r
- * Base class for HTTP_Request2 adapters\r
- */\r
-require_once 'HTTP/Request2/Adapter.php';\r
-\r
-/**\r
- * Adapter for HTTP_Request2 wrapping around cURL extension\r
- *\r
- * @category HTTP\r
- * @package HTTP_Request2\r
- * @author Alexey Borzov <avb@php.net>\r
- * @version Release: 0.4.1\r
- */\r
-class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter\r
-{\r
- /**\r
- * Mapping of header names to cURL options\r
- * @var array\r
- */\r
- protected static $headerMap = array(\r
- 'accept-encoding' => CURLOPT_ENCODING,\r
- 'cookie' => CURLOPT_COOKIE,\r
- 'referer' => CURLOPT_REFERER,\r
- 'user-agent' => CURLOPT_USERAGENT\r
- );\r
-\r
- /**\r
- * Mapping of SSL context options to cURL options\r
- * @var array\r
- */\r
- protected static $sslContextMap = array(\r
- 'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER,\r
- 'ssl_cafile' => CURLOPT_CAINFO,\r
- 'ssl_capath' => CURLOPT_CAPATH,\r
- 'ssl_local_cert' => CURLOPT_SSLCERT,\r
- 'ssl_passphrase' => CURLOPT_SSLCERTPASSWD\r
- );\r
-\r
- /**\r
- * Response being received\r
- * @var HTTP_Request2_Response\r
- */\r
- protected $response;\r
-\r
- /**\r
- * Whether 'sentHeaders' event was sent to observers\r
- * @var boolean\r
- */\r
- protected $eventSentHeaders = false;\r
-\r
- /**\r
- * Whether 'receivedHeaders' event was sent to observers\r
- * @var boolean\r
- */\r
- protected $eventReceivedHeaders = false;\r
-\r
- /**\r
- * Position within request body\r
- * @var integer\r
- * @see callbackReadBody()\r
- */\r
- protected $position = 0;\r
-\r
- /**\r
- * Information about last transfer, as returned by curl_getinfo()\r
- * @var array\r
- */\r
- protected $lastInfo;\r
-\r
- /**\r
- * Sends request to the remote server and returns its response\r
- *\r
- * @param HTTP_Request2\r
- * @return HTTP_Request2_Response\r
- * @throws HTTP_Request2_Exception\r
- */\r
- public function sendRequest(HTTP_Request2 $request)\r
- {\r
- if (!extension_loaded('curl')) {\r
- throw new HTTP_Request2_Exception('cURL extension not available');\r
- }\r
-\r
- $this->request = $request;\r
- $this->response = null;\r
- $this->position = 0;\r
- $this->eventSentHeaders = false;\r
- $this->eventReceivedHeaders = false;\r
-\r
- try {\r
- if (false === curl_exec($ch = $this->createCurlHandle())) {\r
- $errorMessage = 'Error sending request: #' . curl_errno($ch) .\r
- ' ' . curl_error($ch);\r
- }\r
- } catch (Exception $e) {\r
- }\r
- $this->lastInfo = curl_getinfo($ch);\r
- curl_close($ch);\r
-\r
- if (!empty($e)) {\r
- throw $e;\r
- } elseif (!empty($errorMessage)) {\r
- throw new HTTP_Request2_Exception($errorMessage);\r
- }\r
-\r
- if (0 < $this->lastInfo['size_download']) {\r
- $this->request->setLastEvent('receivedBody', $this->response);\r
- }\r
- return $this->response;\r
- }\r
-\r
- /**\r
- * Returns information about last transfer\r
- *\r
- * @return array associative array as returned by curl_getinfo()\r
- */\r
- public function getInfo()\r
- {\r
- return $this->lastInfo;\r
- }\r
-\r
- /**\r
- * Creates a new cURL handle and populates it with data from the request\r
- *\r
- * @return resource a cURL handle, as created by curl_init()\r
- * @throws HTTP_Request2_Exception\r
- */\r
- protected function createCurlHandle()\r
- {\r
- $ch = curl_init();\r
-\r
- curl_setopt_array($ch, array(\r
- // setup callbacks\r
- CURLOPT_READFUNCTION => array($this, 'callbackReadBody'),\r
- CURLOPT_HEADERFUNCTION => array($this, 'callbackWriteHeader'),\r
- CURLOPT_WRITEFUNCTION => array($this, 'callbackWriteBody'),\r
- // disallow redirects\r
- CURLOPT_FOLLOWLOCATION => false,\r
- // buffer size\r
- CURLOPT_BUFFERSIZE => $this->request->getConfig('buffer_size'),\r
- // connection timeout\r
- CURLOPT_CONNECTTIMEOUT => $this->request->getConfig('connect_timeout'),\r
- // save full outgoing headers, in case someone is interested\r
- CURLINFO_HEADER_OUT => true,\r
- // request url\r
- CURLOPT_URL => $this->request->getUrl()->getUrl()\r
- ));\r
-\r
- // request timeout\r
- if ($timeout = $this->request->getConfig('timeout')) {\r
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);\r
- }\r
-\r
- // set HTTP version\r
- switch ($this->request->getConfig('protocol_version')) {\r
- case '1.0':\r
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);\r
- break;\r
- case '1.1':\r
- curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);\r
- }\r
-\r
- // set request method\r
- switch ($this->request->getMethod()) {\r
- case HTTP_Request2::METHOD_GET:\r
- curl_setopt($ch, CURLOPT_HTTPGET, true);\r
- break;\r
- case HTTP_Request2::METHOD_POST:\r
- curl_setopt($ch, CURLOPT_POST, true);\r
- break;\r
- default:\r
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->request->getMethod());\r
- }\r
-\r
- // set proxy, if needed\r
- if ($host = $this->request->getConfig('proxy_host')) {\r
- if (!($port = $this->request->getConfig('proxy_port'))) {\r
- throw new HTTP_Request2_Exception('Proxy port not provided');\r
- }\r
- curl_setopt($ch, CURLOPT_PROXY, $host . ':' . $port);\r
- if ($user = $this->request->getConfig('proxy_user')) {\r
- curl_setopt($ch, CURLOPT_PROXYUSERPWD, $user . ':' .\r
- $this->request->getConfig('proxy_password'));\r
- switch ($this->request->getConfig('proxy_auth_scheme')) {\r
- case HTTP_Request2::AUTH_BASIC:\r
- curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);\r
- break;\r
- case HTTP_Request2::AUTH_DIGEST:\r
- curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST);\r
- }\r
- }\r
- }\r
-\r
- // set authentication data\r
- if ($auth = $this->request->getAuth()) {\r
- curl_setopt($ch, CURLOPT_USERPWD, $auth['user'] . ':' . $auth['password']);\r
- switch ($auth['scheme']) {\r
- case HTTP_Request2::AUTH_BASIC:\r
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);\r
- break;\r
- case HTTP_Request2::AUTH_DIGEST:\r
- curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);\r
- }\r
- }\r
-\r
- // set SSL options\r
- if (0 == strcasecmp($this->request->getUrl()->getScheme(), 'https')) {\r
- foreach ($this->request->getConfig() as $name => $value) {\r
- if ('ssl_verify_host' == $name && null !== $value) {\r
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $value? 2: 0);\r
- } elseif (isset(self::$sslContextMap[$name]) && null !== $value) {\r
- curl_setopt($ch, self::$sslContextMap[$name], $value);\r
- }\r
- }\r
- }\r
-\r
- $headers = $this->request->getHeaders();\r
- // make cURL automagically send proper header\r
- if (!isset($headers['accept-encoding'])) {\r
- $headers['accept-encoding'] = '';\r
- }\r
-\r
- // set headers having special cURL keys\r
- foreach (self::$headerMap as $name => $option) {\r
- if (isset($headers[$name])) {\r
- curl_setopt($ch, $option, $headers[$name]);\r
- unset($headers[$name]);\r
- }\r
- }\r
-\r
- $this->calculateRequestLength($headers);\r
-\r
- // set headers not having special keys\r
- $headersFmt = array();\r
- foreach ($headers as $name => $value) {\r
- $canonicalName = implode('-', array_map('ucfirst', explode('-', $name)));\r
- $headersFmt[] = $canonicalName . ': ' . $value;\r
- }\r
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFmt);\r
-\r
- return $ch;\r
- }\r
-\r
- /**\r
- * Callback function called by cURL for reading the request body\r
- *\r
- * @param resource cURL handle\r
- * @param resource file descriptor (not used)\r
- * @param integer maximum length of data to return\r
- * @return string part of the request body, up to $length bytes \r
- */\r
- protected function callbackReadBody($ch, $fd, $length)\r
- {\r
- if (!$this->eventSentHeaders) {\r
- $this->request->setLastEvent(\r
- 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT)\r
- );\r
- $this->eventSentHeaders = true;\r
- }\r
- if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||\r
- 0 == $this->contentLength || $this->position >= $this->contentLength\r
- ) {\r
- return '';\r
- }\r
- if (is_string($this->requestBody)) {\r
- $string = substr($this->requestBody, $this->position, $length);\r
- } elseif (is_resource($this->requestBody)) {\r
- $string = fread($this->requestBody, $length);\r
- } else {\r
- $string = $this->requestBody->read($length);\r
- }\r
- $this->request->setLastEvent('sentBodyPart', strlen($string));\r
- $this->position += strlen($string);\r
- return $string;\r
- }\r
-\r
- /**\r
- * Callback function called by cURL for saving the response headers\r
- *\r
- * @param resource cURL handle\r
- * @param string response header (with trailing CRLF)\r
- * @return integer number of bytes saved\r
- * @see HTTP_Request2_Response::parseHeaderLine()\r
- */\r
- protected function callbackWriteHeader($ch, $string)\r
- {\r
- // we may receive a second set of headers if doing e.g. digest auth\r
- if ($this->eventReceivedHeaders || !$this->eventSentHeaders) {\r
- // don't bother with 100-Continue responses (bug #15785)\r
- if (!$this->eventSentHeaders ||\r
- $this->response->getStatus() >= 200\r
- ) {\r
- $this->request->setLastEvent(\r
- 'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT)\r
- );\r
- }\r
- $this->eventSentHeaders = true;\r
- // we'll need a new response object\r
- if ($this->eventReceivedHeaders) {\r
- $this->eventReceivedHeaders = false;\r
- $this->response = null;\r
- }\r
- }\r
- if (empty($this->response)) {\r
- $this->response = new HTTP_Request2_Response($string, false);\r
- } else {\r
- $this->response->parseHeaderLine($string);\r
- if ('' == trim($string)) {\r
- // don't bother with 100-Continue responses (bug #15785)\r
- if (200 <= $this->response->getStatus()) {\r
- $this->request->setLastEvent('receivedHeaders', $this->response);\r
- }\r
- $this->eventReceivedHeaders = true;\r
- }\r
- }\r
- return strlen($string);\r
- }\r
-\r
- /**\r
- * Callback function called by cURL for saving the response body\r
- *\r
- * @param resource cURL handle (not used)\r
- * @param string part of the response body\r
- * @return integer number of bytes saved\r
- * @see HTTP_Request2_Response::appendBody()\r
- */\r
- protected function callbackWriteBody($ch, $string)\r
- {\r
- // cURL calls WRITEFUNCTION callback without calling HEADERFUNCTION if \r
- // response doesn't start with proper HTTP status line (see bug #15716)\r
- if (empty($this->response)) {\r
- throw new HTTP_Request2_Exception("Malformed response: {$string}");\r
- }\r
- if ($this->request->getConfig('store_body')) {\r
- $this->response->appendBody($string);\r
- }\r
- $this->request->setLastEvent('receivedBodyPart', $string);\r
- return strlen($string);\r
- }\r
-}\r
-?>\r