WebDAVDeleteMethodHandler.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections.Generic;
  2. using System.Text;
  3. using System.Web;
  4. using WebDAVSharp.Server.Adapters;
  5. using WebDAVSharp.Server.Stores;
  6. using WebDAVSharp.Server.Utilities;
  7. namespace WebDAVSharp.Server.MethodHandlers
  8. {
  9. /// <summary>
  10. /// This class implements the <c>DELETE</c> HTTP method for WebDAV#.
  11. /// </summary>
  12. internal class WebDavDeleteMethodHandler : WebDavMethodHandlerBase
  13. {
  14. #region Properties
  15. /// <summary>
  16. /// Gets the collection of the names of the HTTP methods handled by this instance.
  17. /// </summary>
  18. /// <value>
  19. /// The names.
  20. /// </value>
  21. public override IEnumerable<string> Names => new[]
  22. {
  23. "DELETE"
  24. };
  25. #endregion
  26. #region Functions
  27. /// <summary>
  28. /// Processes the request.
  29. /// </summary>
  30. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  31. /// <param name="context">
  32. /// The
  33. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  34. /// objects to use.
  35. /// </param>
  36. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  37. public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  38. {
  39. IWebDavStoreCollection collection;
  40. // Get the parent collection of the item
  41. collection = GetParentCollection(server, store, context.Request.Url);
  42. // Get the item from the collection
  43. IWebDavStoreItem item = GetItemFromCollection(collection, context.Request.Url);
  44. if (store.LockSystem.GetLocks(item).Count > 0)
  45. {
  46. StringBuilder sb = new StringBuilder(3000);
  47. sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><D:multistatus xmlns:Z=\"urn:schemas-microsoft-com:\" xmlns:D=\"DAV:\"><D:response><D:href>" + context.Request.Url + "</D:href><d:status>HTTP/1.1 423 Locked</d:status><d:error><d:lock-token-submitted/></d:error></d:response> </d:multistatus> ");
  48. byte[] responseBytes = Encoding.UTF8.GetBytes(sb.ToString());
  49. context.Response.StatusCode = (int) WebDavStatusCode.MultiStatus;
  50. context.Response.StatusDescription = HttpWorkerRequest.GetStatusDescription((int) WebDavStatusCode.MultiStatus);
  51. // set the headers of the response
  52. context.Response.ContentLength64 = responseBytes.Length;
  53. context.Response.AdaptedInstance.ContentType = "text/xml";
  54. // the body
  55. context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);
  56. context.Response.Close();
  57. }
  58. // Deletes the item
  59. collection.Delete(item);
  60. context.SendSimpleResponse();
  61. }
  62. #endregion
  63. }
  64. }