PluginProbe
InfiniteWP Client / trunk
InfiniteWP Client vtrunk
1.13.10 1.13.7 trunk 0.1.4 0.1.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.11.0 1.11.1 1.12.1 1.12.3 All 92 releases
iwp-client / lib / Google2 / IO / Curl.php

Curl.php in InfiniteWP Client trunk, at lib/Google2/IO/Curl.php

183 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2014 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /**
19 * Curl based implementation of Google_IO.
20 *
21 * @author Stuart Langley <slangley@google.com>
22 */
23
24 if (!class_exists('Google_Client')) {
25 require_once dirname(__FILE__) . '/../autoload.php';
26 }
27
28 class Google_IO_Curl extends Google_IO_Abstract
29 {
30 // cURL hex representation of version 7.30.0
31 const NO_QUIRK_VERSION = 0x071E00;
32
33 private $options = array();
34
35 public function __construct(Google_Client $client)
36 {
37 if (!extension_loaded('curl')) {
38 $error = 'The cURL IO handler requires the cURL extension to be enabled';
39 $client->getLogger()->critical($error);
40 throw new Google_IO_Exception($error);
41 }
42
43 parent::__construct($client);
44 }
45
46 /**
47 * Execute an HTTP Request
48 *
49 * @param Google_Http_Request $request the http request to be executed
50 * @return array containing response headers, body, and http code
51 * @throws Google_IO_Exception on curl or IO error
52 */
53 public function executeRequest(Google_Http_Request $request)
54 {
55 $curl = curl_init();
56
57 if ($request->getPostBody()) {
58 curl_setopt($curl, CURLOPT_POSTFIELDS, $request->getPostBody());
59 }
60
61 $requestHeaders = $request->getRequestHeaders();
62 if ($requestHeaders && is_array($requestHeaders)) {
63 $curlHeaders = array();
64 foreach ($requestHeaders as $k => $v) {
65 $curlHeaders[] = "$k: $v";
66 }
67 curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders);
68 }
69 curl_setopt($curl, CURLOPT_URL, $request->getUrl());
70 if (defined('IWP_GOOGLE_DRIVE_UPLOAD_HTTP_VERSION')) {
71 curl_setopt($curl, CURLOPT_HTTP_VERSION, IWP_GOOGLE_DRIVE_UPLOAD_HTTP_VERSION);
72 }
73 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $request->getRequestMethod());
74 curl_setopt($curl, CURLOPT_USERAGENT, $request->getUserAgent());
75 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
76 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
77 // 1 is CURL_SSLVERSION_TLSv1, which is not always defined in PHP.
78 // patch
79 // The SDK leaves this on the default setting in later releases
80 // curl_setopt($curl, CURLOPT_SSLVERSION, 1);
81 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
82 curl_setopt($curl, CURLOPT_HEADER, true);
83
84 if ($request->canGzip()) {
85 curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
86 }
87
88 $options = $this->client->getClassConfig('Google_IO_Curl', 'options');
89 if (is_array($options)) {
90 $this->setOptions($options);
91 }
92
93 foreach ($this->options as $key => $var) {
94 curl_setopt($curl, $key, $var);
95 }
96
97 if (!isset($this->options[CURLOPT_CAINFO])) {
98 curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacerts.pem');
99 }
100
101 $this->client->getLogger()->debug(
102 'cURL request',
103 array(
104 'url' => $request->getUrl(),
105 'method' => $request->getRequestMethod(),
106 'headers' => $requestHeaders,
107 'body' => $request->getPostBody()
108 )
109 );
110
111 $response = curl_exec($curl);
112 if ($response === false) {
113 $error = curl_error($curl);
114 $code = curl_errno($curl);
115 $map = $this->client->getClassConfig('Google_IO_Exception', 'retry_map');
116
117 $this->client->getLogger()->error('cURL ' . $error);
118 throw new Google_IO_Exception($error, $code, null, $map);
119 }
120 $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
121
122 list($responseHeaders, $responseBody) = $this->parseHttpResponse($response, $headerSize);
123 $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
124
125 $this->client->getLogger()->debug(
126 'cURL response',
127 array(
128 'code' => $responseCode,
129 'headers' => $responseHeaders,
130 'body' => $responseBody,
131 )
132 );
133
134 return array($responseBody, $responseHeaders, $responseCode);
135 }
136
137 /**
138 * Set options that update the transport implementation's behavior.
139 * @param $options
140 */
141 public function setOptions($options)
142 {
143 $this->options = $options + $this->options;
144 }
145
146 /**
147 * Set the maximum request time in seconds.
148 * @param $timeout in seconds
149 */
150 public function setTimeout($timeout)
151 {
152 // Since this timeout is really for putting a bound on the time
153 // we'll set them both to the same. If you need to specify a longer
154 // CURLOPT_TIMEOUT, or a higher CONNECTTIMEOUT, the best thing to
155 // do is use the setOptions method for the values individually.
156 $this->options[CURLOPT_CONNECTTIMEOUT] = $timeout;
157 $this->options[CURLOPT_TIMEOUT] = $timeout;
158 }
159
160 /**
161 * Get the maximum request time in seconds.
162 * @return timeout in seconds
163 */
164 public function getTimeout()
165 {
166 return $this->options[CURLOPT_TIMEOUT];
167 }
168
169 /**
170 * Test for the presence of a cURL header processing bug
171 *
172 * {@inheritDoc}
173 *
174 * @return boolean
175 */
176 protected function needsQuirk()
177 {
178 $ver = curl_version();
179 $versionNum = $ver['version_number'];
180 return $versionNum < Google_IO_Curl::NO_QUIRK_VERSION;
181 }
182 }
183