[Pkg-owncloud-commits] [php-sabredav] 179/275: Supporting HEAD wherever we support GET.

David Prévot taffit at moszumanska.debian.org
Thu Sep 25 14:56:05 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabredav.

commit d9a2626ea9bea28381653003b912a98915abc3b3
Author: Evert Pot <me at evertpot.com>
Date:   Fri Aug 22 20:04:02 2014 -0400

    Supporting HEAD wherever we support GET.
    
    Fixes #348.
---
 ChangeLog.md                         |  1 +
 lib/DAV/CorePlugin.php               | 75 +++++++++++++++++++-----------------
 lib/DAV/Server.php                   | 66 +++++++++++++++----------------
 tests/Sabre/DAV/ServerSimpleTest.php |  7 +---
 4 files changed, 75 insertions(+), 74 deletions(-)

diff --git a/ChangeLog.md b/ChangeLog.md
index 3b3dcae..2bee92e 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -53,6 +53,7 @@ ChangeLog
   `CARDDAV:supported-address-data`. This functionality has been removed from
   the CardDAV PDO backend.
 * When a REPORT is not supported, we now emit HTTP error 415, instead of 403.
+* `HEAD` requests now work wherever `GET` also works.
 
 
 2.0.4 (????-??-??)
diff --git a/lib/DAV/CorePlugin.php b/lib/DAV/CorePlugin.php
index 21e82eb..47a6706 100644
--- a/lib/DAV/CorePlugin.php
+++ b/lib/DAV/CorePlugin.php
@@ -28,7 +28,7 @@ class CorePlugin extends ServerPlugin {
      * @param Server $server
      * @return void
      */
-    public function initialize(Server $server) {
+    function initialize(Server $server) {
 
         $this->server = $server;
         $server->on('method:GET',       [$this, 'httpGet']);
@@ -58,7 +58,7 @@ class CorePlugin extends ServerPlugin {
      *
      * @return string
      */
-    public function getPluginName() {
+    function getPluginName() {
 
         return 'core';
 
@@ -71,7 +71,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpGet(RequestInterface $request, ResponseInterface $response) {
+    function httpGet(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
         $node = $this->server->tree->getNodeForPath($path,0);
@@ -206,7 +206,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpOptions(RequestInterface $request, ResponseInterface $response) {
+    function httpOptions(RequestInterface $request, ResponseInterface $response) {
 
         $methods = $this->server->getAllowedMethods($request->getPath());
 
@@ -235,31 +235,36 @@ class CorePlugin extends ServerPlugin {
     /**
      * HTTP HEAD
      *
-     * This method is normally used to take a peak at a url, and only get the HTTP response headers, without the body
-     * This is used by clients to determine if a remote file was changed, so they can use a local cached version, instead of downloading it again
+     * This method is normally used to take a peak at a url, and only get the
+     * HTTP response headers, without the body. This is used by clients to
+     * determine if a remote file was changed, so they can use a local cached
+     * version, instead of downloading it again
      *
      * @param RequestInterface $request
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpHead(RequestInterface $request, ResponseInterface $response) {
-
-        $path = $request->getPath();
-
-        $node = $this->server->tree->getNodeForPath($path);
-
-        /* This information is only collection for File objects.
-         * Ideally we want to throw 405 Method Not Allowed for every
-         * non-file, but MS Office does not like this
-         */
-        if ($node instanceof IFile) {
-            $headers = $this->server->getHTTPHeaders($path);
-            if (!isset($headers['Content-Type'])) {
-                $headers['Content-Type'] = 'application/octet-stream';
-            }
-            $response->addHeaders($headers);
+    function httpHead(RequestInterface $request, ResponseInterface $response) {
+
+        // This is implemented by changing the HEAD request to a GET request,
+        // and dropping the response body.
+        $subRequest = clone $request;
+        $subRequest->setMethod('GET');
+
+        try {
+            $this->server->invokeMethod($subRequest, $response);
+            $response->setBody('');
+        } catch (Exception\NotImplemented $e) {
+            // Some clients may do HEAD requests on collections, however, GET
+            // requests and HEAD requests _may_ not be defined on a collection,
+            // which would trigger a 501.
+            // This breaks some clients though, so we're transforming these
+            // 501s into 200s.
+            $response->setStatus(200);
+            $response->setBody('');
+            $response->setHeader('Content-Type', 'text/plain');
+            $response->setHeader('X-Sabre-Real-Status', $e->getHTTPCode());
         }
-        $response->setStatus(200);
 
         // Sending back false will interupt the event chain and tell the server
         // we've handled this method.
@@ -276,7 +281,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return void
      */
-    public function httpDelete(RequestInterface $request, ResponseInterface $response) {
+    function httpDelete(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
 
@@ -309,7 +314,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return void
      */
-    public function httpPropfind(RequestInterface $request, ResponseInterface $response) {
+    function httpPropfind(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
 
@@ -359,7 +364,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpPropPatch(RequestInterface $request, ResponseInterface $response) {
+    function httpPropPatch(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
 
@@ -429,7 +434,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpPut(RequestInterface $request, ResponseInterface $response) {
+    function httpPut(RequestInterface $request, ResponseInterface $response) {
 
         $body = $request->getBodyAsStream();
         $path = $request->getPath();
@@ -535,7 +540,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpMkcol(RequestInterface $request, ResponseInterface $response) {
+    function httpMkcol(RequestInterface $request, ResponseInterface $response) {
 
         $requestBody = $request->getBodyAsString();
         $path = $request->getPath();
@@ -608,7 +613,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpMove(RequestInterface $request, ResponseInterface $response) {
+    function httpMove(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
 
@@ -655,7 +660,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpCopy(RequestInterface $request, ResponseInterface $response) {
+    function httpCopy(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
 
@@ -691,7 +696,7 @@ class CorePlugin extends ServerPlugin {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function httpReport(RequestInterface $request, ResponseInterface $response) {
+    function httpReport(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
 
@@ -723,7 +728,7 @@ class CorePlugin extends ServerPlugin {
      * @param PropPatch $propPatch
      * @return void
      */
-    public function propPatchProtectedPropertyCheck($path, PropPatch $propPatch) {
+    function propPatchProtectedPropertyCheck($path, PropPatch $propPatch) {
 
         // Comparing the mutation list to the list of propetected properties.
         $mutations = $propPatch->getMutations();
@@ -749,7 +754,7 @@ class CorePlugin extends ServerPlugin {
      * @param PropPatch $propPatch
      * @return void
      */
-    public function propPatchNodeUpdate($path, PropPatch $propPatch) {
+    function propPatchNodeUpdate($path, PropPatch $propPatch) {
 
         // This should trigger a 404 if the node doesn't exist.
         $node = $this->server->tree->getNodeForPath($path);
@@ -769,7 +774,7 @@ class CorePlugin extends ServerPlugin {
      * @param INode $node
      * @return void
      */
-    public function propFind(PropFind $propFind, INode $node) {
+    function propFind(PropFind $propFind, INode $node) {
 
         $propFind->handle('{DAV:}getlastmodified', function() use ($node) {
             $lm = $node->getLastModified();
@@ -826,7 +831,7 @@ class CorePlugin extends ServerPlugin {
      * @param INode $node
      * @return void
      */
-    public function propFindNode(PropFind $propFind, INode $node) {
+    function propFindNode(PropFind $propFind, INode $node) {
 
         if ($node instanceof IProperties && $propertyNames = $propFind->get404Properties()) {
 
diff --git a/lib/DAV/Server.php b/lib/DAV/Server.php
index 465b36f..c443005 100644
--- a/lib/DAV/Server.php
+++ b/lib/DAV/Server.php
@@ -190,7 +190,7 @@ class Server extends EventEmitter {
      *
      * @param Tree|INode|array|null $treeOrNode The tree object
      */
-    public function __construct($treeOrNode = null) {
+    function __construct($treeOrNode = null) {
 
         if ($treeOrNode instanceof Tree) {
             $this->tree = $treeOrNode;
@@ -228,7 +228,7 @@ class Server extends EventEmitter {
      *
      * @return void
      */
-    public function exec() {
+    function exec() {
 
         try {
 
@@ -320,7 +320,7 @@ class Server extends EventEmitter {
      * @param string $uri
      * @return void
      */
-    public function setBaseUri($uri) {
+    function setBaseUri($uri) {
 
         // If the baseUri does not end with a slash, we must add it
         if ($uri[strlen($uri)-1]!=='/')
@@ -335,7 +335,7 @@ class Server extends EventEmitter {
      *
      * @return string
      */
-    public function getBaseUri() {
+    function getBaseUri() {
 
         if (is_null($this->baseUri)) $this->baseUri = $this->guessBaseUri();
         return $this->baseUri;
@@ -350,7 +350,7 @@ class Server extends EventEmitter {
      *
      * @return string
      */
-    public function guessBaseUri() {
+    function guessBaseUri() {
 
         $pathInfo = $this->httpRequest->getRawServerValue('PATH_INFO');
         $uri = $this->httpRequest->getRawServerValue('REQUEST_URI');
@@ -392,7 +392,7 @@ class Server extends EventEmitter {
      * @param ServerPlugin $plugin
      * @return void
      */
-    public function addPlugin(ServerPlugin $plugin) {
+    function addPlugin(ServerPlugin $plugin) {
 
         $this->plugins[$plugin->getPluginName()] = $plugin;
         $plugin->initialize($this);
@@ -407,7 +407,7 @@ class Server extends EventEmitter {
      * @param string $name
      * @return ServerPlugin
      */
-    public function getPlugin($name) {
+    function getPlugin($name) {
 
         if (isset($this->plugins[$name]))
             return $this->plugins[$name];
@@ -426,7 +426,7 @@ class Server extends EventEmitter {
      *
      * @return array
      */
-    public function getPlugins() {
+    function getPlugins() {
 
         return $this->plugins;
 
@@ -439,7 +439,7 @@ class Server extends EventEmitter {
      * @param ResponseInterface $response
      * @return void
      */
-    public function invokeMethod(RequestInterface $request, ResponseInterface $response) {
+    function invokeMethod(RequestInterface $request, ResponseInterface $response) {
 
         $method = $request->getMethod();
 
@@ -474,7 +474,7 @@ class Server extends EventEmitter {
      * @param string $path
      * @return array
      */
-    public function getAllowedMethods($path) {
+    function getAllowedMethods($path) {
 
         $methods = [
             'OPTIONS',
@@ -509,7 +509,7 @@ class Server extends EventEmitter {
      *
      * @return string
      */
-    public function getRequestUri() {
+    function getRequestUri() {
 
         return $this->calculateUri($this->httpRequest->getUrl());
 
@@ -522,7 +522,7 @@ class Server extends EventEmitter {
      * @throws Exception\Forbidden A permission denied exception is thrown whenever there was an attempt to supply a uri outside of the base uri
      * @return string
      */
-    public function calculateUri($uri) {
+    function calculateUri($uri) {
 
         if ($uri[0]!='/' && strpos($uri,'://')) {
 
@@ -559,7 +559,7 @@ class Server extends EventEmitter {
      * @param mixed $default
      * @return int
      */
-    public function getHTTPDepth($default = self::DEPTH_INFINITY) {
+    function getHTTPDepth($default = self::DEPTH_INFINITY) {
 
         // If its not set, we'll grab the default
         $depth = $this->httpRequest->getHeader('Depth');
@@ -590,7 +590,7 @@ class Server extends EventEmitter {
      *
      * @return array|null
      */
-    public function getHTTPRange() {
+    function getHTTPRange() {
 
         $range = $this->httpRequest->getHeader('range');
         if (is_null($range)) return null;
@@ -634,7 +634,7 @@ class Server extends EventEmitter {
      *
      * @return array
      */
-    public function getHTTPPrefer() {
+    function getHTTPPrefer() {
 
         $result = [
             'return-asynch'         => false,
@@ -708,7 +708,7 @@ class Server extends EventEmitter {
      *         subtree.
      * @return array
      */
-    public function getCopyAndMoveInfo(RequestInterface $request) {
+    function getCopyAndMoveInfo(RequestInterface $request) {
 
         // Collecting the relevant HTTP headers
         if (!$request->getHeader('Destination')) throw new Exception\BadRequest('The destination header was not supplied');
@@ -773,7 +773,7 @@ class Server extends EventEmitter {
      * @param string $path
      * @param array $propertyNames
      */
-    public function getProperties($path, $propertyNames) {
+    function getProperties($path, $propertyNames) {
 
         $result = $this->getPropertiesForPath($path,$propertyNames,0);
         return $result[0][200];
@@ -792,7 +792,7 @@ class Server extends EventEmitter {
      * @param array $propertyNames
      * @return array
      */
-    public function getPropertiesForChildren($path, $propertyNames) {
+    function getPropertiesForChildren($path, $propertyNames) {
 
         $result = [];
         foreach($this->getPropertiesForPath($path,$propertyNames,1) as $k=>$row) {
@@ -819,7 +819,7 @@ class Server extends EventEmitter {
      * @param string $path
      * @return array
      */
-    public function getHTTPHeaders($path) {
+    function getHTTPHeaders($path) {
 
         $propertyMap = [
             '{DAV:}getcontenttype'   => 'Content-Type',
@@ -892,7 +892,7 @@ class Server extends EventEmitter {
      * @param int $depth
      * @return array
      */
-    public function getPropertiesForPath($path, $propertyNames = [], $depth = 0) {
+    function getPropertiesForPath($path, $propertyNames = [], $depth = 0) {
 
         // The only two options for the depth of a propfind is 0 or 1 - as long as depth infinity is not enabled
         if (!$this->enablePropfindDepthInfinity && $depth != 0) $depth = 1;
@@ -957,7 +957,7 @@ class Server extends EventEmitter {
      * @param array $propertyNames
      * @return array
      */
-    public function getPropertiesForMultiplePaths(array $paths, array $propertyNames = []) {
+    function getPropertiesForMultiplePaths(array $paths, array $propertyNames = []) {
 
         $result = [
         ];
@@ -999,7 +999,7 @@ class Server extends EventEmitter {
      * @param INode $node
      * @return bool
      */
-    public function getPropertiesByNode(PropFind $propFind, INode $node) {
+    function getPropertiesByNode(PropFind $propFind, INode $node) {
 
         return $this->emit('propFind', [$propFind, $node]);
 
@@ -1019,7 +1019,7 @@ class Server extends EventEmitter {
      * @param string   $etag
      * @return bool
      */
-    public function createFile($uri,$data, &$etag = null) {
+    function createFile($uri,$data, &$etag = null) {
 
         list($dir,$name) = URLUtil::splitPath($uri);
 
@@ -1060,7 +1060,7 @@ class Server extends EventEmitter {
      * @param string   $etag
      * @return bool
      */
-    public function updateFile($uri,$data, &$etag = null) {
+    function updateFile($uri,$data, &$etag = null) {
 
         $node = $this->tree->getNodeForPath($uri);
 
@@ -1087,7 +1087,7 @@ class Server extends EventEmitter {
      * @param string $uri
      * @return void
      */
-    public function createDirectory($uri) {
+    function createDirectory($uri) {
 
         $this->createCollection($uri,['{DAV:}collection'], []);
 
@@ -1106,7 +1106,7 @@ class Server extends EventEmitter {
      * @param array $properties A list of properties
      * @return array|null
      */
-    public function createCollection($uri, array $resourceType, array $properties) {
+    function createCollection($uri, array $resourceType, array $properties) {
 
         list($parentUri,$newName) = URLUtil::splitPath($uri);
 
@@ -1230,7 +1230,7 @@ class Server extends EventEmitter {
      * @param array $properties
      * @return array
      */
-    public function updateProperties($path, array $properties) {
+    function updateProperties($path, array $properties) {
 
         $propPatch = new PropPatch($properties);
         $this->emit('propPatch', [$path, $propPatch]);
@@ -1262,7 +1262,7 @@ class Server extends EventEmitter {
      * @param ResponseInterface $response
      * @return bool
      */
-    public function checkPreconditions(RequestInterface $request, ResponseInterface $response) {
+    function checkPreconditions(RequestInterface $request, ResponseInterface $response) {
 
         $path = $request->getPath();
         $node = null;
@@ -1545,7 +1545,7 @@ class Server extends EventEmitter {
      *
      * @return array
      */
-    public function getIfConditions(RequestInterface $request) {
+    function getIfConditions(RequestInterface $request) {
 
         $header = $request->getHeader('If');
         if (!$header) return [];
@@ -1601,7 +1601,7 @@ class Server extends EventEmitter {
      * @param INode $node
      * @return array
      */
-    public function getResourceTypeForNode(INode $node) {
+    function getResourceTypeForNode(INode $node) {
 
         $result = [];
         foreach($this->resourceTypeMapping as $className => $resourceType) {
@@ -1624,7 +1624,7 @@ class Server extends EventEmitter {
      * @param bool strip404s
      * @return string
      */
-    public function generateMultiStatus(array $fileProperties, $strip404s = false) {
+    function generateMultiStatus(array $fileProperties, $strip404s = false) {
 
         $dom = new \DOMDocument('1.0','utf-8');
         //$dom->formatOutput = true;
@@ -1669,7 +1669,7 @@ class Server extends EventEmitter {
      * @param string $body xml body
      * @return array list of properties in need of updating or deletion
      */
-    public function parsePropPatchRequest($body) {
+    function parsePropPatchRequest($body) {
 
         //We'll need to change the DAV namespace declaration to something else in order to make it parsable
         $dom = XMLUtil::loadDOMDocument($body);
@@ -1711,7 +1711,7 @@ class Server extends EventEmitter {
      * @param string $body
      * @return array
      */
-    public function parsePropFindRequest($body) {
+    function parsePropFindRequest($body) {
 
         // If the propfind body was empty, it means IE is requesting 'all' properties
         if (!$body) return [];
diff --git a/tests/Sabre/DAV/ServerSimpleTest.php b/tests/Sabre/DAV/ServerSimpleTest.php
index bcc8078..b8cb109 100644
--- a/tests/Sabre/DAV/ServerSimpleTest.php
+++ b/tests/Sabre/DAV/ServerSimpleTest.php
@@ -254,12 +254,7 @@ class ServerSimpleTest extends AbstractServer{
 
     function testHEADOnCollection() {
 
-        $serverVars = array(
-            'REQUEST_URI'    => '/',
-            'REQUEST_METHOD' => 'HEAD',
-        );
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
+        $request = new HTTP\Request('HEAD', '/');
         $this->server->httpRequest = ($request);
         $this->server->exec();
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list