123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Web;
- using System.Xml;
- using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Adapters;
- using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Exceptions;
- using Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.Stores;
- namespace Mtp2Dav.WebDAVSharp.Server._1d2086a502937936ebc6bfe19cfa15d855be1c31.MethodHandlers
- {
- /// <summary>
- /// This class implements the <c>LOCK</c> HTTP method for WebDAV#.
- /// </summary>
- internal class WebDavLockMethodHandler : WebDavMethodHandlerBase, IWebDavMethodHandler
- {
- /// <summary>
- /// Gets the collection of the names of the HTTP methods handled by this instance.
- /// </summary>
- /// <value>
- /// The names.
- /// </value>
- public IEnumerable<string> Names => new[]
- {
- "LOCK"
- };
- /// <summary>
- /// Processes the request.
- /// </summary>
- /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
- /// <param name="context">
- /// The
- /// <see cref="IHttpListenerContext" /> object containing both the request and response
- /// objects to use.
- /// </param>
- /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
- /// <exception cref="WebDavPreconditionFailedException"></exception>
- public void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
- {
- /***************************************************************************************************
- * Retreive al the information from the request
- ***************************************************************************************************/
- // read the headers
- var depth = GetDepthHeader(context.Request);
- var timeout = GetTimeoutHeader(context.Request);
- // Initiate the XmlNamespaceManager and the XmlNodes
- XmlNamespaceManager manager = null;
- XmlNode lockscopeNode = null, locktypeNode = null, ownerNode = null;
- // try to read the body
- try
- {
- var reader = new StreamReader(context.Request.InputStream, Encoding.UTF8);
- var requestBody = reader.ReadToEnd();
- if (!requestBody.Equals("") && requestBody.Length != 0)
- {
- var requestDocument = new XmlDocument();
- requestDocument.LoadXml(requestBody);
- if (requestDocument.DocumentElement != null && requestDocument.DocumentElement.LocalName != "prop" &&
- requestDocument.DocumentElement.LocalName != "lockinfo")
- {
- }
- manager = new XmlNamespaceManager(requestDocument.NameTable);
- manager.AddNamespace("D", "DAV:");
- manager.AddNamespace("Office", "schemas-microsoft-com:office:office");
- manager.AddNamespace("Repl", "http://schemas.microsoft.com/repl/");
- manager.AddNamespace("Z", "urn:schemas-microsoft-com:");
- // Get the lockscope, locktype and owner as XmlNodes from the XML document
- lockscopeNode = requestDocument.DocumentElement.SelectSingleNode("D:lockscope", manager);
- locktypeNode = requestDocument.DocumentElement.SelectSingleNode("D:locktype", manager);
- ownerNode = requestDocument.DocumentElement.SelectSingleNode("D:owner", manager);
- }
- else
- {
- throw new WebDavPreconditionFailedException();
- }
- }
- catch (Exception)
- {
- throw;
- }
- /***************************************************************************************************
- * Lock the file or folder
- ***************************************************************************************************/
- var isNew = false;
- // Get the parent collection of the item
- var collection = GetParentCollection(server, store, context.Request.Url);
- try
- {
- // Get the item from the collection
- var item = GetItemFromCollection(collection, context.Request.Url);
- }
- catch (Exception)
- {
- collection.CreateDocument(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));
- isNew = true;
- }
- /***************************************************************************************************
- * Create the body for the response
- ***************************************************************************************************/
- // Create the basic response XmlDocument
- var responseDoc = new XmlDocument();
- var responseXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><D:prop " +
- "xmlns:D=\"DAV:\"><D:lockdiscovery><D:activelock/></D:lockdiscovery></D:prop>";
- responseDoc.LoadXml(responseXml);
- // Select the activelock XmlNode
- var activelock = responseDoc.DocumentElement.SelectSingleNode("D:lockdiscovery/D:activelock", manager);
- // Import the given nodes
- activelock.AppendChild(responseDoc.ImportNode(lockscopeNode, true));
- activelock.AppendChild(responseDoc.ImportNode(locktypeNode, true));
- activelock.AppendChild(responseDoc.ImportNode(ownerNode, true));
- // Add the additional elements, e.g. the header elements
- // The timeout element
- var timeoutProperty = new WebDavProperty("timeout", timeout);
- activelock.AppendChild(timeoutProperty.ToXmlElement(responseDoc));
- // The depth element
- var depthProperty = new WebDavProperty("depth", depth == 0 ? "0" : "Infinity");
- activelock.AppendChild(depthProperty.ToXmlElement(responseDoc));
- // The locktoken element
- var locktokenProperty = new WebDavProperty("locktoken", "");
- var locktokenElement = locktokenProperty.ToXmlElement(responseDoc);
- var hrefProperty = new WebDavProperty("href", "opaquelocktoken:e71d4fae-5dec-22df-fea5-00a0c93bd5eb1");
- locktokenElement.AppendChild(hrefProperty.ToXmlElement(responseDoc));
- activelock.AppendChild(locktokenElement);
- /***************************************************************************************************
- * Send the response
- ***************************************************************************************************/
- // convert the StringBuilder
- var resp = responseDoc.InnerXml;
- var responseBytes = Encoding.UTF8.GetBytes(resp);
- if (isNew)
- {
- // HttpStatusCode doesn't contain WebDav status codes, but HttpWorkerRequest can handle these WebDav status codes
- context.Response.StatusCode = (int) HttpStatusCode.Created;
- context.Response.StatusDescription = HttpWorkerRequest.GetStatusDescription((int) HttpStatusCode.Created);
- }
- else
- {
- // HttpStatusCode doesn't contain WebDav status codes, but HttpWorkerRequest can handle these WebDav status codes
- context.Response.StatusCode = (int) HttpStatusCode.OK;
- context.Response.StatusDescription = HttpWorkerRequest.GetStatusDescription((int) HttpStatusCode.OK);
- }
- // set the headers of the response
- context.Response.ContentLength64 = responseBytes.Length;
- context.Response.AdaptedInstance.ContentType = "text/xml";
- // the body
- context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
- context.Response.Close();
- }
- }
- }
|