WebDAVMkColMethodHandler.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using WebDAVSharp.Server.Adapters;
  6. using WebDAVSharp.Server.Exceptions;
  7. using WebDAVSharp.Server.Stores;
  8. namespace WebDAVSharp.Server.MethodHandlers
  9. {
  10. /// <summary>
  11. /// This class implements the <c>MKCOL</c> HTTP method for WebDAV#.
  12. /// </summary>
  13. internal class WebDavMkColMethodHandler : WebDavMethodHandlerBase
  14. {
  15. #region Properties
  16. /// <summary>
  17. /// Gets the collection of the names of the HTTP methods handled by this instance.
  18. /// </summary>
  19. /// <value>
  20. /// The names.
  21. /// </value>
  22. public override IEnumerable<string> Names => new[]
  23. {
  24. "MKCOL"
  25. };
  26. #endregion
  27. #region Functions
  28. /// <summary>
  29. /// Processes the request.
  30. /// </summary>
  31. /// <param name="server">The <see cref="WebDavServer" /> through which the request came in from the client.</param>
  32. /// <param name="context">
  33. /// The
  34. /// <see cref="IHttpListenerContext" /> object containing both the request and response
  35. /// objects to use.
  36. /// </param>
  37. /// <param name="store">The <see cref="IWebDavStore" /> that the <see cref="WebDavServer" /> is hosting.</param>
  38. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnsupportedMediaTypeException"></exception>
  39. /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavMethodNotAllowedException"></exception>
  40. public override void ProcessRequest(WebDavServer server, IHttpListenerContext context, IWebDavStore store)
  41. {
  42. if (context.Request.ContentLength64 > 0)
  43. throw new WebDavUnsupportedMediaTypeException();
  44. IWebDavStoreCollection collection = GetParentCollection(server, store, context.Request.Url);
  45. string collectionName = Uri.UnescapeDataString(context.Request.Url.Segments.Last().TrimEnd('/', '\\'));
  46. if (collection.GetItemByName(collectionName) != null)
  47. throw new WebDavMethodNotAllowedException();
  48. collection.CreateCollection(collectionName);
  49. context.SendSimpleResponse((int) HttpStatusCode.Created);
  50. }
  51. #endregion
  52. }
  53. }