Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream webdav downloads using http client #18653

Merged
merged 3 commits into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions lib/private/files/storage/dav.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OC\Files\Stream\Close;
use Icewind\Streams\IteratorDirectory;
use OC\MemCache\ArrayCache;
use OCP\AppFramework\Http;
use OCP\Constants;
use OCP\Files;
use OCP\Files\FileInfo;
Expand Down Expand Up @@ -337,38 +338,23 @@ public function fopen($path, $mode) {
if (!$this->file_exists($path)) {
return false;
}
//straight up curl instead of sabredav here, sabredav put's the entire get result in memory
$curl = curl_init();
$fp = fopen('php://temp', 'r+');
curl_setopt($curl, CURLOPT_USERPWD, $this->user . ':' . $this->password);
curl_setopt($curl, CURLOPT_URL, $this->createBaseUri() . $this->encodePath($path));
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if(defined('CURLOPT_PROTOCOLS')) {
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
if(defined('CURLOPT_REDIR_PROTOCOLS')) {
curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
if ($this->secure === true) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
if ($this->certPath) {
curl_setopt($curl, CURLOPT_CAINFO, $this->certPath);
}
}

curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode !== 200) {
Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, Util::ERROR);
if ($statusCode === 423) {
$httpClient = \OC::$server->getHTTPClientService();
$response = $httpClient
->newClient()
->get($this->createBaseUri() . $this->encodePath($path), [
'auth' => [$this->user, $this->password],
'stream' => true
]);

if ($response->getStatusCode() !== Http::STATUS_OK) {
if ($response->getStatusCode() === Http::STATUS_LOCKED) {
throw new \OCP\Lock\LockedException($path);
} else {
Util::writeLog("webdav client", 'Guzzle get returned status code ' . $response->getStatusCode(), Util::ERROR);
}
}
curl_close($curl);
rewind($fp);
return $fp;

return $response->getBody();
case 'w':
case 'wb':
case 'a':
Expand Down
3 changes: 2 additions & 1 deletion lib/private/http/client/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ private function getProxyUri() {
*/
public function get($uri, array $options = []) {
$response = $this->client->get($uri, $options);
return new Response($response);
$isStream = isset($options['stream']) && $options['stream'];
return new Response($response, $isStream);
}

/**
Expand Down
15 changes: 12 additions & 3 deletions lib/private/http/client/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ class Response implements IResponse {
/** @var GuzzleResponse */
private $response;

/**
* @var bool
*/
private $stream;

/**
* @param GuzzleResponse $response
* @param bool $stream
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$isStream ? Is it a boolean or can it be an actual stream ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A boolean, to tell the Response class if it should treat the response as a stream

*/
public function __construct(GuzzleResponse $response) {
public function __construct(GuzzleResponse $response, $stream = false) {
$this->response = $response;
$this->stream = $stream;
}

/**
* @return string
* @return string|resource
*/
public function getBody() {
return $this->response->getBody()->getContents();
return $this->stream ?
$this->response->getBody()->detach():
$this->response->getBody()->getContents();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/public/http/client/iresponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
interface IResponse {
/**
* @return string
* @return string|resource
* @since 8.1.0
*/
public function getBody();
Expand Down