/**
  * 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 RequestInterface $request
  * @param ResponseInterface $response
  * @return bool
  */
 function httpPut(RequestInterface $request, ResponseInterface $response)
 {
     $body = $request->getBodyAsStream();
     $path = $request->getPath();
     // Intercepting Content-Range
     if ($request->getHeader('Content-Range')) {
         /**
            An origin server that allows PUT on a given target resource MUST send
            a 400 (Bad Request) response to a PUT request that contains a
            Content-Range header field.
         
            Reference: http://tools.ietf.org/html/rfc7231#section-4.3.4
         */
         throw new Exception\BadRequest('Content-Range on PUT requests are forbidden.');
     }
     // Intercepting the Finder problem
     if (($expected = $request->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;
     }
     if ($this->server->tree->nodeExists($path)) {
         $node = $this->server->tree->getNodeForPath($path);
         // 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->server->updateFile($path, $body, $etag)) {
             return false;
         }
         $response->setHeader('Content-Length', '0');
         if ($etag) {
             $response->setHeader('ETag', $etag);
         }
         $response->setStatus(204);
     } else {
         $etag = null;
         // If we got here, the resource didn't exist yet.
         if (!$this->server->createFile($path, $body, $etag)) {
             // For one reason or another the file was not created.
             return false;
         }
         $response->setHeader('Content-Length', '0');
         if ($etag) {
             $response->setHeader('ETag', $etag);
         }
         $response->setStatus(201);
     }
     // Sending back false will interupt the event chain and tell the server
     // we've handled this method.
     return false;
 }