From ba8dee05209e136a8911862b21ff6ad90d67f149 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Fri, 6 Jul 2012 12:26:54 +0200 Subject: [PATCH] introduce subclassed client.php and revert changes to 3rdparty --- 3rdparty/Sabre/DAV/Client.php | 7 -- lib/connector/sabre/client.php | 168 +++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 7 deletions(-) create mode 100644 lib/connector/sabre/client.php diff --git a/3rdparty/Sabre/DAV/Client.php b/3rdparty/Sabre/DAV/Client.php index d257c86da1..075e84caa1 100644 --- a/3rdparty/Sabre/DAV/Client.php +++ b/3rdparty/Sabre/DAV/Client.php @@ -22,7 +22,6 @@ class Sabre_DAV_Client { protected $userName; protected $password; protected $proxy; - protected $capath; /** * Constructor @@ -50,10 +49,6 @@ class Sabre_DAV_Client { 'proxy' ); - $this->capath = ''; - if (isset($settings['capath'])) { - $this->capath = $settings['capath']; - } foreach($validSettings as $validSetting) { if (isset($settings[$validSetting])) { @@ -259,8 +254,6 @@ class Sabre_DAV_Client { //CURLOPT_SSL_VERIFYPEER => false, ); - if ($this->capath != '') $curlSettings[CURLOPT_CAPATH] = $this->capath; - switch ($method) { case 'PUT': $curlSettings[CURLOPT_PUT] = true; diff --git a/lib/connector/sabre/client.php b/lib/connector/sabre/client.php new file mode 100644 index 0000000000..bcf564c06d --- /dev/null +++ b/lib/connector/sabre/client.php @@ -0,0 +1,168 @@ + + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ + +class OC_Connector_Sabre_Client extends Sabre_DAV_Client { + + protected $curlSettings; + + public function __construct(array $settings) { + //set default curl settings + $this->curlSettings = array( + CURLOPT_RETURNTRANSFER => true, + // Return headers as part of the response + CURLOPT_HEADER => true, + // Automatically follow redirects + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 5, + CURLOPT_SSL_VERIFYPEER => true, + //CURLOPT_SSL_VERIFYPEER => false, + ); + parent::__construct($settings); + } + + public function setCurlSettings($settings) { + if (is_array($settings)) { + foreach ($settings as $k => $v) { + $this->curlSettings[$k] = $v; + } + } + } + + /** + * Copied from SabreDAV with some modification to use user defined curlSettings + * Performs an actual HTTP request, and returns the result. + * + * If the specified url is relative, it will be expanded based on the base + * url. + * + * The returned array contains 3 keys: + * * body - the response body + * * httpCode - a HTTP code (200, 404, etc) + * * headers - a list of response http headers. The header names have + * been lowercased. + * + * @param string $method + * @param string $url + * @param string $body + * @param array $headers + * @return array + */ + public function request($method, $url = '', $body = null, $headers = array()) { + + $this->curlSettings[CURLOPT_POSTFIELDS] = $body; + $url = $this->getAbsoluteUrl($url); + + switch ($method) { + case 'PUT': + $this->curlSettings[CURLOPT_PUT] = true; + break; + case 'HEAD' : + + // do not read body with HEAD requests (this is neccessary because cURL does not ignore the body with HEAD + // requests when the Content-Length header is given - which in turn is perfectly valid according to HTTP + // specs...) cURL does unfortunately return an error in this case ("transfer closed transfer closed with + // ... bytes remaining to read") this can be circumvented by explicitly telling cURL to ignore the + // response body + $this->curlSettings[CURLOPT_NOBODY] = true; + $this->curlSettings[CURLOPT_CUSTOMREQUEST] = 'HEAD'; + break; + + default: + $this->curlSettings[CURLOPT_CUSTOMREQUEST] = $method; + break; + + } + + // Adding HTTP headers + $nHeaders = array(); + foreach($headers as $key=>$value) { + + $nHeaders[] = $key . ': ' . $value; + + } + $this->curlSettings[CURLOPT_HTTPHEADER] = $nHeaders; + + if ($this->proxy) { + $this->curlSettings[CURLOPT_PROXY] = $this->proxy; + } + + if ($this->userName) { + $this->curlSettings[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC | CURLAUTH_DIGEST; + $this->curlSettings[CURLOPT_USERPWD] = $this->userName . ':' . $this->password; + } + + list( + $response, + $curlInfo, + $curlErrNo, + $curlError + ) = $this->curlRequest($url, $this->curlSettings); + + $headerBlob = substr($response, 0, $curlInfo['header_size']); + $response = substr($response, $curlInfo['header_size']); + + // In the case of 100 Continue, or redirects we'll have multiple lists + // of headers for each separate HTTP response. We can easily split this + // because they are separated by \r\n\r\n + $headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n")); + + // We only care about the last set of headers + $headerBlob = $headerBlob[count($headerBlob)-1]; + + // Splitting headers + $headerBlob = explode("\r\n", $headerBlob); + + $headers = array(); + foreach($headerBlob as $header) { + $parts = explode(':', $header, 2); + if (count($parts)==2) { + $headers[strtolower(trim($parts[0]))] = trim($parts[1]); + } + } + + $response = array( + 'body' => $response, + 'statusCode' => $curlInfo['http_code'], + 'headers' => $headers + ); + + if ($curlErrNo) { + throw new Sabre_DAV_Exception('[CURL] Error while making request: ' . $curlError . ' (error code: ' . $curlErrNo . ')'); + } + + if ($response['statusCode']>=400) { + switch ($response['statusCode']) { + case 404: + throw new Sabre_DAV_Exception_NotFound('Resource ' . $url . ' not found.'); + break; + + default: + throw new Sabre_DAV_Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')'); + } + } + + return $response; + + } + +} \ No newline at end of file