/** * WebDAV HTTP COPY method * * This method copies one uri to a different uri, and works much like the MOVE request * A lot of the actual request processing is done in getCopyMoveInfo * * @param string $uri * @return bool */ protected function httpCopy($uri) { $copyInfo = $this->getCopyAndMoveInfo(); // If the destination is part of the source tree, we must fail if ($copyInfo['destination'] == $uri) { throw new Sabre_DAV_Exception_Forbidden('Source and destination uri are identical.'); } if (strpos($copyInfo['destination'], $uri) === 0) { throw new Sabre_DAV_Exception_Forbidden('destination is part of the source tree.'); } if ($this->tree->nodeExists($uri) == false) { throw new Sabre_DAV_Exception_NotFound('Source doesn\'t exists.'); } if ($copyInfo['destinationExists']) { if (!$this->broadcastEvent('beforeUnbind', array($copyInfo['destination']))) { return false; } $this->tree->delete($copyInfo['destination']); } if (!$this->broadcastEvent('beforeBind', array($copyInfo['destination']))) { return false; } $this->tree->copy($uri, $copyInfo['destination']); $this->broadcastEvent('afterBind', array($copyInfo['destination'])); // If a resource was overwritten we should send a 204, otherwise a 201 $this->httpResponse->setHeader('Content-Length', '0'); $this->httpResponse->sendStatus($copyInfo['destinationExists'] ? 204 : 201); }
/** * HTTP PUT method * * This HTTP method updates a file, or creates a new one. * * If a new resource was created, a 201 Created status code should be returned. If an existing resource is updated, it's a 204 No Content * * @param string $uri * @return bool */ protected function httpPut($uri) { $body = $this->httpRequest->getBody(); // Intercepting Content-Range if ($this->httpRequest->getHeader('Content-Range')) { /** Content-Range is dangerous for PUT requests: PUT per definition stores a full resource. draft-ietf-httpbis-p2-semantics-15 says in section 7.6: An origin server SHOULD reject any PUT request that contains a Content-Range header field, since it might be misinterpreted as partial content (or might be partial content that is being mistakenly PUT as a full representation). Partial content updates are possible by targeting a separately identified resource with state that overlaps a portion of the larger resource, or by using a different method that has been specifically defined for partial updates (for example, the PATCH method defined in [RFC5789]). This clarifies RFC2616 section 9.6: The recipient of the entity MUST NOT ignore any Content-* (e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases. OTOH is a PUT request with a Content-Range currently the only way to continue an aborted upload request and is supported by curl, mod_dav, Tomcat and others. Since some clients do use this feature which results in unexpected behaviour (cf PEAR::HTTP_WebDAV_Client 1.0.1), we reject all PUT requests with a Content-Range for now. */ throw new Sabre_DAV_Exception_NotImplemented('PUT with Content-Range is not allowed.'); } // Intercepting the Finder problem if (($expected = $this->httpRequest->getHeader('X-Expected-Entity-Length')) && $expected > 0) { /** Many webservers will not cooperate well with Finder PUT requests, because it uses 'Chunked' transfer encoding for the request body. The symptom of this problem is that Finder sends files to the server, but they arrive as 0-length files in PHP. If we don't do anything, the user might think they are uploading files successfully, but they end up empty on the server. Instead, we throw back an error if we detect this. The reason Finder uses Chunked, is because it thinks the files might change as it's being uploaded, and therefore the Content-Length can vary. Instead it sends the X-Expected-Entity-Length header with the size of the file at the very start of the request. If this header is set, but we don't get a request body we will fail the request to protect the end-user. */ // Only reading first byte $firstByte = fread($body, 1); if (strlen($firstByte) !== 1) { throw new Sabre_DAV_Exception_Forbidden('This server is not compatible with OS/X finder. Consider using a different WebDAV client or webserver.'); } // The body needs to stay intact, so we copy everything to a // temporary stream. $newBody = fopen('php://temp', 'r+'); fwrite($newBody, $firstByte); stream_copy_to_stream($body, $newBody); rewind($newBody); $body = $newBody; } if ($this->tree->nodeExists($uri)) { $node = $this->tree->getNodeForPath($uri); // Checking If-None-Match and related headers. if (!$this->checkPreconditions()) { return; } // If the node is a collection, we'll deny it if (!$node instanceof Sabre_DAV_IFile) { throw new Sabre_DAV_Exception_Conflict('PUT is not allowed on non-files.'); } if (!$this->broadcastEvent('beforeWriteContent', array($uri, $node, &$body))) { return false; } $etag = $node->put($body); $this->broadcastEvent('afterWriteContent', array($uri, $node)); $this->httpResponse->setHeader('Content-Length', '0'); if ($etag) { $this->httpResponse->setHeader('ETag', $etag); } $this->httpResponse->sendStatus(204); } else { $etag = null; // If we got here, the resource didn't exist yet. if (!$this->createFile($this->getRequestUri(), $body, $etag)) { // For one reason or another the file was not created. return; } $this->httpResponse->setHeader('Content-Length', '0'); if ($etag) { $this->httpResponse->setHeader('ETag', $etag); } $this->httpResponse->sendStatus(201); } }
/** * HTTP PUT method * * This HTTP method updates a file, or creates a new one. * * If a new resource was created, a 201 Created status code should be returned. If an existing resource is updated, it's a 200 Ok * * @param string $uri * @return void */ protected function httpPut($uri) { $body = $this->httpRequest->getBody(); // Intercepting the Finder problem if (($expected = $this->httpRequest->getHeader('X-Expected-Entity-Length')) && $expected > 0) { /** Many webservers will not cooperate well with Finder PUT requests, because it uses 'Chunked' transfer encoding for the request body. The symptom of this problem is that Finder sends files to the server, but they arrive as 0-lenght files in PHP. If we don't do anything, the user might think they are uploading files successfully, but they end up empty on the server. Instead, we throw back an error if we detect this. The reason Finder uses Chunked, is because it thinks the files might change as it's being uploaded, and therefore the Content-Length can vary. Instead it sends the X-Expected-Entity-Length header with the size of the file at the very start of the request. If this header is set, but we don't get a request body we will fail the request to protect the end-user. */ // Only reading first byte $firstByte = fread($body, 1); if (strlen($firstByte) !== 1) { throw new Sabre_DAV_Exception_Forbidden('This server is not compatible with OS/X finder. Consider using a different WebDAV client or webserver.'); } // The body needs to stay intact, so we copy everything to a // temporary stream. $newBody = fopen('php://temp', 'r+'); fwrite($newBody, $firstByte); stream_copy_to_stream($body, $newBody); rewind($newBody); $body = $newBody; } if ($this->tree->nodeExists($uri)) { $node = $this->tree->getNodeForPath($uri); // Checking If-None-Match and related headers. if (!$this->checkPreconditions()) { return; } // If the node is a collection, we'll deny it if (!$node instanceof Sabre_DAV_IFile) { throw new Sabre_DAV_Exception_Conflict('PUT is not allowed on non-files.'); } if (!$this->broadcastEvent('beforeWriteContent', array($this->getRequestUri()))) { return false; } $node->put($body); $this->httpResponse->setHeader('Content-Length', '0'); $this->httpResponse->sendStatus(200); } else { // If we got here, the resource didn't exist yet. $this->createFile($this->getRequestUri(), $body); $this->httpResponse->setHeader('Content-Length', '0'); $this->httpResponse->sendStatus(201); } }