forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curlio.php
201 lines (180 loc) · 7.07 KB
/
curlio.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the class moodle_google_curlio.
*
* @package core_google
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/filelib.php');
require_once($CFG->libdir . '/google/io/Google_IO.php');
/**
* Class moodle_google_curlio.
*
* The initial purpose of this class is to add support for our
* class curl in Google_CurlIO.
*
* @package core_google
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class moodle_google_curlio extends Google_CurlIO {
/** @var array associate array of constant value and their name. */
private static $constants = null;
/**
* Private variables have to be redefined here.
*/
private static $ENTITY_HTTP_METHODS = array("POST" => null, "PUT" => null);
private static $HOP_BY_HOP = array(
'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization',
'te', 'trailers', 'transfer-encoding', 'upgrade');
private $curlParams = array(
'CURLOPT_RETURNTRANSFER' => true,
'CURLOPT_FOLLOWLOCATION' => 0,
'CURLOPT_FAILONERROR' => false,
'CURLOPT_SSL_VERIFYPEER' => true,
'CURLOPT_HEADER' => true,
'CURLOPT_VERBOSE' => false
);
/**
* Send the request via our curl object.
*
* @param curl $curl prepared curl object.
* @param Google_HttpRequest $request The request.
* @return string result of the request.
*/
private function do_request($curl, $request) {
$url = $request->getUrl();
$method = $request->getRequestMethod();
switch (strtoupper($method)) {
case 'POST':
$ret = $curl->post($url, $request->getPostBody());
break;
case 'GET':
$ret = $curl->get($url);
break;
case 'HEAD':
$ret = $curl->head($url);
break;
case 'PUT':
$ret = $curl->put($url);
break;
default:
throw new coding_exception('Unknown request type: ' . $method);
break;
}
return $ret;
}
/**
* Send an API request to Google.
*
* This method overwrite the parent one so that the Google SDK will use our class
* curl to proceed with the requests. This allows us to have control over the
* proxy parameters and other stuffs.
*
* Note that the caching support of the Google SDK has been removed from this function.
*
* @param Google_HttpRequest $request the http request to be executed
* @return Google_HttpRequest http request with the response http code, response
* headers and response body filled in
* @throws Google_IOException on curl or IO error
*/
public function makeRequest(Google_HttpRequest $request) {
if (array_key_exists($request->getRequestMethod(), self::$ENTITY_HTTP_METHODS)) {
$request = $this->processEntityRequest($request);
}
$curl = new curl();
$curl->setopt($this->curlParams);
$curl->setopt(array('CURLOPT_URL' => $request->getUrl()));
$requestHeaders = $request->getRequestHeaders();
if ($requestHeaders && is_array($requestHeaders)) {
$parsed = array();
foreach ($requestHeaders as $k => $v) {
$parsed[] = "$k: $v";
}
$curl->setHeader($parsed);
}
$curl->setopt(array(
'CURLOPT_CUSTOMREQUEST' => $request->getRequestMethod(),
'CURLOPT_USERAGENT' => $request->getUserAgent()
));
$respdata = $this->do_request($curl, $request);
// Retry if certificates are missing.
if ($curl->get_errno() == CURLE_SSL_CACERT) {
error_log('SSL certificate problem, verify that the CA cert is OK.' .
' Retrying with the CA cert bundle from google-api-php-client.');
$curl->setopt(array('CURLOPT_CAINFO' => dirname(__FILE__) . '/io/cacerts.pem'));
$respdata = $this->do_request($curl, $request);
}
$infos = $curl->get_info();
$respheadersize = $infos['header_size'];
$resphttpcode = (int) $infos['http_code'];
$curlerrornum = $curl->get_errno();
$curlerror = $curl->error;
if ($curlerrornum != CURLE_OK) {
throw new Google_IOException("HTTP Error: ($resphttpcode) $curlerror");
}
// Parse out the raw response into usable bits.
list($responseHeaders, $responseBody) = self::parseHttpResponse($respdata, $respheadersize);
// Fill in the apiHttpRequest with the response values.
$request->setResponseHttpCode($resphttpcode);
$request->setResponseHeaders($responseHeaders);
$request->setResponseBody($responseBody);
return $request;
}
/**
* Set curl options.
*
* We overwrite this method to ensure that the data passed meets
* the requirement of our curl implementation and so that the keys
* are strings, and not curl constants.
*
* @param array $optCurlParams Multiple options used by a cURL session.
* @return void
*/
public function setOptions($optCurlParams) {
$safeParams = array();
foreach ($optCurlParams as $name => $value) {
if (!is_string($name)) {
$name = $this->get_option_name_from_constant($name);
}
$safeParams[$name] = $value;
}
parent::setOptions($safeParams);
}
/**
* Return the name of an option based on the constant value.
*
* @param int $constant value of a CURL constant.
* @return string name of the constant if found, or throws exception.
* @throws coding_exception when the constant is not found.
* @since 2.5
*/
public function get_option_name_from_constant($constant) {
if (is_null(self::$constants)) {
$constants = get_defined_constants(true);
$constants = isset($constants['curl']) ? $constants['curl'] : array();
$constants = array_flip($constants);
self::$constants = $constants;
}
if (isset(self::$constants[$constant])) {
return self::$constants[$constant];
}
throw new coding_exception('Unknown curl constant value: ' . $constant);
}
}