From 280854dc21b2d88b0351d8f58cd5e6a4c36bf647 Mon Sep 17 00:00:00 2001 From: Andreas Grabs Date: Sun, 6 Nov 2011 22:10:49 +0100 Subject: [PATCH] MDL-30154 - WebDAV-Repository fails on some WebDAV-Server --- lib/webdavlib.php | 44 +++++++++++++++++++++++++++++++-------- repository/webdav/lib.php | 3 ++- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/webdavlib.php b/lib/webdavlib.php index 4eddc7d3340..33867d6814b 100644 --- a/lib/webdavlib.php +++ b/lib/webdavlib.php @@ -797,7 +797,7 @@ EOD; if (strcmp($response['status']['status-code'],'207') == 0 ) { // ok so far // next there should be a Content-Type: text/xml; charset="utf-8" header line - if (preg_match('#text/xml;\s?charset=[\'\"]?utf-8[\'\"]?#i', $response['header']['Content-Type'])) { + if (preg_match('#(application|text)/xml;\s?charset=[\'\"]?utf-8[\'\"]?#i', $response['header']['Content-Type'])) { // ok let's get the content of the xml stuff $this->_parser = xml_parser_create_ns('UTF-8'); // forget old data... @@ -1401,22 +1401,48 @@ EOD; // check for a specified content-length case preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/',$header,$matches): $this->_error_log('Getting data using Content-Length '. $matches[1]); + // check if we the content data size is small enough to get it as one block if ($matches[1] <= $max_chunk_size ) { // only read something if Content-Length is bigger than 0 if ($matches[1] > 0 ) { $buffer = fread($this->sock, $matches[1]); + $loadsize = strlen($buffer); + //did we realy get the full length? + if ($loadsize < $matches[1]) { + $max_chunk_size = $loadsize; + do { + $mod = $max_chunk_size % ($matches[1] - strlen($buffer)); + $chunk_size = ($mod == $max_chunk_size ? $max_chunk_size : $matches[1] - strlen($buffer)); + $buffer .= fread($this->sock, $chunk_size); + $this->_error_log('mod: ' . $mod . ' chunk: ' . $chunk_size . ' total: ' . strlen($buffer)); + } while ($mod == $max_chunk_size); + break; + } else { + break; + } } else { $buffer = ''; + break; } - } else { - // data is to big to handle it as one. Get it chunk per chunk... - do { - $mod = $max_chunk_size % ($matches[1] - strlen($buffer)); - $chunk_size = ($mod == $max_chunk_size ? $max_chunk_size : $matches[1] - strlen($buffer)); - $buffer .= fread($this->sock, $chunk_size); - $this->_error_log('mod: ' . $mod . ' chunk: ' . $chunk_size . ' total: ' . strlen($buffer)); - } while ($mod == $max_chunk_size); + } + + // data is to big to handle it as one. Get it chunk per chunk... + //trying to get the full length of max_chunk_size + $buffer = fread($this->sock, $max_chunk_size); + $loadsize = strlen($buffer); + if ($loadsize < $max_chunk_size) { + $max_chunk_size = $loadsize; + } + do { + $mod = $max_chunk_size % ($matches[1] - strlen($buffer)); + $chunk_size = ($mod == $max_chunk_size ? $max_chunk_size : $matches[1] - strlen($buffer)); + $buffer .= fread($this->sock, $chunk_size); + $this->_error_log('mod: ' . $mod . ' chunk: ' . $chunk_size . ' total: ' . strlen($buffer)); + } while ($mod == $max_chunk_size); + $loadsize = strlen($buffer); + if ($loadsize < $matches[1]) { + $buffer .= fread($this->sock, $matches[1] - $loadsize); } break; diff --git a/repository/webdav/lib.php b/repository/webdav/lib.php index 4436b104e81..32a908f9906 100644 --- a/repository/webdav/lib.php +++ b/repository/webdav/lib.php @@ -118,7 +118,7 @@ class repository_webdav extends repository { } if (!empty($v['resourcetype']) && $v['resourcetype'] == 'collection') { // a folder - if ($path != $v['href']) { + if (ltrim($path, '/') != ltrim($v['href'], '/')) { $matches = array(); preg_match('#(\w+)$#i', $v['href'], $matches); if (!empty($matches[1])) { @@ -137,6 +137,7 @@ class repository_webdav extends repository { } }else{ // a file + $path = rtrim($path,'/'); $title = urldecode(substr($v['href'], strpos($v['href'], $path)+strlen($path))); $title = basename($title); $size = !empty($v['getcontentlength'])? $v['getcontentlength']:'';