mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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.
This commit is contained in:
parent
f003359047
commit
920fd71a2f
@ -17,6 +17,27 @@ class RestfulService extends ViewableData {
|
||||
protected $customHeaders = array();
|
||||
protected $proxy;
|
||||
protected static $default_proxy;
|
||||
protected static $default_curl_options = array();
|
||||
|
||||
/**
|
||||
* set a curl option that will be applied to all requests as default
|
||||
* {@see http://php.net/manual/en/function.curl-setopt.php#refsect1-function.curl-setopt-parameters}
|
||||
*
|
||||
* @param int $option The cURL opt Constant
|
||||
* @param mixed $value The cURL opt value
|
||||
*/
|
||||
public static function set_default_curl_option($option, $value) {
|
||||
self::$default_curl_options[$option] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* set many defauly curl options at once
|
||||
*/
|
||||
public static function set_default_curl_options($optionArray) {
|
||||
foreach ($optionArray as $option => $value) {
|
||||
self::set_default_curl_option($option, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets default proxy settings for outbound RestfulService connections
|
||||
@ -119,7 +140,7 @@ class RestfulService extends ViewableData {
|
||||
$method,
|
||||
$data,
|
||||
array_merge((array)$this->customHeaders, (array)$headers),
|
||||
$curlOptions,
|
||||
array_merge(self::$default_curl_options,$curlOptions),
|
||||
$this->getBasicAuthString()
|
||||
));
|
||||
|
||||
@ -175,6 +196,7 @@ class RestfulService extends ViewableData {
|
||||
$timeout = 5;
|
||||
$sapphireInfo = new SapphireInfo();
|
||||
$useragent = 'SilverStripe/' . $sapphireInfo->Version();
|
||||
$curlOptions = array_merge(self::$default_curl_options, $curlOptions);
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
@ -183,7 +205,6 @@ class RestfulService extends ViewableData {
|
||||
if(!ini_get('open_basedir')) curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||
//include headers in the response
|
||||
//curl_setopt($ch, CURLOPT_VERBOSE, true);
|
||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||
|
||||
// Add headers
|
||||
@ -220,17 +241,9 @@ class RestfulService extends ViewableData {
|
||||
|
||||
// Run request
|
||||
$rawResponse = curl_exec($ch);
|
||||
$curlError = curl_error($ch);
|
||||
$responseHeaders = array();
|
||||
$responseBody = '';
|
||||
$this->extractResponse($ch, $rawResponse, $responseBody, $responseHeaders);
|
||||
|
||||
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
if($curlError !== '' || $statusCode == 0) $statusCode = 500;
|
||||
$response = $this->extractResponse($ch, $rawResponse);
|
||||
curl_close($ch);
|
||||
|
||||
$response = new RestfulService_Response($responseBody, $statusCode, $responseHeaders);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
@ -279,14 +292,23 @@ class RestfulService extends ViewableData {
|
||||
*
|
||||
* @param curl_handle $ch The curl handle for the request
|
||||
* @param string $rawResponse The raw response text
|
||||
* @param string &$body the body text
|
||||
* @param array &headers The header array
|
||||
*
|
||||
* @return RestfulService_Response The response object
|
||||
*/
|
||||
protected function extractResponse($ch, $rawResponse, &$body, &$headers) {
|
||||
protected function extractResponse($ch, $rawResponse) {
|
||||
//get the status code
|
||||
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
//normalise the status code
|
||||
if($curlError !== '' || $statusCode == 0) $statusCode = 500;
|
||||
//calculate the length of the header and extract it
|
||||
$headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
||||
$rawHeaders = substr($rawResponse, 0, $headerLength);
|
||||
//extract the body
|
||||
$body = substr($rawResponse, $headerLength);
|
||||
$headers = self::parse_raw_headers($rawHeaders);
|
||||
//parse the headers
|
||||
$headers = $this->parseRawHeaders($rawHeaders);
|
||||
//return the response object
|
||||
return new RestfulService_Response($body, $statusCode, $headers);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -294,7 +316,7 @@ class RestfulService extends ViewableData {
|
||||
*
|
||||
* Any header that we see more than once is turned into an array.
|
||||
*
|
||||
* This is meant to mimic htt_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
|
||||
* This is meant to mimic http_parse_headers {@link http://php.net/manual/en/function.http-parse-headers.php}
|
||||
* thanks to comment #77241 on that page for foundation of this
|
||||
*
|
||||
* @param string $rawHeaders The raw header string
|
||||
@ -315,12 +337,12 @@ class RestfulService extends ViewableData {
|
||||
$headers[$match[1]] = array($headers[$match[1]]);
|
||||
}
|
||||
$headers[$match[1]][] = $match[2];
|
||||
} else {
|
||||
$headers[$match[1]] = trim($match[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $headers;
|
||||
} else {
|
||||
$headers[$match[1]] = trim($match[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $headers;
|
||||
}
|
||||
|
||||
|
||||
@ -514,7 +536,7 @@ class RestfulService_Response extends SS_HTTPResponse {
|
||||
protected $simpleXML;
|
||||
|
||||
/**
|
||||
* @var boolean It should be populated with cached content
|
||||
* @var boolean It should be populated with cached request
|
||||
* when a request referring to this response was unsuccessful
|
||||
*/
|
||||
protected $cachedResponse = false;
|
||||
@ -556,6 +578,19 @@ class RestfulService_Response extends SS_HTTPResponse {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
*/
|
||||
public function setCachedBody($content) {
|
||||
Deprecation::notice('3.1', 'Setting the response body is now deprecated, set the cached request instead');
|
||||
if (!$this->cachedResponse) {
|
||||
$this->cachedResponse = new RestfulService_Response($content);
|
||||
}
|
||||
else {
|
||||
$this->cachedResponse->setBody = $content;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string
|
||||
|
@ -184,7 +184,7 @@ class RestfulServiceTest extends SapphireTest {
|
||||
'bar=foo'
|
||||
)
|
||||
);
|
||||
$headerFunction = new ReflectionMethod('RestfulService', 'parse_raw_headers');
|
||||
$headerFunction = new ReflectionMethod('RestfulService', 'parseRawHeaders');
|
||||
$headerFunction->setAccessible(true);
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
|
Loading…
Reference in New Issue
Block a user