Permalink
Browse files

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.
  • Loading branch information...
1 parent f003359 commit 920fd71a2f4967fb0e19c9661a11429344113fa5 @dhensby dhensby committed Jan 31, 2013
Showing with 59 additions and 24 deletions.
  1. +58 −23 api/RestfulService.php
  2. +1 −1 tests/api/RestfulServiceTest.php
View
@@ -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 @@ public function request($subURL = '', $method = "GET", $data = null, $headers =
$method,
$data,
array_merge((array)$this->customHeaders, (array)$headers),
- $curlOptions,
+ array_merge(self::$default_curl_options,$curlOptions),
$this->getBasicAuthString()
));
@@ -175,6 +196,7 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
$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 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
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 @@ public function curlRequest($url, $method, $data = null, $headers = null, $curlO
// 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,22 +292,31 @@ protected function getCachePath($cacheData) {
*
* @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);
}
/**
* Takes raw headers and parses them to turn them to an associative array
*
* 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 @@ protected function parseRawHeaders($rawHeaders) {
$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 @@ public function getCachedBody() {
}
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 @@ public function testHttpHeaderParseing() {
'bar=foo'
)
);
- $headerFunction = new ReflectionMethod('RestfulService', 'parse_raw_headers');
+ $headerFunction = new ReflectionMethod('RestfulService', 'parseRawHeaders');
$headerFunction->setAccessible(true);
$this->assertEquals(
$expected,

0 comments on commit 920fd71

Please sign in to comment.