Skip to content

Commit 920fd71

Browse files
committed
Adding default curl options
Because I removed completely the static setting of SSL_VERIFYPEER I've added the ability to declare default curl options on the class. This means that users that really want to one line turn off SSL_VERIFYPEER can do so without needing to pass a custom option in every request() call.
1 parent f003359 commit 920fd71

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

api/RestfulService.php

+58-23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ class RestfulService extends ViewableData {
1717
protected $customHeaders = array();
1818
protected $proxy;
1919
protected static $default_proxy;
20+
protected static $default_curl_options = array();
21+
22+
/**
23+
* set a curl option that will be applied to all requests as default
24+
* {@see http://php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-parameters}
25+
*
26+
* @param int $option The cURL opt Constant
27+
* @param mixed $value The cURL opt value
28+
*/
29+
public static function set_default_curl_option($option, $value) {
30+
self::$default_curl_options[$option] = $value;
31+
}
32+
33+
/**
34+
* set many defauly curl options at once
35+
*/
36+
public static function set_default_curl_options($optionArray) {
37+
foreach ($optionArray as $option => $value) {
38+
self::set_default_curl_option($option, $value);
39+
}
40+
}
2041

2142
/**
2243
* Sets default proxy settings for outbound RestfulService connections
@@ -119,7 +140,7 @@ public function request($subURL = '', $method = "GET", $data = null, $headers =
119140
$method,
120141
$data,
121142
array_merge((array)$this->customHeaders, (array)$headers),
122-
$curlOptions,
143+
array_merge(self::$default_curl_options,$curlOptions),
123144
$this->getBasicAuthString()
124145
));
125146

@@ -175,6 +196,7 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
175196
$timeout = 5;
176197
$sapphireInfo = new SapphireInfo();
177198
$useragent = 'SilverStripe/' . $sapphireInfo->Version();
199+
$curlOptions = array_merge(self::$default_curl_options, $curlOptions);
178200

179201
curl_setopt($ch, CURLOPT_URL, $url);
180202
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@@ -183,7 +205,6 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
183205
if(!ini_get('open_basedir')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
184206
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
185207
//include headers in the response
186-
//curl_setopt($ch, CURLOPT_VERBOSE, true);
187208
curl_setopt($ch, CURLOPT_HEADER, true);
188209

189210
// Add headers
@@ -220,17 +241,9 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
220241

221242
// Run request
222243
$rawResponse = curl_exec($ch);
223-
$curlError = curl_error($ch);
224-
$responseHeaders = array();
225-
$responseBody = '';
226-
$this->extractResponse($ch, $rawResponse, $responseBody, $responseHeaders);
227-
228-
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
229-
if($curlError !== '' || $statusCode == 0) $statusCode = 500;
244+
$response = $this->extractResponse($ch, $rawResponse);
230245
curl_close($ch);
231246

232-
$response = new RestfulService_Response($responseBody, $statusCode, $responseHeaders);
233-
234247
return $response;
235248
}
236249

@@ -279,22 +292,31 @@ protected function getCachePath($cacheData) {
279292
*
280293
* @param curl_handle $ch The curl handle for the request
281294
* @param string $rawResponse The raw response text
282-
* @param string &$body the body text
283-
* @param array &headers The header array
295+
*
296+
* @return RestfulService_Response The response object
284297
*/
285-
protected function extractResponse($ch, $rawResponse, &$body, &$headers) {
298+
protected function extractResponse($ch, $rawResponse) {
299+
//get the status code
300+
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
301+
//normalise the status code
302+
if($curlError !== '' || $statusCode == 0) $statusCode = 500;
303+
//calculate the length of the header and extract it
286304
$headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
287305
$rawHeaders = substr($rawResponse, 0, $headerLength);
306+
//extract the body
288307
$body = substr($rawResponse, $headerLength);
289-
$headers = self::parse_raw_headers($rawHeaders);
308+
//parse the headers
309+
$headers = $this->parseRawHeaders($rawHeaders);
310+
//return the response object
311+
return new RestfulService_Response($body, $statusCode, $headers);
290312
}
291313

292314
/**
293315
* Takes raw headers and parses them to turn them to an associative array
294316
*
295317
* Any header that we see more than once is turned into an array.
296318
*
297-
* This is meant to mimic htt_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
319+
* This is meant to mimic http_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
298320
* thanks to comment #77241 on that page for foundation of this
299321
*
300322
* @param string $rawHeaders The raw header string
@@ -315,12 +337,12 @@ protected function parseRawHeaders($rawHeaders) {
315337
$headers[$match[1]] = array($headers[$match[1]]);
316338
}
317339
$headers[$match[1]][] = $match[2];
318-
} else {
319-
$headers[$match[1]] = trim($match[2]);
320-
}
321-
}
322-
}
323-
return $headers;
340+
} else {
341+
$headers[$match[1]] = trim($match[2]);
342+
}
343+
}
344+
}
345+
return $headers;
324346
}
325347

326348

@@ -514,7 +536,7 @@ class RestfulService_Response extends SS_HTTPResponse {
514536
protected $simpleXML;
515537

516538
/**
517-
* @var boolean It should be populated with cached content
539+
* @var boolean It should be populated with cached request
518540
* when a request referring to this response was unsuccessful
519541
*/
520542
protected $cachedResponse = false;
@@ -556,6 +578,19 @@ public function getCachedBody() {
556578
}
557579
return false;
558580
}
581+
582+
/**
583+
* @param string
584+
*/
585+
public function setCachedBody($content) {
586+
Deprecation::notice('3.1', 'Setting the response body is now deprecated, set the cached request instead');
587+
if (!$this->cachedResponse) {
588+
$this->cachedResponse = new RestfulService_Response($content);
589+
}
590+
else {
591+
$this->cachedResponse->setBody = $content;
592+
}
593+
}
559594

560595
/**
561596
* @param string

tests/api/RestfulServiceTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function testHttpHeaderParseing() {
184184
'bar=foo'
185185
)
186186
);
187-
$headerFunction = new ReflectionMethod('RestfulService', 'parse_raw_headers');
187+
$headerFunction = new ReflectionMethod('RestfulService', 'parseRawHeaders');
188188
$headerFunction->setAccessible(true);
189189
$this->assertEquals(
190190
$expected,

0 commit comments

Comments
 (0)