[7588] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | * |
---|
| 31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
| 32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
| 33 | * @link http://www.solarium-project.org/ |
---|
| 34 | * |
---|
| 35 | * @package Solarium |
---|
| 36 | * @subpackage Client |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * cURL HTTP adapter |
---|
| 41 | * |
---|
| 42 | * @author Intervals <info@myintervals.com> |
---|
| 43 | * @package Solarium |
---|
| 44 | * @subpackage Client |
---|
| 45 | */ |
---|
| 46 | class Solarium_Client_Adapter_Curl extends Solarium_Client_Adapter |
---|
| 47 | { |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Initialization hook |
---|
| 51 | * |
---|
| 52 | * Checks the availability of Curl_http |
---|
| 53 | */ |
---|
| 54 | protected function _init() |
---|
| 55 | { |
---|
| 56 | // @codeCoverageIgnoreStart |
---|
| 57 | if (!function_exists('curl_init')) { |
---|
| 58 | throw new Solarium_Exception('cURL is not available, install it to use the CurlHttp adapter'); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | parent::_init(); |
---|
| 62 | // @codeCoverageIgnoreEnd |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Execute a Solr request using the cURL Http |
---|
| 67 | * |
---|
| 68 | * @param Solarium_Client_Request $request |
---|
| 69 | * @return Solarium_Client_Response |
---|
| 70 | */ |
---|
| 71 | public function execute($request) |
---|
| 72 | { |
---|
| 73 | return $this->_getData($request); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * Execute request |
---|
| 78 | * |
---|
| 79 | * @param Solarium_Client_Request $request |
---|
| 80 | * @return array |
---|
| 81 | */ |
---|
| 82 | protected function _getData($request) |
---|
| 83 | { |
---|
| 84 | // @codeCoverageIgnoreStart |
---|
| 85 | $handle = $this->createHandle($request); |
---|
| 86 | $httpResponse = curl_exec($handle); |
---|
| 87 | |
---|
| 88 | return $this->getResponse($handle, $httpResponse); |
---|
| 89 | // @codeCoverageIgnoreEnd |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Get the response for a curl handle |
---|
| 94 | * |
---|
| 95 | * @param resource $handle |
---|
| 96 | * @param string $httpResponse |
---|
| 97 | * @return Solarium_Client_Response |
---|
| 98 | */ |
---|
| 99 | public function getResponse($handle, $httpResponse) |
---|
| 100 | { |
---|
| 101 | // @codeCoverageIgnoreStart |
---|
| 102 | if ($httpResponse !== false) { |
---|
| 103 | $data = $httpResponse; |
---|
| 104 | $info = curl_getinfo($handle); |
---|
| 105 | $headers = array(); |
---|
| 106 | $headers[] = 'HTTP/1.1 ' . $info['http_code']. ' OK'; |
---|
| 107 | } else { |
---|
| 108 | $headers = array(); |
---|
| 109 | $data = ''; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | curl_close($handle); |
---|
| 113 | $this->check($data, $headers); |
---|
| 114 | return new Solarium_Client_Response($data, $headers); |
---|
| 115 | // @codeCoverageIgnoreEnd |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Create curl handle for a request |
---|
| 120 | * |
---|
| 121 | * @param Solarium_Client_Request $request |
---|
| 122 | * @return resource |
---|
| 123 | */ |
---|
| 124 | public function createHandle($request) |
---|
| 125 | { |
---|
| 126 | // @codeCoverageIgnoreStart |
---|
| 127 | $uri = $this->getBaseUri() . $request->getUri(); |
---|
| 128 | $method = $request->getMethod(); |
---|
| 129 | $options = $this->_createOptions($request); |
---|
| 130 | |
---|
| 131 | $handler = curl_init(); |
---|
| 132 | curl_setopt($handler, CURLOPT_URL, $uri); |
---|
| 133 | curl_setopt($handler, CURLOPT_RETURNTRANSFER, true); |
---|
| 134 | curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true); |
---|
| 135 | curl_setopt($handler, CURLOPT_TIMEOUT, $options['timeout']); |
---|
| 136 | |
---|
| 137 | if (!isset($options['headers']['Content-Type'])) { |
---|
| 138 | $options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | if (count($options['headers'])) { |
---|
| 142 | $headers = array(); |
---|
| 143 | foreach ($options['headers'] as $key => $value) { |
---|
| 144 | $headers[] = $key . ": " . $value; |
---|
| 145 | } |
---|
| 146 | curl_setopt($handler, CURLOPT_HTTPHEADER, $headers); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | if ($method == Solarium_Client_Request::METHOD_POST) { |
---|
| 150 | curl_setopt($handler, CURLOPT_POST, true); |
---|
| 151 | curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData()); |
---|
| 152 | } else if ($method == Solarium_Client_Request::METHOD_GET) { |
---|
| 153 | curl_setopt($handler, CURLOPT_HTTPGET, true); |
---|
| 154 | } else if ($method == Solarium_Client_Request::METHOD_HEAD) { |
---|
| 155 | curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD'); |
---|
| 156 | } else { |
---|
| 157 | throw new Solarium_Exception("unsupported method: $method"); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | return $handler; |
---|
| 161 | // @codeCoverageIgnoreEnd |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * Create http request options from request. |
---|
| 166 | * |
---|
| 167 | * @param Solarium_Client_Request $request |
---|
| 168 | * @return array |
---|
| 169 | */ |
---|
| 170 | protected function _createOptions($request) |
---|
| 171 | { |
---|
| 172 | // @codeCoverageIgnoreStart |
---|
| 173 | $options = array( |
---|
| 174 | 'timeout' => $this->getTimeout() |
---|
| 175 | ); |
---|
| 176 | foreach ($request->getHeaders() as $headerLine) { |
---|
| 177 | list($header, $value) = explode(':', $headerLine); |
---|
| 178 | if ($header = trim($header)) { |
---|
| 179 | $options['headers'][$header] = trim($value); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | return $options; |
---|
| 183 | // @codeCoverageIgnoreEnd |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Check result of a request |
---|
| 188 | * |
---|
| 189 | * @throws Solarium_Client_HttpException |
---|
| 190 | * @param string $data |
---|
| 191 | * @param array $headers |
---|
| 192 | * @return void |
---|
| 193 | */ |
---|
| 194 | public function check($data, $headers) |
---|
| 195 | { |
---|
| 196 | // if there is no data and there are no headers it's a total failure, |
---|
| 197 | // a connection to the host was impossible. |
---|
| 198 | if (empty($data) && count($headers) == 0) { |
---|
| 199 | throw new Solarium_Client_HttpException("HTTP request failed"); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | } |
---|