示例#1
0
 /**
  * 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 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 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;
     }
     // Checking If-None-Match and related headers.
     if (!$this->checkPreconditions()) {
         return;
     }
     if ($this->tree->nodeExists($uri)) {
         $node = $this->tree->getNodeForPath($uri);
         // If the node is a collection, we'll deny it
         if (!$node instanceof IFile) {
             throw new 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);
     }
 }